mysql.connection

A native D driver for the MySQL database system. Source file mysql.d.

This module attempts to provide composite objects and methods that will allow a wide range of common database operations, but be relatively easy to use. The design is a first attempt to illustrate the structure of a set of modules to cover popular database systems and ODBC.

It has no dependecies on GPL header files or libraries, instead communicating directly with the server via the published client/server protocol.

http://dev.mysql.com/doc/internals/en/client-server-protocol.html

This version is not by any means comprehensive, and there is still a good deal of work to do. As a general design position it avoids providing wrappers for operations that can be accomplished by simple SQL sommands, unless the command produces a result set. There are some instances of the latter category to provide simple meta-data for the database,

Its primary objects are:

  • Connection:
    • Connection to the server, and querying and setting of server parameters.
  • Command: Handling of SQL requests/queries/commands, with principal methods:
    • execSQL() - plain old SQL query.
    • execTuple() - get a set of values from a select or similar query into a matching tuple of D variables.
    • execPrepared() - execute a prepared statement.
    • execResult() - execute a raw SQL statement and get a complete result set.
    • execSequence() - execute a raw SQL statement and handle the rows one at a time.
    • execPreparedResult() - execute a prepared statement and get a complete result set.
    • execPreparedSequence() - execute a prepared statement and handle the rows one at a time.
    • execFunction() - execute a stored function with D variables as input and output.
    • execProcedure() - execute a stored procedure with D variables as input.
  • ResultSet:
    • A random access range of rows, where a Row is basically an array of variant.
  • ResultSequence:
      $(LIAn input range of similar rows.)

There are numerous examples of usage in the unittest sections.

The file mysqld.sql, included with the module source code, can be used to generate the tables required by the unit tests.

This module supports both Phobos sockets and http://vibed.org/, Vibe.d sockets. Vibe.d support is disabled by default, to avoid unnecessary depencency on Vibe.d. To enable Vibe.d support, use: -version=Have_vibe_d

If you compile using https://github.com/rejectedsoftware/dub, DUB, and your project uses Vibe.d, then the -version flag above will be included automatically.

This requires DMD v2.061 or later, and a MySQL server v4.1.1 or later. Older versions of MySQL server are obsolete, use known-insecure authentication, and are not supported by this module.

There is an outstanding issue with Connections. Normally MySQL clients sonnect to a server on the same machine via a Unix socket on *nix systems, and through a named pipe on Windows. Neither of these conventions is currently supported. TCP must be used for all connections.

Members

Aliases

CSN
alias CSN = ColumnSpecialization
Undocumented in source.
MYX
alias MYX = MySQLException
Undocumented in source.
MYXProtocol
alias MYXProtocol = MySQLProtocolException
Undocumented in source.
MYXReceived
alias MYXReceived = MySQLReceivedException
Undocumented in source.
OpenSocketCallbackPhobos
alias OpenSocketCallbackPhobos = PlainPhobosSocket function(string, ushort)
Undocumented in source.
OpenSocketCallbackVibeD
alias OpenSocketCallbackVibeD = PlainVibeDSocket function(string, ushort)
Undocumented in source.
PSN
alias PSN = ParameterSpecialization
Undocumented in source.

Classes

Connection
class Connection

A struct representing a database connection.

MySQLException
class MySQLException

An exception type to distinguish exceptions thrown by this module.

MySQLProtocolException
class MySQLProtocolException

Received invalid data from the server which violates the MySQL network protocol.

MySQLReceivedException
class MySQLReceivedException

The server sent back a MySQL error code and message. If the server is 4.1+, there should also be an ANSI/ODBC-standard SQLSTATE error code.

Enums

CommandType
enum CommandType

Type of Command Packet (COM_XXX)

FieldFlags
enum FieldFlags

Field Flags

MySQLSocketType
enum MySQLSocketType
Undocumented in source.
RefreshFlags
enum RefreshFlags

Server refresh flags

ResultPacketMarker
enum ResultPacketMarker

Magic marker sent in the first byte of mysql results in response to auth or command packets

SQLType
enum SQLType

Column type codes

SvrCapFlags
enum SvrCapFlags

Server capability flags.

Functions

consume
T consume(MySQLSocket conn)
Undocumented in source. Be warned that the author may not have intended to support it.
consume
string consume(ubyte[] packet)
Undocumented in source. Be warned that the author may not have intended to support it.
consume
string consume(ubyte[] packet, size_t N)
Undocumented in source.
consume
ubyte[] consume(ubyte[] packet, size_t N)

Returns N number of bytes from the packet and advances the array

consume
T consume(ubyte[] packet, int n)
Undocumented in source. Be warned that the author may not have intended to support it.
consume
TimeOfDay consume(ubyte[] packet)
Undocumented in source.
consume
Date consume(ubyte[] packet)
Undocumented in source.
consume
DateTime consume(ubyte[] packet)
Undocumented in source.
consume
bool consume(ubyte[] packet)
Undocumented in source. Be warned that the author may not have intended to support it.
consume
T consume(ubyte[] packet)
Undocumented in source.
consume
T consume(ubyte[] packet)
Undocumented in source.
consume
string consume(ubyte[] packet)

Parse Length Coded String

consumeBinaryValueIfComplete
SQLValue consumeBinaryValueIfComplete(ubyte[] packet, bool unsigned)
Undocumented in source. Be warned that the author may not have intended to support it.
consumeIfComplete
SQLValue consumeIfComplete(ubyte[] packet, bool binary, bool unsigned)
Undocumented in source. Be warned that the author may not have intended to support it.
consumeIfComplete
SQLValue consumeIfComplete(ubyte[] packet, SQLType sqlType, bool binary, bool unsigned)
Undocumented in source. Be warned that the author may not have intended to support it.
consumeIfComplete
T consumeIfComplete(ubyte[] packet)

Decodes a Length Coded Binary from a packet

consumeNonBinaryValueIfComplete
SQLValue consumeNonBinaryValueIfComplete(ubyte[] packet, bool unsigned)
Undocumented in source. Be warned that the author may not have intended to support it.
decode
T decode(ubyte[] packet, size_t n)
Undocumented in source. Be warned that the author may not have intended to support it.
decode
T decode(ubyte[] packet)
Undocumented in source.
decode
T decode(ubyte[] packet)
Undocumented in source.
decode
LCB decode(ubyte[] packet)

Decodes a Length Coded Binary from a packet

decodeLCBHeader
LCB decodeLCBHeader(ubyte[] packet)
Undocumented in source.
getNumLCBBytes
byte getNumLCBBytes(ubyte lcbHeader)

Extract number of bytes used for this LCB

isEOFPacket
bool isEOFPacket(ubyte[] packet)
Undocumented in source.
myto
T myto(string value)
Undocumented in source. Be warned that the author may not have intended to support it.
pack
ubyte[] pack(TimeOfDay tod)

Function to pack a TimeOfDay into a binary encoding for transmission to the server.

pack
ubyte[] pack(Date dt)

Function to pack a Date into a binary encoding for transmission to the server.

pack
ubyte[] pack(DateTime dt)

Function to pack a DateTime into a binary encoding for transmission to the server.

packInto
void packInto(T value, ubyte[] array)

Converts a value into a sequence of bytes and fills the supplied array

packLCS
ubyte[] packLCS(void[] a)
Undocumented in source. Be warned that the author may not have intended to support it.
packLength
ubyte[] packLength(size_t l, size_t offset)
Undocumented in source.
parseLCB
ulong parseLCB(ubyte* ubp, bool nullFlag)
ulong parseLCB(ubyte* ubp)

Parse Length Coded Binary

setPacketHeader
void setPacketHeader(ubyte[] packet, ubyte packetNumber)

Set packet length and number. It's important that the length of packet has already been set to the final state as its length is used

setPacketHeader
void setPacketHeader(ubyte[] packet, ubyte packetNumber, uint dataLength)
Undocumented in source.
skip
T[] skip(T[] array, size_t n)

Skips over n items, advances the array, and return the newly advanced array to allow method chaining

toDate
Date toDate(ubyte[] a)

Function to extract a Date from a binary encoded row.

toDate
Date toDate(string s)

Function to extract a Date from a text encoded column value.

toDateTime
DateTime toDateTime(ubyte[] a)

Function to extract a DateTime from a binary encoded row.

toDateTime
DateTime toDateTime(string s)

Function to extract a DateTime from a text encoded column value.

toDateTime
DateTime toDateTime(ulong x)

Function to extract a DateTime from a ulong.

toTimeDiff
TimeDiff toTimeDiff(ubyte[] a)

Function to extract a time difference from a binary encoded row.

toTimeDiff
TimeDiff toTimeDiff(string s)

Function to extract a time difference from a text encoded column value.

toTimeOfDay
TimeOfDay toTimeOfDay(ubyte[] a)

Function to extract a TimeOfDay from a binary encoded row.

toTimeOfDay
TimeOfDay toTimeOfDay(string s)

Function to extract a TimeOfDay from a text encoded column value.

Properties

hasEnoughBytes
bool hasEnoughBytes [@property getter]
Undocumented in source.

Structs

ColumnInfo
struct ColumnInfo

A struct to hold column metadata

ColumnSpecialization
struct ColumnSpecialization

A struct to represent specializations of prepared statement parameters.

Command
struct Command

Encapsulation of an SQL command or query.

DBValue
struct DBValue

Composite representation of a column value

EOFPacket
struct EOFPacket

A struct representing an EOF packet from the server

FieldDescription
struct FieldDescription

A struct representing a field (column) description packet

LCB
struct LCB

Length Coded Binary Value

LCS
struct LCS

Length Coded String

MetaData
struct MetaData

Facilities to recover meta-data from a connection

MySQLProcedure
struct MySQLProcedure

A struct to hold stored function metadata

OKErrorPacket
struct OKErrorPacket

A struct representing an OK or Error packet

ParamDescription
struct ParamDescription

A struct representing a prepared statement parameter description packet

ParameterSpecialization
struct ParameterSpecialization

A struct to represent specializations of prepared statement parameters.

PreparedStmtHeaders
struct PreparedStmtHeaders

A struct representing the collation of a prepared statement parameter description sequence

ResultSequence
struct ResultSequence

An input range of Rows.

ResultSet
struct ResultSet

A Random access range of Rows.

ResultSetHeaders
struct ResultSetHeaders

A struct representing the collation of a sequence of FieldDescription packets.

Row
struct Row

A struct to represent a single row of a result set.

SQLValue
struct SQLValue
Undocumented in source.
TimeDiff
struct TimeDiff

A simple struct to represent time difference.

Timestamp
struct Timestamp

A D struct to stand for a TIMESTAMP

Variables

defaultClientFlags
SvrCapFlags defaultClientFlags;
Undocumented in source.

Meta

Authors

Steve Teale, James W. Oliphant, simendsjo, Sönke Ludwig, sshamov, Nick Sabalausky