@@ -13,6 +13,7 @@ import mysql.protocol.comms;
13
13
import mysql.protocol.constants;
14
14
import mysql.protocol.packets;
15
15
import mysql.result;
16
+ import mysql.types;
16
17
debug (MYSQLN_TESTS )
17
18
import mysql.test.common;
18
19
@@ -133,20 +134,6 @@ unittest
133
134
}
134
135
}
135
136
136
- // temporary trait fix to allow setting/getting of @safe types from args.
137
- private template isSafeType (T)
138
- {
139
- U foo (U)(U t) {
140
- static if (is (typeof (t = t)))
141
- t = t;
142
- return t;
143
- }
144
-
145
- enum isSafeType = is (typeof (() @safe => foo(T.init)));
146
- }
147
- static assert (isSafeType! (int ));
148
- static assert (isSafeType! (typeof (null )));
149
-
150
137
/+ +
151
138
Encapsulation of a prepared statement.
152
139
@@ -168,7 +155,7 @@ private:
168
155
package :
169
156
ushort _numParams; // / Number of parameters this prepared statement takes
170
157
PreparedStmtHeaders _headers;
171
- Variant [] _inParams;
158
+ MySQLVal [] _inParams;
172
159
ParameterSpecialization[] _psa;
173
160
ColumnSpecialization[] _columnSpecials;
174
161
ulong _lastInsertID;
@@ -205,23 +192,15 @@ public:
205
192
_psa.length = numParams;
206
193
}
207
194
208
- private void _assignArgImpl (T)(size_t index, ref T val)
195
+ private void _assignArgImpl (T)(size_t index, auto ref T val)
209
196
{
210
- static if (isSafeType! T)
211
- {
212
- // override Variant's unsafe mechanism, we know everything is safe.
213
- (() @trusted { _inParams[index] = val; })();
214
- }
215
- else
216
- {
217
- _inParams[index] = val;
218
- }
197
+ _inParams[index] = val;
219
198
}
220
199
221
200
/+ +
222
201
Prepared statement parameter setter.
223
202
224
- The value may, but doesn't have to be, wrapped in a Variant . If so,
203
+ The value may, but doesn't have to be, wrapped in a MySQLVal . If so,
225
204
null is handled correctly.
226
205
227
206
The value may, but doesn't have to be, a pointer to the desired value.
@@ -240,7 +219,7 @@ public:
240
219
Params: index = The zero based index
241
220
+/
242
221
void setArg (T)(size_t index, T val, ParameterSpecialization psn = PSN (0 , SQLType.INFER_FROM_D_TYPE , 0 , null ))
243
- if (! isInstanceOf! (Nullable, T))
222
+ if (! isInstanceOf! (Nullable, T) && ! is (T == Variant ) )
244
223
{
245
224
// Now in theory we should be able to check the parameter type here, since the
246
225
// protocol is supposed to send us type information for the parameters, but this
@@ -266,6 +245,17 @@ public:
266
245
setArg(index, val.get (), psn);
267
246
}
268
247
248
+ deprecated (" Using Variant is deprecated, please use MySQLVal instead" )
249
+ void setArg (T)(size_t index, T val, ParameterSpecialization psn = PSN (0 , SQLType.INFER_FROM_D_TYPE , 0 , null ))
250
+ if (is (T == Variant ))
251
+ {
252
+ enforce! MYX (index < _numParams, " Parameter index out of range." );
253
+
254
+ _assignArgImpl(index, _toVal (val));
255
+ psn.pIndex = index;
256
+ _psa[index] = psn;
257
+ }
258
+
269
259
@(" setArg-typeMods" )
270
260
debug (MYSQLN_TESTS )
271
261
unittest
@@ -305,11 +295,13 @@ public:
305
295
// Note: Variant doesn't seem to support
306
296
// `shared(T)` or `shared(const(T)`. Only `shared(immutable(T))`.
307
297
298
+ // Further note, shared immutable(int) is really
299
+ // immutable(int). This test is a duplicate, so removed.
308
300
// Test shared immutable(int)
309
- {
301
+ /* {
310
302
shared immutable(int) i = 113;
311
303
assert(cn.exec(insertSQL, i) == 1);
312
- }
304
+ }*/
313
305
}
314
306
315
307
/+ +
@@ -324,7 +316,7 @@ public:
324
316
Type_Mappings: $(TYPE_MAPPINGS)
325
317
+/
326
318
void setArgs (T... )(T args)
327
- if (T.length == 0 || ! is (T[0 ] == Variant []))
319
+ if (T.length == 0 || ( ! is (T[0 ] == Variant []) && ! is (T[ 0 ] == MySQLVal[]) ))
328
320
{
329
321
enforce! MYX (args.length == _numParams, " Argument list supplied does not match the number of parameters." );
330
322
@@ -333,9 +325,9 @@ public:
333
325
}
334
326
335
327
/+ +
336
- Bind a Variant [] as the parameters of a prepared statement.
328
+ Bind a MySQLVal [] as the parameters of a prepared statement.
337
329
338
- You can use this method to bind a set of variables in Variant form to
330
+ You can use this method to bind a set of variables in MySQLVal form to
339
331
the parameters of a prepared statement.
340
332
341
333
Parameter specializations (ie, for chunked transfer) can be added if required.
@@ -359,13 +351,12 @@ public:
359
351
Type_Mappings: $(TYPE_MAPPINGS)
360
352
361
353
Params:
362
- args = External list of Variants to be used as parameters
354
+ args = External list of MySQLVal to be used as parameters
363
355
psnList = Any required specializations
364
356
+/
365
- void setArgs (Variant [] args, ParameterSpecialization[] psnList=null )
357
+ void setArgs (MySQLVal [] args, ParameterSpecialization[] psnList=null ) @safe
366
358
{
367
359
enforce! MYX (args.length == _numParams, " Param count supplied does not match prepared statement" );
368
- // can't be @safe because of the postblit for Variant
369
360
_inParams[] = args[];
370
361
if (psnList ! is null )
371
362
{
@@ -374,14 +365,41 @@ public:
374
365
}
375
366
}
376
367
368
+ // / ditto
369
+ deprecated (" Using Variant is deprecated, please use MySQLVal instead" )
370
+ void setArgs (Variant [] args, ParameterSpecialization[] psnList=null )
371
+ {
372
+ enforce! MYX (args.length == _numParams, " Param count supplied does not match prepared statement" );
373
+ foreach (i, ref arg; args)
374
+ setArg(i, arg);
375
+ if (psnList ! is null )
376
+ {
377
+ foreach (PSN psn; psnList)
378
+ _psa[psn.pIndex] = psn;
379
+ }
380
+ }
381
+
377
382
/+ +
378
383
Prepared statement parameter getter.
379
384
380
385
Type_Mappings: $(TYPE_MAPPINGS)
381
386
382
387
Params: index = The zero based index
388
+
389
+ Note: The Variant version of this function, getArg, is deprecated.
390
+ safeGetArg will eventually be renamed getArg when it is removed.
383
391
+/
392
+ deprecated (" Using Variant is deprecated, please use safeGetArg instead" )
384
393
Variant getArg (size_t index)
394
+ {
395
+ enforce! MYX (index < _numParams, " Parameter index out of range." );
396
+
397
+ // convert to Variant.
398
+ return _toVar (_inParams[index]);
399
+ }
400
+
401
+ // / ditto
402
+ MySQLVal safeGetArg (size_t index) @safe
385
403
{
386
404
enforce! MYX (index < _numParams, " Parameter index out of range." );
387
405
return _inParams[index];
@@ -458,7 +476,7 @@ public:
458
476
assert (rs[1 ].isNull(0 ));
459
477
assert (rs[1 ][0 ].type == typeid (typeof (null )));
460
478
461
- preparedInsert.setArg(0 , Variant (null ));
479
+ preparedInsert.setArg(0 , MySQLVal (null ));
462
480
cn.exec(preparedInsert);
463
481
rs = cn.query(selectSQL).array;
464
482
assert (rs.length == 3 );
0 commit comments