Build sql for inserting multiple rows
This commit is contained in:
+1
-1
@@ -101,7 +101,7 @@ app v1schema reqBody req =
|
|||||||
([table], "POST") ->
|
([table], "POST") ->
|
||||||
handleJsonObj reqBody $ \obj -> do
|
handleJsonObj reqBody $ \obj -> do
|
||||||
let qt = QualifiedTable schema (cs table)
|
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"
|
echoRequested = lookup "Prefer" hdrs == Just "return=representation"
|
||||||
row <- H.maybeEx query
|
row <- H.maybeEx query
|
||||||
let (Identity insertedJson) = fromMaybe (Identity "{}" :: Identity Text) row
|
let (Identity insertedJson) = fromMaybe (Identity "{}" :: Identity Text) row
|
||||||
|
|||||||
+9
-4
@@ -108,15 +108,20 @@ returningStarT s = s { B.stmtTemplate = B.stmtTemplate s <> " RETURNING *" }
|
|||||||
deleteFrom :: QualifiedTable -> PStmt
|
deleteFrom :: QualifiedTable -> PStmt
|
||||||
deleteFrom t = B.Stmt ("delete from " <> fromQt t) empty True
|
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
|
insertInto t [] _ = B.Stmt
|
||||||
("insert into " <> fromQt t <> " default values returning *") empty True
|
("insert into " <> fromQt t <> " default values returning *") empty True
|
||||||
insertInto t cols vals = B.Stmt
|
insertInto t cols vals = B.Stmt
|
||||||
("insert into " <> fromQt t <> " (" <>
|
("insert into " <> fromQt t <> " (" <>
|
||||||
T.intercalate ", " (map pgFmtIdent cols) <>
|
T.intercalate ", " (map pgFmtIdent cols) <>
|
||||||
") values ("
|
") values "
|
||||||
<> T.intercalate ", " (map insertableValue vals)
|
<> T.intercalate ", "
|
||||||
<> ") returning row_to_json(" <> fromQt t <> ".*)")
|
(map (\v -> "("
|
||||||
|
<> T.intercalate ", " (map insertableValue v)
|
||||||
|
<> ")"
|
||||||
|
) vals
|
||||||
|
)
|
||||||
|
<> " returning row_to_json(" <> fromQt t <> ".*)")
|
||||||
empty True
|
empty True
|
||||||
|
|
||||||
insertSelect :: QualifiedTable -> [T.Text] -> [JSON.Value] -> PStmt
|
insertSelect :: QualifiedTable -> [T.Text] -> [JSON.Value] -> PStmt
|
||||||
|
|||||||
Reference in New Issue
Block a user