dialect/sql: make WriteOp chainable (#710)

This commit is contained in:
Ariel Mashraki
2020-08-29 21:18:10 +03:00
committed by GitHub
parent 77122ce70e
commit e215ae0cd7
2 changed files with 12 additions and 1 deletions

View File

@@ -1915,10 +1915,11 @@ var ops = [...]string{
} }
// WriteOp writes an operator to the builder. // WriteOp writes an operator to the builder.
func (b *Builder) WriteOp(op Op) { func (b *Builder) WriteOp(op Op) *Builder {
if op >= OpEQ && op <= OpLike { if op >= OpEQ && op <= OpLike {
b.Pad().WriteString(ops[op]).Pad() b.Pad().WriteString(ops[op]).Pad()
} }
return b
} }
// JSONOption allows for calling database JSON paths with functional options. // JSONOption allows for calling database JSON paths with functional options.

View File

@@ -1249,6 +1249,16 @@ WHERE
wantQuery: `SELECT * FROM "users" WHERE CAST("a"->'b'->'c'->1->'d' AS int) = $1`, wantQuery: `SELECT * FROM "users" WHERE CAST("a"->'b'->'c'->1->'d' AS int) = $1`,
wantArgs: []interface{}{1}, wantArgs: []interface{}{1},
}, },
{
input: Dialect(dialect.Postgres).
Select("*").
From(Table("test")).
Where(P(func(b *Builder) {
b.WriteString("nlevel(").Ident("path").WriteByte(')').WriteOp(OpGT).Arg(1)
})),
wantQuery: `SELECT * FROM "test" WHERE nlevel("path") > $1`,
wantArgs: []interface{}{1},
},
} }
for i, tt := range tests { for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) { t.Run(strconv.Itoa(i), func(t *testing.T) {