2019-06-30 13:42:01 +02:00
|
|
|
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
|
|
|
|
|
|
2019-06-30 17:16:00 +02:00
|
|
|
Following clauses are supported:
|
2019-06-30 13:42:01 +02:00
|
|
|
- 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
|
2019-06-30 17:16:00 +02:00
|
|
|
- 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.
|
2019-06-30 13:42:01 +02:00
|
|
|
|
|
|
|
|
_This list might be extended with feature Jet releases._
|
|
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
// replace all Bing links with Yahoo
|
|
|
|
|
updateStmt := Link.
|
|
|
|
|
UPDATE(Link.Name, Link.URL).
|
|
|
|
|
SET("Yahoo", "http://yahoo.com").
|
|
|
|
|
WHERE(Link.Name.EQ(String("Bing")))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Debug sql of above statement:
|
|
|
|
|
```sql
|
|
|
|
|
UPDATE test_sample.link -- 'test_sample' is name of the schema
|
2019-06-30 17:16:00 +02:00
|
|
|
SET (name, url) = ('Yahoo', 'http://yahoo.com')
|
2019-06-30 13:42:01 +02:00
|
|
|
WHERE link.name = 'Bing';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Short-hand notation to extract model data for column values:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
yahoo := model.Link{
|
|
|
|
|
URL: "http://www.yahoo.com",
|
|
|
|
|
Name: "Yahoo",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateStmt := Link.
|
|
|
|
|
UPDATE(Link.Name, Link.URL, Link.Description).
|
|
|
|
|
MODEL(yahoo).
|
|
|
|
|
WHERE(Link.Name.EQ(String("Bing")))
|
|
|
|
|
```
|
|
|
|
|
|
2019-06-30 17:16:00 +02:00
|
|
|
`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.
|
2019-06-30 13:42:01 +02:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
updateStmt := Link.
|
|
|
|
|
UPDATE(Link.MutableColumns).
|
|
|
|
|
MODEL(yahoo).
|
|
|
|
|
WHERE(Link.Name.EQ(String("Bing")))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Execute statement
|
|
|
|
|
|
|
|
|
|
To execute update statement and get sql.Result:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
res, err := updateStmt.Exec(db)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To execute update statement and return row records updated, statement has to have RETURNING clause:
|
|
|
|
|
```
|
|
|
|
|
updateStmt := Link.
|
|
|
|
|
UPDATE(Link.MutableColumns).
|
|
|
|
|
MODEL(yahoo).
|
|
|
|
|
WHERE(Link.Name.EQ(String("Bing"))).
|
|
|
|
|
RETURNING(Link.AllColumns)
|
|
|
|
|
|
|
|
|
|
dest := []model.Link{}
|
|
|
|
|
|
|
|
|
|
err := updateStmt.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Use `ExecContext` and `QueryContext` to provide context object to execution.
|
|
|
|
|
|
2019-06-30 17:16:00 +02:00
|
|
|
##### SQL table used for the example:
|
2019-06-30 13:42:01 +02:00
|
|
|
```sql
|
|
|
|
|
CREATE TABLE IF NOT EXISTS link (
|
|
|
|
|
id serial PRIMARY KEY,
|
|
|
|
|
url VARCHAR (255) NOT NULL,
|
|
|
|
|
name VARCHAR (255) NOT NULL,
|
|
|
|
|
description VARCHAR (255)
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|