Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Date type for unnamed parameters #859

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func DateNamed(name string, value time.Time, scale TimeUnit) driver.NamedDateVal
}
}

func Date(name string, value time.Time, scale TimeUnit) driver.DateValue {
return driver.DateValue{
Value: value,
Scale: uint8(scale),
}
}

var bindNumericRe = regexp.MustCompile(`\$[0-9]+`)
var bindPositionalRe = regexp.MustCompile(`[^\\][?]`)

Expand Down Expand Up @@ -102,12 +109,17 @@ func bindPositional(tz *time.Location, query string, args ...interface{}) (_ str
params = make([]string, len(args))
)
for i, v := range args {
if fn, ok := v.(std_driver.Valuer); ok {
if v, err = fn.Value(); err != nil {
switch vv := v.(type) {
case driver.DateValue:
params[i], err = format(tz, TimeUnit(vv.Scale), vv.Value)
case std_driver.Valuer:
if v, err = vv.Value(); err != nil {
return "", nil
}
params[i], err = format(tz, Seconds, v)
default:
params[i], err = format(tz, Seconds, v)
}
params[i], err = format(tz, Seconds, v)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -135,25 +147,32 @@ func bindNumeric(tz *time.Location, query string, args ...interface{}) (_ string
var (
unbind = make(map[string]struct{})
params = make(map[string]string)
val string
)
for i, v := range args {
if fn, ok := v.(std_driver.Valuer); ok {
if v, err = fn.Value(); err != nil {
switch vv := v.(type) {
case driver.DateValue:
val, err = format(tz, TimeUnit(vv.Scale), vv.Value)
case std_driver.Valuer:
if v, err = vv.Value(); err != nil {
return "", nil
}
val, err = format(tz, Seconds, v)
default:
val, err = format(tz, Seconds, v)
}
val, err := format(tz, Seconds, v)
if err != nil {
return "", err
}
params[fmt.Sprintf("$%d", i+1)] = val
}
query = bindNumericRe.ReplaceAllStringFunc(query, func(n string) string {
if _, found := params[n]; !found {
if param, found := params[n]; !found {
unbind[n] = struct{}{}
return ""
} else {
return param
}
return params[n]
})
for param := range unbind {
return "", fmt.Errorf("have no arg for %s param", param)
Expand Down
95 changes: 61 additions & 34 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,58 @@ import (
)

func TestBindNumeric(t *testing.T) {
_, err := bind(time.Local, `
SELECT * FROM t WHERE col = $1
AND col2 = $2
AND col3 = $1
ANS col4 = $3
AND null_coll = $4
)
`, 1, 2, "I'm a string param", nil)
if assert.NoError(t, err) {
assets := []struct {
query string
params []interface{}
expected string
}{
{
query: "SELECT $1",
params: []interface{}{1},
expected: "SELECT 1",
},
{
query: "SELECT $2 $1 $3",
params: []interface{}{1, 2, 3},
expected: "SELECT 2 1 3",
},
{
query: "SELECT $2 $1 $3",
params: []interface{}{"a", "b", "c"},
expected: "SELECT 'b' 'a' 'c'",
},
}
assets := []struct {
query string
params []interface{}
expected string
}{
{
query: "SELECT $1",
params: []interface{}{1},
expected: "SELECT 1",
},
{
query: "SELECT $2 $1 $3",
params: []interface{}{1, 2, 3},
expected: "SELECT 2 1 3",
},
{
query: "SELECT $2 $1 $3",
params: []interface{}{"a", "b", "c"},
expected: "SELECT 'b' 'a' 'c'",
},
{
query: "SELECT $1",
params: []interface{}{time.Unix(1671565275, 0).UTC()},
expected: "SELECT toDateTime('2022-12-20 19:41:15', 'UTC')",
},
{
query: "SELECT $1",
params: []interface{}{Date("Time", time.Unix(1671565275, 10).UTC(), NanoSeconds)},
expected: "SELECT toDateTime64('2022-12-20 19:41:15.000000010', 9, 'UTC')",
},
{
query: `
SELECT * FROM t WHERE col = $1
AND col2 = $2
AND col3 = $1
ANS col4 = $3
AND null_coll = $4
`,
params: []interface{}{1, 2, "I'm a string param", nil},
expected: `
SELECT * FROM t WHERE col = 1
AND col2 = 2
AND col3 = 1
ANS col4 = 'I\'m a string param'
AND null_coll = NULL
`,
},
}

for _, asset := range assets {
if actual, err := bind(time.Local, asset.query, asset.params...); assert.NoError(t, err) {
assert.Equal(t, asset.expected, actual)
}
for _, asset := range assets {
if actual, err := bind(time.Local, asset.query, asset.params...); assert.NoError(t, err) {
assert.Equal(t, asset.expected, actual)
}
}
}
Expand Down Expand Up @@ -111,6 +128,11 @@ func TestBindNamed(t *testing.T) {
},
expected: "SELECT 'b' 'a' 'c'",
},
{
query: "SELECT @Time",
params: []interface{}{DateNamed("Time", time.Unix(1671565275, 10).UTC(), NanoSeconds)},
expected: "SELECT toDateTime64('2022-12-20 19:41:15.000000010', 9, 'UTC')",
},
}
for _, asset := range assets {
if actual, err := bind(time.Local, asset.query, asset.params...); assert.NoError(t, err) {
Expand Down Expand Up @@ -160,6 +182,11 @@ func TestBindPositional(t *testing.T) {
params: []interface{}{"a"},
expected: "SELECT x where col = 'blah?' AND col2 = 'a'",
},
{
query: "SELECT ?",
params: []interface{}{Date("Time", time.Unix(1671565275, 10).UTC(), NanoSeconds)},
expected: "SELECT toDateTime64('2022-12-20 19:41:15.000000010', 9, 'UTC')",
},
}

for _, asset := range assets {
Expand Down
5 changes: 5 additions & 0 deletions lib/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type (
Scale uint8
}

DateValue struct {
Value time.Time
Scale uint8
}

Stats struct {
MaxOpenConns int
MaxIdleConns int
Expand Down