Allow Raw helper to accept named arguments
This commit is contained in:
parent
9385f462df
commit
7af9072b8d
12 changed files with 340 additions and 36 deletions
|
|
@ -81,9 +81,24 @@ var TimestampExp = jet.TimestampExp
|
|||
// Does not add sql cast to generated sql builder output.
|
||||
var TimestampzExp = jet.TimestampzExp
|
||||
|
||||
// Raw can be used for any unsupported functions, operators or expressions.
|
||||
// For example: Raw("current_database()")
|
||||
var Raw = jet.Raw
|
||||
// RawArgs is type used to pass optional arguments to Raw method
|
||||
type RawArgs = map[string]interface{}
|
||||
|
||||
var (
|
||||
// Raw can be used for any unsupported functions, operators or expressions.
|
||||
// For example: Raw("current_database()")
|
||||
Raw = jet.Raw
|
||||
|
||||
// Raw helper methods for each of the postgres type
|
||||
RawInt = jet.RawInt
|
||||
RawFloat = jet.RawFloat
|
||||
RawString = jet.RawString
|
||||
RawTime = jet.RawTime
|
||||
RawTimez = jet.RawTimez
|
||||
RawTimestamp = jet.RawTimestamp
|
||||
RawTimestampz = jet.RawTimestampz
|
||||
RawDate = jet.RawDate
|
||||
)
|
||||
|
||||
// Func can be used to call an custom or as of yet unsupported function in the database.
|
||||
var Func = jet.Func
|
||||
|
|
|
|||
64
postgres/expressions_test.go
Normal file
64
postgres/expressions_test.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRaw(t *testing.T) {
|
||||
assertSerialize(t, Raw("current_database()"), "(current_database())")
|
||||
|
||||
assertSerialize(t, Raw(":first_arg + table.colInt + :second_arg", RawArgs{":first_arg": 11, ":second_arg": 22}),
|
||||
"($1 + table.colInt + $2)", 11, 22)
|
||||
|
||||
assertSerialize(t,
|
||||
Int(700).ADD(RawInt(":first_arg + table.colInt + :second_arg", RawArgs{":first_arg": 11, ":second_arg": 22})),
|
||||
"($1 + ($2 + table.colInt + $3))",
|
||||
int64(700), 11, 22)
|
||||
}
|
||||
|
||||
func TestDuplicateArguments(t *testing.T) {
|
||||
|
||||
assertSerialize(t, Raw(":arg + table.colInt + :arg", RawArgs{":arg": 11}),
|
||||
"($1 + table.colInt + $1)", 11)
|
||||
|
||||
assertSerialize(t, Raw("#age + table.colInt + #year + #age + #year + 11", RawArgs{"#age": 11, "#year": 2000}),
|
||||
"($1 + table.colInt + $2 + $1 + $2 + 11)", 11, 2000)
|
||||
|
||||
assertSerialize(t, Raw("#1 + all_types.integer + #2 + #1 + #2 + #3 + #4",
|
||||
RawArgs{"#1": 11, "#2": 22, "#3": 33, "#4": 44}),
|
||||
`($1 + all_types.integer + $2 + $1 + $2 + $3 + $4)`, 11, 22, 11, 22, 33, 44)
|
||||
}
|
||||
|
||||
func TestRawInvalidArguments(t *testing.T) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
require.Equal(t, "jet: named argument 'first_arg' does not appear in raw query", r)
|
||||
}()
|
||||
|
||||
assertSerialize(t, Raw("table.colInt + :second_arg", RawArgs{
|
||||
"first_arg": 11,
|
||||
"second_arg": 22,
|
||||
}), "(table.colInt + $1)", 22)
|
||||
}
|
||||
|
||||
func TestRawHelperMethods(t *testing.T) {
|
||||
assertSerialize(t, RawFloat("table.colInt + :float", RawArgs{":float": 11.22}).EQ(Float(3.14)),
|
||||
"((table.colInt + $1) = $2)", 11.22, 3.14)
|
||||
assertSerialize(t, RawString("table.colStr || str", RawArgs{"str": "doe"}).EQ(String("john doe")),
|
||||
"((table.colStr || $1) = $2)", "doe", "john doe")
|
||||
|
||||
now := time.Now()
|
||||
assertSerialize(t, RawTime("table.colTime").EQ(TimeT(now)),
|
||||
"((table.colTime) = $1::time without time zone)", now)
|
||||
assertSerialize(t, RawTimez("table.colTime").EQ(TimezT(now)),
|
||||
"((table.colTime) = $1::time with time zone)", now)
|
||||
assertSerialize(t, RawTimestamp("table.colTimestamp").EQ(TimestampT(now)),
|
||||
"((table.colTimestamp) = $1::timestamp without time zone)", now)
|
||||
assertSerialize(t, RawTimestampz("table.colTimestampz").EQ(TimestampzT(now)),
|
||||
"((table.colTimestampz) = $1::timestamp with time zone)", now)
|
||||
assertSerialize(t, RawDate("table.colDate").EQ(DateT(now)),
|
||||
"((table.colDate) = $1::date)", now)
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ func INTERVAL(quantityAndUnit ...quantityAndUnit) IntervalExpression {
|
|||
|
||||
newInterval := &intervalExpression{}
|
||||
|
||||
newInterval.Expression = jet.Raw(intervalStr, newInterval)
|
||||
newInterval.Expression = jet.RawWithParent(intervalStr, newInterval)
|
||||
newInterval.intervalInterfaceImpl.parent = newInterval
|
||||
|
||||
return newInterval
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue