Skip to content

Commit 28f2ea6

Browse files
committed
Building version of @safe mysql-native. Needs some more work, will
squash
1 parent 85de2ff commit 28f2ea6

File tree

10 files changed

+217
-106
lines changed

10 files changed

+217
-106
lines changed

dub.sdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ copyright "Copyright (c) 2011-2019 Steve Teale, James W. Oliphant, Simen Endsj
55
authors "Steve Teale" "James W. Oliphant" "Simen Endsjø" "Sönke Ludwig" "Sergey Shamov" "Nick Sabalausky"
66

77
dependency "vibe-core" version="~>1.7.0" optional=true
8-
dependency "taggedalgebraic" version="~>0.11.6"
8+
dependency "taggedalgebraic" version="~>0.11.7"
99

1010
sourcePaths "source/"
1111
importPaths "source/"

dub.selections.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"memutils": "0.4.13",
99
"openssl": "1.1.4+1.0.1g",
1010
"stdx-allocator": "2.77.5",
11-
"taggedalgebraic": "0.11.6",
12-
"unit-threaded": "0.7.45",
11+
"taggedalgebraic": "0.11.7",
12+
"unit-threaded": "0.7.55",
1313
"vibe-core": "1.7.0"
1414
}
1515
}

source/mysql/commands.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct ColumnSpecialization
4646
size_t cIndex; // parameter number 0 - number of params-1
4747
ushort type;
4848
uint chunkSize; /// In bytes
49-
void delegate(const(ubyte)[] chunk, bool finished) chunkDelegate;
49+
void delegate(const(ubyte)[] chunk, bool finished) @safe chunkDelegate;
5050
}
5151
///ditto
5252
alias CSN = ColumnSpecialization;
@@ -76,7 +76,7 @@ unittest
7676
immutable selectSQL = "SELECT `data` FROM `columnSpecial`";
7777
ubyte[] received;
7878
bool lastValueOfFinished;
79-
void receiver(const(ubyte)[] chunk, bool finished)
79+
void receiver(const(ubyte)[] chunk, bool finished) @safe
8080
{
8181
assert(lastValueOfFinished == false);
8282

@@ -381,7 +381,7 @@ package ResultRange queryImpl(ColumnSpecialization[] csa,
381381
conn._rsh.addSpecializations(csa);
382382

383383
conn._headersPending = false;
384-
return ResultRange(conn, conn._rsh, conn._rsh.fieldNames);
384+
return ResultRange(SafeResultRange(conn, conn._rsh, conn._rsh.fieldNames));
385385
}
386386

387387
/++

source/mysql/prepared.d

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import mysql.protocol.comms;
1313
import mysql.protocol.constants;
1414
import mysql.protocol.packets;
1515
import mysql.result;
16+
import mysql.types;
1617
debug(MYSQLN_TESTS)
1718
import mysql.test.common;
1819

@@ -133,20 +134,6 @@ unittest
133134
}
134135
}
135136

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-
150137
/++
151138
Encapsulation of a prepared statement.
152139
@@ -168,7 +155,7 @@ private:
168155
package:
169156
ushort _numParams; /// Number of parameters this prepared statement takes
170157
PreparedStmtHeaders _headers;
171-
Variant[] _inParams;
158+
MySQLVal[] _inParams;
172159
ParameterSpecialization[] _psa;
173160
ColumnSpecialization[] _columnSpecials;
174161
ulong _lastInsertID;
@@ -205,23 +192,15 @@ public:
205192
_psa.length = numParams;
206193
}
207194

208-
private void _assignArgImpl(T)(size_t index, ref T val)
195+
private void _assignArgImpl(T)(size_t index, auto ref T val)
209196
{
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;
219198
}
220199

221200
/++
222201
Prepared statement parameter setter.
223202
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,
225204
null is handled correctly.
226205
227206
The value may, but doesn't have to be, a pointer to the desired value.
@@ -240,7 +219,7 @@ public:
240219
Params: index = The zero based index
241220
+/
242221
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))
244223
{
245224
// Now in theory we should be able to check the parameter type here, since the
246225
// protocol is supposed to send us type information for the parameters, but this
@@ -266,6 +245,17 @@ public:
266245
setArg(index, val.get(), psn);
267246
}
268247

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+
269259
@("setArg-typeMods")
270260
debug(MYSQLN_TESTS)
271261
unittest
@@ -305,11 +295,13 @@ public:
305295
// Note: Variant doesn't seem to support
306296
// `shared(T)` or `shared(const(T)`. Only `shared(immutable(T))`.
307297

298+
// Further note, shared immutable(int) is really
299+
// immutable(int). This test is a duplicate, so removed.
308300
// Test shared immutable(int)
309-
{
301+
/*{
310302
shared immutable(int) i = 113;
311303
assert(cn.exec(insertSQL, i) == 1);
312-
}
304+
}*/
313305
}
314306

315307
/++
@@ -324,7 +316,7 @@ public:
324316
Type_Mappings: $(TYPE_MAPPINGS)
325317
+/
326318
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[])))
328320
{
329321
enforce!MYX(args.length == _numParams, "Argument list supplied does not match the number of parameters.");
330322

@@ -333,9 +325,9 @@ public:
333325
}
334326

335327
/++
336-
Bind a Variant[] as the parameters of a prepared statement.
328+
Bind a MySQLVal[] as the parameters of a prepared statement.
337329
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
339331
the parameters of a prepared statement.
340332
341333
Parameter specializations (ie, for chunked transfer) can be added if required.
@@ -359,13 +351,12 @@ public:
359351
Type_Mappings: $(TYPE_MAPPINGS)
360352
361353
Params:
362-
args = External list of Variants to be used as parameters
354+
args = External list of MySQLVal to be used as parameters
363355
psnList = Any required specializations
364356
+/
365-
void setArgs(Variant[] args, ParameterSpecialization[] psnList=null)
357+
void setArgs(MySQLVal[] args, ParameterSpecialization[] psnList=null) @safe
366358
{
367359
enforce!MYX(args.length == _numParams, "Param count supplied does not match prepared statement");
368-
// can't be @safe because of the postblit for Variant
369360
_inParams[] = args[];
370361
if (psnList !is null)
371362
{
@@ -374,14 +365,41 @@ public:
374365
}
375366
}
376367

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+
377382
/++
378383
Prepared statement parameter getter.
379384
380385
Type_Mappings: $(TYPE_MAPPINGS)
381386
382387
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.
383391
+/
392+
deprecated("Using Variant is deprecated, please use safeGetArg instead")
384393
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
385403
{
386404
enforce!MYX(index < _numParams, "Parameter index out of range.");
387405
return _inParams[index];
@@ -458,7 +476,7 @@ public:
458476
assert(rs[1].isNull(0));
459477
assert(rs[1][0].type == typeid(typeof(null)));
460478

461-
preparedInsert.setArg(0, Variant(null));
479+
preparedInsert.setArg(0, MySQLVal(null));
462480
cn.exec(preparedInsert);
463481
rs = cn.query(selectSQL).array;
464482
assert(rs.length == 3);

0 commit comments

Comments
 (0)