Update Exec signature to return the number of affected rows

pull/10/head
Vinícius Garcia 2021-11-26 22:13:56 -03:00
parent a1403dc9d3
commit 6a4ec2cd51
8 changed files with 19 additions and 17 deletions

View File

@ -99,7 +99,7 @@ type Provider interface {
QueryOne(ctx context.Context, record interface{}, query string, params ...interface{}) error
QueryChunks(ctx context.Context, parser ChunkParser) error
Exec(ctx context.Context, query string, params ...interface{}) error
Exec(ctx context.Context, query string, params ...interface{}) (rowsAffected int64, _ error)
Transaction(ctx context.Context, fn func(Provider) error) error
}
```
@ -175,7 +175,7 @@ func main() {
// In the definition below, please note that BLOB is
// the only type we can use in sqlite for storing JSON.
err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
_, err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
age INTEGER,
name TEXT,

View File

@ -27,7 +27,7 @@ type Provider interface {
QueryOne(ctx context.Context, record interface{}, query string, params ...interface{}) error
QueryChunks(ctx context.Context, parser ChunkParser) error
Exec(ctx context.Context, query string, params ...interface{}) error
Exec(ctx context.Context, query string, params ...interface{}) (rowsAffected int64, _ error)
Transaction(ctx context.Context, fn func(Provider) error) error
}

View File

@ -48,7 +48,7 @@ func main() {
if err != nil {
log.Fatalf("unable to open database, reason: %s", err)
}
err = db.Exec(ctx, `CREATE TABLE users (
_, err = db.Exec(ctx, `CREATE TABLE users (
id serial PRIMARY KEY,
age INT,
name VARCHAR(50),
@ -64,7 +64,7 @@ func main() {
if err != nil {
log.Fatalf("unable to open database: %s", err)
}
err = db.Exec(ctx, `CREATE TABLE users (
_, err = db.Exec(ctx, `CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
age INT,
name VARCHAR(50),
@ -83,7 +83,7 @@ func main() {
// In the example below NVARCHAR is the format
// we are using for storing JSON:
err = db.Exec(ctx, `CREATE TABLE users (
_, err = db.Exec(ctx, `CREATE TABLE users (
id INT IDENTITY(1,1) PRIMARY KEY,
age INT,
name VARCHAR(50),
@ -102,7 +102,7 @@ func main() {
// In the definition below, please note that BLOB is
// the only type we can use in sqlite for storing JSON.
err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
_, err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
age INTEGER,
name TEXT,

View File

@ -59,7 +59,7 @@ func main() {
// In the definition below, please note that BLOB is
// the only type we can use in sqlite for storing JSON.
err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
_, err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
age INTEGER,
name TEXT,

View File

@ -50,15 +50,16 @@ func (mr *MockProviderMockRecorder) Delete(ctx, table, idOrRecord interface{}) *
}
// Exec mocks base method.
func (m *MockProvider) Exec(ctx context.Context, query string, params ...interface{}) error {
func (m *MockProvider) Exec(ctx context.Context, query string, params ...interface{}) (int64, error) {
m.ctrl.T.Helper()
varargs := []interface{}{ctx, query}
for _, a := range params {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "Exec", varargs...)
ret0, _ := ret[0].(error)
return ret0
ret0, _ := ret[0].(int64)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Exec indicates an expected call of Exec.

View File

@ -859,9 +859,10 @@ func buildUpdateQuery(
}
// Exec just runs an SQL command on the database returning no rows.
func (c DB) Exec(ctx context.Context, query string, params ...interface{}) error {
_, err := c.db.ExecContext(ctx, query, params...)
return err
func (c DB) Exec(ctx context.Context, query string, params ...interface{}) (rowsAffected int64, _ error) {
result, err := c.db.ExecContext(ctx, query, params...)
rowsAffected, _ = result.RowsAffected()
return rowsAffected, err
}
// Transaction just runs an SQL command on the database returning no rows.

View File

@ -1736,7 +1736,7 @@ func TestTransaction(t *testing.T) {
assert.Equal(t, nil, err)
err = db.Insert(ctx, UsersTable, &User{Name: "User4"})
assert.Equal(t, nil, err)
err = db.Exec(ctx, "UPDATE users SET age = 22")
_, err = db.Exec(ctx, "UPDATE users SET age = 22")
assert.Equal(t, nil, err)
return errors.New("fake-error")

View File

@ -17,7 +17,7 @@ type Mock struct {
QueryOneFn func(ctx context.Context, record interface{}, query string, params ...interface{}) error
QueryChunksFn func(ctx context.Context, parser ChunkParser) error
ExecFn func(ctx context.Context, query string, params ...interface{}) error
ExecFn func(ctx context.Context, query string, params ...interface{}) (rowsAffected int64, _ error)
TransactionFn func(ctx context.Context, fn func(db Provider) error) error
}
@ -127,7 +127,7 @@ func (m Mock) QueryChunks(ctx context.Context, parser ChunkParser) error {
}
// Exec ...
func (m Mock) Exec(ctx context.Context, query string, params ...interface{}) error {
func (m Mock) Exec(ctx context.Context, query string, params ...interface{}) (rowsAffected int64, _ error) {
if m.ExecFn == nil {
panic(fmt.Errorf("Mock.Exec(ctx, %s, %v) called but the ksql.Mock.ExecFn() is not set", query, params))
}