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

Override view name in querier #155

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions internal/test/sql/mssql_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CREATE TABLE [people] (
[updated_at] datetime2
);

EXEC('CREATE VIEW [people_copy] AS SELECT * from [people]')

CREATE TABLE [projects] (
[name] varchar(255) NOT NULL,
[id] varchar(255) PRIMARY KEY,
Expand Down
2 changes: 2 additions & 0 deletions internal/test/sql/mysql_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CREATE TABLE people (
PRIMARY KEY (id)
);

CREATE VIEW people_copy AS SELECT * from people;

CREATE TABLE projects (
name varchar(255) NOT NULL,
id varchar(255) NOT NULL,
Expand Down
2 changes: 2 additions & 0 deletions internal/test/sql/postgres_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CREATE TABLE people (
-- updated_at timestamp without time zone
);

CREATE VIEW people_copy AS SELECT * from people;

CREATE TABLE projects (
name varchar NOT NULL,
id varchar PRIMARY KEY,
Expand Down
2 changes: 2 additions & 0 deletions internal/test/sql/sqlite3_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CREATE TABLE people (
updated_at datetime
);

CREATE VIEW people_copy AS SELECT * from people;

CREATE TABLE projects (
name varchar NOT NULL,
id varchar NOT NULL PRIMARY KEY,
Expand Down
15 changes: 13 additions & 2 deletions querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// Querier performs queries and commands.
type Querier struct {
dbtx DBTX
tag string
dbtx DBTX
tag string
viewName string
Dialect
Logger Logger
}
Expand Down Expand Up @@ -53,9 +54,19 @@ func (q *Querier) WithTag(format string, args ...interface{}) *Querier {
return newQ
}

// WithView returns a copy of Querier with appointed view name.
func (q *Querier) WithView(viewName string) *Querier {
newQ := newQuerier(q.dbtx, q.Dialect, q.Logger)
newQ.viewName = viewName
return newQ
}

// QualifiedView returns quoted qualified view name.
func (q *Querier) QualifiedView(view View) string {
v := q.QuoteIdentifier(view.Name())
if q.viewName != "" {
v = q.QuoteIdentifier(q.viewName)
}
if view.Schema() != "" {
v = q.QuoteIdentifier(view.Schema()) + "." + v
}
Expand Down
11 changes: 11 additions & 0 deletions querier_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ func ExampleQuerier_WithTag() {
// Name: `Vicious Baron` (string), ID: `baron` (string), Start: 2014-06-01 00:00:00 +0000 UTC (time.Time), End: 2016-02-21 00:00:00 +0000 UTC (*time.Time)
}

func ExampleQuerier_WithView() {
id := 1
person, err := DB.WithView("people_copy").FindByPrimaryKeyFrom(PersonTable, id)
if err != nil {
log.Fatal(err)
}
fmt.Println(person)
// Output:
// ID: 1 (int32), GroupID: 65534 (*int32), Name: `Denis Mills` (string), Email: <nil> (*string), CreatedAt: 2009-11-10 23:00:00 +0000 UTC (time.Time), UpdatedAt: <nil> (*time.Time)
}

func ExampleQuerier_SelectRows() {
tail := fmt.Sprintf("WHERE created_at < %s ORDER BY id", DB.Placeholder(1))
y2010 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC)
Expand Down
10 changes: 5 additions & 5 deletions querier_selects.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (q *Querier) SelectAllFrom(view View, tail string, args ...interface{}) (st
}

// findTail returns a tail of SELECT query for given view, column and arg.
func (q *Querier) findTail(view string, column string, arg interface{}, limit1 bool) (tail string, needArg bool) {
qi := q.QuoteIdentifier(view) + "." + q.QuoteIdentifier(column)
func (q *Querier) findTail(view View, column string, arg interface{}, limit1 bool) (tail string, needArg bool) {
qi := q.QualifiedView(view) + "." + q.QuoteIdentifier(column)
if arg == nil {
tail = fmt.Sprintf("WHERE %s IS NULL", qi)
} else {
Expand All @@ -142,7 +142,7 @@ func (q *Querier) findTail(view string, column string, arg interface{}, limit1 b
// If there are no rows in result, it returns ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFinder errors.
func (q *Querier) FindOneTo(str Struct, column string, arg interface{}) error {
tail, needArg := q.findTail(str.View().Name(), column, arg, true)
tail, needArg := q.findTail(str.View(), column, arg, true)
if needArg {
return q.SelectOneTo(str, tail, arg)
}
Expand All @@ -155,7 +155,7 @@ func (q *Querier) FindOneTo(str Struct, column string, arg interface{}) error {
// If there are no rows in result, it returns nil, ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFinder errors.
func (q *Querier) FindOneFrom(view View, column string, arg interface{}) (Struct, error) {
tail, needArg := q.findTail(view.Name(), column, arg, true)
tail, needArg := q.findTail(view, column, arg, true)
if needArg {
return q.SelectOneFrom(view, tail, arg)
}
Expand All @@ -169,7 +169,7 @@ func (q *Querier) FindOneFrom(view View, column string, arg interface{}) (Struct
//
// See SelectRows example for idiomatic usage.
func (q *Querier) FindRows(view View, column string, arg interface{}) (*sql.Rows, error) {
tail, needArg := q.findTail(view.Name(), column, arg, false)
tail, needArg := q.findTail(view, column, arg, false)
if needArg {
return q.SelectRows(view, tail, arg)
}
Expand Down
6 changes: 5 additions & 1 deletion reform-db/cmd_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func (s *ReformDBSuite) TestInit() {

fis, err := ioutil.ReadDir(dir)
s.Require().NoError(err)
s.Require().Len(fis, 4)
if s.db.Dialect == sqlite3.Dialect {
s.Require().Len(fis, 4) // generate 4 struct files for sqlite3
} else {
s.Require().Len(fis, 5) // generate 5 struct files for other DBs
}

ff := filepath.Join(dir, "people.go")
actual, err := parse.File(ff)
Expand Down