Fetch
and Exec
methods.
Query | Fetch | Exec |
---|---|---|
SELECT | ✓ | ✓ |
INSERT | ✓ | ✓ |
UPDATE | ✓ | ✓ |
DELETE | ✓ | ✓ |
Query | Fetch | Exec |
---|---|---|
SELECT | ✓ | |
INSERT | ✓ | |
UPDATE | ✓ | |
DELETE | ✓ |
Exec()
.
Exec can optionally return the number of rows affected, but the Erowsaffected
flag must be passed to it.
Otherwise, the rowsAffected returned will always be 0.
Postgres' SELECT query can run Exec because it will still report the number of rowsAffected by the SELECT. go-mysql-driver does not seem to do this.
Exec
takes in an ExecFlag. Here are the possible values:
// Postgres
const (
Erowsaffected ExecFlag = 1 << iota
)
// MySQL
const (
ElastInsertID ExecFlag = 1 << iota // ElastInsertID only applies to INSERT
Erowsaffected ExecFlag
)
The ExecFlag dictates whether Exec
will compute the lastInsertID or rowsAffected.
If not specified, the value of the lastInsertID or rowsAffected returned will always be 0.
Only MySQL has the ElastInsertID
flag, and it is only applicable to the InsertQuery.
For Postgres, use Returningx/ReturningRowx
with Fetch
to get the lastInsertID.
Erowsaffected
ExecFlag to Exec.
// Postgres
rowsAffected, err := InsertInto(tbl).
Column(tbl.A, tbl.B).
Values("a", "b").
Exec(DB, Erowsaffected)
// ^ Erowsaffected must be used otherwise
// rowsAffected will always be 0
// MySQL
lastInsertID, rowsAffected, err := InsertInto(tbl).
Column(tbl.A, tbl.B).
Values("a", "b").
Exec(DB, ElastInsertID|Erowsaffected)
// ^ ElastInsertID and Erowsaffected must be used otherwise
// lastInsertID and rowsAffected will always be 0