Skip to content

Commit 0c75f2c

Browse files
committedApr 22, 2022
Address review, add safe migration macro for ddoc.
1 parent 7f01a4d commit 0c75f2c

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed
 

‎SAFE_MIGRATION.md

+26-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22

33
Starting with version 3.1.0, mysql-native is transitioning to using a fully `@safe` API. This document describes how mysql-native is migrating, and how you can migrate your existing code to the new version.
44

5-
First, note that the latest version of mysql, while it supports a safe API, is defaulted to supporting the original unsafe API. We highly recommend reading and following the recommendations in this document so you can start using the safe version and be prepared for future changes that will deprecate and remove the unsafe API.
5+
First, note that the latest version of mysql-native, while it supports a safe API, defaults to using the unsafe mechanisms. This is so the new version can be selected *without modifying* any existing code or imports. Even though the default is unsafe, many tools are provided to allow you to use both safe and unsafe APIs in the same project. I have tried to make every effort to make this transition as seamless as possible. For details, read on.
6+
7+
## Table of contents
8+
9+
* [Why safe?](#why-safe)
10+
* [Roadmap](#roadmap)
11+
* [The Major Changes](#the-major-changes)
12+
* [The safe/unsafe API](#the-safeunsafe-api)
13+
* [Importing both safe and unsafe](#importing-both-safe-and-unsafe)
14+
* [Migrating from Variant to MySQLVal](#migrating-from-variant-to-mysqlval)
15+
* [Row and ResultRange](#row-and-resultrange)
16+
* [Prepared](#prepared)
17+
* [Connection](#connection)
18+
* [MySQLPool](#mysqlpool)
19+
* [The commands module](#the-commands-module)
20+
* [Recommended Transition Method](#recommended-transition-method)
21+
622

723
## Why Safe?
824

@@ -34,18 +50,18 @@ In some cases, fixing memory safety in mysql-native was as simple as adding a `@
3450

3551
But for the rest, to achieve full backwards compatibility, we have divided the API into two major sections -- safe and unsafe. The package `mysql.safe` will import all the safe versions of the API, the package `mysql.unsafe` will import the unsafe versions. If you import `mysql`, it will currently point at the unsafe version for backwards compatibility (see [Roadmap](#Roadmap) for details on how this will change).
3652

37-
The following modules have been split into mysql.safe.*modname* and mysql.unsafe.*modname*. Importing mysql.*modname* will currently import the unsafe version for backwards compatibility.
38-
* `mysql.commands`
39-
* `mysql.pool`
40-
* `mysql.result`
41-
* `mysql.prepared`
42-
* `mysql.connection`
53+
The following modules have been split into mysql.safe.*modname* and mysql.unsafe.*modname*. Importing mysql.*modname* will currently import the unsafe version for backwards compatibility. In a future major version, the default will be to import the safe api.
54+
* `mysql.[safe|unsafe].commands`
55+
* `mysql.[safe|unsafe].pool`
56+
* `mysql.[safe|unsafe].result`
57+
* `mysql.[safe|unsafe].prepared`
58+
* `mysql.[safe|unsafe].connection`
4359

44-
Each of these modules in unsafe mode provides the same API as the previous version of mysql. The safe version provides aliases to the original type names for the safe versions of types, and also provides the same functions as before that can be called via safe code. The one exception is in `mysql.safe.commands`, where some functions were for the deprecated `BackwardCompatPrepared`, which will eventually be removed.
60+
Each of these modules in unsafe mode provides the same API as the previous version of mysql. The safe version provides aliases to the original type names for the safe versions of types, and also provides the same functions as before that can be called via safe code. The one exception is in `mysql.safe.commands`, where some functions were for the deprecated `BackwardCompatPrepared`, which will be removed in the next major revision.
4561

4662
If you are currently importing any of the above modules directly, or importing the `mysql` package, a first step to migration is to use the `mysql.safe` package. From there you will find that almost everything works exactly the same.
4763

48-
In addition to these two new packages, we have introduced a package called `mysql.impl`. This package contains the common implementations of the `safe` and `unsafe` modules, and should NOT be directly imported ever. These modules are documented simply because that is where the code lives. But in the version of mysql that removes the `unsafe` API, this package will be removed. You should always use the `unsafe` or `safe` packages instead, which generally publicly import the `impl` modules anyway.
64+
In addition to these two new packages, we have introduced a package called `mysql.impl` (for internal use). This package contains the common implementations of the `safe` and `unsafe` modules, and should NEVER be directly imported. These modules are documented simply because that is where the code lives. But in a future version of mysql, this package will be removed. You should always use the `unsafe` or `safe` packages instead of trying to import the `mysql.impl` package.
4965

5066
#### Importing both safe and unsafe
5167

@@ -158,7 +174,7 @@ The `mysql.commands` module has been factored into 2 versions, a safe and unsafe
158174

159175
Even in cases where you elect to defer updating code, you can still import the `safe` API, and use `unsafe` conversion functions to keep existing code working. In most cases, this will not be necessary as the API is kept as similar as possible.
160176

161-
## Rcommended Transition Method
177+
## Recommended Transition Method
162178

163179
We recommend following these steps to transition. In most cases, you should see very little breakage of code:
164180

‎ddoc/macros.ddoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ Macros:
66
DOLLAR = $
77
COLON = :
88
EM = $(B $(I $0) )
9-
9+
1010
TYPE_MAPPINGS = See the $(LINK2 $(DDOX_ROOT_DIR)/mysql.html, MySQL/D Type Mappings tables)
11-
11+
SAFE_MIGRATION = See the $(LINK2 https://github.com/mysql-d/mysql-native/blob/master/SAFE_MIGRATION.md, Safe Migration) document for more details.
12+
1213
STD_DATETIME_DATE = $(D_INLINECODE $(LINK2 https://dlang.org/phobos/std_datetime_date.html#$0, $0))
1314
STD_EXCEPTION = $(D_INLINECODE $(LINK2 https://dlang.org/phobos/std_exception.html#$0, $0))
1415
STD_FILE = $(D_INLINECODE $(LINK2 https://dlang.org/phobos/std_file.html#$0, $0))

‎source/mysql/types.d

+9-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private union _MYTYPE
8484
}
8585

8686
/++
87-
MySQLVal is MySQL's tagged algebraic type that supports only @safe usage
87+
MySQLVal is mysql-native's tagged algebraic type that supports only @safe usage
8888
(see $(LINK2, http://code.dlang.org/packages/taggedalgebraic, TaggedAlgebraic)
8989
for more information on the features of this type). Note that TaggedAlgebraic
9090
has UFCS methods that are not available without importing that module in your
@@ -167,6 +167,12 @@ auto val2 = conn.queryValue("SELECT some_float FROM sometable WHERE id=5") + 100
167167
static assert(is(typeof(val2) == double));
168168
------
169169
170+
Note that per [TaggedAlgebraic's API](https://vibed.org/api/taggedalgebraic.taggedalgebraic/TaggedAlgebraic),
171+
using operators or members of a MySQLVal that aren't valid for the currently
172+
held type will throw an assertion error. If you wish to avoid this, and are not
173+
sure of the actual type, first validate the type is as you expect using the
174+
`kind` member.
175+
170176
MySQLVal is used in all operations interally for mysql-native, and explicitly
171177
for all safe API calls. Version 3.0.x and earlier of the mysql-native library
172178
used Variant, so this module provides multiple shims to allow code to "just
@@ -263,7 +269,7 @@ TaggedAlgebraic.
263269
264270
The `get` shim works differently than the TaggedAlgebraic version, as the
265271
Variant get function would provide implicit type conversions, but the
266-
TaggedAlgebraic version did not.
272+
TaggedAlgebraic version does not.
267273
268274
All shims other than `type` will likely remain as convenience features.
269275
@@ -287,7 +293,7 @@ T get(T)(auto ref MySQLVal val)
287293
else
288294
{
289295
import mysql.exceptions;
290-
throw new MYX("Cannot get type " ~ T.stringof ~ " with MySQLVal storing type " ~ V.stringof);
296+
throw new MYX("Cannot get type " ~ T.stringof ~ " from MySQLVal storing type " ~ V.stringof);
291297
}
292298
}
293299
return val.apply!convert();

0 commit comments

Comments
 (0)
Please sign in to comment.