Fix insert

This commit is contained in:
Joe Nelson
2014-12-06 17:42:20 -08:00
parent eb7e9586ab
commit 8e9bbe6dcb
2 changed files with 14 additions and 2 deletions
+1 -1
View File
@@ -130,7 +130,7 @@ app req =
let vals = elems obj let vals = elems obj
H.unit . coerce $ iffNotT H.unit . coerce $ iffNotT
(whereT qq $ update qt cols vals) (whereT qq $ update qt cols vals)
(insertInto qt cols vals) (insertSelect qt cols vals)
return $ responseLBS status204 [ jsonH ] "" return $ responseLBS status204 [ jsonH ] ""
else return $ if Prelude.null tableCols else return $ if Prelude.null tableCols
+13 -1
View File
@@ -68,7 +68,7 @@ parentheticT (sql, params, pre) =
iffNotT :: DynamicSQL -> StatementT iffNotT :: DynamicSQL -> StatementT
iffNotT (aq, ap, apre) (bq, bp, bpre) = iffNotT (aq, ap, apre) (bq, bp, bpre) =
("WITH aaa AS (" <> aq <> " returning *) " <> ("WITH aaa AS (" <> aq <> " returning *) " <>
bq <> "WHERE NOT EXISTS (SELECT * FROM aaa)" bq <> " WHERE NOT EXISTS (SELECT * FROM aaa)"
, ap ++ bp , ap ++ bp
, All $ getAll apre && getAll bpre , All $ getAll apre && getAll bpre
) )
@@ -100,6 +100,18 @@ insertInto t cols vals =
, mempty , mempty
) )
insertSelect :: QualifiedTable -> [Text] -> [JSON.Value] -> DynamicSQL
insertSelect t [] _ =
("insert into " <> fromQt t <> " default values returning *", [], mempty)
insertSelect t cols vals =
("insert into " <> fromQt t <> " (" <>
cs (intercalate ", " (map pgFmtIdent cols)) <>
") select " <>
cs (intercalate ", " (map (const "?") vals))
, map pgParam vals
, mempty
)
update :: QualifiedTable -> [Text] -> [JSON.Value] -> DynamicSQL update :: QualifiedTable -> [Text] -> [JSON.Value] -> DynamicSQL
update t cols vals = update t cols vals =
("update " <> fromQt t <> " set (" <> ("update " <> fromQt t <> " set (" <>