Merge pull request #618 from begriffs/post-empty-obj
Use table defaults for empty object insert
This commit is contained in:
@@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- Ensure that Location header in 201 response is URL-encoded - @league
|
- Ensure that Location header in 201 response is URL-encoded - @league
|
||||||
- Fix garbage collector CPU leak - @ruslantalpa et al.
|
- Fix garbage collector CPU leak - @ruslantalpa et al.
|
||||||
- Return deleted items when return=representation header is sent - @ruslantalpa
|
- Return deleted items when return=representation header is sent - @ruslantalpa
|
||||||
|
- Use table default values for empty object inserts - @begriffs
|
||||||
|
|
||||||
## [0.3.1.1] - 2016-03-28
|
## [0.3.1.1] - 2016-03-28
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ import qualified Data.HashMap.Strict as HM
|
|||||||
import Data.List (find)
|
import Data.List (find)
|
||||||
import Data.Monoid ((<>))
|
import Data.Monoid ((<>))
|
||||||
import Data.Text (Text, intercalate, unwords, replace, isInfixOf, toLower, split)
|
import Data.Text (Text, intercalate, unwords, replace, isInfixOf, toLower, split)
|
||||||
import qualified Data.Text as T (map, takeWhile)
|
import qualified Data.Text as T (map, takeWhile, null)
|
||||||
import qualified Data.Text.Encoding as T
|
import qualified Data.Text.Encoding as T
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
import Control.Applicative ((<|>))
|
import Control.Applicative ((<|>))
|
||||||
@@ -336,13 +336,15 @@ requestToQuery schema (DbRead (Node (Select colSelects tbls conditions ord range
|
|||||||
requestToQuery schema (DbMutate (Insert mainTbl (PayloadJSON (UniformObjects rows)))) =
|
requestToQuery schema (DbMutate (Insert mainTbl (PayloadJSON (UniformObjects rows)))) =
|
||||||
let qi = QualifiedIdentifier schema mainTbl
|
let qi = QualifiedIdentifier schema mainTbl
|
||||||
cols = map pgFmtIdent $ fromMaybe [] (HM.keys <$> (rows V.!? 0))
|
cols = map pgFmtIdent $ fromMaybe [] (HM.keys <$> (rows V.!? 0))
|
||||||
colsString = intercalate ", " cols in
|
colsString = intercalate ", " cols
|
||||||
unwords [
|
insInto = unwords [ "INSERT INTO" , fromQi qi,
|
||||||
"INSERT INTO ", fromQi qi,
|
if T.null colsString then "" else "(" <> colsString <> ")"
|
||||||
" (" <> colsString <> ")" <>
|
]
|
||||||
" SELECT " <> colsString <>
|
vals = unwords $ if T.null colsString
|
||||||
" FROM json_populate_recordset(null::" , fromQi qi, ", $1)"
|
then ["DEFAULT VALUES"]
|
||||||
]
|
else ["SELECT", colsString, "FROM json_populate_recordset(null::" , fromQi qi, ", $1)"] in
|
||||||
|
insInto <> vals
|
||||||
|
|
||||||
requestToQuery schema (DbMutate (Update mainTbl (PayloadJSON (UniformObjects rows)) conditions)) =
|
requestToQuery schema (DbMutate (Update mainTbl (PayloadJSON (UniformObjects rows)) conditions)) =
|
||||||
case rows V.!? 0 of
|
case rows V.!? 0 of
|
||||||
Just obj ->
|
Just obj ->
|
||||||
|
|||||||
@@ -189,6 +189,14 @@ spec = do
|
|||||||
, matchHeaders = ["Location" <:> location]
|
, matchHeaders = ["Location" <:> location]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context "empty object" $
|
||||||
|
it "successfully populates table with all-default columns" $
|
||||||
|
post "/items" "{}" `shouldRespondWith` ResponseMatcher {
|
||||||
|
matchBody = Just ""
|
||||||
|
, matchStatus = 201
|
||||||
|
, matchHeaders = []
|
||||||
|
}
|
||||||
|
|
||||||
describe "CSV insert" $ do
|
describe "CSV insert" $ do
|
||||||
|
|
||||||
context "disparate csv types" $
|
context "disparate csv types" $
|
||||||
|
|||||||
Vendored
+1
-1
@@ -204,7 +204,7 @@ INSERT INTO items VALUES (15);
|
|||||||
-- Name: items_id_seq; Type: SEQUENCE SET; Schema: test; Owner: -
|
-- Name: items_id_seq; Type: SEQUENCE SET; Schema: test; Owner: -
|
||||||
--
|
--
|
||||||
|
|
||||||
SELECT pg_catalog.setval('items_id_seq', 1, true);
|
SELECT pg_catalog.setval('items_id_seq', 15, true);
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
|
|||||||
Reference in New Issue
Block a user