mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 17:48:15 +00:00
core: Mobile.hs postgres interface (#5545)
* core: Mobile.hs postgres interface * sqlite * fix * errors * postgres * rename * rename, refactor * merge files * rename * update simplexmq --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
This commit is contained in:
1 parent
0e940719c1
commit
20fa30eacc
23 files changed
+177
-182
No files matched your search
@@ -1,37 +1,68 @@
|
||||
{-# LANGUAGE ApplicativeDo #-}
|
||||
{-# LANGUAGE DuplicateRecordFields #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
|
||||
module Simplex.Chat.Options.Postgres where
|
||||
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import Foreign.C.String
|
||||
import Options.Applicative
|
||||
import Simplex.Messaging.Agent.Store.Interface (DBOpts (..))
|
||||
|
||||
data ChatDbOpts = ChatDbOpts
|
||||
{ dbName :: String,
|
||||
dbUser :: String,
|
||||
{ dbConnstr :: String,
|
||||
dbSchemaPrefix :: String
|
||||
}
|
||||
|
||||
chatDbOptsP :: FilePath -> String -> Parser ChatDbOpts
|
||||
chatDbOptsP _appDir defaultDbName = do
|
||||
dbName <-
|
||||
dbConnstr <-
|
||||
strOption
|
||||
( long "database"
|
||||
<> short 'd'
|
||||
<> metavar "DB_NAME"
|
||||
<> help "Database name"
|
||||
<> value defaultDbName
|
||||
<> metavar "DB_CONN"
|
||||
<> help "Database connection string"
|
||||
<> value ("postgresql://simplex@/" <> defaultDbName)
|
||||
<> showDefault
|
||||
)
|
||||
dbUser <-
|
||||
dbSchemaPrefix <-
|
||||
strOption
|
||||
( long "database-user"
|
||||
<> short 'u'
|
||||
<> metavar "DB_USER"
|
||||
<> help "Database user"
|
||||
<> value "simplex"
|
||||
( long "schema-prefix"
|
||||
<> metavar "DB_SCHEMA_PREFIX"
|
||||
<> help "Database schema prefix"
|
||||
<> value "simplex_v1"
|
||||
<> showDefault
|
||||
)
|
||||
pure ChatDbOpts {dbName, dbUser, dbSchemaPrefix = ""}
|
||||
pure ChatDbOpts {dbConnstr, dbSchemaPrefix}
|
||||
|
||||
dbString :: ChatDbOpts -> String
|
||||
dbString ChatDbOpts {dbName} = dbName
|
||||
dbString ChatDbOpts {dbConnstr} = dbConnstr
|
||||
|
||||
toDBOpts :: ChatDbOpts -> String -> Bool -> DBOpts
|
||||
toDBOpts ChatDbOpts {dbConnstr, dbSchemaPrefix} dbSuffix _keepKey =
|
||||
DBOpts
|
||||
{ connstr = B.pack dbConnstr,
|
||||
schema = if null dbSchemaPrefix then "simplex_v1" <> dbSuffix else dbSchemaPrefix <> dbSuffix
|
||||
}
|
||||
|
||||
chatSuffix :: String
|
||||
chatSuffix = "_chat_schema"
|
||||
|
||||
agentSuffix :: String
|
||||
agentSuffix = "_agent_schema"
|
||||
|
||||
mobileDbOpts :: CString -> CString -> IO ChatDbOpts
|
||||
mobileDbOpts schemaPrefix connstr = do
|
||||
dbSchemaPrefix <- peekCString schemaPrefix
|
||||
dbConnstr <- peekCString connstr
|
||||
pure $
|
||||
ChatDbOpts
|
||||
{ dbConnstr,
|
||||
dbSchemaPrefix
|
||||
}
|
||||
|
||||
removeDbKey :: ChatDbOpts -> ChatDbOpts
|
||||
removeDbKey = id
|
||||
|
||||
errorDbStr :: DBOpts -> String
|
||||
errorDbStr DBOpts {schema} = schema
|
||||
Reference in new issue
Block a user