Skip to content

Commit

Permalink
Allow RETURNING on INSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
aarroyoc committed Oct 17, 2023
1 parent 7d02254 commit 19fbca9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Items of the DSL:
* `fetch_first(N, type)` - equivalent to `FETCH FIRST N ROWS type`. Type can be `only (ONLY)` or `with_ties (WITH TIES)`.
* `insert_into(table, [col1, col2,...])` - equivalent to `INSERT INTO table (col1, col2, ...)`. Must be followed by `values`.
* `values(val1, val2, ...)` - equivalent to `VALUES (val1, val2, ...)`. Strings here are escaped so it's **safe**.
* `returning(col1, col2, ...)` equivalent to `RETURNING col1, col2`.
* `update(table)` - equivalent to `UPDATE table`. Must be followed by `set`
* `set(Sets)` - equivalent to `SET Col1 = Val1, ...`. Similar to WHERE but only = is allowed. Optionally, you can add a `where+` after a `set`.
* `delete(table)` - equivalent to `DELETE table`. Must be followed by `where`.
Expand Down Expand Up @@ -114,4 +115,4 @@ test :-
postgresql:sql(Connection, [update(test_table), set(name = "test2"), where(id = 1)], data([])),
postgresql:sql(Connection, [select(id, name), from(test_table), where(name = "test")], Rows2),
Rows2 = data([]).
```
```
13 changes: 13 additions & 0 deletions sql_query.pl
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,26 @@
") ",
sql_query_insert_values(Values, Vars).

sql_query_insert_values([Values0|Next], Vars) -->
{ Values0 =.. [values|Values] },
"VALUES (",
{ values_args(Values, Args, [], Vars) },
quoted_comma_separated_list(Args),
")",
sql_query_insert_returning(Next).

sql_query_insert_values([Values0], Vars) -->
{ Values0 =.. [values|Values] },
"VALUES (",
{ values_args(Values, Args, [], Vars) },
quoted_comma_separated_list(Args),
")".

sql_query_insert_returning([Returning0]) -->
{ Returning0 =.. [returning|Columns] },
"RETURNING ",
quoted_comma_separated_list(Columns).

values_args([], [], X, X).
values_args([Value|Values], [Arg|Args], Vars0, Vars) :-
sql_var(Value, Arg, Vars0, Vars1),
Expand Down
4 changes: 2 additions & 2 deletions tests.lgt
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@
postgresql:sql(Connection, [insert_into(country, [iso_code, name]), values("PT", "Portugal")], data([])),
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Miguel de Cervantes", "ES", 1547)], data([])),
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Magallanes", "PT", 1480)], data([])),
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Picasso", "ES", 1881)], data([])),
postgresql:sql(Connection, [insert_into(famous, [name, country, year]), values("Picasso", "ES", 1881), returning(name, country)], data([["Picasso", "ES"]])),
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),
Result = data([["Miguel de Cervantes", "España"], ["Picasso", "España"]]).


:- end_object.
:- end_object.

0 comments on commit 19fbca9

Please sign in to comment.