Skip to content

Commit d194063

Browse files
committed
Add better example.
1 parent db00617 commit d194063

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

internal/test/sql/mssql_init.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ CREATE TABLE [people] (
77
[updated_at] datetime2
88
);
99

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

1215
CREATE TABLE [projects] (
1316
[name] varchar(255) NOT NULL,

internal/test/sql/mysql_init.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ CREATE TABLE people (
1111
PRIMARY KEY (id)
1212
);
1313

14-
CREATE VIEW people_copy AS SELECT * from people;
14+
CREATE VIEW people_0 AS SELECT * FROM people WHERE (id % 3) = 0;
15+
CREATE VIEW people_1 AS SELECT * FROM people WHERE (id % 3) = 1;
16+
CREATE VIEW people_2 AS SELECT * FROM people WHERE (id % 3) = 2;
1517

1618
CREATE TABLE projects (
1719
name varchar(255) NOT NULL,

internal/test/sql/postgres_init.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ CREATE TABLE people (
99
-- updated_at timestamp without time zone
1010
);
1111

12-
CREATE VIEW people_copy AS SELECT * from people;
12+
CREATE VIEW people_0 AS SELECT * FROM people WHERE (id % 3) = 0;
13+
CREATE VIEW people_1 AS SELECT * FROM people WHERE (id % 3) = 1;
14+
CREATE VIEW people_2 AS SELECT * FROM people WHERE (id % 3) = 2;
1315

1416
CREATE TABLE projects (
1517
name varchar NOT NULL,

internal/test/sql/sqlite3_init.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ CREATE TABLE people (
77
updated_at datetime
88
);
99

10-
CREATE VIEW people_copy AS SELECT * from people;
10+
CREATE VIEW people_0 AS SELECT * FROM people WHERE (id % 3) = 0;
11+
CREATE VIEW people_1 AS SELECT * FROM people WHERE (id % 3) = 1;
12+
CREATE VIEW people_2 AS SELECT * FROM people WHERE (id % 3) = 2;
1113

1214
CREATE TABLE projects (
1315
name varchar NOT NULL,

querier_examples_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ func ExampleQuerier_WithTag() {
8383

8484
func ExampleQuerier_WithView() {
8585
id := 1
86-
person, err := DB.WithView("people_copy").FindByPrimaryKeyFrom(PersonTable, id)
86+
view := fmt.Sprintf("people_%d", id%3)
87+
person, err := DB.WithView(view).FindByPrimaryKeyFrom(PersonTable, id)
8788
if err != nil {
8889
log.Fatal(err)
8990
}
90-
fmt.Println(person)
91+
fmt.Printf("%s: %s", view, person)
9192
// Output:
92-
// 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)
93+
// 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)
9394
}
9495

9596
func ExampleQuerier_SelectRows() {

reform-db/cmd_init_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ func (s *ReformDBSuite) TestInit() {
3838

3939
fis, err := ioutil.ReadDir(dir)
4040
s.Require().NoError(err)
41-
if s.db.Dialect == sqlite3.Dialect {
42-
s.Require().Len(fis, 4) // generate 4 struct files for sqlite3
43-
} else {
44-
s.Require().Len(fis, 5) // generate 5 struct files for other DBs
45-
}
41+
s.Require().Len(fis, 7) // 4 tables + views people_0, people_1, people_2
4642

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

0 commit comments

Comments
 (0)