Build sql for inserting multiple rows

This commit is contained in:
Joe Nelson
2015-04-04 20:20:13 -07:00
parent 532cfdff95
commit a22cf82688
2 changed files with 10 additions and 5 deletions
+1 -1
View File
@@ -101,7 +101,7 @@ app v1schema reqBody req =
([table], "POST") ->
handleJsonObj reqBody $ \obj -> do
let qt = QualifiedTable schema (cs table)
query = insertInto qt (map cs $ keys obj) (elems obj)
query = insertInto qt (map cs $ keys obj) [(elems obj)]
echoRequested = lookup "Prefer" hdrs == Just "return=representation"
row <- H.maybeEx query
let (Identity insertedJson) = fromMaybe (Identity "{}" :: Identity Text) row
+9 -4
View File
@@ -108,15 +108,20 @@ returningStarT s = s { B.stmtTemplate = B.stmtTemplate s <> " RETURNING *" }
deleteFrom :: QualifiedTable -> PStmt
deleteFrom t = B.Stmt ("delete from " <> fromQt t) empty True
insertInto :: QualifiedTable -> [T.Text] -> [JSON.Value] -> PStmt
insertInto :: QualifiedTable -> [T.Text] -> [[JSON.Value]] -> PStmt
insertInto t [] _ = B.Stmt
("insert into " <> fromQt t <> " default values returning *") empty True
insertInto t cols vals = B.Stmt
("insert into " <> fromQt t <> " (" <>
T.intercalate ", " (map pgFmtIdent cols) <>
") values ("
<> T.intercalate ", " (map insertableValue vals)
<> ") returning row_to_json(" <> fromQt t <> ".*)")
") values "
<> T.intercalate ", "
(map (\v -> "("
<> T.intercalate ", " (map insertableValue v)
<> ")"
) vals
)
<> " returning row_to_json(" <> fromQt t <> ".*)")
empty True
insertSelect :: QualifiedTable -> [T.Text] -> [JSON.Value] -> PStmt