Skip to content

Commit

Permalink
Add better example.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Dec 12, 2018
1 parent db00617 commit d194063
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
5 changes: 4 additions & 1 deletion internal/test/sql/mssql_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ CREATE TABLE [people] (
[updated_at] datetime2
);

EXEC('CREATE VIEW [people_copy] AS SELECT * from [people]')
-- EXEC is workaround for "'CREATE VIEW' must be the first statement in a query batch."
EXEC('CREATE VIEW [people_0] AS SELECT * FROM [people] WHERE (id % 3) = 0');
EXEC('CREATE VIEW [people_1] AS SELECT * FROM [people] WHERE (id % 3) = 1');
EXEC('CREATE VIEW [people_2] AS SELECT * FROM [people] WHERE (id % 3) = 2');

CREATE TABLE [projects] (
[name] varchar(255) NOT NULL,
Expand Down
4 changes: 3 additions & 1 deletion internal/test/sql/mysql_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ CREATE TABLE people (
PRIMARY KEY (id)
);

CREATE VIEW people_copy AS SELECT * from people;
CREATE VIEW people_0 AS SELECT * FROM people WHERE (id % 3) = 0;
CREATE VIEW people_1 AS SELECT * FROM people WHERE (id % 3) = 1;
CREATE VIEW people_2 AS SELECT * FROM people WHERE (id % 3) = 2;

CREATE TABLE projects (
name varchar(255) NOT NULL,
Expand Down
4 changes: 3 additions & 1 deletion internal/test/sql/postgres_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ CREATE TABLE people (
-- updated_at timestamp without time zone
);

CREATE VIEW people_copy AS SELECT * from people;
CREATE VIEW people_0 AS SELECT * FROM people WHERE (id % 3) = 0;
CREATE VIEW people_1 AS SELECT * FROM people WHERE (id % 3) = 1;
CREATE VIEW people_2 AS SELECT * FROM people WHERE (id % 3) = 2;

CREATE TABLE projects (
name varchar NOT NULL,
Expand Down
4 changes: 3 additions & 1 deletion internal/test/sql/sqlite3_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ CREATE TABLE people (
updated_at datetime
);

CREATE VIEW people_copy AS SELECT * from people;
CREATE VIEW people_0 AS SELECT * FROM people WHERE (id % 3) = 0;
CREATE VIEW people_1 AS SELECT * FROM people WHERE (id % 3) = 1;
CREATE VIEW people_2 AS SELECT * FROM people WHERE (id % 3) = 2;

CREATE TABLE projects (
name varchar NOT NULL,
Expand Down
7 changes: 4 additions & 3 deletions querier_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ func ExampleQuerier_WithTag() {

func ExampleQuerier_WithView() {
id := 1
person, err := DB.WithView("people_copy").FindByPrimaryKeyFrom(PersonTable, id)
view := fmt.Sprintf("people_%d", id%3)
person, err := DB.WithView(view).FindByPrimaryKeyFrom(PersonTable, id)
if err != nil {
log.Fatal(err)
}
fmt.Println(person)
fmt.Printf("%s: %s", view, 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)
// people_1: 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() {
Expand Down
6 changes: 1 addition & 5 deletions reform-db/cmd_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ func (s *ReformDBSuite) TestInit() {

fis, err := ioutil.ReadDir(dir)
s.Require().NoError(err)
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
}
s.Require().Len(fis, 7) // 4 tables + views people_0, people_1, people_2

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

0 comments on commit d194063

Please sign in to comment.