forked from yesodweb/persistent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sqlite.hs
294 lines (272 loc) · 8.84 KB
/
Sqlite.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | A sqlite backend for persistent.
module Database.Persist.Sqlite
( withSqlitePool
, withSqliteConn
, createSqlitePool
, module Database.Persist
, module Database.Persist.GenericSql
, SqliteConf (..)
) where
import Database.Persist hiding (Entity (..))
import Database.Persist.Store
import Database.Persist.EntityDef
import Database.Persist.GenericSql hiding (Key)
import Database.Persist.GenericSql.Internal
import qualified Database.Sqlite as Sqlite
import Control.Monad.IO.Class (MonadIO (..))
import Data.List (intercalate)
import Data.IORef
import qualified Data.Map as Map
import Control.Monad.Trans.Control (control)
import qualified Control.Exception as E
import Data.Text (Text, pack)
import Control.Monad (mzero)
import Data.Aeson
import qualified Data.Text as T
import Data.Conduit
import qualified Data.Conduit.List as CL
import Control.Applicative
createSqlitePool :: MonadIO m => Text -> Int -> m ConnectionPool
createSqlitePool s = createSqlPool $ open' s
withSqlitePool :: (MonadBaseControl IO m, MonadIO m)
=> Text
-> Int -- ^ number of connections to open
-> (ConnectionPool -> m a) -> m a
withSqlitePool s = withSqlPool $ open' s
withSqliteConn :: (MonadBaseControl IO m, MonadIO m)
=> Text -> (Connection -> m a) -> m a
withSqliteConn = withSqlConn . open'
open' :: Text -> IO Connection
open' s = do
conn <- Sqlite.open s
smap <- newIORef $ Map.empty
return Connection
{ prepare = prepare' conn
, stmtMap = smap
, insertSql = insertSql'
, close = Sqlite.close conn
, migrateSql = migrate'
, begin = helper "BEGIN"
, commitC = helper "COMMIT"
, rollbackC = ignoreExceptions . helper "ROLLBACK"
, escapeName = escape
, noLimit = "LIMIT -1"
}
where
helper t getter = do
stmt <- getter t
execute stmt []
reset stmt
ignoreExceptions = E.handle (\(_ :: E.SomeException) -> return ())
prepare' :: Sqlite.Connection -> Text -> IO Statement
prepare' conn sql = do
stmt <- Sqlite.prepare conn sql
return Statement
{ finalize = Sqlite.finalize stmt
, reset = Sqlite.reset stmt
, execute = execute' stmt
, withStmt = withStmt' stmt
}
insertSql' :: DBName -> [DBName] -> DBName -> InsertSqlResult
insertSql' t cols _ =
ISRInsertGet (pack ins) sel
where
sel = "SELECT last_insert_rowid()"
ins = concat
[ "INSERT INTO "
, escape' t
, "("
, intercalate "," $ map escape' cols
, ") VALUES("
, intercalate "," (map (const "?") cols)
, ")"
]
execute' :: Sqlite.Statement -> [PersistValue] -> IO ()
execute' stmt vals = flip finally (liftIO $ Sqlite.reset stmt) $ do
Sqlite.bind stmt vals
Sqlite.Done <- Sqlite.step stmt
return ()
withStmt'
:: MonadResource m
=> Sqlite.Statement
-> [PersistValue]
-> Source m [PersistValue]
withStmt' stmt vals = bracketP
(Sqlite.bind stmt vals >> return stmt)
Sqlite.reset
(const pull)
where
pull = do
x <- liftIO $ Sqlite.step stmt
case x of
Sqlite.Done -> return ()
Sqlite.Row -> do
cols <- liftIO $ Sqlite.columns stmt
yield cols
pull
showSqlType :: SqlType -> String
showSqlType SqlString = "VARCHAR"
showSqlType SqlInt32 = "INTEGER"
showSqlType SqlInt64 = "INTEGER"
showSqlType SqlReal = "REAL"
showSqlType SqlDay = "DATE"
showSqlType SqlTime = "TIME"
showSqlType SqlDayTimeZoned = "TIMESTAMP"
showSqlType SqlDayTime = "TIMESTAMP"
showSqlType SqlBlob = "BLOB"
showSqlType SqlBool = "BOOLEAN"
showSqlType (SqlOther t) = T.unpack t
migrate' :: PersistEntity val
=> [EntityDef]
-> (Text -> IO Statement)
-> val
-> IO (Either [Text] [(Bool, Text)])
migrate' allDefs getter val = do
let (cols, uniqs) = mkColumns allDefs val
let newSql = mkCreateTable False def (cols, uniqs)
stmt <- getter "SELECT sql FROM sqlite_master WHERE type='table' AND name=?"
oldSql' <- runResourceT
$ withStmt stmt [PersistText $ unDBName table] $$ go
case oldSql' of
Nothing -> return $ Right [(False, newSql)]
Just oldSql ->
if oldSql == newSql
then return $ Right []
else do
sql <- getCopyTable allDefs getter val
return $ Right sql
where
def = entityDef val
table = entityDB def
go = do
x <- CL.head
case x of
Nothing -> return Nothing
Just [PersistText y] -> return $ Just y
Just y -> error $ "Unexpected result from sqlite_master: " ++ show y
getCopyTable :: PersistEntity val
=> [EntityDef]
-> (Text -> IO Statement)
-> val
-> IO [(Bool, Sql)]
getCopyTable allDefs getter val = do
stmt <- getter $ pack $ "PRAGMA table_info(" ++ escape' table ++ ")"
oldCols' <- runResourceT $ withStmt stmt [] $$ getCols
let oldCols = map DBName $ filter (/= "id") oldCols' -- need to update for table id attribute ?
let newCols = map cName cols
let common = filter (`elem` oldCols) newCols
let id_ = entityID $ entityDef val
return [ (False, tmpSql)
, (False, copyToTemp $ id_ : common)
, (common /= oldCols, pack dropOld)
, (False, newSql)
, (False, copyToFinal $ id_ : newCols)
, (False, pack dropTmp)
]
where
def = entityDef val
getCols = do
x <- CL.head
case x of
Nothing -> return []
Just (_:PersistText name:_) -> do
names <- getCols
return $ name : names
Just y -> error $ "Invalid result from PRAGMA table_info: " ++ show y
table = entityDB def
tableTmp = DBName $ unDBName table `T.append` "_backup"
(cols, uniqs) = mkColumns allDefs val
newSql = mkCreateTable False def (cols, uniqs)
tmpSql = mkCreateTable True def { entityDB = tableTmp } (cols, uniqs)
dropTmp = "DROP TABLE " ++ escape' tableTmp
dropOld = "DROP TABLE " ++ escape' table
copyToTemp common = pack $ concat
[ "INSERT INTO "
, escape' tableTmp
, "("
, intercalate "," $ map escape' common
, ") SELECT "
, intercalate "," $ map escape' common
, " FROM "
, escape' table
]
copyToFinal newCols = pack $ concat
[ "INSERT INTO "
, T.unpack $ escape table
, " SELECT "
, intercalate "," $ map escape' newCols
, " FROM "
, escape' tableTmp
]
escape' :: DBName -> String
escape' = T.unpack . escape
mkCreateTable :: Bool -> EntityDef -> ([Column], [UniqueDef]) -> Sql
mkCreateTable isTemp entity (cols, uniqs) = pack $ concat
[ "CREATE"
, if isTemp then " TEMP" else ""
, " TABLE "
, T.unpack $ escape $ entityDB entity
, "("
, T.unpack $ escape $ entityID entity
, " INTEGER PRIMARY KEY"
, concatMap sqlColumn cols
, concatMap sqlUnique uniqs
, ")"
]
sqlColumn :: Column -> String
sqlColumn (Column name isNull typ def _maxLen ref) = concat
[ ","
, T.unpack $ escape name
, " "
, showSqlType typ
, if isNull then " NULL" else " NOT NULL"
, case def of
Nothing -> ""
Just d -> " DEFAULT " ++ T.unpack d
, case ref of
Nothing -> ""
Just (table, _) -> " REFERENCES " ++ T.unpack (escape table)
]
sqlUnique :: UniqueDef -> String
sqlUnique (UniqueDef _ cname cols _) = concat
[ ",CONSTRAINT "
, T.unpack $ escape cname
, " UNIQUE ("
, intercalate "," $ map (T.unpack . escape . snd) cols
, ")"
]
type Sql = Text
escape :: DBName -> Text
escape (DBName s) =
T.concat [q, T.concatMap go s, q]
where
q = T.singleton '"'
go '"' = "\"\""
go c = T.singleton c
-- | Information required to connect to a sqlite database
data SqliteConf = SqliteConf
{ sqlDatabase :: Text
, sqlPoolSize :: Int
}
instance PersistConfig SqliteConf where
type PersistConfigBackend SqliteConf = SqlPersist
type PersistConfigPool SqliteConf = ConnectionPool
createPoolConfig (SqliteConf cs size) = createSqlitePool cs size
runPool _ = runSqlPool
loadConfig (Object o) =
SqliteConf <$> o .: "database"
<*> o .: "poolsize"
loadConfig _ = mzero
finally :: MonadBaseControl IO m
=> m a -- ^ computation to run first
-> m b -- ^ computation to run afterward (even if an exception was raised)
-> m a
finally a sequel = control $ \runInIO ->
E.finally (runInIO a)
(runInIO sequel)
{-# INLINABLE finally #-}