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 #-}
18 module Gargantext.Database.Utils where
20 import qualified Database.PostgreSQL.Simple as PGS
22 import Data.Monoid ((<>))
23 import Data.Either.Extra (Either(Left, Right))
24 import Gargantext.Prelude
25 import Data.Text (unpack, pack)
26 import Text.Read (read)
27 import Data.Ini (readIniFile, lookupValue)
28 import Data.Word (Word16)
29 import System.IO (FilePath)
30 import Database.PostgreSQL.Simple (Connection, connect)
33 import Opaleye (Query, Unpackspec, showSqlForPostgres)
34 import Data.Profunctor.Product.Default (Default)
35 import Data.Maybe (maybe)
36 import Prelude (id, putStrLn)
37 -- TODO add a reader Monad here
38 -- read this in the init file
40 databaseParameters :: FilePath -> IO PGS.ConnectInfo
41 databaseParameters fp = do
43 let ini'' = case ini of
44 Left e -> panic (pack $ "No ini file error" <> show e)
47 let val x = case (lookupValue (pack "django") (pack x) ini'') of
48 Left _ -> panic (pack $ "no" <> x)
51 pure $ PGS.ConnectInfo { PGS.connectHost = val "DB_HOST"
52 , PGS.connectPort = read (val "DB_PORT") :: Word16
53 , PGS.connectUser = val "DB_USER"
54 , PGS.connectPassword = val "DB_PASS"
55 , PGS.connectDatabase = val "DB_NAME"
58 connectGargandb :: FilePath -> IO Connection
59 connectGargandb fp = do
60 parameters <- databaseParameters fp
63 printSql :: Default Unpackspec a a => Query a -> IO ()
64 printSql = putStrLn . maybe "Empty query" id . showSqlForPostgres