Skip to content

Commit a859267

Browse files
authored
Merge pull request #2 from swiftcarrot/hotfix/testType
添加各类型的测试
2 parents 3ac27b2 + 1c830e8 commit a859267

37 files changed

+770
-125
lines changed

.gitattributes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.gotmpl linguist-language=Go
1+
*.go linguist-language=Go linguist-generated=false
2+
*.gotmpl linguist-language=Go linguist-generated=false

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test-postgresql: install
2020
cd internal/integration && QUERYX_ENV=test queryx db:create --schema postgresql.hcl
2121
cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema postgresql.hcl
2222
cd internal/integration && QUERYX_ENV=test queryx generate --schema postgresql.hcl
23-
cd internal/integration && go test -v ./...
23+
cd internal/integration && go test ./...
2424
# cd internal/integration && QUERYX_ENV=test queryx db:drop --schema postgresql.hcl
2525

2626
test-mysql: install mysql-drop

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,21 @@ post, err := c.QueryPost().Where(c.PostTitle.EQ("post title")).First()
172172

173173
Queryx supports association definition in the schema file. It also generates corresponding preload query methods to avoid "N+1" query.
174174

175-
## has_one and belongs_to
175+
## belongs_to
176+
177+
```hcl
178+
model "Post" {
179+
belongs_to "Author" {
180+
model_name = "User"
181+
}
182+
}
183+
```
184+
185+
```go
186+
c.QueryPost().PreloadAuthor().All()
187+
```
188+
189+
## has_one
176190

177191
```hcl
178192
model "User" {
@@ -199,7 +213,7 @@ c.QueryUser().PreloadAccount().All()
199213
c.QueryAccount().PreloadUser().All()
200214
```
201215

202-
## has_many and belongs_to
216+
## has_many
203217

204218
```hcl
205219
model "User" {

generator/client/golang/templates/[model].gotmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type {{ $.model.Name }} struct {
1616
{{ $b.Name | pascal }} *{{ $b.ModelName }} `json:"{{ camel $b.Name }}"`
1717
{{- end }}
1818
{{- range $h := $.model.HasMany }}
19-
{{ $h.Name | pascal }} []*{{ $h.ModelName }}
19+
{{ $h.Name | pascal }} []*{{ $h.ModelName }} `json:"{{ $h.Name | camel }}"`
2020
{{- end }}
2121
{{- range $h := $.model.HasOne }}
22-
{{ $h.Name | pascal }} *{{ $h.ModelName }}
22+
{{ $h.Name | pascal }} *{{ $h.ModelName }} `json:"{{ $h.Name | camel }}"`
2323
{{- end }}
2424

2525
schema *queryx.Schema

generator/client/golang/templates/[model]_query.gotmpl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ func (q *{{ $.model.Name }}Query) Preload{{ pascal $b.Name }}() *{{ $.model.Name
193193
func (q *{{ $.model.Name }}Query) preload{{ pascal $b.Name }}(rows []*{{ $.model.Name }}) error {
194194
ids := []int64{}
195195
for _, r := range rows {
196-
if !r.{{ $b.ModelName }}ID.Null {
197-
ids = append(ids, r.{{ $b.ModelName }}ID.Val)
196+
if !r.{{ $b.ForeignKey | pascal }}.Null {
197+
ids = append(ids, r.{{ $b.ForeignKey | pascal }}.Val)
198198
}
199199
}
200200
rows1, err := q.queries.Query{{ $b.ModelName }}().Where(q.schema.{{ $b.ModelName }}ID.In(ids)).All()
@@ -207,8 +207,8 @@ func (q *{{ $.model.Name }}Query) preload{{ pascal $b.Name }}(rows []*{{ $.model
207207
m[r.ID] = r
208208
}
209209
for _, r := range rows {
210-
if !r.{{ $b.ModelName }}ID.Null {
211-
r.{{ $b.ModelName }} = m[r.{{ $b.ModelName }}ID.Val]
210+
if !r.{{ $b.ForeignKey | pascal }}.Null {
211+
r.{{ $b.Name | pascal }} = m[r.{{ $b.ForeignKey | pascal }}.Val]
212212
}
213213
}
214214

@@ -240,7 +240,11 @@ func (q *{{ $.model.Name }}Query) preload{{ pascal $h.Name }}(rows []*{{ $.model
240240
m1[r.{{ $.model.Name }}ID.Val] = append(m1[r.{{ $.model.Name }}ID.Val], r)
241241
}
242242
for _, r := range rows {
243-
r.{{ $h.Through | pascal }} = m1[r.ID]
243+
if m1[r.ID] != nil {
244+
r.{{ $h.Through | pascal }} = m1[r.ID]
245+
} else {
246+
r.{{ $h.Through | pascal }} = make([]*{{ $m }}, 0)
247+
}
244248
}
245249

246250
ids1 := []int64{}
@@ -264,7 +268,11 @@ func (q *{{ $.model.Name }}Query) preload{{ pascal $h.Name }}(rows []*{{ $.model
264268
m3[r.{{ $.model.Name }}ID.Val] = append(m3[r.{{ $.model.Name }}ID.Val], r.{{ $h.ModelName }})
265269
}
266270
for _, r := range rows {
267-
r.{{ $h.Name | pascal }} = m3[r.ID]
271+
if m3[r.ID] != nil {
272+
r.{{ $h.Name | pascal }} = m3[r.ID]
273+
} else {
274+
r.{{ $h.Name | pascal }} = make([]*{{ $h.ModelName }},0)
275+
}
268276
}
269277
{{- else }}
270278
rows1, err := q.queries.Query{{ $h.ModelName }}().Where(q.schema.{{ $h.ModelName }}{{ $.model.Name }}ID.In(ids)).All()
@@ -277,7 +285,11 @@ func (q *{{ $.model.Name }}Query) preload{{ pascal $h.Name }}(rows []*{{ $.model
277285
m[r.{{ $.model.Name }}ID.Val] = append(m[r.{{ $.model.Name }}ID.Val], r)
278286
}
279287
for _, r := range rows {
280-
r.{{ $h.Name | pascal }} = m[r.ID]
288+
if m[r.ID] != nil {
289+
r.{{ $h.Name | pascal }} = m[r.ID]
290+
} else {
291+
r.{{ $h.Name | pascal }} = make([]*{{ $h.ModelName }}, 0)
292+
}
281293
}
282294
{{- end }}
283295

@@ -320,15 +332,15 @@ func (q *{{.model.Name}}Query) All() ([]*{{.model.Name}}, error) {
320332
}
321333
var rows []{{ $.model.Name }}
322334
{{- $var1 := $.model.Name | camel | plural }}
323-
var {{ $var1 }} []*{{ $.model.Name }}
335+
{{ $var1 }} := make([]*{{ $.model.Name }}, 0)
324336
query, args := q.selectStatement.ToSQL()
325337
err := q.adapter.Query(query, args...).Scan(&rows)
326338
if err != nil {
327339
return nil, err
328340
}
329341

330342
if len(rows) == 0 {
331-
return nil, err
343+
return {{ $var1 }}, nil
332344
}
333345

334346
for i := range rows {

generator/client/golang/templates/queryx/bigint.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package queryx
55
import (
66
"database/sql"
77
"database/sql/driver"
8+
"encoding/json"
89
)
910

1011
type BigInt struct {
@@ -43,10 +44,22 @@ func (b BigInt) Value() (driver.Value, error) {
4344
return b.Val, nil
4445
}
4546

47+
// MarshalJSON implements the json.Marshaler interface.
4648
func (b BigInt) MarshalJSON() ([]byte, error) {
47-
return nil, nil
49+
if b.Null {
50+
return json.Marshal(nil)
51+
}
52+
return json.Marshal(b.Val)
4853
}
4954

50-
func (b *BigInt) UnmarshalJSON(text []byte) error {
55+
// UnmarshalJSON implements the json.Unmarshaler interface.
56+
func (b *BigInt) UnmarshalJSON(data []byte) error {
57+
if string(data) == "null" {
58+
b.Null = true
59+
return nil
60+
}
61+
if err := json.Unmarshal(data, &b.Val); err != nil {
62+
return err
63+
}
5164
return nil
5265
}

generator/client/golang/templates/queryx/bigint_column.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (c *BigIntColumn) GE(v int64) *Clause {
5959
}
6060

6161
func (c *BigIntColumn) In(v []int64) *Clause {
62+
if len(v) == 0 {
63+
return &Clause{
64+
fragment: fmt.Sprintf("1=0"),
65+
}
66+
}
6267
return &Clause{
6368
fragment: fmt.Sprintf("%s.%s IN (?)", c.Table.Name, c.Name),
6469
args: []interface{}{v},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Code generated by queryx, DO NOT EDIT.
2+
3+
package queryx
4+
5+
import (
6+
"encoding/json"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNewBigInt(t *testing.T) {
13+
b1 := NewBigInt(2)
14+
require.Equal(t, int64(2), b1.Val)
15+
require.Equal(t, false, b1.Null)
16+
17+
b2 := NewNullableBigInt(nil)
18+
require.Equal(t, true, b2.Null)
19+
}
20+
21+
func TestBigIntJSON(t *testing.T) {
22+
type Foo struct {
23+
X BigInt `json:"x"`
24+
Y BigInt `json:"y"`
25+
}
26+
x := NewBigInt(2)
27+
y := NewNullableBigInt(nil)
28+
s := `{"x":2,"y":null}`
29+
30+
f1 := Foo{X: x, Y: y}
31+
b, err := json.Marshal(f1)
32+
require.NoError(t, err)
33+
require.Equal(t, s, string(b))
34+
35+
var f2 Foo
36+
err = json.Unmarshal([]byte(s), &f2)
37+
require.NoError(t, err)
38+
require.Equal(t, x, f2.X)
39+
require.Equal(t, y, f2.Y)
40+
}

generator/client/golang/templates/queryx/boolean.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package queryx
55
import (
66
"database/sql"
77
"database/sql/driver"
8+
"encoding/json"
89
)
910

1011
type Boolean struct {
@@ -42,3 +43,23 @@ func (b Boolean) Value() (driver.Value, error) {
4243
}
4344
return b.Val, nil
4445
}
46+
47+
// MarshalJSON implements the json.Marshaler interface.
48+
func (b Boolean) MarshalJSON() ([]byte, error) {
49+
if b.Null {
50+
return json.Marshal(nil)
51+
}
52+
return json.Marshal(b.Val)
53+
}
54+
55+
// UnmarshalJSON implements the json.Unmarshaler interface.
56+
func (b *Boolean) UnmarshalJSON(data []byte) error {
57+
if string(data) == "null" {
58+
b.Null = true
59+
return nil
60+
}
61+
if err := json.Unmarshal(data, &b.Val); err != nil {
62+
return err
63+
}
64+
return nil
65+
}

generator/client/golang/templates/queryx/boolean_column.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ func (c *BooleanColumn) NE(v bool) *Clause {
2929
args: []interface{}{v},
3030
}
3131
}
32+
33+
func (b *BooleanColumn) Asc() string {
34+
return fmt.Sprintf("%s.%s ASC", b.Table.Name, b.Name)
35+
}
36+
37+
func (b *BooleanColumn) Desc() string {
38+
return fmt.Sprintf("%s.%s DESC", b.Table.Name, b.Name)
39+
}

0 commit comments

Comments
 (0)