mirror of https://github.com/jackc/pgx.git
parent
dfb6489612
commit
52f2151422
|
@ -14,6 +14,9 @@ import (
|
||||||
//
|
//
|
||||||
// conn.Query(ctx, "select * from widgets where foo = @foo and bar = @bar", pgx.NamedArgs{"foo": 1, "bar": 2})
|
// conn.Query(ctx, "select * from widgets where foo = @foo and bar = @bar", pgx.NamedArgs{"foo": 1, "bar": 2})
|
||||||
// conn.Query(ctx, "select * from widgets where foo = $1 and bar = $2", 1, 2)
|
// conn.Query(ctx, "select * from widgets where foo = $1 and bar = $2", 1, 2)
|
||||||
|
//
|
||||||
|
// Named placeholders are case sensitive and must start with a letter or underscore. Subsequent characters can be
|
||||||
|
// letters, numbers, or underscores.
|
||||||
type NamedArgs map[string]any
|
type NamedArgs map[string]any
|
||||||
|
|
||||||
// RewriteQuery implements the QueryRewriter interface.
|
// RewriteQuery implements the QueryRewriter interface.
|
||||||
|
@ -80,7 +83,7 @@ func rawState(l *sqlLexer) stateFn {
|
||||||
return doubleQuoteState
|
return doubleQuoteState
|
||||||
case '@':
|
case '@':
|
||||||
nextRune, _ := utf8.DecodeRuneInString(l.src[l.pos:])
|
nextRune, _ := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||||
if isLetter(nextRune) {
|
if isLetter(nextRune) || nextRune == '_' {
|
||||||
if l.pos-l.start > 0 {
|
if l.pos-l.start > 0 {
|
||||||
l.parts = append(l.parts, l.src[l.start:l.pos-width])
|
l.parts = append(l.parts, l.src[l.start:l.pos-width])
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,10 +38,10 @@ func TestNamedArgsRewriteQuery(t *testing.T) {
|
||||||
expectedArgs: []any{int32(42), "foo"},
|
expectedArgs: []any{int32(42), "foo"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: "select @Abc::int, @b_4::text",
|
sql: "select @Abc::int, @b_4::text, @_c::int",
|
||||||
namedArgs: pgx.NamedArgs{"Abc": int32(42), "b_4": "foo"},
|
namedArgs: pgx.NamedArgs{"Abc": int32(42), "b_4": "foo", "_c": int32(1)},
|
||||||
expectedSQL: "select $1::int, $2::text",
|
expectedSQL: "select $1::int, $2::text, $3::int",
|
||||||
expectedArgs: []any{int32(42), "foo"},
|
expectedArgs: []any{int32(42), "foo", int32(1)},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: "at end @",
|
sql: "at end @",
|
||||||
|
@ -50,15 +50,15 @@ func TestNamedArgsRewriteQuery(t *testing.T) {
|
||||||
expectedArgs: []any{},
|
expectedArgs: []any{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: "ignores without letter after @ foo bar",
|
sql: "ignores without valid character after @ foo bar",
|
||||||
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
|
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
|
||||||
expectedSQL: "ignores without letter after @ foo bar",
|
expectedSQL: "ignores without valid character after @ foo bar",
|
||||||
expectedArgs: []any{},
|
expectedArgs: []any{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: "name must start with letter @1 foo bar",
|
sql: "name cannot start with number @1 foo bar",
|
||||||
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
|
namedArgs: pgx.NamedArgs{"a": int32(42), "b": "foo"},
|
||||||
expectedSQL: "name must start with letter @1 foo bar",
|
expectedSQL: "name cannot start with number @1 foo bar",
|
||||||
expectedArgs: []any{},
|
expectedArgs: []any{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue