Permalink
Browse files

Persist-Postgresql assumes in insertSql' fixed column "id" (#102)

  • Loading branch information...
1 parent f22fe17 commit f1e9b94bb7b8eb33e2cfdb1f964651aff4a16d56 @snoyberg snoyberg committed Nov 20, 2012
@@ -126,8 +126,8 @@ prepare' conn sql = do
-- | SQL code to be executed when inserting an entity.
-insertSql' :: DBName -> [DBName] -> Either Text (Text, Text)
-insertSql' t cols = Right (doInsert, "SELECT LAST_INSERT_ID()")
+insertSql' :: DBName -> [DBName] -> DBName -> InsertSqlResult
+insertSql' t cols _ = ISRInsertGet doInsert "SELECT LAST_INSERT_ID()"
where
doInsert = pack $ concat
[ "INSERT INTO "
@@ -1,5 +1,5 @@
name: persistent-mysql
-version: 1.0.1
+version: 1.0.2
license: MIT
license-file: LICENSE
author: Felipe Lessa <felipe.lessa@gmail.com>, Michael Snoyman
@@ -28,7 +28,7 @@ library
, transformers >= 0.2.1 && < 0.4
, mysql-simple >= 0.2.2.3 && < 0.3
, mysql >= 0.1.1.3 && < 0.2
- , persistent >= 1.0 && < 1.1
+ , persistent >= 1.0.2 && < 1.1
, containers >= 0.2
, bytestring >= 0.9
, text >= 0.11.0.6 && < 0.12
@@ -127,15 +127,16 @@ prepare' conn sql = do
, withStmt = withStmt' conn query
}
-insertSql' :: DBName -> [DBName] -> Either Text (Text, Text)
-insertSql' t cols = Left $ pack $ concat
+insertSql' :: DBName -> [DBName] -> DBName -> InsertSqlResult
+insertSql' t cols id' = ISRSingle $ pack $ concat
[ "INSERT INTO "
, T.unpack $ escape t
, "("
, intercalate "," $ map (T.unpack . escape) cols
, ") VALUES("
, intercalate "," (map (const "?") cols)
- , ") RETURNING id"
+ , ") RETURNING "
+ , T.unpack $ escape id'
]
execute' :: PG.Connection -> PG.Query -> [PersistValue] -> IO ()
@@ -1,5 +1,5 @@
name: persistent-postgresql
-version: 1.0.0
+version: 1.0.1
license: MIT
license-file: LICENSE
author: Felipe Lessa, Michael Snoyman <michael@snoyman.com>
@@ -17,7 +17,7 @@ library
, transformers >= 0.2.1 && < 0.4
, postgresql-simple >= 0.2 && < 0.3
, postgresql-libpq >= 0.6.1 && < 0.9
- , persistent >= 1.0 && < 1.1
+ , persistent >= 1.0.2 && < 1.1
, containers >= 0.2
, bytestring >= 0.9
, text >= 0.7 && < 0.12
@@ -88,9 +88,9 @@ prepare' conn sql = do
, withStmt = withStmt' stmt
}
-insertSql' :: DBName -> [DBName] -> Either Text (Text, Text)
-insertSql' t cols =
- Right (pack ins, sel)
+insertSql' :: DBName -> [DBName] -> DBName -> InsertSqlResult
+insertSql' t cols _ =
+ ISRInsertGet (pack ins) sel
where
sel = "SELECT last_insert_rowid()"
ins = concat
@@ -1,5 +1,5 @@
name: persistent-sqlite
-version: 1.0.0
+version: 1.0.1
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
@@ -20,7 +20,7 @@ library
build-depends: base >= 4 && < 5
, bytestring >= 0.9.1
, transformers >= 0.2.1 && < 0.4
- , persistent >= 1.0 && < 1.1
+ , persistent >= 1.0.2 && < 1.1
, monad-control >= 0.2 && < 0.4
, containers >= 0.2
, text >= 0.7 && < 1
@@ -97,16 +97,16 @@ runSqlConn (SqlPersist r) conn = do
instance (MonadBaseControl IO m, MonadIO m, C.MonadThrow m, C.MonadUnsafeIO m, MonadLogger m) => PersistStore SqlPersist m where
insert val = do
conn <- SqlPersist ask
- let esql = insertSql conn (entityDB t) (map fieldDB $ entityFields t)
+ let esql = insertSql conn (entityDB t) (map fieldDB $ entityFields t) (entityID t)
i <-
case esql of
- Left sql -> C.runResourceT $ R.withStmt sql vals C.$$ do
+ ISRSingle sql -> C.runResourceT $ R.withStmt sql vals C.$$ do
x <- CL.head
case x of
Just [PersistInt64 i] -> return i
Nothing -> error $ "SQL insert did not return a result giving the generated ID"
Just vals' -> error $ "Invalid result from a SQL insert, got: " P.++ P.show vals'
- Right (sql1, sql2) -> do
+ ISRInsertGet sql1 sql2 -> do
execute' sql1 vals
C.runResourceT $ R.withStmt sql2 [] C.$$ do
Just [PersistInt64 i] <- CL.head
@@ -124,7 +124,9 @@ instance (MonadBaseControl IO m, MonadIO m, C.MonadThrow m, C.MonadUnsafeIO m, M
, escapeName conn (entityDB t)
, " SET "
, T.intercalate "," (map (go conn . fieldDB) $ entityFields t)
- , " WHERE id=?"
+ , " WHERE "
+ , escapeName conn $ entityID t
+ , "=?"
]
vals = map toPersistValue (toPersistFields val) `mappend` [unKey k]
execute' sql vals
@@ -148,7 +150,9 @@ instance (MonadBaseControl IO m, MonadIO m, C.MonadThrow m, C.MonadUnsafeIO m, M
, cols
, " FROM "
, escapeName conn $ entityDB t
- , " WHERE id=?"
+ , " WHERE "
+ , escapeName conn $ entityID t
+ , "=?"
]
vals' = [unKey k]
C.runResourceT $ R.withStmt sql vals' C.$$ do
@@ -168,7 +172,9 @@ instance (MonadBaseControl IO m, MonadIO m, C.MonadThrow m, C.MonadUnsafeIO m, M
sql conn = concat
[ "DELETE FROM "
, escapeName conn $ entityDB t
- , " WHERE id=?"
+ , " WHERE "
+ , escapeName conn $ entityID t
+ , "=?"
]
insrepHelper :: (MonadIO m, PersistEntity val, MonadLogger m)
@@ -15,6 +15,7 @@ module Database.Persist.GenericSql.Internal
, mkColumns
, Column (..)
, logSQL
+ , InsertSqlResult (..)
) where
import qualified Data.Map as Map
@@ -35,10 +36,13 @@ import Language.Haskell.TH.Syntax (Q, Exp)
import Control.Monad.Logger (logDebugS)
import Data.Maybe (mapMaybe, listToMaybe)
+data InsertSqlResult = ISRSingle Text
+ | ISRInsertGet Text Text
+
data Connection = Connection
{ prepare :: Text -> IO Statement
- -- ^ table name, column names, either 1 or 2 statements to run
- , insertSql :: DBName -> [DBName] -> Either Text (Text, Text)
+ -- ^ table name, column names, id name, either 1 or 2 statements to run
+ , insertSql :: DBName -> [DBName] -> DBName -> InsertSqlResult
, stmtMap :: IORef (Map.Map Text Statement)
, close :: IO ()
, migrateSql :: forall v. PersistEntity v
@@ -1,5 +1,5 @@
name: persistent
-version: 1.0.1.2
+version: 1.0.2
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>

0 comments on commit f1e9b94

Please sign in to comment.