| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package sqlparser |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"reflect" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/xwb1989/sqlparser/dependency/querypb" |
| 25 |
"github.com/xwb1989/sqlparser/dependency/sqltypes" |
| 26 |
) |
| 27 |
|
| 28 |
func TestNormalize(t *testing.T) { |
| 29 |
prefix := "bv" |
| 30 |
testcases := []struct { |
| 31 |
in string |
| 32 |
outstmt string |
| 33 |
outbv map[string]*querypb.BindVariable |
| 34 |
}{{ |
| 35 |
|
| 36 |
in: "select * from t where v1 = 'aa'", |
| 37 |
outstmt: "select * from t where v1 = :bv1", |
| 38 |
outbv: map[string]*querypb.BindVariable{ |
| 39 |
"bv1": sqltypes.BytesBindVariable([]byte("aa")), |
| 40 |
}, |
| 41 |
}, { |
| 42 |
|
| 43 |
in: "select * from t where v1 = 1", |
| 44 |
outstmt: "select * from t where v1 = :bv1", |
| 45 |
outbv: map[string]*querypb.BindVariable{ |
| 46 |
"bv1": sqltypes.Int64BindVariable(1), |
| 47 |
}, |
| 48 |
}, { |
| 49 |
|
| 50 |
in: "select * from t where v1 = 1.2", |
| 51 |
outstmt: "select * from t where v1 = :bv1", |
| 52 |
outbv: map[string]*querypb.BindVariable{ |
| 53 |
"bv1": sqltypes.Float64BindVariable(1.2), |
| 54 |
}, |
| 55 |
}, { |
| 56 |
|
| 57 |
in: "select * from t where v1 = 1.2 and v2 = 2", |
| 58 |
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2", |
| 59 |
outbv: map[string]*querypb.BindVariable{ |
| 60 |
"bv1": sqltypes.Float64BindVariable(1.2), |
| 61 |
"bv2": sqltypes.Int64BindVariable(2), |
| 62 |
}, |
| 63 |
}, { |
| 64 |
|
| 65 |
in: "select * from t where v1 = :bv1 and v2 = 1", |
| 66 |
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2", |
| 67 |
outbv: map[string]*querypb.BindVariable{ |
| 68 |
"bv2": sqltypes.Int64BindVariable(1), |
| 69 |
}, |
| 70 |
}, { |
| 71 |
|
| 72 |
in: "select * from t where v1 = 1 and v2 = 1", |
| 73 |
outstmt: "select * from t where v1 = :bv1 and v2 = :bv1", |
| 74 |
outbv: map[string]*querypb.BindVariable{ |
| 75 |
"bv1": sqltypes.Int64BindVariable(1), |
| 76 |
}, |
| 77 |
}, { |
| 78 |
|
| 79 |
in: "select * from t where v1 = 1 and v2 = '1'", |
| 80 |
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2", |
| 81 |
outbv: map[string]*querypb.BindVariable{ |
| 82 |
"bv1": sqltypes.Int64BindVariable(1), |
| 83 |
"bv2": sqltypes.BytesBindVariable([]byte("1")), |
| 84 |
}, |
| 85 |
}, { |
| 86 |
|
| 87 |
in: "insert into a values(1, 1)", |
| 88 |
outstmt: "insert into a values (:bv1, :bv2)", |
| 89 |
outbv: map[string]*querypb.BindVariable{ |
| 90 |
"bv1": sqltypes.Int64BindVariable(1), |
| 91 |
"bv2": sqltypes.Int64BindVariable(1), |
| 92 |
}, |
| 93 |
}, { |
| 94 |
|
| 95 |
in: "update a set v1=(select 5 from t), v2=5, v3=(select 5 from t), v4=5", |
| 96 |
outstmt: "update a set v1 = (select :bv1 from t), v2 = :bv2, v3 = (select :bv1 from t), v4 = :bv3", |
| 97 |
outbv: map[string]*querypb.BindVariable{ |
| 98 |
"bv1": sqltypes.Int64BindVariable(5), |
| 99 |
"bv2": sqltypes.Int64BindVariable(5), |
| 100 |
"bv3": sqltypes.Int64BindVariable(5), |
| 101 |
}, |
| 102 |
}, { |
| 103 |
|
| 104 |
in: "update a set v1=5 where v2 in (1, 4, 5)", |
| 105 |
outstmt: "update a set v1 = :bv1 where v2 in ::bv2", |
| 106 |
outbv: map[string]*querypb.BindVariable{ |
| 107 |
"bv1": sqltypes.Int64BindVariable(5), |
| 108 |
"bv2": sqltypes.TestBindVariable([]interface{}{1, 4, 5}), |
| 109 |
}, |
| 110 |
}, { |
| 111 |
|
| 112 |
in: "select * from t where v1 = 0x1234", |
| 113 |
outstmt: "select * from t where v1 = 0x1234", |
| 114 |
outbv: map[string]*querypb.BindVariable{}, |
| 115 |
}, { |
| 116 |
|
| 117 |
in: "update a set v1 = 0x1234", |
| 118 |
outstmt: "update a set v1 = 0x1234", |
| 119 |
outbv: map[string]*querypb.BindVariable{}, |
| 120 |
}, { |
| 121 |
|
| 122 |
in: fmt.Sprintf("select * from t where v1 = '%256s' and v2 = '%256s'", "a", "a"), |
| 123 |
outstmt: "select * from t where v1 = :bv1 and v2 = :bv1", |
| 124 |
outbv: map[string]*querypb.BindVariable{ |
| 125 |
"bv1": sqltypes.BytesBindVariable([]byte(fmt.Sprintf("%256s", "a"))), |
| 126 |
}, |
| 127 |
}, { |
| 128 |
|
| 129 |
in: fmt.Sprintf("select * from t where v1 = '%257s' and v2 = '%257s'", "b", "b"), |
| 130 |
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2", |
| 131 |
outbv: map[string]*querypb.BindVariable{ |
| 132 |
"bv1": sqltypes.BytesBindVariable([]byte(fmt.Sprintf("%257s", "b"))), |
| 133 |
"bv2": sqltypes.BytesBindVariable([]byte(fmt.Sprintf("%257s", "b"))), |
| 134 |
}, |
| 135 |
}, { |
| 136 |
|
| 137 |
in: "select * from t where v1 = 12345678901234567890", |
| 138 |
outstmt: "select * from t where v1 = 12345678901234567890", |
| 139 |
outbv: map[string]*querypb.BindVariable{}, |
| 140 |
}, { |
| 141 |
|
| 142 |
in: "select * from t where v1 = v2", |
| 143 |
outstmt: "select * from t where v1 = v2", |
| 144 |
outbv: map[string]*querypb.BindVariable{}, |
| 145 |
}, { |
| 146 |
|
| 147 |
in: "select * from t where v1 in ::list", |
| 148 |
outstmt: "select * from t where v1 in ::list", |
| 149 |
outbv: map[string]*querypb.BindVariable{}, |
| 150 |
}, { |
| 151 |
|
| 152 |
in: "select * from t where v1 in (1, a)", |
| 153 |
outstmt: "select * from t where v1 in (:bv1, a)", |
| 154 |
outbv: map[string]*querypb.BindVariable{ |
| 155 |
"bv1": sqltypes.Int64BindVariable(1), |
| 156 |
}, |
| 157 |
}, { |
| 158 |
|
| 159 |
in: "select * from t where v1 in (1, '2')", |
| 160 |
outstmt: "select * from t where v1 in ::bv1", |
| 161 |
outbv: map[string]*querypb.BindVariable{ |
| 162 |
"bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}), |
| 163 |
}, |
| 164 |
}, { |
| 165 |
|
| 166 |
in: "select * from t where v1 not in (1, '2')", |
| 167 |
outstmt: "select * from t where v1 not in ::bv1", |
| 168 |
outbv: map[string]*querypb.BindVariable{ |
| 169 |
"bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}), |
| 170 |
}, |
| 171 |
}} |
| 172 |
for _, tc := range testcases { |
| 173 |
stmt, err := Parse(tc.in) |
| 174 |
if err != nil { |
| 175 |
t.Error(err) |
| 176 |
continue |
| 177 |
} |
| 178 |
bv := make(map[string]*querypb.BindVariable) |
| 179 |
Normalize(stmt, bv, prefix) |
| 180 |
outstmt := String(stmt) |
| 181 |
if outstmt != tc.outstmt { |
| 182 |
t.Errorf("Query:\n%s:\n%s, want\n%s", tc.in, outstmt, tc.outstmt) |
| 183 |
} |
| 184 |
if !reflect.DeepEqual(tc.outbv, bv) { |
| 185 |
t.Errorf("Query:\n%s:\n%v, want\n%v", tc.in, bv, tc.outbv) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestGetBindVars(t *testing.T) { |
| 191 |
stmt, err := Parse("select * from t where :v1 = :v2 and :v2 = :v3 and :v4 in ::v5") |
| 192 |
if err != nil { |
| 193 |
t.Fatal(err) |
| 194 |
} |
| 195 |
got := GetBindvars(stmt) |
| 196 |
want := map[string]struct{}{ |
| 197 |
"v1": {}, |
| 198 |
"v2": {}, |
| 199 |
"v3": {}, |
| 200 |
"v4": {}, |
| 201 |
"v5": {}, |
| 202 |
} |
| 203 |
if !reflect.DeepEqual(got, want) { |
| 204 |
t.Errorf("GetBindVars: %v, want: %v", got, want) |
| 205 |
} |
| 206 |
} |