Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DBI #1857

Merged
merged 1 commit into from
Mar 26, 2021
Merged

Add DBI #1857

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
4 changes: 2 additions & 2 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,11 @@ func (db *baseDB) Ping(ctx context.Context) error {
}

// Model returns new query for the model.
func (db *baseDB) Model(model ...interface{}) *orm.Query {
func (db *baseDB) Model(model ...interface{}) *Query {
return orm.NewQuery(db.db, model...)
}

func (db *baseDB) ModelContext(c context.Context, model ...interface{}) *orm.Query {
func (db *baseDB) ModelContext(c context.Context, model ...interface{}) *Query {
return orm.NewQueryContext(c, db.db, model...)
}

Expand Down
8 changes: 4 additions & 4 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,10 +1394,10 @@ var _ = Describe("ORM", func() {
Relation("Editor.Avatar").
Relation("Genres").
Relation("Comments").
Relation("Translations", func(q *orm.Query) (*orm.Query, error) {
Relation("Translations", func(q *pg.Query) (*pg.Query, error) {
return q.Order("id"), nil
}).
Relation("Translations.Comments", func(q *orm.Query) (*orm.Query, error) {
Relation("Translations.Comments", func(q *pg.Query) (*pg.Query, error) {
return q.Order("text"), nil
}).
First()
Expand Down Expand Up @@ -2074,7 +2074,7 @@ var _ = Describe("ORM", func() {
var book Book
err := db.Model(&book).
Column("book.id").
Relation("Translations", func(q *orm.Query) (*orm.Query, error) {
Relation("Translations", func(q *pg.Query) (*pg.Query, error) {
return q.Where("lang = 'ru'"), nil
}).
First()
Expand All @@ -2091,7 +2091,7 @@ var _ = Describe("ORM", func() {
var book Book
err := db.Model(&book).
Column("book.id").
Relation("Genres", func(q *orm.Query) (*orm.Query, error) {
Relation("Genres", func(q *pg.Query) (*pg.Query, error) {
return q.Where("genre__rating > 999"), nil
}).
First()
Expand Down
2 changes: 1 addition & 1 deletion example_many2many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ExampleDB_Model_manyToMany() {

order = new(Order)
err = db.Model(order).
Relation("Items", func(q *orm.Query) (*orm.Query, error) {
Relation("Items", func(q *pg.Query) (*pg.Query, error) {
q = q.OrderExpr("item.id DESC")
return q, nil
}).
Expand Down
6 changes: 3 additions & 3 deletions example_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func ExampleDB_Model_selectWhereGroup() {

var books []Book
err := db.Model(&books).
WhereGroup(func(q *orm.Query) (*orm.Query, error) {
WhereGroup(func(q *pg.Query) (*pg.Query, error) {
q = q.WhereOr("id = 1").
WhereOr("id = 2")
return q, nil
Expand Down Expand Up @@ -443,7 +443,7 @@ func ExampleDB_Model_selectApplyFunc() {
var authorId int
var editorId int

filter := func(q *orm.Query) (*orm.Query, error) {
filter := func(q *pg.Query) (*pg.Query, error) {
if authorId != 0 {
q = q.Where("author_id = ?", authorId)
}
Expand Down Expand Up @@ -700,7 +700,7 @@ func ExampleDB_Model_hasMany() {
var user User
err := db.Model(&user).
Column("user.*").
Relation("Profiles", func(q *orm.Query) (*orm.Query, error) {
Relation("Profiles", func(q *pg.Query) (*pg.Query, error) {
return q.Where("active IS TRUE"), nil
}).
First()
Expand Down
8 changes: 4 additions & 4 deletions orm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func (q *Query) WhereOr(condition string, params ...interface{}) *Query {
// WhereGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereGroup(func(q *orm.Query) (*orm.Query, error) {
// WhereGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.WhereOr("FALSE").WhereOr("TRUE").
// return q, nil
// })
Expand All @@ -498,7 +498,7 @@ func (q *Query) WhereGroup(fn func(*Query) (*Query, error)) *Query {
// WhereGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereNotGroup(func(q *orm.Query) (*orm.Query, error) {
// WhereNotGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.WhereOr("FALSE").WhereOr("TRUE").
// return q, nil
// })
Expand All @@ -513,7 +513,7 @@ func (q *Query) WhereNotGroup(fn func(*Query) (*Query, error)) *Query {
// WhereOrGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
// WhereOrGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.Where("FALSE").Where("TRUE").
// return q, nil
// })
Expand All @@ -528,7 +528,7 @@ func (q *Query) WhereOrGroup(fn func(*Query) (*Query, error)) *Query {
// WhereOrGroup encloses conditions added in the function in parentheses.
//
// q.Where("TRUE").
// WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
// WhereOrGroup(func(q *pg.Query) (*pg.Query, error) {
// q = q.Where("FALSE").Where("TRUE").
// return q, nil
// })
Expand Down
50 changes: 40 additions & 10 deletions pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ var Discard orm.Discard
// PostgreSQL NULL.
type NullTime = types.NullTime

// Model returns new query for the optional model.
func Model(model ...interface{}) *orm.Query {
return orm.NewQuery(nil, model...)
}

// ModelContext returns a new query for the optional model with a context.
func ModelContext(c context.Context, model ...interface{}) *orm.Query {
return orm.NewQueryContext(c, nil, model...)
}

// Scan returns ColumnScanner that copies the columns in the
// row into the values.
func Scan(values ...interface{}) orm.ColumnScanner {
Expand Down Expand Up @@ -96,6 +86,46 @@ func SetLogger(logger internal.Logging) {

//------------------------------------------------------------------------------

type Query = orm.Query

// Model returns a new query for the optional model.
func Model(model ...interface{}) *Query {
return orm.NewQuery(nil, model...)
}

// ModelContext returns a new query for the optional model with a context.
func ModelContext(c context.Context, model ...interface{}) *Query {
return orm.NewQueryContext(c, nil, model...)
}

// DBI is a DB interface implemented by *DB and *Tx.
type DBI interface {
Model(model ...interface{}) *Query
ModelContext(c context.Context, model ...interface{}) *Query

Exec(query interface{}, params ...interface{}) (Result, error)
ExecContext(c context.Context, query interface{}, params ...interface{}) (Result, error)
ExecOne(query interface{}, params ...interface{}) (Result, error)
ExecOneContext(c context.Context, query interface{}, params ...interface{}) (Result, error)
Query(model, query interface{}, params ...interface{}) (Result, error)
QueryContext(c context.Context, model, query interface{}, params ...interface{}) (Result, error)
QueryOne(model, query interface{}, params ...interface{}) (Result, error)
QueryOneContext(c context.Context, model, query interface{}, params ...interface{}) (Result, error)

Begin() (*Tx, error)
RunInTransaction(ctx context.Context, fn func(*Tx) error) error

CopyFrom(r io.Reader, query interface{}, params ...interface{}) (Result, error)
CopyTo(w io.Writer, query interface{}, params ...interface{}) (Result, error)
}

var (
_ DBI = (*DB)(nil)
_ DBI = (*Tx)(nil)
)

//------------------------------------------------------------------------------

// Strings is a type alias for a slice of strings.
type Strings []string

Expand Down
4 changes: 2 additions & 2 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ func (tx *Tx) queryOne(
}

// Model is an alias for DB.Model.
func (tx *Tx) Model(model ...interface{}) *orm.Query {
func (tx *Tx) Model(model ...interface{}) *Query {
return orm.NewQuery(tx, model...)
}

// ModelContext acts like Model but additionally receives a context.
func (tx *Tx) ModelContext(c context.Context, model ...interface{}) *orm.Query {
func (tx *Tx) ModelContext(c context.Context, model ...interface{}) *Query {
return orm.NewQueryContext(c, tx, model...)
}

Expand Down