| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqlparser |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"fmt" |
| 22 |
|
| 23 |
"github.com/xwb1989/sqlparser/dependency/querypb" |
| 24 |
"github.com/xwb1989/sqlparser/dependency/sqltypes" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type ParsedQuery struct { |
| 30 |
Query string |
| 31 |
bindLocations []bindLocation |
| 32 |
} |
| 33 |
|
| 34 |
type bindLocation struct { |
| 35 |
offset, length int |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func NewParsedQuery(node SQLNode) *ParsedQuery { |
| 40 |
buf := NewTrackedBuffer(nil) |
| 41 |
buf.Myprintf("%v", node) |
| 42 |
return buf.ParsedQuery() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
func (pq *ParsedQuery) GenerateQuery(bindVariables map[string]*querypb.BindVariable, extras map[string]Encodable) ([]byte, error) { |
| 49 |
if len(pq.bindLocations) == 0 { |
| 50 |
return []byte(pq.Query), nil |
| 51 |
} |
| 52 |
buf := bytes.NewBuffer(make([]byte, 0, len(pq.Query))) |
| 53 |
current := 0 |
| 54 |
for _, loc := range pq.bindLocations { |
| 55 |
buf.WriteString(pq.Query[current:loc.offset]) |
| 56 |
name := pq.Query[loc.offset : loc.offset+loc.length] |
| 57 |
if encodable, ok := extras[name[1:]]; ok { |
| 58 |
encodable.EncodeSQL(buf) |
| 59 |
} else { |
| 60 |
supplied, _, err := FetchBindVar(name, bindVariables) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
EncodeValue(buf, supplied) |
| 65 |
} |
| 66 |
current = loc.offset + loc.length |
| 67 |
} |
| 68 |
buf.WriteString(pq.Query[current:]) |
| 69 |
return buf.Bytes(), nil |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func EncodeValue(buf *bytes.Buffer, value *querypb.BindVariable) { |
| 74 |
if value.Type != querypb.Type_TUPLE { |
| 75 |
|
| 76 |
v, _ := sqltypes.BindVariableToValue(value) |
| 77 |
v.EncodeSQL(buf) |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
buf.WriteByte('(') |
| 83 |
for i, bv := range value.Values { |
| 84 |
if i != 0 { |
| 85 |
buf.WriteString(", ") |
| 86 |
} |
| 87 |
sqltypes.ProtoToValue(bv).EncodeSQL(buf) |
| 88 |
} |
| 89 |
buf.WriteByte(')') |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func FetchBindVar(name string, bindVariables map[string]*querypb.BindVariable) (val *querypb.BindVariable, isList bool, err error) { |
| 94 |
name = name[1:] |
| 95 |
if name[0] == ':' { |
| 96 |
name = name[1:] |
| 97 |
isList = true |
| 98 |
} |
| 99 |
supplied, ok := bindVariables[name] |
| 100 |
if !ok { |
| 101 |
return nil, false, fmt.Errorf("missing bind var %s", name) |
| 102 |
} |
| 103 |
|
| 104 |
if isList { |
| 105 |
if supplied.Type != querypb.Type_TUPLE { |
| 106 |
return nil, false, fmt.Errorf("unexpected list arg type (%v) for key %s", supplied.Type, name) |
| 107 |
} |
| 108 |
if len(supplied.Values) == 0 { |
| 109 |
return nil, false, fmt.Errorf("empty list supplied for %s", name) |
| 110 |
} |
| 111 |
return supplied, true, nil |
| 112 |
} |
| 113 |
|
| 114 |
if supplied.Type == querypb.Type_TUPLE { |
| 115 |
return nil, false, fmt.Errorf("unexpected arg type (TUPLE) for non-list key %s", name) |
| 116 |
} |
| 117 |
|
| 118 |
return supplied, false, nil |
| 119 |
} |