Add RETURNING to Delete statement.

Add DELETE statement wiki page.
This commit is contained in:
go-jet 2019-06-30 17:16:00 +02:00
parent 3d38946eda
commit 5e0e2f2908
10 changed files with 189 additions and 37 deletions

58
wiki/DELETE.md Normal file
View file

@ -0,0 +1,58 @@
DELETE statement deletes rows that satisfy the WHERE clause from the specified table. More about delete statement
in PostgreSQL: https://www.postgresql.org/docs/11/sql-delete.html
Following clauses are supported:
- WHERE(delete_condition) - Only rows for which delete condition returns true will be deleted.
- RETURNING(output_expression...) - An expressions to be computed and returned by the DELETE command after each row is deleted.
The expression can use any column names of the table. Write _TableName_.AllColumns to return all columns.
### Example
```
// delete all links with name 'Gmail' and 'Outlook'
deleteStmt := Link.
DELETE().
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
```
Debug sql of above statement:
```sql
DELETE FROM test_sample.link -- test_sample is name of the schema
WHERE link.name IN ('Gmail', 'Outlook');
```
### Execute statement
To execute delete statement and get sql.Result:
```
res, err := deleteStmt.Exec(db)
```
To execute delete statement and return records deleted,
delete statement has to have RETURNING clause:
```
deleteStmt := Link.
DELETE().
WHERE(Link.Name.IN(String("Gmail"), String("Outlook"))).
RETURNING(Link.AllColumns)
dest := []model.Link{}
err := deleteStmt.Query(db, &dest)
```
Use `ExecContext` and `QueryContext` to provide context object to execution.
##### SQL table used for the example:
```sql
CREATE TABLE IF NOT EXISTS link (
id serial PRIMARY KEY,
url VARCHAR (255) NOT NULL,
name VARCHAR (255) NOT NULL,
description VARCHAR (255)
);
```

View file

@ -9,7 +9,9 @@ Following clauses are supported:
- MODEL(model) - list of values for columns will be extracted from model object
- MODELS([]model) - list of values for columns will be extracted from list of model objects
- QUERY(select) - select statement that supplies the rows to be inserted.
- RETURNING(columns...) - list of columns to return as statement result
- RETURNING(output_expression...) - An expressions to be computed and returned by the INSERT statement after each row is inserted.
The expressions can use any column names of the table. Write _TableName_.AllColumns to return all columns.
_This list might be extended with feature Jet releases._
@ -124,7 +126,7 @@ err := insertStmt.Query(db, &dest)
Use `ExecContext` and `QueryContext` to provide context object to execution.
Insert example SQL table:
##### SQL table used for the example:
```sql
CREATE TABLE IF NOT EXISTS link (
id serial PRIMARY KEY,

View file

@ -1,12 +1,13 @@
UPDATE changes the values of the specified columns in all rows that satisfy the condition.
More about UPDATE statement in PostgreSQL: https://www.postgresql.org/docs/11/sql-update.html
Following clauses are supported
Following clauses are supported:
- UPDATE(columns...) - list of columns to update
- SET(values...) - list of values for columns
- MODEL(model) - list of values for columns will be extracted from model object
- WHERE(condition) - row condition to update
- RETURNING(columns...) - list of columns to return as statement result
- WHERE(condition) - only rows for which condition returns true will be updated.
- RETURNING(output_expression...) - An expressions to be computed and returned by the UPDATE statement after each row is updated.
The expressions can use any column names of the table. Write _TableName_.AllColumns to return all columns.
_This list might be extended with feature Jet releases._
@ -23,7 +24,7 @@ updateStmt := Link.
Debug sql of above statement:
```sql
UPDATE test_sample.link -- 'test_sample' is name of the schema
SET (name, url) = ('Bong', 'http://bong.com')
SET (name, url) = ('Yahoo', 'http://yahoo.com')
WHERE link.name = 'Bing';
```
@ -41,8 +42,8 @@ updateStmt := Link.
WHERE(Link.Name.EQ(String("Bing")))
```
`Link.Name, Link.URL, Link.Description` - can be replaced with Link.MutableColumns. All columns minus primary key columns.
Primary key columns are not updated usually.
`Link.Name, Link.URL, Link.Description` - can be replaced with Link.MutableColumns(all columns minus primary key column).
Primary key columns usually are not updated.
```
updateStmt := Link.
@ -75,7 +76,7 @@ err := updateStmt.Query(db, &dest)
Use `ExecContext` and `QueryContext` to provide context object to execution.
Update example SQL table:
##### SQL table used for the example:
```sql
CREATE TABLE IF NOT EXISTS link (
id serial PRIMARY KEY,