Adding ' notation' and 'Executing multiple statements' sub-sections.
This commit is contained in:
36
README.md
36
README.md
@@ -170,6 +170,42 @@ $ migrate -source="github://user:personal-access-token@owner/repo/path#ref" -dat
|
||||
|
||||
More information about this functionality and a full list of the supported remote resources can be [found here](https://github.com/golang-migrate/migrate#migration-sources).
|
||||
|
||||
### $N notation
|
||||
|
||||
A nice feature of the PostgreSQL placeholder parameter **$N** notation is that you can use the same parameter value in multiple places in your SQL statement. For example, it's perfectly acceptable to write code like :
|
||||
|
||||
```go
|
||||
// This SQL statement uses the $1 parameter twice, and the value `123` will be used in both locations where $1 appears
|
||||
stmt := "UPDATE foo SET bar = $1 + $2 WHERE bar = $1"
|
||||
err := db.Exec(stmt, 123, 456)
|
||||
if err != nil {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
### Executing multiple statements
|
||||
|
||||
Occasionally, you might find yourself in the position where you want to execute more than one SQL statement in the same database call, like this :
|
||||
|
||||
```go
|
||||
stmt := `
|
||||
UPDATE foo SET bar = true;
|
||||
UPDATE foo SET bar = false;`
|
||||
|
||||
err := db.Exec(stmt)
|
||||
if err != nil {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Having multiple statements in the same call is supported by the **pq** driver, so long as the statements do not contain any placeholder parameters. If they do contain placeholder parameters, then you'll receive the following error message at runtime:
|
||||
|
||||
```bash
|
||||
pq: cannot insert multiple commands into a prepared statement
|
||||
```
|
||||
|
||||
To work around this, you will need to either split out the statements into separate database calls, or if that's not possible, you can create a [custom function](https://www.postgresql.org/docs/current/xfunc-sql.html) in PostgreSQL which acts as a wrapper around the multiple SQL statements that you want to run.
|
||||
|
||||
## Additional Information
|
||||
|
||||
### How different Go Types are encoded
|
||||
|
||||
Reference in New Issue
Block a user