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 COLON Bind resource #851

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
DOLLAR
NAMED
AT
COLON
)

var defaultBinds = map[int][]string{
Expand Down Expand Up @@ -78,6 +79,8 @@ func Rebind(bindType int, query string) string {
rqb = append(rqb, ':', 'a', 'r', 'g')
case AT:
rqb = append(rqb, '@', 'p')
case COLON:
rqb = append(rqb, ':')
}

j++
Expand Down
6 changes: 6 additions & 0 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ func compileNamedQuery(qs []byte, bindType int) (query string, names []string, e
rebound = append(rebound, byte(b))
}
currentVar++
case COLON:
rebound = append(rebound, ':')
for _, b := range strconv.Itoa(currentVar) {
rebound = append(rebound, byte(b))
}
currentVar++
case AT:
rebound = append(rebound, '@', 'p')
for _, b := range strconv.Itoa(currentVar) {
Expand Down
15 changes: 13 additions & 2 deletions named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func TestCompileQuery(t *testing.T) {
table := []struct {
Q, R, D, T, N string
V []string
Q, R, D, T, N, C string
V []string
}{
// basic test for named parameters, invalid char ',' terminating
{
Expand All @@ -18,6 +18,7 @@ func TestCompileQuery(t *testing.T) {
D: `INSERT INTO foo (a,b,c,d) VALUES ($1, $2, $3, $4)`,
T: `INSERT INTO foo (a,b,c,d) VALUES (@p1, @p2, @p3, @p4)`,
N: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last)`,
C: `INSERT INTO foo (a,b,c,d) VALUES (:1, :2, :3, :4)`,
V: []string{"name", "age", "first", "last"},
},
// This query tests a named parameter ending the string as well as numbers
Expand All @@ -27,6 +28,7 @@ func TestCompileQuery(t *testing.T) {
D: `SELECT * FROM a WHERE first_name=$1 AND last_name=$2`,
T: `SELECT * FROM a WHERE first_name=@p1 AND last_name=@p2`,
N: `SELECT * FROM a WHERE first_name=:name1 AND last_name=:name2`,
C: `SELECT * FROM a WHERE first_name=:1 AND last_name=:2`,
V: []string{"name1", "name2"},
},
{
Expand All @@ -35,6 +37,7 @@ func TestCompileQuery(t *testing.T) {
D: `SELECT ":foo" FROM a WHERE first_name=$1 AND last_name=$2`,
T: `SELECT ":foo" FROM a WHERE first_name=@p1 AND last_name=@p2`,
N: `SELECT ":foo" FROM a WHERE first_name=:name1 AND last_name=:name2`,
C: `SELECT ":foo" FROM a WHERE first_name=:1 AND last_name=:2`,
V: []string{"name1", "name2"},
},
{
Expand All @@ -43,6 +46,7 @@ func TestCompileQuery(t *testing.T) {
D: `SELECT 'a:b:c' || first_name, '::ABC:_:' FROM person WHERE first_name=$1 AND last_name=$2`,
T: `SELECT 'a:b:c' || first_name, '::ABC:_:' FROM person WHERE first_name=@p1 AND last_name=@p2`,
N: `SELECT 'a:b:c' || first_name, '::ABC:_:' FROM person WHERE first_name=:first_name AND last_name=:last_name`,
C: `SELECT 'a:b:c' || first_name, '::ABC:_:' FROM person WHERE first_name=:1 AND last_name=:2`,
V: []string{"first_name", "last_name"},
},
{
Expand All @@ -51,6 +55,7 @@ func TestCompileQuery(t *testing.T) {
D: `SELECT @name := "name", $1, $2, $3`,
N: `SELECT @name := "name", :age, :first, :last`,
T: `SELECT @name := "name", @p1, @p2, @p3`,
C: `SELECT @name := "name", :1, :2, :3`,
V: []string{"age", "first", "last"},
},
/* This unicode awareness test sadly fails, because of our byte-wise worldview.
Expand Down Expand Up @@ -96,6 +101,12 @@ func TestCompileQuery(t *testing.T) {
if qq != test.N {
t.Errorf("\nexpected: `%s`\ngot: `%s`\n(len: %d vs %d)", test.N, qq, len(test.N), len(qq))
}

qc, _, _ := compileNamedQuery([]byte(test.Q), COLON)
if qc != test.C {
t.Errorf("\nexpected: `%s`\ngot: `%s`\n(len: %d vs %d)", test.C, qc, len(test.C), len(qc))
}

}
}

Expand Down