Add UPDATE statement wiki page.
This commit is contained in:
parent
5f678a7e82
commit
3d38946eda
4 changed files with 124 additions and 18 deletions
|
|
@ -170,7 +170,7 @@ func TestUpdateWithModelData(t *testing.T) {
|
||||||
|
|
||||||
stmt := Link.
|
stmt := Link.
|
||||||
UPDATE(Link.AllColumns).
|
UPDATE(Link.AllColumns).
|
||||||
USING(link).
|
MODEL(link).
|
||||||
WHERE(Link.ID.EQ(Int(int64(link.ID))))
|
WHERE(Link.ID.EQ(Int(int64(link.ID))))
|
||||||
|
|
||||||
expectedSql := `
|
expectedSql := `
|
||||||
|
|
@ -197,7 +197,7 @@ func TestUpdateWithModelDataAndPredefinedColumnList(t *testing.T) {
|
||||||
|
|
||||||
stmt := Link.
|
stmt := Link.
|
||||||
UPDATE(updateColumnList).
|
UPDATE(updateColumnList).
|
||||||
USING(link).
|
MODEL(link).
|
||||||
WHERE(Link.ID.EQ(Int(int64(link.ID))))
|
WHERE(Link.ID.EQ(Int(int64(link.ID))))
|
||||||
|
|
||||||
var expectedSql = `
|
var expectedSql = `
|
||||||
|
|
@ -233,7 +233,7 @@ func TestUpdateWithInvalidModelData(t *testing.T) {
|
||||||
|
|
||||||
stmt := Link.
|
stmt := Link.
|
||||||
UPDATE(Link.AllColumns).
|
UPDATE(Link.AllColumns).
|
||||||
USING(link).
|
MODEL(link).
|
||||||
WHERE(Link.ID.EQ(Int(int64(link.Ident))))
|
WHERE(Link.ID.EQ(Int(int64(link.Ident))))
|
||||||
|
|
||||||
var expectedSql = `
|
var expectedSql = `
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ type UpdateStatement interface {
|
||||||
Statement
|
Statement
|
||||||
|
|
||||||
SET(value interface{}, values ...interface{}) UpdateStatement
|
SET(value interface{}, values ...interface{}) UpdateStatement
|
||||||
USING(data interface{}) UpdateStatement
|
MODEL(data interface{}) UpdateStatement
|
||||||
|
|
||||||
WHERE(expression BoolExpression) UpdateStatement
|
WHERE(expression BoolExpression) UpdateStatement
|
||||||
RETURNING(projections ...projection) UpdateStatement
|
RETURNING(projections ...projection) UpdateStatement
|
||||||
|
|
@ -39,8 +39,8 @@ func (u *updateStatementImpl) SET(value interface{}, values ...interface{}) Upda
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *updateStatementImpl) USING(modelData interface{}) UpdateStatement {
|
func (u *updateStatementImpl) MODEL(data interface{}) UpdateStatement {
|
||||||
u.row = unwindRowFromModel(u.columns, modelData)
|
u.row = unwindRowFromModel(u.columns, data)
|
||||||
|
|
||||||
return u
|
return u
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,18 @@
|
||||||
The PostgreSQL INSERT statement is used to insert a single record or multiple records
|
The PostgreSQL INSERT statement is used to insert a single record or multiple records
|
||||||
into a table. More about PostgreSQL INSERT statement can be found here: https://www.postgresql.org/docs/11/sql-insert.html
|
into a table. More about PostgreSQL INSERT statement can be found here: https://www.postgresql.org/docs/11/sql-insert.html
|
||||||
|
|
||||||
## Insert statement
|
Following clauses are supported:
|
||||||
|
- INSERT(columns...) - list of columns for insert
|
||||||
|
- VALUES(values...) - list of values
|
||||||
|
- 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
|
||||||
|
|
||||||
Insert example SQL table:
|
_This list might be extended with feature Jet releases._
|
||||||
```sql
|
|
||||||
CREATE TABLE IF NOT EXISTS link (
|
|
||||||
id serial PRIMARY KEY,
|
|
||||||
url VARCHAR (255) NOT NULL,
|
|
||||||
name VARCHAR (255) NOT NULL,
|
|
||||||
description VARCHAR (255)
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
## Example
|
||||||
### Insert row by row
|
### Insert row by row
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
@ -23,6 +23,16 @@ insertStmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||||
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
||||||
VALUES(102, "http://www.yahoo.com", "Yahoo", nil)
|
VALUES(102, "http://www.yahoo.com", "Yahoo", nil)
|
||||||
```
|
```
|
||||||
|
Debug SQL of above insert statement:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
INSERT INTO test_sample.link (id, url, name, description) VALUES
|
||||||
|
(100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
||||||
|
(101, 'http://www.google.com', 'Google', DEFAULT),
|
||||||
|
(102, 'http://www.yahoo.com', 'Yahoo', NULL)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
There is also shorthand notation for inserting model data:
|
There is also shorthand notation for inserting model data:
|
||||||
```
|
```
|
||||||
|
|
@ -80,7 +90,6 @@ insertStmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description, Link.D
|
||||||
```
|
```
|
||||||
|
|
||||||
### Insert using query
|
### Insert using query
|
||||||
A query (SELECT statement) that supplies the rows to be inserted.
|
|
||||||
```
|
```
|
||||||
// duplicate first 10 entries
|
// duplicate first 10 entries
|
||||||
insertStmt := Link.
|
insertStmt := Link.
|
||||||
|
|
@ -105,7 +114,7 @@ To execute insert statement and return row records inserted, insert statement ha
|
||||||
insertStmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
insertStmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||||
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
||||||
RETURNING(Link.ID, Link.URL, Link.Name, Link.Description)
|
RETURNING(Link.ID, Link.URL, Link.Name, Link.Description) // or RETURNING(Link.AllColumns)
|
||||||
|
|
||||||
dest := []model.Link{}
|
dest := []model.Link{}
|
||||||
|
|
||||||
|
|
@ -115,3 +124,13 @@ err := insertStmt.Query(db, &dest)
|
||||||
|
|
||||||
Use `ExecContext` and `QueryContext` to provide context object to execution.
|
Use `ExecContext` and `QueryContext` to provide context object to execution.
|
||||||
|
|
||||||
|
Insert example SQL table:
|
||||||
|
```sql
|
||||||
|
CREATE TABLE IF NOT EXISTS link (
|
||||||
|
id serial PRIMARY KEY,
|
||||||
|
url VARCHAR (255) NOT NULL,
|
||||||
|
name VARCHAR (255) NOT NULL,
|
||||||
|
description VARCHAR (255)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
||||||
87
wiki/UPDATE.md
Normal file
87
wiki/UPDATE.md
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
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
|
||||||
|
- 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
|
||||||
|
|
||||||
|
_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
|
||||||
|
SET (name, url) = ('Bong', 'http://bong.com')
|
||||||
|
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")))
|
||||||
|
```
|
||||||
|
|
||||||
|
`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.
|
||||||
|
|
||||||
|
```
|
||||||
|
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.
|
||||||
|
|
||||||
|
Update example SQL table:
|
||||||
|
```sql
|
||||||
|
CREATE TABLE IF NOT EXISTS link (
|
||||||
|
id serial PRIMARY KEY,
|
||||||
|
url VARCHAR (255) NOT NULL,
|
||||||
|
name VARCHAR (255) NOT NULL,
|
||||||
|
description VARCHAR (255)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue