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 -- TODO add a reader Monad here
37 -- read this in the init file
39 databaseParameters :: FilePath -> IO PGS.ConnectInfo
40 databaseParameters fp = do
42 let ini'' = case ini of
43 Left e -> panic (pack $ "No ini file error" <> show e)
46 let val x = case (lookupValue (pack "django") (pack x) ini'') of
47 Left _ -> panic (pack $ "no" <> x)
50 pure $ PGS.ConnectInfo { PGS.connectHost = val "DB_HOST"
51 , PGS.connectPort = read (val "DB_PORT") :: Word16
52 , PGS.connectUser = val "DB_USER"
53 , PGS.connectPassword = val "DB_PASS"
54 , PGS.connectDatabase = val "DB_NAME"
57 connectGargandb :: FilePath -> IO Connection
58 connectGargandb fp = do
59 parameters <- databaseParameters fp
62 printSql :: Default Unpackspec a a => Query a -> IO ()
63 printSql = putStrLn . maybe "Empty query" identity . showSqlForPostgres