Skip to content

Commit 1c5f93b

Browse files
committed
Create a safe and unsafe command module, separating out the two systems. Add a
MySQLSafeMode version to prepare for users that want to switch. This should be fully backwards compatible.
1 parent 22ac03a commit 1c5f93b

File tree

11 files changed

+1927
-1139
lines changed

11 files changed

+1927
-1139
lines changed

source/mysql/commands.d

Lines changed: 4 additions & 1082 deletions
Large diffs are not rendered by default.

source/mysql/connection.d

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import std.socket;
99
import std.string;
1010
import std.typecons;
1111

12-
import mysql.commands;
12+
import mysql.safe.commands;
1313
import mysql.exceptions;
1414
import mysql.prepared;
1515
import mysql.protocol.comms;
@@ -309,14 +309,14 @@ struct BackwardCompatPrepared
309309
deprecated("Change 'preparedStmt.query()' to 'conn.query(preparedStmt)'")
310310
ResultRange query()
311311
{
312-
return .query(_conn, _prepared);
312+
return .query(_conn, _prepared).unsafe;
313313
}
314314

315315
///ditto
316316
deprecated("Change 'preparedStmt.queryRow()' to 'conn.queryRow(preparedStmt)'")
317317
Nullable!Row queryRow()
318318
{
319-
return .queryRow(_conn, _prepared);
319+
return .queryRow(_conn, _prepared).unsafe;
320320
}
321321

322322
///ditto
@@ -328,9 +328,9 @@ struct BackwardCompatPrepared
328328

329329
///ditto
330330
deprecated("Change 'preparedStmt.queryValue()' to 'conn.queryValue(preparedStmt)'")
331-
Nullable!MySQLVal queryValue()
331+
Nullable!Variant queryValue() @system
332332
{
333-
return .queryValue(_conn, _prepared);
333+
return .queryValue(_conn, _prepared).asVariant;
334334
}
335335
}
336336

source/mysql/metadata.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import std.conv;
66
import std.datetime;
77
import std.exception;
88

9-
import mysql.commands;
9+
import mysql.safe.commands;
1010
import mysql.exceptions;
1111
import mysql.protocol.sockets;
1212
import mysql.result;

source/mysql/prepared.d

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import std.traits;
77
import std.typecons;
88
import std.variant;
99

10-
import mysql.commands;
10+
import mysql.safe.commands;
1111
import mysql.exceptions;
1212
import mysql.protocol.comms;
1313
import mysql.protocol.constants;
@@ -241,7 +241,6 @@ public:
241241
setArg(index, val.get(), psn);
242242
}
243243

244-
deprecated("Using Variant is deprecated, please use MySQLVal instead")
245244
void setArg(T)(size_t index, T val, ParameterSpecialization psn = PSN(0, SQLType.INFER_FROM_D_TYPE, 0, null)) @system
246245
if(is(T == Variant))
247246
{
@@ -362,7 +361,6 @@ public:
362361
}
363362

364363
/// ditto
365-
deprecated("Using Variant is deprecated, please use MySQLVal instead")
366364
void setArgs(Variant[] args, ParameterSpecialization[] psnList=null) @system
367365
{
368366
enforce!MYX(args.length == _numParams, "Param count supplied does not match prepared statement");
@@ -393,7 +391,6 @@ public:
393391
}
394392

395393
/// ditto
396-
deprecated("Using Variant is deprecated, please use getArg instead")
397394
Variant vGetArg(size_t index) @system
398395
{
399396
// convert to Variant.
@@ -438,7 +435,7 @@ public:
438435
immutable selectSQL = "SELECT * FROM `setNullArg`";
439436
auto preparedInsert = cn.prepare(insertSQL);
440437
assert(preparedInsert.sql == insertSQL);
441-
Row[] rs;
438+
SafeRow[] rs;
442439

443440
{
444441
Nullable!int nullableInt;

source/mysql/protocol/comms.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ package(mysql) ubyte[] makeToken(string password, ubyte[] authBuf)
809809
}
810810

811811
/// Get the next `mysql.result.Row` of a pending result set.
812-
package(mysql) Row getNextRow(Connection conn)
812+
package(mysql) SafeRow getNextRow(Connection conn)
813813
{
814814
scope(failure) conn.kill();
815815

@@ -819,7 +819,7 @@ package(mysql) Row getNextRow(Connection conn)
819819
conn._headersPending = false;
820820
}
821821
ubyte[] packet;
822-
Row rr;
822+
SafeRow rr;
823823
packet = conn.getPacket();
824824
if(packet.front == ResultPacketMarker.error)
825825
throw new MYXReceived(OKErrorPacket(packet), __FILE__, __LINE__);
@@ -830,9 +830,9 @@ package(mysql) Row getNextRow(Connection conn)
830830
return rr;
831831
}
832832
if (conn._binaryPending)
833-
rr = Row(conn, packet, conn._rsh, true);
833+
rr = SafeRow(conn, packet, conn._rsh, true);
834834
else
835-
rr = Row(conn, packet, conn._rsh, false);
835+
rr = SafeRow(conn, packet, conn._rsh, false);
836836
//rr._valid = true;
837837
return rr;
838838
}

source/mysql/result.d

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import std.conv;
55
import std.exception;
66
import std.range;
77
import std.string;
8-
import std.variant;
98

109
import mysql.connection;
1110
import mysql.exceptions;
1211
import mysql.protocol.comms;
1312
import mysql.protocol.extra_types;
1413
import mysql.protocol.packets;
1514
public import mysql.types;
15+
import std.typecons : Nullable;
16+
import std.variant;
1617

1718
/++
1819
A struct to represent a single row of a result set.
@@ -28,7 +29,7 @@ I have been agitating for some kind of null indicator that can be set for a
2829
Variant without destroying its inherent type information. If this were the
2930
case, then the bool array could disappear.
3031
+/
31-
struct Row
32+
struct SafeRow
3233
{
3334
import mysql.connection;
3435

@@ -88,7 +89,7 @@ public:
8889
unittest
8990
{
9091
import mysql.test.common;
91-
import mysql.commands;
92+
import mysql.safe.commands;
9293
mixin(scopedCn);
9394
cn.exec("DROP TABLE IF EXISTS `row_getName`");
9495
cn.exec("CREATE TABLE `row_getName` (someValue INTEGER, another INTEGER) ENGINE=InnoDB DEFAULT CHARSET=utf8");
@@ -177,21 +178,41 @@ public:
177178
}
178179

179180
/// ditto
180-
deprecated("Usage of Variant is deprecated. Please switch code to use safe MySQLVal types")
181-
UnsafeRow unsafe(Row r)
181+
struct UnsafeRow
182182
{
183-
return UnsafeRow(r);
183+
SafeRow _safe;
184+
alias _safe this;
185+
Variant opIndex(size_t idx) {
186+
return _safe[idx].asVariant;
187+
}
184188
}
185189

186190
/// ditto
187-
struct UnsafeRow
191+
UnsafeRow unsafe(SafeRow r) @safe
188192
{
189-
Row safe;
190-
alias safe this;
191-
deprecated("Variant support is deprecated. Please switch to using MySQLVal")
192-
Variant opIndex(size_t idx) {
193-
return safe[idx].asVariant;
194-
}
193+
return Row(r);
194+
}
195+
196+
/// ditto
197+
Nullable!UnsafeRow unsafe(Nullable!SafeRow r) @safe
198+
{
199+
if(r.isNull)
200+
return Nullable!UnsafeRow();
201+
return Nullable!UnsafeRow(r.get.unsafe);
202+
}
203+
204+
205+
SafeRow safe(UnsafeRow r) @safe
206+
{
207+
return r._safe;
208+
}
209+
210+
211+
Nullable!SafeRow safe(Nullable!UnsafeRow r) @safe
212+
{
213+
if(r.isNull)
214+
return Nullable!SafeRow();
215+
return Nullable!SafeRow(r.get.safe);
195216
}
196217

197218
/++
@@ -224,13 +245,13 @@ ResultRange oneAtATime = myConnection.query("SELECT * from myTable");
224245
Row[] allAtOnce = myConnection.query("SELECT * from myTable").array;
225246
---
226247
+/
227-
struct ResultRange
248+
struct SafeResultRange
228249
{
229250
private:
230251
@safe:
231252
Connection _con;
232253
ResultSetHeaders _rsh;
233-
Row _row; // current row
254+
SafeRow _row; // current row
234255
string[] _colNames;
235256
size_t[string] _colNameIndicies;
236257
ulong _numRowsFetched;
@@ -280,7 +301,7 @@ public:
280301
/++
281302
Gets the current row
282303
+/
283-
@property inout(Row) front() pure inout
304+
@property inout(SafeRow) front() pure inout
284305
{
285306
ensureValid();
286307
enforce!MYX(!empty, "Attempted 'front' on exhausted result sequence.");
@@ -345,21 +366,13 @@ public:
345366
@property ulong rowCount() const pure nothrow { return _numRowsFetched; }
346367
}
347368

348-
/// ditto
349-
deprecated("Usage of Variant is deprecated. Please switch code to use safe MySQLVal types")
350-
auto unsafe(ResultRange r)
351-
{
352-
return UnsafeResultRange(r);
353-
}
354-
355369
/// ditto
356370
struct UnsafeResultRange
357371
{
358-
ResultRange safe;
372+
SafeResultRange safe;
359373
alias safe this;
360-
inout(UnsafeRow) front() inout { return inout(UnsafeRow)(safe.front); }
374+
inout(Row) front() inout { return inout(Row)(safe.front); }
361375

362-
deprecated("Variant support is deprecated. Please switch to using MySQLVal")
363376
Variant[string] asAA()
364377
{
365378
ensureValid();
@@ -370,3 +383,20 @@ struct UnsafeResultRange
370383
return aa;
371384
}
372385
}
386+
387+
/// ditto
388+
UnsafeResultRange unsafe(SafeResultRange r) @safe
389+
{
390+
return UnsafeResultRange(r);
391+
}
392+
393+
version(MySQLSafeMode)
394+
{
395+
alias Row = SafeRow;
396+
alias ResultRange = SafeResultRange;
397+
}
398+
else
399+
{
400+
alias Row = UnsafeRow;
401+
alias ResultRange = UnsafeResultRange;
402+
}

0 commit comments

Comments
 (0)