| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqlparser |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/xwb1989/sqlparser/dependency/sqltypes" |
| 24 |
|
| 25 |
"github.com/xwb1989/sqlparser/dependency/querypb" |
| 26 |
) |
| 27 |
|
| 28 |
func TestNewParsedQuery(t *testing.T) { |
| 29 |
stmt, err := Parse("select * from a where id =:id") |
| 30 |
if err != nil { |
| 31 |
t.Error(err) |
| 32 |
return |
| 33 |
} |
| 34 |
pq := NewParsedQuery(stmt) |
| 35 |
want := &ParsedQuery{ |
| 36 |
Query: "select * from a where id = :id", |
| 37 |
bindLocations: []bindLocation{{offset: 27, length: 3}}, |
| 38 |
} |
| 39 |
if !reflect.DeepEqual(pq, want) { |
| 40 |
t.Errorf("GenerateParsedQuery: %+v, want %+v", pq, want) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestGenerateQuery(t *testing.T) { |
| 45 |
tcases := []struct { |
| 46 |
desc string |
| 47 |
query string |
| 48 |
bindVars map[string]*querypb.BindVariable |
| 49 |
extras map[string]Encodable |
| 50 |
output string |
| 51 |
}{ |
| 52 |
{ |
| 53 |
desc: "no substitutions", |
| 54 |
query: "select * from a where id = 2", |
| 55 |
bindVars: map[string]*querypb.BindVariable{ |
| 56 |
"id": sqltypes.Int64BindVariable(1), |
| 57 |
}, |
| 58 |
output: "select * from a where id = 2", |
| 59 |
}, { |
| 60 |
desc: "missing bind var", |
| 61 |
query: "select * from a where id1 = :id1 and id2 = :id2", |
| 62 |
bindVars: map[string]*querypb.BindVariable{ |
| 63 |
"id1": sqltypes.Int64BindVariable(1), |
| 64 |
}, |
| 65 |
output: "missing bind var id2", |
| 66 |
}, { |
| 67 |
desc: "simple bindvar substitution", |
| 68 |
query: "select * from a where id1 = :id1 and id2 = :id2", |
| 69 |
bindVars: map[string]*querypb.BindVariable{ |
| 70 |
"id1": sqltypes.Int64BindVariable(1), |
| 71 |
"id2": sqltypes.NullBindVariable, |
| 72 |
}, |
| 73 |
output: "select * from a where id1 = 1 and id2 = null", |
| 74 |
}, { |
| 75 |
desc: "tuple *querypb.BindVariable", |
| 76 |
query: "select * from a where id in ::vals", |
| 77 |
bindVars: map[string]*querypb.BindVariable{ |
| 78 |
"vals": sqltypes.TestBindVariable([]interface{}{1, "aa"}), |
| 79 |
}, |
| 80 |
output: "select * from a where id in (1, 'aa')", |
| 81 |
}, { |
| 82 |
desc: "list bind vars 0 arguments", |
| 83 |
query: "select * from a where id in ::vals", |
| 84 |
bindVars: map[string]*querypb.BindVariable{ |
| 85 |
"vals": sqltypes.TestBindVariable([]interface{}{}), |
| 86 |
}, |
| 87 |
output: "empty list supplied for vals", |
| 88 |
}, { |
| 89 |
desc: "non-list bind var supplied", |
| 90 |
query: "select * from a where id in ::vals", |
| 91 |
bindVars: map[string]*querypb.BindVariable{ |
| 92 |
"vals": sqltypes.Int64BindVariable(1), |
| 93 |
}, |
| 94 |
output: "unexpected list arg type (INT64) for key vals", |
| 95 |
}, { |
| 96 |
desc: "list bind var for non-list", |
| 97 |
query: "select * from a where id = :vals", |
| 98 |
bindVars: map[string]*querypb.BindVariable{ |
| 99 |
"vals": sqltypes.TestBindVariable([]interface{}{1}), |
| 100 |
}, |
| 101 |
output: "unexpected arg type (TUPLE) for non-list key vals", |
| 102 |
}, { |
| 103 |
desc: "single column tuple equality", |
| 104 |
query: "select * from a where b = :equality", |
| 105 |
extras: map[string]Encodable{ |
| 106 |
"equality": &TupleEqualityList{ |
| 107 |
Columns: []ColIdent{NewColIdent("pk")}, |
| 108 |
Rows: [][]sqltypes.Value{ |
| 109 |
{sqltypes.NewInt64(1)}, |
| 110 |
{sqltypes.NewVarBinary("aa")}, |
| 111 |
}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
output: "select * from a where b = pk in (1, 'aa')", |
| 115 |
}, { |
| 116 |
desc: "multi column tuple equality", |
| 117 |
query: "select * from a where b = :equality", |
| 118 |
extras: map[string]Encodable{ |
| 119 |
"equality": &TupleEqualityList{ |
| 120 |
Columns: []ColIdent{NewColIdent("pk1"), NewColIdent("pk2")}, |
| 121 |
Rows: [][]sqltypes.Value{ |
| 122 |
{ |
| 123 |
sqltypes.NewInt64(1), |
| 124 |
sqltypes.NewVarBinary("aa"), |
| 125 |
}, |
| 126 |
{ |
| 127 |
sqltypes.NewInt64(2), |
| 128 |
sqltypes.NewVarBinary("bb"), |
| 129 |
}, |
| 130 |
}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
output: "select * from a where b = (pk1 = 1 and pk2 = 'aa') or (pk1 = 2 and pk2 = 'bb')", |
| 134 |
}, |
| 135 |
} |
| 136 |
|
| 137 |
for _, tcase := range tcases { |
| 138 |
tree, err := Parse(tcase.query) |
| 139 |
if err != nil { |
| 140 |
t.Errorf("parse failed for %s: %v", tcase.desc, err) |
| 141 |
continue |
| 142 |
} |
| 143 |
buf := NewTrackedBuffer(nil) |
| 144 |
buf.Myprintf("%v", tree) |
| 145 |
pq := buf.ParsedQuery() |
| 146 |
bytes, err := pq.GenerateQuery(tcase.bindVars, tcase.extras) |
| 147 |
var got string |
| 148 |
if err != nil { |
| 149 |
got = err.Error() |
| 150 |
} else { |
| 151 |
got = string(bytes) |
| 152 |
} |
| 153 |
if got != tcase.output { |
| 154 |
t.Errorf("for test case: %s, got: '%s', want '%s'", tcase.desc, got, tcase.output) |
| 155 |
} |
| 156 |
} |
| 157 |
} |