Skip to content

Commit

Permalink
Add direct exec option
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <[email protected]>
  • Loading branch information
peterbroadhurst committed Feb 12, 2024
1 parent 3d7d71c commit bec7b66
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pkg/dbsql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,20 @@ func (s *Database) DeleteTx(ctx context.Context, table string, tx *TXWrapper, q
return nil
}

func (s *Database) ExecTx(ctx context.Context, table string, tx *TXWrapper, sqlQuery string, args []interface{}) (sql.Result, error) {
l := log.L(ctx)
before := time.Now()
l.Tracef(`SQL-> exec: %s args: %+v`, sqlQuery, args)
res, err := tx.sqlTX.ExecContext(ctx, sqlQuery, args...)
if err != nil {
l.Errorf(`SQL exec: %s sql=[ %s ]: %s`, err, sqlQuery, err)
return nil, i18n.WrapError(ctx, err, i18n.MsgDBExecFailed)
}
ra, _ := res.RowsAffected()
l.Debugf(`SQL<- exec: %s affected=%d (%.2fms)`, table, ra, floatMillisSince(before))
return res, nil
}

func (s *Database) UpdateTx(ctx context.Context, table string, tx *TXWrapper, q sq.UpdateBuilder, postCommit func()) (int64, error) {
l := log.L(ctx)
sqlQuery, args, err := q.PlaceholderFormat(s.features.PlaceholderFormat).ToSql()
Expand Down
27 changes: 27 additions & 0 deletions pkg/dbsql/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,30 @@ func TestInsertTxRowsIncompleteReturn(t *testing.T) {
err = s.InsertTxRows(ctx, "table1", tx, sb, nil, []int64{1, 2}, false)
assert.Regexp(t, "FF00177", err)
}

func TestExecTxOk(t *testing.T) {
s, mdb := NewMockProvider().UTInit()
mdb.ExpectBegin()
mdb.ExpectExec("INSERT.*").WillReturnResult(driver.ResultNoRows)
ctx, tx, _, err := s.BeginOrUseTx(context.Background())
assert.NoError(t, err)
q := sq.Insert("mytable").Columns("some").Values("thing")
sqlQuery, args, err := q.PlaceholderFormat(s.features.PlaceholderFormat).ToSql()
assert.NoError(t, err)
res, err := s.ExecTx(ctx, "mytable", tx, sqlQuery, args)
assert.NoError(t, err)
assert.NotNil(t, res)
}

func TestExecTxFail(t *testing.T) {
s, mdb := NewMockProvider().UTInit()
mdb.ExpectBegin()
mdb.ExpectExec("INSERT.*").WillReturnError(fmt.Errorf("pop"))
ctx, tx, _, err := s.BeginOrUseTx(context.Background())
assert.NoError(t, err)
q := sq.Insert("mytable").Columns("some").Values("thing")
sqlQuery, args, err := q.PlaceholderFormat(s.features.PlaceholderFormat).ToSql()
assert.NoError(t, err)
_, err = s.ExecTx(ctx, "mytable", tx, sqlQuery, args)
assert.Regexp(t, "pop", err)
}
3 changes: 2 additions & 1 deletion pkg/i18n/en_base_error_messages.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -178,4 +178,5 @@ var (
MsgJSONQueryOpUnsupportedMod = ffe("FF00240", "Operation '%s' does not support modifiers: %v", 400)
MsgJSONQueryValueUnsupported = ffe("FF00241", "Field value not supported (must be string, number, or boolean): %s", 400)
MsgJSONQuerySortUnsupported = ffe("FF00242", "Invalid 'order' for sort (must be 'asc', 'ascending', 'desc' or 'descending'): %s", 400)
MsgDBExecFailed = ffe("FF00243", "Database update failed")
)

0 comments on commit bec7b66

Please sign in to comment.