on dev help sign in
insertInto
commite1912e93521cf5e02906d12e081befa206c2fcc1 authorEitan Chatav <eitan.chatav@gmail.com> dateThu 10 Aug 2017, 10:13 (9 years ago) parent0e94cc0
3 files changed+20−25
squeel-postgresql/exe/Example.hs+2−2 squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+12−17 squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+6−6
squeel-postgresql/exe/Example.hs+2−2
@@ -41,8 +41,8 @@ main = do
4141 Char8.putStrLn "querying"
4242 connection2 <- flip execPQ (connection1 :: Connection Tables) $ do
4343 for_ [I i :* I (i+1) :* Nil | i <- [1::Int32,3..9]] $ pqExecParams
44− ( into #table1
45− (insertRow (param1 `As` #col1 :* param2 `As` #col2 :* Nil))
44+ ( insertInto #table1
45+ (Values (param1 `As` #col1 :* param2 `As` #col2 :* Nil) [])
4646 Conflict ReturningNil )
4747 Just result <- pqExecNil . query $
4848 selectStar (from (Table (#table1 `As` #table1)))
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+12−17
@@ -983,39 +983,34 @@ alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
983983INSERT statements
984984-----------------------------------------}
985985
986−into
986+insertInto
987987 :: (SListI columns, SListI results, HasTable table schema columns)
988988 => Alias table
989− -> InsertClause schema params columns
989+ -> ValuesClause schema params columns
990990 -> ConflictClause params columns
991991 -> ReturningClause params columns results
992992 -> Manipulation schema params results
993−into table insert conflict returning = UnsafeManipulation $
993+insertInto table insert conflict returning = UnsafeManipulation $
994994 "INSERT" <+> "INTO" <+> renderAlias table
995− <+> renderInsertClause insert
995+ <+> renderValuesClause insert
996996 <> renderConflictClause conflict
997997 <> renderReturningClause returning
998998
999−data InsertClause
999+data ValuesClause
10001000 (schema :: [(Symbol,[(Symbol,ColumnType)])])
10011001 (params :: [ColumnType])
10021002 (columns :: [(Symbol,ColumnType)])
1003− = InsertValues
1003+ = Values
10041004 (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
10051005 [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1006− | InsertQuery (Query schema params columns)
1006+ | ValuesQuery (Query schema params columns)
10071007
1008−insertRow
1009− :: (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1010− -> InsertClause schema params columns
1011−insertRow row = InsertValues row []
1012−
1013−renderInsertClause
1008+renderValuesClause
10141009 :: SListI columns
1015− => InsertClause schema params columns
1010+ => ValuesClause schema params columns
10161011 -> ByteString
1017−renderInsertClause = \case
1018− InsertValues row rows ->
1012+renderValuesClause = \case
1013+ Values row rows ->
10191014 parenthesized (renderCommaSeparated renderAliasPart row)
10201015 <+> "VALUES"
10211016 <+> commaSeparated
@@ -1025,7 +1020,7 @@ renderInsertClause = \case
10251020 :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
10261021 renderAliasPart (_ `As` name) = renderAlias name
10271022 renderValuePart (value `As` _) = renderExpression value
1028− InsertQuery q -> renderQuery q
1023+ ValuesQuery q -> renderQuery q
10291024
10301025data ReturningClause
10311026 (params :: [ColumnType])
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+6−6
@@ -98,7 +98,7 @@ spec = do
9898 let
9999 statement :: Manipulation Tables '[] '[]
100100 statement =
101− into #table1 (insertRow $ 2 `As` #col1 :* 4 `As` #col2 :* Nil)
101+ insertInto #table1 (Values (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
102102 Conflict ReturningNil
103103 statement `manipulationRenders`
104104 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
@@ -106,7 +106,7 @@ spec = do
106106 let
107107 statement :: Manipulation Tables '[] SumAndCol1
108108 statement =
109− into #table1 (insertRow $ 2 `As` #col1 :* 4 `As` #col2 :* Nil)
109+ insertInto #table1 (Values (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
110110 Conflict
111111 (Returning $ (#col1 + #col2) `As` #sum :* #col1 `As` #col1 :* Nil)
112112 statement `manipulationRenders`
@@ -136,7 +136,7 @@ spec = do
136136 let
137137 statement :: Manipulation Tables '[] '[]
138138 statement =
139− into #table1 (insertRow $ 2 `As` #col1 :* 4 `As` #col2 :* Nil)
139+ insertInto #table1 (Values (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
140140 (OnConflictDoUpdate
141141 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil) (#col1 /=* #col2))
142142 ReturningNil
@@ -148,7 +148,7 @@ spec = do
148148 let
149149 statement :: Manipulation Tables '[] SumAndCol1
150150 statement =
151− into #table1 (insertRow $ 2 `As` #col1 :* 4 `As` #col2 :* Nil)
151+ insertInto #table1 (Values (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
152152 (OnConflictDoUpdate
153153 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
154154 (#col1 /=* #col2))
@@ -168,8 +168,8 @@ spec = do
168168 it "should be safe against SQL injection in literal text" $ do
169169 let
170170 statement :: Manipulation StudentsTable '[] '[]
171− statement = into #students
172− (insertRow $ "Robert'); DROP TABLE students;" `As` #name :* Nil)
171+ statement = insertInto #students
172+ (Values ("Robert'); DROP TABLE students;" `As` #name :* Nil) [])
173173 Conflict ReturningNil
174174 statement `manipulationRenders`
175175 "INSERT INTO students (name) VALUES (E'Robert''); DROP TABLE students;');"