github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/bind/positional_args_test.go (about) 1 package bind 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/ydb-platform/ydb-go-sdk/v3/table" 10 "github.com/ydb-platform/ydb-go-sdk/v3/table/types" 11 ) 12 13 func TestPositionalArgsBindRewriteQuery(t *testing.T) { 14 var ( 15 now = time.Now() 16 b = PositionalArgs{} 17 ) 18 for _, tt := range []struct { 19 sql string 20 args []interface{} 21 yql string 22 params []interface{} 23 err error 24 }{ 25 { 26 sql: `SELECT ?, ?`, 27 args: []interface{}{ 28 100, 29 200, 30 }, 31 yql: `-- origin query with positional args replacement 32 SELECT $p0, $p1`, 33 params: []interface{}{ 34 table.ValueParam("$p0", types.Int32Value(100)), 35 table.ValueParam("$p1", types.Int32Value(200)), 36 }, 37 }, 38 { 39 sql: `SELECT ?, ?`, 40 args: []interface{}{ 41 100, 42 }, 43 err: ErrInconsistentArgs, 44 }, 45 { 46 sql: `SELECT ?, "?"`, 47 args: []interface{}{ 48 100, 49 }, 50 yql: `-- origin query with positional args replacement 51 SELECT $p0, "?"`, 52 params: []interface{}{ 53 table.ValueParam("$p0", types.Int32Value(100)), 54 }, 55 }, 56 { 57 sql: `SELECT ?, '?'`, 58 args: []interface{}{ 59 100, 60 }, 61 yql: `-- origin query with positional args replacement 62 SELECT $p0, '?'`, 63 params: []interface{}{ 64 table.ValueParam("$p0", types.Int32Value(100)), 65 }, 66 }, 67 { 68 sql: "SELECT ?, `?`", 69 args: []interface{}{ 70 100, 71 }, 72 yql: "-- origin query with positional args replacement\nSELECT $p0, `?`", 73 params: []interface{}{ 74 table.ValueParam("$p0", types.Int32Value(100)), 75 }, 76 }, 77 { 78 sql: "SELECT ?, $1, $p0", 79 err: ErrInconsistentArgs, 80 }, 81 { 82 sql: "SELECT ?, ?, ?", 83 args: []interface{}{ 84 1, 85 "test", 86 []string{ 87 "test1", 88 "test2", 89 "test3", 90 }, 91 }, 92 yql: `-- origin query with positional args replacement 93 SELECT $p0, $p1, $p2`, 94 params: []interface{}{ 95 table.ValueParam("$p0", types.Int32Value(1)), 96 table.ValueParam("$p1", types.TextValue("test")), 97 table.ValueParam("$p2", types.ListValue( 98 types.TextValue("test1"), 99 types.TextValue("test2"), 100 types.TextValue("test3"), 101 )), 102 }, 103 }, 104 { 105 sql: "SELECT ?, ?, ?", 106 args: []interface{}{ 107 types.Int32Value(1), 108 types.TextValue("test"), 109 types.ListValue( 110 types.TextValue("test1"), 111 types.TextValue("test2"), 112 types.TextValue("test3"), 113 ), 114 }, 115 yql: `-- origin query with positional args replacement 116 SELECT $p0, $p1, $p2`, 117 params: []interface{}{ 118 table.ValueParam("$p0", types.Int32Value(1)), 119 table.ValueParam("$p1", types.TextValue("test")), 120 table.ValueParam("$p2", types.ListValue( 121 types.TextValue("test1"), 122 types.TextValue("test2"), 123 types.TextValue("test3"), 124 )), 125 }, 126 }, 127 { 128 sql: "SELECT a, b, c WHERE id = ? AND date < ? AND value IN (?)", 129 args: []interface{}{ 130 1, now, []string{"3"}, 131 }, 132 yql: `-- origin query with positional args replacement 133 SELECT a, b, c WHERE id = $p0 AND date < $p1 AND value IN ($p2)`, 134 params: []interface{}{ 135 table.ValueParam("$p0", types.Int32Value(1)), 136 table.ValueParam("$p1", types.TimestampValueFromTime(now)), 137 table.ValueParam("$p2", types.ListValue(types.TextValue("3"))), 138 }, 139 }, 140 { 141 sql: "SELECT 1", 142 yql: "SELECT 1", 143 }, 144 { 145 sql: ` 146 SELECT 1`, 147 yql: ` 148 SELECT 1`, 149 }, 150 { 151 sql: "SELECT ?, ?", 152 args: []interface{}{ 153 1, 154 }, 155 err: ErrInconsistentArgs, 156 }, 157 { 158 sql: "SELECT ?, ? -- some comment with ?", 159 args: []interface{}{ 160 100, 161 200, 162 }, 163 yql: `-- origin query with positional args replacement 164 SELECT $p0, $p1 -- some comment with ?`, 165 params: []interface{}{ 166 table.ValueParam("$p0", types.Int32Value(100)), 167 table.ValueParam("$p1", types.Int32Value(200)), 168 }, 169 }, 170 { 171 sql: "SELECT ? /* some comment with ? */, ?", 172 args: []interface{}{ 173 100, 174 200, 175 }, 176 yql: `-- origin query with positional args replacement 177 SELECT $p0 /* some comment with ? */, $p1`, 178 params: []interface{}{ 179 table.ValueParam("$p0", types.Int32Value(100)), 180 table.ValueParam("$p1", types.Int32Value(200)), 181 }, 182 }, 183 { 184 sql: "SELECT ?, ? -- some comment with ?", 185 args: []interface{}{ 186 100, 187 }, 188 err: ErrInconsistentArgs, 189 }, 190 { 191 sql: "SELECT ?, ?, ?", 192 args: []interface{}{ 193 100, 194 200, 195 }, 196 err: ErrInconsistentArgs, 197 }, 198 { 199 sql: ` 200 SELECT ? /* some comment with ? */, ?`, 201 args: []interface{}{ 202 100, 203 200, 204 }, 205 yql: `-- origin query with positional args replacement 206 207 SELECT $p0 /* some comment with ? */, $p1`, 208 params: []interface{}{ 209 table.ValueParam("$p0", types.Int32Value(100)), 210 table.ValueParam("$p1", types.Int32Value(200)), 211 }, 212 }, 213 { 214 sql: "SELECT ?, ?", 215 args: []interface{}{ 216 100, 217 200, 218 }, 219 yql: `-- origin query with positional args replacement 220 SELECT $p0, $p1`, 221 params: []interface{}{ 222 table.ValueParam("$p0", types.Int32Value(100)), 223 table.ValueParam("$p1", types.Int32Value(200)), 224 }, 225 }, 226 { 227 sql: "SELECT ?, ?", 228 args: []interface{}{ 229 100, 230 200, 231 }, 232 yql: `-- origin query with positional args replacement 233 SELECT $p0, $p1`, 234 params: []interface{}{ 235 table.ValueParam("$p0", types.Int32Value(100)), 236 table.ValueParam("$p1", types.Int32Value(200)), 237 }, 238 }, 239 } { 240 t.Run("", func(t *testing.T) { 241 yql, params, err := b.RewriteQuery(tt.sql, tt.args...) 242 if tt.err != nil { 243 require.Error(t, err) 244 require.ErrorIs(t, err, tt.err) 245 } else { 246 require.NoError(t, err) 247 require.Equal(t, tt.yql, yql) 248 require.Equal(t, tt.params, params) 249 } 250 }) 251 } 252 }