/
Config.hs
61 lines (53 loc) · 1.95 KB
/
Config.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE OverloadedStrings #-}
module Database.Selda.SQL.Print.Config (PPConfig (..), defPPConfig) where
import Data.Text (Text)
import qualified Data.Text as T
import Database.Selda.SqlType
import Database.Selda.Table
-- | Backend-specific configuration for the SQL pretty-printer.
data PPConfig = PPConfig
{ -- | The SQL type name of the given type.
ppType :: SqlTypeRep -> Text
-- | Parameter placeholder for the @n@th parameter.
, ppPlaceholder :: Int -> Text
-- | List of column attributes.
, ppColAttrs :: [ColAttr] -> Text
-- | The value used for the next value for an auto-incrementing column.
-- For instance, @DEFAULT@ for PostgreSQL, and @NULL@ for SQLite.
, ppAutoIncInsert :: Text
-- | Insert queries may have at most this many parameters; if an insertion
-- has more parameters than this, it will be chunked.
--
-- Note that only insertions of multiple rows are chunked. If your table
-- has more than this many columns, you should really rethink
-- your database design.
, ppMaxInsertParams :: Maybe Int
}
-- | Default settings for pretty-printing.
-- Geared towards SQLite.
defPPConfig :: PPConfig
defPPConfig = PPConfig
{ ppType = defType
, ppPlaceholder = T.cons '$' . T.pack . show
, ppColAttrs = T.unwords . map defColAttr
, ppAutoIncInsert = "NULL"
, ppMaxInsertParams = Nothing
}
-- | Default compilation for SQL types.
defType :: SqlTypeRep -> Text
defType TText = "TEXT"
defType TRowID = "INTEGER"
defType TInt = "INT"
defType TFloat = "DOUBLE"
defType TBool = "BOOLEAN"
defType TDateTime = "DATETIME"
defType TDate = "DATE"
defType TTime = "TIME"
defType TBlob = "BLOB"
-- | Default compilation for a column attribute.
defColAttr :: ColAttr -> Text
defColAttr Primary = "PRIMARY KEY"
defColAttr AutoIncrement = "AUTOINCREMENT"
defColAttr Required = "NOT NULL"
defColAttr Optional = "NULL"
defColAttr Unique = "UNIQUE"