Skip to content

Commit 19fbca9

Browse files
committed
Allow RETURNING on INSERT
1 parent 7d02254 commit 19fbca9

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Items of the DSL:
4545
* `fetch_first(N, type)` - equivalent to `FETCH FIRST N ROWS type`. Type can be `only (ONLY)` or `with_ties (WITH TIES)`.
4646
* `insert_into(table, [col1, col2,...])` - equivalent to `INSERT INTO table (col1, col2, ...)`. Must be followed by `values`.
4747
* `values(val1, val2, ...)` - equivalent to `VALUES (val1, val2, ...)`. Strings here are escaped so it's **safe**.
48+
* `returning(col1, col2, ...)` equivalent to `RETURNING col1, col2`.
4849
* `update(table)` - equivalent to `UPDATE table`. Must be followed by `set`
4950
* `set(Sets)` - equivalent to `SET Col1 = Val1, ...`. Similar to WHERE but only = is allowed. Optionally, you can add a `where+` after a `set`.
5051
* `delete(table)` - equivalent to `DELETE table`. Must be followed by `where`.
@@ -114,4 +115,4 @@ test :-
114115
postgresql:sql(Connection, [update(test_table), set(name = "test2"), where(id = 1)], data([])),
115116
postgresql:sql(Connection, [select(id, name), from(test_table), where(name = "test")], Rows2),
116117
Rows2 = data([]).
117-
```
118+
```

sql_query.pl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,26 @@
269269
") ",
270270
sql_query_insert_values(Values, Vars).
271271

272+
sql_query_insert_values([Values0|Next], Vars) -->
273+
{ Values0 =.. [values|Values] },
274+
"VALUES (",
275+
{ values_args(Values, Args, [], Vars) },
276+
quoted_comma_separated_list(Args),
277+
")",
278+
sql_query_insert_returning(Next).
279+
272280
sql_query_insert_values([Values0], Vars) -->
273281
{ Values0 =.. [values|Values] },
274282
"VALUES (",
275283
{ values_args(Values, Args, [], Vars) },
276284
quoted_comma_separated_list(Args),
277285
")".
278286

287+
sql_query_insert_returning([Returning0]) -->
288+
{ Returning0 =.. [returning|Columns] },
289+
"RETURNING ",
290+
quoted_comma_separated_list(Columns).
291+
279292
values_args([], [], X, X).
280293
values_args([Value|Values], [Arg|Args], Vars0, Vars) :-
281294
sql_var(Value, Arg, Vars0, Vars1),

tests.lgt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@
166166
postgresql:sql(Connection, [insert_into(country, [iso_code, name]), values("PT", "Portugal")], data([])),
167167
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Miguel de Cervantes", "ES", 1547)], data([])),
168168
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Magallanes", "PT", 1480)], data([])),
169-
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Picasso", "ES", 1881)], data([])),
169+
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Picasso", "ES", 1881), returning(name, country)], data([["Picasso", "ES"]])),
170170
postgresql:sql(Connection, [select('famous.name','country.name'),from(famous),join(country),on('famous.country' = 'country.iso_code'),where((year > 1500,year < 2000)),order_by(asc(year))], Result),
171171
Result = data([["Miguel de Cervantes", "España"], ["Picasso", "España"]]).
172172

173173

174-
:- end_object.
174+
:- end_object.

0 commit comments

Comments
 (0)