mysql-native: Seamlessly supports Phobos-only sockets

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu May 16 23:08:05 PDT 2013


For those who aren't aware: mysql-native (aka "mysqln") is a native-D
MySQL client library that has zero dependencies on the official MySQL
client lib or any other [L]GPL software. It was originally created by
Steve Teale, but various other people have since been maintaining it.
Originally it used Phobos's sockets, but then it was switched over to
Vibe.d sockets in order to support Vibe.d users.

Contrary to what I originally thought, it turned out to be possible to
add optional support for Phobos sockets back in without any breaking
changes at all. It's now up in the main repo's master:

https://github.com/rejectedsoftware/mysql-native

If you've already been using mysql-native with Vibe.d, then this should
work out-of-the-box with no changes at all.

If you want to use Phobos sockets, you can do it two ways:

1. Pass -version=MySQLN_NoVibeD to the compiler. This will completely
eliminate all dependencies on Vibe.d (and remove all Vibe.d
capabilities, naturally) and automatically use Phobos sockets.
Everything else will be exactly the same (But see the "caveat" below).

2. Add "MySQLSocketType.phobos" as the (optional) first argument to
Connection's constructor:

----------------
auto conn = Connection(MySQLSocketType.phobos, "localhost", "user",
"pass", etc...);
----------------

While it's probably best to stick to Vibe.d sockets in any app that
uses Vibe.d, if you have any reason to choose the socket type at
run-time, you can do so using this method.

Additionally, you're not limited to the stock sockets: You can also use
any custom socket type derived from either std.socket.TcpSocket or
vibe.core.net.TcpConnection (dependency injection, perhaps?) like this:

----------------
static auto openCustomSocket(string host, ushort port)
{
    auto s = new MyCustomTcpSocket();
    s.open(host, port);
    return s;
}
auto conn = Connection(&openCustomSocket, "localhost", "user", "pass",
etc...);
----------------

One caveat:

The EventedObject feature and the related MySQL connection pool class
("mysql.db.MysqlDB") are still Vibe.d-only, and will likely stay that
way since they're entirely Vibe.d-specific features.

The EventedObject methods (ie: acquire/release/isOwner/amOwner) still
exist in mysql-native's Connection class regardless of socket type (and
regardless of -version=MySQLN_NoVibeD) and can always be used. But for
Phobos-based sockets they're stubbed out as no-ops. This won't affect
most people at all, but it does mean that the MySQL connection pool
class ("mysql.db.MysqlDB") *always* uses Vibe.d sockets, and cannot
even be imported at all if you use -version=MySQLN_NoVibeD



More information about the Digitalmars-d-announce mailing list