Skip to content

Commit

Permalink
fix(tx): added context in tx.QueryBuilder and tx.QueryRowBuilder (#42)
Browse files Browse the repository at this point in the history
* fix(tx): added context in tx.QueryBuilder and tx.QueryRowBuilder

* chore(docs): updated CHANGELOG.md
  • Loading branch information
cnlangzi authored Apr 23, 2024
1 parent edb8304 commit 6f57b8a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.4.6] - 2014-04-21
## [1.4.6] - 2014-04-23
### Changed
- implements json.Marshaler and json.Unmarshaler on `ID` (#39)
- implements json.Marshaler and json.Unmarshaler on `ID` (#41)
- added `context` support in `tx.QueryRowBuilder` and `tx.QueryBuilder` (#42)

## [1.4.5] - 2014-04-20
### Changed
- used int64 instead of int in `Queryer.Count` (#37)
### Fixes
- fix(sqlbuilder): fixed WithWhere/WithOrderBy for empty builder (#39)
- fixed timer performance issue (#38)
- fixed StmtMaxIdleTime missing issue (#38)
- used int64 instead of int in `Queryer.Count` (#37)

## [1.4.4] - 2014-04-19
### Added
Expand Down
8 changes: 4 additions & 4 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func (tx *Tx) Query(query string, args ...any) (*Rows, error) {
return tx.QueryContext(context.Background(), query, args...)
}

func (tx *Tx) QueryBuilder(b *Builder) (*Rows, error) {
func (tx *Tx) QueryBuilder(ctx context.Context, b *Builder) (*Rows, error) {
query, args, err := b.Build()
if err != nil {
return nil, err
}

return tx.QueryContext(context.TODO(), query, args...)
return tx.QueryContext(ctx, query, args...)
}

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
Expand Down Expand Up @@ -76,15 +76,15 @@ func (tx *Tx) QueryRow(query string, args ...any) *Row {
return tx.QueryRowContext(context.Background(), query, args...)
}

func (tx *Tx) QueryRowBuilder(b *Builder) *Row {
func (tx *Tx) QueryRowBuilder(ctx context.Context, b *Builder) *Row {
query, args, err := b.Build()
if err != nil {
return &Row{
err: err,
}
}

return tx.QueryRowContext(context.TODO(), query, args...)
return tx.QueryRowContext(ctx, query, args...)
}

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
Expand Down
4 changes: 2 additions & 2 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestTx(t *testing.T) {
require.Equal(t, 3, users[2].ID)

users2 := make([]user, 0, 3)
rows, err = tx.Query("SELECT * FROM users WHERE id<4")
rows, err = tx.QueryBuilder(context.Background(), New("SELECT * FROM users WHERE id<{id}").Param("id", 4))
require.NoError(t, err)
err = rows.Bind(&users2)
require.NoError(t, err)
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestTx(t *testing.T) {
require.Equal(t, "1zzzz", u.Salt)

var u2 user
row2 := tx.QueryRow("SELECT * FROM users WHERE id=?", 2)
row2 := tx.QueryRowBuilder(context.Background(), New("SELECT * FROM users WHERE id={id}").Param("id", 2))
err = row2.Bind(&u2)
require.NoError(t, err)

Expand Down

0 comments on commit 6f57b8a

Please sign in to comment.