Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dialects/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/go-sql-driver/mysql"
"github.com/slicebit/qb"
"github.com/orus-io/qb"
)

//go:generate go run ./tools/generrors.go
Expand Down
2 changes: 1 addition & 1 deletion dialects/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/go-sql-driver/mysql"
"github.com/slicebit/qb"
"github.com/orus-io/qb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand Down
2 changes: 1 addition & 1 deletion dialects/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/lib/pq"
"github.com/slicebit/qb"
"github.com/orus-io/qb"
)

// Dialect is a type of dialect that can be used with postgres driver
Expand Down
2 changes: 1 addition & 1 deletion dialects/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/lib/pq"
"github.com/slicebit/qb"
"github.com/orus-io/qb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand Down
2 changes: 1 addition & 1 deletion dialects/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/mattn/go-sqlite3"
"github.com/slicebit/qb"
"github.com/orus-io/qb"
)

// Dialect is a type of dialect that can be used with sqlite driver
Expand Down
2 changes: 1 addition & 1 deletion dialects/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/mattn/go-sqlite3"
"github.com/slicebit/qb"
"github.com/orus-io/qb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand Down
83 changes: 72 additions & 11 deletions engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package qb

import (
"context"
"database/sql"
"log"
"os"
Expand Down Expand Up @@ -82,9 +83,14 @@ func (e *Engine) log(statement *Stmt) {

// Exec executes insert & update type queries and returns sql.Result and error
func (e *Engine) Exec(builder Builder) (sql.Result, error) {
return e.ExecContext(context.Background(), builder)
}

// ExecContext executes insert & update type queries and returns sql.Result and error
func (e *Engine) ExecContext(ctx context.Context, builder Builder) (sql.Result, error) {
statement := builder.Build(e.dialect)
e.log(statement)
res, err := e.db.Exec(statement.SQL(), statement.Bindings()...)
res, err := e.db.ExecContext(ctx, statement.SQL(), statement.Bindings()...)
return res, e.TranslateError(err)
}

Expand All @@ -101,36 +107,56 @@ func (r Row) Scan(dest ...interface{}) error {

// QueryRow wraps *sql.DB.QueryRow()
func (e *Engine) QueryRow(builder Builder) Row {
return e.QueryRowContext(context.Background(), builder)
}

// QueryRowContext wraps *sql.DB.QueryRow()
func (e *Engine) QueryRowContext(ctx context.Context, builder Builder) Row {
statement := builder.Build(e.dialect)
e.log(statement)
return Row{
e.db.QueryRow(statement.SQL(), statement.Bindings()...),
e.db.QueryRowContext(ctx, statement.SQL(), statement.Bindings()...),
e.TranslateError,
}
}

// Query wraps *sql.DB.Query()
func (e *Engine) Query(builder Builder) (*sql.Rows, error) {
return e.QueryContext(context.Background(), builder)
}

// QueryContext wraps *sql.DB.QueryContext()
func (e *Engine) QueryContext(ctx context.Context, builder Builder) (*sql.Rows, error) {
statement := builder.Build(e.dialect)
e.log(statement)
rows, err := e.db.Query(statement.SQL(), statement.Bindings()...)
rows, err := e.db.QueryContext(ctx, statement.SQL(), statement.Bindings()...)
return rows, e.TranslateError(err)
}

// Get maps the single row to a model
func (e *Engine) Get(builder Builder, model interface{}) error {
return e.GetContext(context.Background(), builder, model)
}

// GetContext maps the single row to a model
func (e *Engine) GetContext(ctx context.Context, builder Builder, model interface{}) error {
statement := builder.Build(e.dialect)
e.log(statement)
return e.TranslateError(
e.db.Get(model, statement.SQL(), statement.Bindings()...))
e.db.GetContext(ctx, model, statement.SQL(), statement.Bindings()...))
}

// Select maps multiple rows to a model array
func (e *Engine) Select(builder Builder, model interface{}) error {
return e.SelectContext(context.Background(), builder, model)
}

// SelectContext maps multiple rows to a model array
func (e *Engine) SelectContext(ctx context.Context, builder Builder, model interface{}) error {
statement := builder.Build(e.dialect)
e.log(statement)
return e.TranslateError(
e.db.Select(model, statement.SQL(), statement.Bindings()...))
e.db.SelectContext(ctx, model, statement.SQL(), statement.Bindings()...))
}

// DB returns sql.DB of wrapped engine connection
Expand All @@ -143,6 +169,11 @@ func (e *Engine) Ping() error {
return e.db.Ping()
}

// PingContext pings the db using connection and returns error if connectivity is not present
func (e *Engine) PingContext(ctx context.Context) error {
return e.db.PingContext(ctx)
}

// Close closes the sqlx db connection
func (e *Engine) Close() error {
return e.db.Close()
Expand All @@ -160,7 +191,12 @@ func (e *Engine) Dsn() string {

// Begin begins a transaction and return a *qb.Tx
func (e *Engine) Begin() (*Tx, error) {
tx, err := e.db.Beginx()
return e.BeginContext(context.Background())
}

// BeginContext begins a transaction and return a *qb.Tx
func (e *Engine) BeginContext(ctx context.Context) (*Tx, error) {
tx, err := e.db.BeginTxx(ctx, nil)
if err != nil {
return nil, e.dialect.WrapError(err)
}
Expand Down Expand Up @@ -190,42 +226,67 @@ func (tx *Tx) Rollback() error {

// Exec executes insert & update type queries and returns sql.Result and error
func (tx *Tx) Exec(builder Builder) (sql.Result, error) {
return tx.ExecContext(context.Background(), builder)
}

// ExecContext executes insert & update type queries and returns sql.Result and error
func (tx *Tx) ExecContext(ctx context.Context, builder Builder) (sql.Result, error) {
statement := builder.Build(tx.engine.dialect)
tx.engine.log(statement)
res, err := tx.tx.Exec(statement.SQL(), statement.Bindings()...)
res, err := tx.tx.ExecContext(ctx, statement.SQL(), statement.Bindings()...)
return res, tx.engine.TranslateError(err)
}

// QueryRow wraps *sql.DB.QueryRow()
func (tx *Tx) QueryRow(builder Builder) Row {
return tx.QueryRowContext(context.Background(), builder)
}

// QueryRowContext wraps *sql.DB.QueryRow()
func (tx *Tx) QueryRowContext(ctx context.Context, builder Builder) Row {
statement := builder.Build(tx.engine.dialect)
tx.engine.log(statement)
return Row{
tx.tx.QueryRow(statement.SQL(), statement.Bindings()...),
tx.tx.QueryRowContext(ctx, statement.SQL(), statement.Bindings()...),
tx.engine.TranslateError,
}
}

// Query wraps *sql.DB.Query()
func (tx *Tx) Query(builder Builder) (*sql.Rows, error) {
return tx.QueryContext(context.Background(), builder)
}

// QueryContext wraps *sql.DB.QueryContext()
func (tx *Tx) QueryContext(ctx context.Context, builder Builder) (*sql.Rows, error) {
statement := builder.Build(tx.engine.dialect)
tx.engine.log(statement)
rows, err := tx.tx.Query(statement.SQL(), statement.Bindings()...)
rows, err := tx.tx.QueryContext(ctx, statement.SQL(), statement.Bindings()...)
return rows, tx.engine.TranslateError(err)
}

// Get maps the single row to a model
func (tx *Tx) Get(builder Builder, model interface{}) error {
return tx.GetContext(context.Background(), builder, model)
}

// GetContext maps the single row to a model
func (tx *Tx) GetContext(ctx context.Context, builder Builder, model interface{}) error {
statement := builder.Build(tx.engine.dialect)
tx.engine.log(statement)
return tx.engine.TranslateError(
tx.tx.Get(model, statement.SQL(), statement.Bindings()...))
tx.tx.GetContext(ctx, model, statement.SQL(), statement.Bindings()...))
}

// Select maps multiple rows to a model array
func (tx *Tx) Select(builder Builder, model interface{}) error {
return tx.SelectContext(context.Background(), builder, model)
}

// SelectContext maps multiple rows to a model array
func (tx *Tx) SelectContext(ctx context.Context, builder Builder, model interface{}) error {
statement := builder.Build(tx.engine.dialect)
tx.engine.log(statement)
return tx.engine.TranslateError(
tx.tx.Select(model, statement.SQL(), statement.Bindings()...))
tx.tx.SelectContext(ctx, model, statement.SQL(), statement.Bindings()...))
}
4 changes: 2 additions & 2 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

_ "github.com/mattn/go-sqlite3"
"github.com/slicebit/qb"
_ "github.com/slicebit/qb/dialects/sqlite"
"github.com/orus-io/qb"
_ "github.com/orus-io/qb/dialects/sqlite"
"github.com/stretchr/testify/assert"
)

Expand Down
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type Error struct {
Constraint string
}

func (err Error) Unwrap() error {
return err.Orig
}

func (err Error) Error() string {
switch err.Code {
case ErrAny:
Expand Down
17 changes: 13 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
module github.com/slicebit/qb
module github.com/orus-io/qb

go 1.21.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.4.1
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.0.0
github.com/mattn/go-sqlite3 v1.10.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.5.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
Loading