Skip to content

Commit 2bb6fd3

Browse files
committed
Fix using boolean fields in condition indexes
1 parent 54a6f0d commit 2bb6fd3

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

internal/pkg/backend/postgres/tmpl/pkg/index.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ var index{{ $ind.Name }} = postgres.Index{
158158
{{ $des := $packerparam.StringDeserializer }}
159159
{{ if ne (len $des) 0 }}
160160
{{ index $des 0 }}"{{ $v }}"{{ index $des 1 }},
161+
{{ else if eq $ifield.Format "bool" }}
162+
{{ $v }},
161163
{{ else }}
162164
"{{ $v }}",
163165
{{ end }}

internal/pkg/parser/index_condition_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,45 @@ func TestParseIndexConditionTag(t *testing.T) {
170170
},
171171
wantErr: false,
172172
},
173+
{
174+
name: "boolean true condition",
175+
condTag: "IsActive[=]true",
176+
fieldsMap: map[string]int{"IsActive": 0},
177+
want: map[int]ds.IndexCondition{
178+
0: {
179+
ConditionType: "=",
180+
Value: []string{"true"},
181+
IsNullCheck: false,
182+
},
183+
},
184+
wantErr: false,
185+
},
186+
{
187+
name: "boolean false condition",
188+
condTag: "IsActive[=]false",
189+
fieldsMap: map[string]int{"IsActive": 0},
190+
want: map[int]ds.IndexCondition{
191+
0: {
192+
ConditionType: "=",
193+
Value: []string{"false"},
194+
IsNullCheck: false,
195+
},
196+
},
197+
wantErr: false,
198+
},
199+
{
200+
name: "boolean not equal true",
201+
condTag: "IsActive[!=]true",
202+
fieldsMap: map[string]int{"IsActive": 0},
203+
want: map[int]ds.IndexCondition{
204+
0: {
205+
ConditionType: "!=",
206+
Value: []string{"true"},
207+
IsNullCheck: false,
208+
},
209+
},
210+
wantErr: false,
211+
},
173212
}
174213

175214
for _, tt := range tests {

pkg/postgres/postgres_b_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,94 @@ func TestGenerateSelect(t *testing.T) {
475475
},
476476
wantErr: false,
477477
},
478+
{
479+
name: "condition_with_boolean_true",
480+
args: args{
481+
tableName: "users",
482+
fieldNames: []string{"id", "name", "is_active"},
483+
index: postgres.Index{
484+
Unique: false,
485+
Fields: postgres.OrderedFields{postgres.OrderField{Field: "id", Order: postgres.ASC}},
486+
Condition: []postgres.Condition{{Field: "is_active", Operator: "=", Values: []any{true}}},
487+
},
488+
keys: [][]any{{1}},
489+
offset: 0,
490+
limit: 0,
491+
cursor: postgres.CursorPosition{},
492+
},
493+
want: &postgres.Query{
494+
QueryString: `SELECT id, name, is_active FROM "users" WHERE is_active = $1 AND id = $2 ORDER BY id ASC`,
495+
ConditionExists: true,
496+
Params: []any{true, 1},
497+
},
498+
wantErr: false,
499+
},
500+
{
501+
name: "condition_with_boolean_false",
502+
args: args{
503+
tableName: "users",
504+
fieldNames: []string{"id", "name", "is_deleted"},
505+
index: postgres.Index{
506+
Unique: false,
507+
Fields: postgres.OrderedFields{postgres.OrderField{Field: "id", Order: postgres.ASC}},
508+
Condition: []postgres.Condition{{Field: "is_deleted", Operator: "=", Values: []any{false}}},
509+
},
510+
keys: [][]any{{2}},
511+
offset: 0,
512+
limit: 0,
513+
cursor: postgres.CursorPosition{},
514+
},
515+
want: &postgres.Query{
516+
QueryString: `SELECT id, name, is_deleted FROM "users" WHERE is_deleted = $1 AND id = $2 ORDER BY id ASC`,
517+
ConditionExists: true,
518+
Params: []any{false, 2},
519+
},
520+
wantErr: false,
521+
},
522+
{
523+
name: "condition_with_boolean_not_equal",
524+
args: args{
525+
tableName: "users",
526+
fieldNames: []string{"id", "name", "is_verified"},
527+
index: postgres.Index{
528+
Unique: false,
529+
Fields: postgres.OrderedFields{postgres.OrderField{Field: "id", Order: postgres.ASC}},
530+
Condition: []postgres.Condition{{Field: "is_verified", Operator: "!=", Values: []any{false}}},
531+
},
532+
keys: [][]any{{3}},
533+
offset: 0,
534+
limit: 0,
535+
cursor: postgres.CursorPosition{},
536+
},
537+
want: &postgres.Query{
538+
QueryString: `SELECT id, name, is_verified FROM "users" WHERE is_verified != $1 AND id = $2 ORDER BY id ASC`,
539+
ConditionExists: true,
540+
Params: []any{false, 3},
541+
},
542+
wantErr: false,
543+
},
544+
{
545+
name: "bulk_query_with_boolean_condition",
546+
args: args{
547+
tableName: "users",
548+
fieldNames: []string{"id", "name", "is_active"},
549+
index: postgres.Index{
550+
Unique: false,
551+
Fields: postgres.OrderedFields{postgres.OrderField{Field: "id", Order: postgres.ASC}},
552+
Condition: []postgres.Condition{{Field: "is_active", Operator: "=", Values: []any{true}}},
553+
},
554+
keys: [][]any{{1}, {2}, {3}},
555+
offset: 0,
556+
limit: 10,
557+
cursor: postgres.CursorPosition{},
558+
},
559+
want: &postgres.Query{
560+
QueryString: `SELECT id, name, is_active FROM "users" WHERE is_active = $1 AND id IN ($2, $3, $4) ORDER BY id ASC LIMIT 10`,
561+
ConditionExists: true,
562+
Params: []any{true, 1, 2, 3},
563+
},
564+
wantErr: false,
565+
},
478566
}
479567

480568
for _, tt := range tests {

0 commit comments

Comments
 (0)