2 Module : Gargantext.Database.Util
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE OverloadedStrings #-}
17 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
19 module Gargantext.Database.Utils where
21 import Control.Applicative (Applicative)
22 import Control.Monad.Reader
23 import Data.Aeson (Result(Error,Success), fromJSON, FromJSON)
24 import Data.Either.Extra (Either(Left, Right))
25 import Data.Ini (readIniFile, lookupValue)
26 import Data.Maybe (maybe)
27 import Data.Monoid ((<>))
28 import Data.Profunctor.Product.Default (Default)
29 import Data.Text (unpack, pack)
30 import Data.Typeable (Typeable)
31 import Data.Word (Word16)
32 import Database.PostgreSQL.Simple (Connection, connect)
33 import Database.PostgreSQL.Simple.FromField ( Conversion, ResultError(ConversionFailed), fromField, returnError)
34 import Database.PostgreSQL.Simple.Internal (Field)
35 import Gargantext.Prelude
36 import Opaleye (Query, Unpackspec, showSqlForPostgres)
37 import System.IO (FilePath)
38 import Text.Read (read)
39 import qualified Data.ByteString as DB
40 import qualified Database.PostgreSQL.Simple as PGS
42 ------------------------------------------------------------------------
43 {- | Reader Monad reinvented here:
45 newtype Cmd a = Cmd { unCmd :: Connection -> IO a }
47 instance Monad Cmd where
48 return a = Cmd $ \_ -> return a
50 m >>= f = Cmd $ \c -> do
55 newtype Cmd a = Cmd (ReaderT Connection IO a)
56 deriving (Functor, Applicative, Monad, MonadReader Connection, MonadIO)
58 runCmd :: Connection -> Cmd a -> IO a
59 runCmd c (Cmd f) = runReaderT f c
61 mkCmd :: (Connection -> IO a) -> Cmd a
64 ------------------------------------------------------------------------
66 databaseParameters :: FilePath -> IO PGS.ConnectInfo
67 databaseParameters fp = do
69 let ini'' = case ini of
70 Left e -> panic (pack $ "No ini file error" <> show e)
73 let val x = case (lookupValue (pack "django") (pack x) ini'') of
74 Left _ -> panic (pack $ "no" <> x)
77 pure $ PGS.ConnectInfo { PGS.connectHost = val "DB_HOST"
78 , PGS.connectPort = read (val "DB_PORT") :: Word16
79 , PGS.connectUser = val "DB_USER"
80 , PGS.connectPassword = val "DB_PASS"
81 , PGS.connectDatabase = val "DB_NAME"
84 connectGargandb :: FilePath -> IO Connection
85 connectGargandb fp = databaseParameters fp >>= \params -> connect params
87 printSql :: Default Unpackspec a a => Query a -> IO ()
88 printSql = putStrLn . maybe "Empty query" identity . showSqlForPostgres
90 fromField' :: (Typeable b, FromJSON b) => Field -> Maybe DB.ByteString -> Conversion b
91 fromField' field mb = do
92 v <- fromField field mb
95 valueToHyperdata v = case fromJSON v of
97 Error _err -> returnError ConversionFailed field "cannot parse hyperdata"
99 -- | Opaleye leftJoin* functions
100 -- TODO add here from Node.hs