.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "DBD::MariaDB 3"
.TH DBD::MariaDB 3 2023-10-24 "perl v5.38.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
DBD::MariaDB \- MariaDB and MySQL driver for the Perl5 Database Interface (DBI)
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 1
\& use DBI;
\&
\& my $dsn = "DBI:MariaDB:database=$database;host=$hostname;port=$port";
\& my $dbh = DBI\->connect($dsn, $user, $password);
\&
\& my $sth = $dbh\->prepare(
\& \*(AqSELECT id, first_name, last_name FROM authors WHERE last_name = ?\*(Aq
\& ) or die \*(Aqprepare statement failed: \*(Aq . $dbh\->errstr();
\& $sth\->execute(\*(AqEggers\*(Aq) or die \*(Aqexecution failed: \*(Aq . $dbh\->errstr();
\& print $sth\->rows() . " rows found.\en";
\& while (my $ref = $sth\->fetchrow_hashref()) {
\& print "Found a row: id = $ref\->{\*(Aqid\*(Aq}, fn = $ref\->{\*(Aqfirst_name\*(Aq}\en";
\& }
.Ve
.SH EXAMPLE
.IX Header "EXAMPLE"
.Vb 1
\& #!/usr/bin/perl
\&
\& use strict;
\& use warnings;
\& use DBI;
\&
\& # Connect to the database.
\& my $dbh = DBI\->connect(\*(AqDBI:MariaDB:database=test;host=localhost\*(Aq,
\& \*(Aqjoe\*(Aq, q(joe\*(Aqs password),
\& { RaiseError => 1, PrintError => 0 });
\&
\& # Drop table \*(Aqfoo\*(Aq. This may fail, if \*(Aqfoo\*(Aq doesn\*(Aqt exist
\& # Thus we put an eval around it.
\& eval {
\& $dbh\->do(\*(AqDROP TABLE foo\*(Aq);
\& } or do {
\& print \*(AqDropping foo failed: \*(Aq . $dbh\->errstr() . "\en";
\& };
\&
\& # Create a new table \*(Aqfoo\*(Aq. This must not fail, thus we don\*(Aqt
\& # catch errors.
\& $dbh\->do(\*(AqCREATE TABLE foo (id INTEGER, name VARCHAR(20))\*(Aq);
\&
\& # INSERT some data into \*(Aqfoo\*(Aq using placeholders
\& $dbh\->do(\*(AqINSERT INTO foo VALUES (?, ?)\*(Aq, undef, 2, \*(AqJochen\*(Aq);
\&
\& # now retrieve data from the table.
\& my $sth = $dbh\->prepare(\*(AqSELECT * FROM foo\*(Aq);
\& $sth\->execute();
\& while (my $ref = $sth\->fetchrow_hashref()) {
\& print "Found a row: id = $ref\->{\*(Aqid\*(Aq}, name = $ref\->{\*(Aqname\*(Aq}\en";
\& }
\&
\& # Disconnect from the database.
\& $dbh\->disconnect();
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
\&\fBDBD::MariaDB\fR is the Perl5 Database Interface driver for MariaDB and MySQL
databases. In other words: DBD::MariaDB is an interface between the Perl
programming language and the MariaDB/MySQL programming API that comes with the
MariaDB/MySQL relational database management system. Most functions provided by
this programming API are supported. Some rarely used functions are missing,
mainly because no-one ever requested them. :\-)
.PP
In what follows we first discuss the use of DBD::MariaDB, because this is what
you will need the most. For installation, see the separate document
DBD::MariaDB::INSTALL. See "EXAMPLE" for a simple example above.
.PP
From perl you activate the interface with the statement
.PP
.Vb 1
\& use DBI;
.Ve
.PP
After that you can connect to multiple MariaDB and MySQL database servers and
send multiple queries to any of them via a simple object oriented interface. Two
types of objects are available: database handles and statement handles. Perl
returns a database handle to the connect method like so:
.PP
.Vb 3
\& my $dbh = DBI\->connect("DBI:MariaDB:database=$db;host=$host",
\& $user, $password,
\& { RaiseError => 1, PrintError => 0 });
.Ve
.PP
Once you have connected to a database, you can execute SQL statements with:
.PP
.Vb 1
\& $dbh\->do(\*(AqINSERT INTO foo VALUES (?, ?)\*(Aq, undef, $number, $name);
.Ve
.PP
See DBI do method for details. See also the
bind_param method in DBI. See "DATABASE HANDLES"
below for more details on database handles.
.PP
If you want to retrieve results, you need to create a so-called statement handle
with:
.PP
.Vb 2
\& my $sth = $dbh\->prepare(\*(AqSELECT * FROM \*(Aq . $dbh\->quote_identifier($table));
\& $sth\->execute();
.Ve
.PP
This statement handle can be used for multiple things. First of all you can
retrieve a row of data:
.PP
.Vb 1
\& my $row = $sth\->fetchrow_hashref();
.Ve
.PP
If your table has columns \f(CW\*(C`ID\*(C'\fR and \f(CW\*(C`NAME\*(C'\fR, then \f(CW$row\fR will be hash ref with
keys \f(CW\*(C`ID\*(C'\fR and \f(CW\*(C`NAME\*(C'\fR. See "STATEMENT HANDLES" below for more details on
statement handles.
.PP
But now for a more formal approach:
.SS "Class Methods"
.IX Subsection "Class Methods"
.IP connect 4
.IX Item "connect"
.Vb 1
\& use DBI;
\&
\& my $dsn = "DBI:MariaDB:$database";
\& my $dsn = "DBI:MariaDB:database=$database;host=$hostname";
\& my $dsn = "DBI:MariaDB:database=$database;host=$hostname;port=$port";
\& my $dsn = "DBI:MariaDB:database=$database;mariadb_socket=$socket";
\&
\& my $dbh = DBI\->connect($dsn, $user, $password);
.Ve
.Sp
The \fIdatabase\fR is not a required attribute, but please note that MariaDB and
MySQL has no such thing as a default database. If you don't specify the database
at connection time your active database will be null and you'd need to prefix
your tables with the database name; i.e. \f(CW\*(C`SELECT * FROM mydb.mytable\*(C'\fR.
.Sp
This is similar to the behavior of the \f(CW\*(C`mariadb\*(C'\fR or \f(CW\*(C`mysql\*(C'\fR command line
client. Also, \f(CW\*(C`SELECT DATABASE()\*(C'\fR will return the current database active for
the handle.
.Sp
The DBD::MariaDB driver understands any connection attribute specified in
"DATABASE HANDLES" section which is read/write and additionally also any
other attribute described below.
.RS 4
.IP host 4
.IX Item "host"
.PD 0
.IP port 4
.IX Item "port"
.PD
The \fIhost\fR, if not specified or specified as empty string or \f(CW\*(C`localhost\*(C'\fR, will
default to a MariaDB or MySQL server running on the local machine using the
default for the UNIX socket. To connect to a MariaDB or MySQL server on the
local machine via TCP, you must specify the loopback IP address \f(CW127.0.0.1\fR as
the \fIhost\fR.
.Sp
Should the MariaDB or MySQL server be running on a non-standard port number, you
may explicitly state the \f(CW\*(C`port number\*(C'\fR to connect to in the \fIhost\fR argument,
by concatenating the \f(CW\*(C`hostname\*(C'\fR and \f(CW\*(C`port number\*(C'\fR together separated by a
colon (\f(CW\*(C`:\*(C'\fR) character or by using the \fIport\fR argument.
.Sp
To connect to a MariaDB or MySQL server on localhost using TCP/IP, you must
specify the \fIhost\fR as \f(CW127.0.0.1\fR with the optional \fIport\fR, e.g. \f(CW3306\fR.
.Sp
When connecting to a MariaDB or MySQL Server with IPv6, a bracketed IPv6 address
should be used. Example DSN:
.Sp
.Vb 1
\& my $dsn = \*(AqDBI:MariaDB:;host=[1a12:2800:6f2:85::f20:8cf];port=3306\*(Aq;
.Ve
.IP mariadb_client_found_rows 4
.IX Item "mariadb_client_found_rows"
If TRUE (Default), sets the \f(CW\*(C`CLIENT_FOUND_ROWS\*(C'\fR flag when connecting to the
MariaDB or MySQL server. This causes \f(CW\*(C`UPDATE\*(C'\fR statements to return the number
of rows \fBmatched\fR, not the number of rows actually changed.
.Sp
If you want the number of rows changed in response to an \f(CW\*(C`UPDATE\*(C'\fR statement,
specify \f(CW\*(C`mariadb_client_found_rows=0\*(C'\fR in the DSN.
.IP mariadb_compression 4
.IX Item "mariadb_compression"
If your DSN contains the option \f(CW\*(C`mariadb_compression=1\*(C'\fR, then the communication
between client and server will be compressed.
.IP mariadb_connect_timeout 4
.IX Item "mariadb_connect_timeout"
If your DSN contains the option \f(CW\*(C`mariadb_connect_timeout=##\*(C'\fR, the connect
request to the server will timeout if it has not been successful after the given
number of seconds. Zero value means infinite timeout.
.IP mariadb_write_timeout 4
.IX Item "mariadb_write_timeout"
If your DSN contains the option \f(CW\*(C`mariadb_write_timeout=##\*(C'\fR, the write operation
to the server will timeout if it has not been successful after the given number
of seconds. Zero value means infinite timeout.
.IP mariadb_read_timeout 4
.IX Item "mariadb_read_timeout"
If your DSN contains the option \f(CW\*(C`mariadb_read_timeout=##\*(C'\fR, the read operation
to the server will timeout if it has not been successful after the given number
of seconds. Zero value means infinite timeout.
.IP mariadb_init_command 4
.IX Item "mariadb_init_command"
If your DSN contains the option \f(CW\*(C`mariadb_init_command=SQL\*(C'\fR, then this \f(CW\*(C`SQL\*(C'\fR
statement is executed when connecting to the MariaDB or MySQL server. It is
automatically re-executed if reconnection occurs.
.IP mariadb_skip_secure_auth 4
.IX Item "mariadb_skip_secure_auth"
This option is for older MySQL databases that don't have secure auth set.
.IP mariadb_read_default_file 4
.IX Item "mariadb_read_default_file"
.PD 0
.IP mariadb_read_default_group 4
.IX Item "mariadb_read_default_group"
.PD
These options can be used to read a config file like \fI/etc/my.cnf\fR or
\&\fI~/.my.cnf\fR. By default MariaDB's and MySQL's C client library doesn't use any
config files unlike the client programs (mysql, mysqladmin, ...) that do, but
outside of the C client library. Thus you need to explicitly request reading a
config file, as in
.Sp
.Vb 2
\& my $dsn = \*(AqDBI:MariaDB:test;mariadb_read_default_file=/home/joe/my.cnf\*(Aq;
\& my $dbh = DBI\->connect($dsn, $user, $password);
.Ve
.Sp
The option \fImariadb_read_default_group\fR can be used to specify the default
group in the config file: Usually this is the \f(CW\*(C`client\*(C'\fR group, but see the
following example:
.Sp
.Vb 2
\& [client]
\& host=localhost
\&
\& [perl]
\& host=perlhost
.Ve
.Sp
(Note the order of the entries! The example won't work, if you reverse
the \f(CW\*(C`[client]\*(C'\fR and \f(CW\*(C`[perl]\*(C'\fR sections!)
.Sp
If you read this config file, then you'll be typically connected to
\&\f(CW\*(C`localhost\*(C'\fR. However, by using
.Sp
.Vb 3
\& my $dsn = \*(AqDBI:MariaDB:test;mariadb_read_default_group=perl;\*(Aq
\& . \*(Aqmariadb_read_default_file=/home/joe/my.cnf\*(Aq;
\& my $dbh = DBI\->connect($dsn, $user, $password);
.Ve
.Sp
you'll be connected to \f(CW\*(C`perlhost\*(C'\fR. Note that if you specify a default group and
do not specify a file, then the default config files will all be read. See the
documentation of the C function \f(CWmysql_options()\fR for details.
.IP mariadb_socket 4
.IX Item "mariadb_socket"
It is possible to choose the Unix socket that is used for connecting to the
server. This is done, for example, with
.Sp
.Vb 2
\& my $dsn = \*(AqDBI:MariaDB:database=test;\*(Aq
\& . \*(Aqmariadb_socket=/var/run/mysqld/mysqld.sock\*(Aq;
.Ve
.Sp
Usually there's no need for this option, unless you are using another location
for the socket than that built into the client.
.IP mariadb_auth_plugin 4
.IX Item "mariadb_auth_plugin"
Specify authentication method used during connection to MariaDB or MySQL server.
MySQL protocol uses \f(CW\*(C`mysql_native_password\*(C'\fR authentication method and MySQL
version 5.5.7 extended protocol and added support for using other authentication
methods which can be provided to clients by plugins. MySQL version 8.0.4 changed
the default authentication method to \f(CW\*(C`caching_sha2_password\*(C'\fR.
.Sp
Example how to use \f(CW\*(C`caching_sha2_password\*(C'\fR authentication method:
.Sp
.Vb 2
\& my $dsn = \*(AqDBI:MariaDB:database=test;host=hostname;\*(Aq
\& . \*(Aqmariadb_auth_plugin=caching_sha2_password\*(Aq;
.Ve
.IP mariadb_ssl 4
.IX Item "mariadb_ssl"
A true value enforces SSL encryption when connecting to the MariaDB or MySQL
server. A false value (which is default) disables SSL encryption with the MariaDB
or MySQL server.
.Sp
When enabling SSL encryption you should set also other SSL options, at least
\&\fImariadb_ssl_ca_file\fR or
\&\fImariadb_ssl_ca_path\fR.
.Sp
.Vb 3
\& my $dsn = \*(AqDBI:MariaDB:database=test;host=hostname;port=3306;\*(Aq
\& . \*(Aqmariadb_ssl=1;mariadb_ssl_verify_server_cert=1;\*(Aq
\& . \*(Aqmariadb_ssl_ca_file=/path/to/ca_cert.pem\*(Aq;
.Ve
.Sp
This means that your communication with the server will be encrypted.
.IP mariadb_ssl_ca_file 4
.IX Item "mariadb_ssl_ca_file"
The path to a file in PEM format that contains a list of trusted SSL certificate
authorities.
.Sp
When set MariaDB or MySQL server certificate is checked that it is signed by
some CA certificate in the list. \fICommon Name\fR value is not verified unless
\&\fImariadb_ssl_verify_server_cert\fR is enabled.
.IP mariadb_ssl_ca_path 4
.IX Item "mariadb_ssl_ca_path"
The path to a directory that contains trusted SSL certificate authority
certificates in PEM format.
.Sp
When set MariaDB or MySQL server certificate is checked that it is signed by
some CA certificate in the list. \fICommon Name\fR value is not verified unless
\&\fImariadb_ssl_verify_server_cert\fR is enabled.
.Sp
Please note that this option is supported only if your MariaDB or MySQL client
was compiled with OpenSSL library, and not with default yaSSL library.
.IP mariadb_ssl_verify_server_cert 4
.IX Item "mariadb_ssl_verify_server_cert"
Checks the server's \fICommon Name\fR value in the certificate that the server
sends to the client. The client verifies that name against the host name the
client uses for connecting to the server, and the connection fails if there is a
mismatch. For encrypted connections, this option helps prevent man-in-the-middle
attacks.
.Sp
Verification of the host name is disabled by default.
.IP mariadb_ssl_client_key 4
.IX Item "mariadb_ssl_client_key"
The name of the SSL key file in PEM format to use for establishing a secure
connection.
.IP mariadb_ssl_client_cert 4
.IX Item "mariadb_ssl_client_cert"
The name of the SSL certificate file in PEM format to use for establishing a
secure connection.
.IP mariadb_ssl_cipher 4
.IX Item "mariadb_ssl_cipher"
A list of permissible ciphers to use for connection encryption. If no cipher in
the list is supported, encrypted connections will not work.
.Sp
.Vb 2
\& mariadb_ssl_cipher=AES128\-SHA
\& mariadb_ssl_cipher=DHE\-RSA\-AES256\-SHA:AES128\-SHA
.Ve
.IP mariadb_ssl_optional 4
.IX Item "mariadb_ssl_optional"
Setting \fImariadb_ssl_optional\fR to true disables strict SSL enforcement and
makes SSL connection optional. This option opens security hole for
man-in-the-middle attacks. Default value is false which means that
\&\fImariadb_ssl\fR set to true enforces SSL encryption.
.Sp
Due to The BACKRONYM and
The Riddle vulnerabilities in libmariadb and
libmysqlclient libraries, enforcement of SSL encryption was not possible and
therefore \f(CW\*(C`mariadb_ssl_optional=1\*(C'\fR was effectively set for old DBD::mysql
driver prior DBD::MariaDB fork was created. DBD::MariaDB with \f(CW\*(C`mariadb_ssl=1\*(C'\fR
could refuse connection to MariaDB or MySQL server if underlying libmariadb or
libmysqlclient library is vulnerable. Option \fImariadb_ssl_optional\fR can be used
to make SSL connection vulnerable.
.IP mariadb_local_infile 4
.IX Item "mariadb_local_infile"
The \f(CW\*(C`LOCAL\*(C'\fR capability for \f(CW\*(C`LOAD DATA\*(C'\fR may be disabled in the MariaDB or MySQL
client library by default. If your DSN contains the option
\&\f(CW\*(C`mariadb_local_infile=1\*(C'\fR, \f(CW\*(C`LOAD DATA LOCAL\*(C'\fR will be enabled. However, this
option is \fBineffective\fR if the server has also been configured to disallow
\&\f(CW\*(C`LOCAL\*(C'\fR.
.IP mariadb_embedded_options 4
.IX Item "mariadb_embedded_options"
The option \fImariadb_embedded_options\fR can be used to pass command line options
to the embedded server. When you want to start and connect embedded server, use
\&\f(CW\*(C`host=embedded\*(C'\fR in dsn as connection parameter.
.Sp
Example:
.Sp
.Vb 6
\& use DBI;
\& my $datadir = \*(Aq/var/lib/mysql/\*(Aq;
\& my $langdir = \*(Aq/usr/share/mysql/english\*(Aq;
\& my $dsn = \*(AqDBI:MariaDB:host=embedded;database=test;\*(Aq
\& . "mariadb_embedded_options=\-\-datadir=$datadir,\-\-language=$langdir";
\& my $dbh = DBI\->connect($dsn, undef, undef);
.Ve
.Sp
This would start embedded server with language directory \f(CW$langdir\fR, database
directory \f(CW$datadir\fR and connects to database \f(CW\*(C`test\*(C'\fR. Embedded server does not
have to be supported by configured MariaDB or MySQL library. In that case
\&\f(CW\*(C`DBI\->connect()\*(C'\fR returns an error.
.IP mariadb_embedded_groups 4
.IX Item "mariadb_embedded_groups"
The option \fImariadb_embedded_groups\fR can be used to specify the groups in the
config file (\fImy.cnf\fR) which will be used to get options for the embedded
server. If not specified \f(CW\*(C`[server]\*(C'\fR and \f(CW\*(C`[embedded]\*(C'\fR groups will be used.
.Sp
Example:
.Sp
.Vb 2
\& my $dsn = \*(AqDBI:MariaDB:host=embedded;database=test;\*(Aq
\& . \*(Aqmariadb_embedded_groups=embedded_server,common\*(Aq;
.Ve
.IP mariadb_conn_attrs 4
.IX Item "mariadb_conn_attrs"
The option \fImariadb_conn_attrs\fR is a hash of attribute names and values which
can be used to send custom connection attributes to the server. Some attributes
like \f(CW\*(C`_os\*(C'\fR, \f(CW\*(C`_platform\*(C'\fR, \f(CW\*(C`_client_name\*(C'\fR and \f(CW\*(C`_client_version\*(C'\fR are added by
libmariadb or libmysqlclient.
.Sp
You can then later read these attributes from the performance schema tables
which can be quite helpful for profiling your database or creating statistics.
You'll have to use both server and client at least in version MariaDB 10.0.5 or
MySQL 5.6 to leverage this feature. It is a good idea to provides additional
\&\f(CW\*(C`program_name\*(C'\fR attribute.
.Sp
.Vb 8
\& my $dbh= DBI\->connect($dsn, $user, $password, {
\& AutoCommit => 0,
\& mariadb_conn_attrs => {
\& program_name => $0,
\& foo => \*(Aqbar\*(Aq,
\& wiz => \*(Aqbang\*(Aq
\& },
\& });
.Ve
.Sp
Now you can select the results from the performance schema tables. You can do
this in the same session, but also afterwards. It can be very useful to answer
questions like \fIwhich script sent this query?\fR
.Sp
.Vb 4
\& my $results = $dbh\->selectall_hashref(
\& \*(AqSELECT * FROM performance_schema.session_connect_attrs\*(Aq,
\& \*(AqATTR_NAME\*(Aq
\& );
.Ve
.Sp
This returns:
.Sp
.Vb 10
\& $result = {
\& \*(Aq_client_name\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aqlibmysql\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aq_client_name\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq1\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aq_client_version\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aq5.6.24\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aq_client_version\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq7\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aq_os\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aqosx10.8\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aq_os\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq0\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aq_pid\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aq59860\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aq_pid\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq2\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aq_platform\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aqx86_64\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aq_platform\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq4\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aqfoo\*(Aq => {
\& \*(AqATTR_NAME\*(Aq => \*(Aqfoo\*(Aq,
\& \*(AqATTR_VALUE\*(Aq => \*(Aqbar\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq6\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aqprogram_name\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aq./foo.pl\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aqprogram_name\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq5\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& \*(Aqwiz\*(Aq => {
\& \*(AqATTR_VALUE\*(Aq => \*(Aqbang\*(Aq,
\& \*(AqATTR_NAME\*(Aq => \*(Aqwiz\*(Aq,
\& \*(AqORDINAL_POSITION\*(Aq => \*(Aq3\*(Aq,
\& \*(AqPROCESSLIST_ID\*(Aq => \*(Aq3\*(Aq,
\& },
\& };
.Ve
.RE
.RS 4
.RE
.IP data_sources 4
.IX Item "data_sources"
.Vb 8
\& use DBI;
\& my @dsns = DBI\->data_sources(\*(AqMariaDB\*(Aq, {
\& host => $hostname,
\& port => $port,
\& user => $username,
\& password => $password,
\& ...
\& });
.Ve
.Sp
Returns a list of all databases in dsn format suitable for connect
method, managed by the MariaDB or MySQL server. It accepts all attributes from
connect method.
.SH "DATABASE HANDLES"
.IX Header "DATABASE HANDLES"
The DBD::MariaDB driver supports the following attributes of database handles
(read only):
.PP
.Vb 10
\& my $errno = $dbh\->{\*(Aqmariadb_errno\*(Aq};
\& my $error = $dbh\->{\*(Aqmariadb_error\*(Aq};
\& my $hostinfo = $dbh\->{\*(Aqmariadb_hostinfo\*(Aq};
\& my $info = $dbh\->{\*(Aqmariadb_info\*(Aq};
\& my $insertid = $dbh\->{\*(Aqmariadb_insertid\*(Aq};
\& my $protoinfo = $dbh\->{\*(Aqmariadb_protoinfo\*(Aq};
\& my $serverinfo = $dbh\->{\*(Aqmariadb_serverinfo\*(Aq};
\& my $ssl_cipher = $dbh\->{\*(Aqmariadb_ssl_cipher\*(Aq};
\& my $stat = $dbh\->{\*(Aqmariadb_stat\*(Aq};
\& my $thread_id = $dbh\->{\*(Aqmariadb_thread_id\*(Aq};
.Ve
.PP
These correspond to \f(CWmysql_errno()\fR, \f(CWmysql_error()\fR,
\&\f(CWmysql_get_host_info()\fR, \f(CWmysql_info()\fR, \f(CWmysql_insert_id()\fR,
\&\f(CWmysql_get_proto_info()\fR, \f(CWmysql_get_server_info()\fR, \f(CWmysql_stat()\fR,
\&\f(CWmysql_get_ssl_cipher()\fR and \f(CWmysql_thread_id()\fR respectively.
.PP
Portable DBI applications should not use them. Instead they should use standard
DBI methods: \f(CW\*(C`$dbh\->err()\*(C'\fR and
\&\f(CW\*(C`$dbh\->errstr()\*(C'\fR for error number and string,
\&\f(CW\*(C`$dbh\->get_info($GetInfoType{SQL_SERVER_NAME})\*(C'\fR for server host name,
\&\f(CW\*(C`$dbh\->get_info($GetInfoType{SQL_DBMS_NAME})\*(C'\fR and
\&\f(CW\*(C`$dbh\->get_info($GetInfoType{SQL_DBMS_VER})\*(C'\fR for server database name and
version, \f(CW\*(C`$dbh\->last_insert_id()\*(C'\fR or
\&\f(CW\*(C`$sth\->last_insert_id()\*(C'\fR for insert id.
.IP mariadb_clientinfo 2
.IX Item "mariadb_clientinfo"
.PD 0
.IP mariadb_clientversion 2
.IX Item "mariadb_clientversion"
.PD
List information of the MariaDB or MySQL client library that DBD::MariaDB was
built against:
.Sp
.Vb 1
\& print "$dbh\->{mariadb_clientinfo}\en";
\&
\& 5.2.0\-MariaDB
\&
\& print "$dbh\->{mariadb_clientversion}\en";
\&
\& 50200
.Ve
.Sp
Portable DBI applications should not be interested in version of underlying
client library. DBD::MariaDB is there to hide any possible incompatibility and
works correctly with any available version.
.IP mariadb_serverversion 2
.IX Item "mariadb_serverversion"
.Vb 1
\& print "$dbh\->{mariadb_serverversion}\en";
\&
\& 50200
.Ve
.Sp
Portable DBI applications should use
\&\f(CW\*(C`$dbh\->get_info($GetInfoType{SQL_DBMS_NAME})\*(C'\fR and
\&\f(CW\*(C`$dbh\->get_info($GetInfoType{SQL_DBMS_VER})\*(C'\fR for server database name and
version instead.
.IP mariadb_ssl_cipher 2
.IX Item "mariadb_ssl_cipher"
Returns the SSL encryption cipher used for the given connection to the server.
In case SSL encryption was not enabled with \fImariadb_ssl\fR or
was not established returns \f(CW\*(C`undef\*(C'\fR.
.Sp
.Vb 6
\& my $ssl_cipher = $dbh\->{mariadb_ssl_cipher};
\& if (defined $ssl_cipher) {
\& print "Connection with server is encrypted with cipher: $ssl_cipher\en";
\& } else {
\& print "Connection with server is not encrypted\en";
\& }
.Ve
.IP mariadb_dbd_stats 2
.IX Item "mariadb_dbd_stats"
.Vb 1
\& my $info_hashref = $dbh\->{mariadb_dbd_stats};
.Ve
.Sp
DBD::MariaDB keeps track of some statistics in the \fImariadb_dbd_stats\fR
attribute. The following stats are being maintained:
.RS 2
.IP auto_reconnects_ok 8
.IX Item "auto_reconnects_ok"
The number of times that DBD::MariaDB successfully reconnected to the MariaDB or
MySQL server.
.IP auto_reconnects_failed 8
.IX Item "auto_reconnects_failed"
The number of times that DBD::MariaDB tried to reconnect to MariaDB or MySQL but
failed.
.RE
.RS 2
.RE
.PP
The DBD::MariaDB driver also supports the following attributes of database
handles (read/write):
.IP mariadb_auto_reconnect 4
.IX Item "mariadb_auto_reconnect"
This attribute determines whether DBD::MariaDB will automatically reconnect to
MariaDB or MySQL server if the connection be lost. This feature defaults to off.
Setting \fImariadb_auto_reconnect\fR to \f(CW1\fR is not advised if \f(CW\*(C`LOCK TABLES\*(C'\fR is
used because if DBD::MariaDB reconnect to MariaDB or MySQL server all table
locks will be lost. This attribute is ignored when AutoCommit
is turned off, and when AutoCommit is turned off, DBD::MariaDB
will not automatically reconnect to the server.
.Sp
It is also possible to set the default value of the \fImariadb_auto_reconnect\fR
attribute for the \f(CW$dbh\fR by passing it in the \f(CW\*(C`\e%attr\*(C'\fR hash for
\&\f(CW\*(C`DBI\->connect\*(C'\fR.
.Sp
.Vb 1
\& $dbh\->{mariadb_auto_reconnect} = 1;
.Ve
.Sp
or
.Sp
.Vb 3
\& my $dbh = DBI\->connect($dsn, $user, $password, {
\& mariadb_auto_reconnect => 1,
\& });
.Ve
.Sp
Note that if you are using a module or framework that performs reconnections for
you (for example DBIx::Connector in fixup mode), this value
must be set to \f(CW0\fR.
.IP mariadb_use_result 4
.IX Item "mariadb_use_result"
This attribute forces the driver to use \f(CWmysql_use_result()\fR rather than
\&\f(CWmysql_store_result()\fR library function. The former is faster and less memory
consuming, but tends to block other processes. \f(CWmysql_store_result()\fR is the
default due to that fact storing the result is expected behavior with most
applications.
.Sp
It is possible to set the default value of the \fImariadb_use_result\fR attribute
for the \f(CW$dbh\fR via the DSN:
.Sp
.Vb 1
\& my $dbh = DBI\->connect(\*(AqDBI:MariaDB:test;mariadb_use_result=1\*(Aq, $user, $pass);
.Ve
.Sp
You can also set it after creation of the database handle:
.Sp
.Vb 2
\& $dbh\->{mariadb_use_result} = 0; # disable
\& $dbh\->{mariadb_use_result} = 1; # enable
.Ve
.Sp
You can also set or unset the \fImariadb_use_result\fR setting on your statement
handle, when creating the statement handle or after it has been created. See
"STATEMENT HANDLES".
.Sp
Library functions \f(CWmysql_use_result()\fR and \f(CWmysql_store_result()\fR are not
used for server side prepared statements, therefore this \fImariadb_use_result\fR
attribute has no effect when server side prepared statements are enabled.
.Sp
Note that library function \f(CWmysql_use_result()\fR does not provide number of rows
in result set. Therefore if this \fImariadb_use_result\fR attribute is enabled then
DBI variable \f(CW$DBI::rows\fR and statement methods
execute and rows returns \f(CW\-1\fR (which means that the
number of rows is not known). However, statement method rows returns
correct number of rows after all rows were already fetched and additional fetch
call was issued (for this special case driver knows that it processed all rows
and hence it knows what is the total number of rows).
.Sp
Enabling \fImariadb_use_result\fR attribute on one statement handle disallow usage
of all other statement and database handles until all data rows from active
statement handle are fetched. Trying to use other statement or database handles
when there is active \fImariadb_use_result\fR attribute on some statement handle
cause unpredictable errors.
.Sp
In most cases there is no benefit in usage of this \fImariadb_use_result\fR
attribute, it should stay disabled.
.IP mariadb_bind_type_guessing 4
.IX Item "mariadb_bind_type_guessing"
This attribute causes the driver (emulated prepare statements) to attempt to
guess if a value being bound is a numeric value, and if so, doesn't quote the
value. This was created by Dragonchild and is one way to deal with the
performance issue of using quotes in a statement that is inserting or updating a
large numeric value.
.Sp
CAVEAT: Even though you can insert an integer value into a character column, if
this column is indexed, if you query that column with the integer value not
being quoted, it will not use the index:
.Sp
.Vb 10
\& MariaDB [test]> explain select * from test where value0 = \*(Aq3\*(Aq \eG
\& *************************** 1. row ***************************
\& id: 1
\& select_type: SIMPLE
\& table: test
\& type: ref
\& possible_keys: value0
\& key: value0
\& key_len: 13
\& ref: const
\& rows: 1
\& Extra: Using index condition
\& 1 row in set (0.00 sec)
\&
\& MariaDB [test]> explain select * from test where value0 = 3
\& \-> \eG
\& *************************** 1. row ***************************
\& id: 1
\& select_type: SIMPLE
\& table: test
\& type: ALL
\& possible_keys: value0
\& key: NULL
\& key_len: NULL
\& ref: NULL
\& rows: 6
\& Extra: Using where
\& 1 row in set (0.00 sec)
.Ve
.Sp
See bug:
.Sp
\&\fImariadb_bind_type_guessing\fR can be turned on via
.Sp
\&\- through DSN
.Sp
.Vb 3
\& my $dbh = DBI\->connect(\*(AqDBI:MariaDB:test\*(Aq, \*(Aqusername\*(Aq, \*(Aqpass\*(Aq, {
\& mariadb_bind_type_guessing => 1
\& });
.Ve
.Sp
\&\- OR after handle creation
.Sp
.Vb 1
\& $dbh\->{mariadb_bind_type_guessing} = 1;
.Ve
.IP mariadb_bind_comment_placeholders 4
.IX Item "mariadb_bind_comment_placeholders"
This attribute causes the driver (emulated prepare statements) will cause any
placeholders in comments to be bound. This is not correct prepared statement
behavior, but some developers have come to depend on this behavior.
.IP mariadb_no_autocommit_cmd 4
.IX Item "mariadb_no_autocommit_cmd"
This attribute causes the driver to not issue \f(CW\*(C`SET AUTOCOMMIT\*(C'\fR either through
explicit or using \f(CWmysql_autocommit()\fR. This is particularly useful in the case
of using MySQL Proxy.
.Sp
See the bug report:
.Sp
\&\fImariadb_no_autocommit_cmd\fR can be turned on when creating the database handle:
.Sp
.Vb 3
\& my $dbh = DBI\->connect(\*(AqDBI:MariaDB:test\*(Aq, \*(Aqusername\*(Aq, \*(Aqpass\*(Aq, {
\& mariadb_no_autocommit_cmd => 1
\& });
.Ve
.Sp
or using an existing database handle:
.Sp
.Vb 1
\& $dbh\->{mariadb_no_autocommit_cmd} = 1;
.Ve
.IP mariadb_max_allowed_packet 4
.IX Item "mariadb_max_allowed_packet"
This attribute controls the maximum size of one packet, any generated or
intermediate string and any bind parameter. Default value depends on client
MariaDB/MySQL library and should be 1GB.
.Sp
.Vb 1
\& $dbh\->{mariadb_max_allowed_packet} = 32*1024*1024; # limit max size to 32MB
.Ve
.IP mariadb_multi_statements 4
.IX Item "mariadb_multi_statements"
Support for multiple statements separated by a semicolon (\f(CW\*(C`;\*(C'\fR) may be enabled
by using this option. Enabling this option may cause problems if server-side
prepared statements are also enabled.
.IP mariadb_server_prepare 4
.IX Item "mariadb_server_prepare"
This option is used to enable server side prepared statements. By default
prepared statements are not used and placeholder replacement is done by
DBD::MariaDB prior to sending SQL statement to MariaDB or MySQL server.
.Sp
This default behavior may change in the future.
.Sp
To use server side prepared statements, all you need to do is set the variable
\&\fImariadb_server_prepare\fR in the connect:
.Sp
.Vb 6
\& my $dbh = DBI\->connect(
\& \*(AqDBI:MariaDB:database=test;host=localhost;mariadb_server_prepare=1\*(Aq,
\& \*(Aquser\*(Aq,
\& \*(Aqpassword\*(Aq,
\& { RaiseError => 1, PrintError => 0 },
\& );
.Ve
.Sp
or:
.Sp
.Vb 6
\& my $dbh = DBI\->connect(
\& \*(AqDBI:MariaDB:database=test;host=localhost\*(Aq,
\& \*(Aquser\*(Aq,
\& \*(Aqpassword\*(Aq,
\& { RaiseError => 1, PrintError => 0, mariadb_server_prepare => 1 },
\& );
.Ve
.Sp
There are many benefits to using server side prepare statements, mostly if you
are using SQL statements with placeholders or performing many inserts because of
that fact that a single statement is prepared to accept multiple insert values.
.Sp
Please note that MariaDB or MySQL server cannot prepare or execute some prepared
statements. In this case DBD::MariaDB fallbacks to normal non-prepared statement
and tries again.
.IP mariadb_server_prepare_disable_fallback 4
.IX Item "mariadb_server_prepare_disable_fallback"
This option disable fallback to normal non-prepared statement when MariaDB or
MySQL server does not support execution of current statement as prepared.
.Sp
Useful when you want to be sure that the statement is going to be executed as
server side prepared. Error message and code in case of failure is propagated
back to DBI.
.Sp
This default behavior may change in the future.
.PP
Documentation for some DBD::MariaDB methods of database handles:
.IP ping 2
.IX Item "ping"
This can be used to send a ping to the server. See DBI ping.
.Sp
.Vb 1
\& my $rc = $dbh\->ping();
.Ve
.IP get_info 2
.IX Item "get_info"
This method can be used to retrieve information about MariaDB or MySQL server.
See DBI get_info. Some useful information: \f(CW\*(C`SQL_DBMS_NAME\*(C'\fR
returns server database name, either \f(CW\*(C`MariaDB\*(C'\fR or \f(CW\*(C`MySQL\*(C'\fR. \f(CW\*(C`SQL_DBMS_VER\*(C'\fR
returns server database version and \f(CW\*(C`SQL_SERVER_NAME\*(C'\fR returns server host name.
.Sp
.Vb 1
\& use DBI::Const::GetInfoType;
\&
\& print $dbh\->get_info($GetInfoType{SQL_DBMS_NAME});
\&
\& MariaDB
\&
\& print $dbh\->get_info($GetInfoType{SQL_DBMS_VER});
\&
\& 10.01.2600
\&
\& print $dbh\->get_info($GetInfoType{SQL_SERVER_NAME});
\&
\& Localhost via UNIX socket
.Ve
.SH "STATEMENT HANDLES"
.IX Header "STATEMENT HANDLES"
The statement handles of DBD::MariaDB support a number of attributes. You access
these by using, for example,
.PP
.Vb 1
\& my $numFields = $sth\->{NUM_OF_FIELDS};
.Ve
.PP
Note, that most attributes are valid only after a successful
execute. An \f(CW\*(C`undef\*(C'\fR value will returned otherwise. The most
important exception is the \fImariadb_use_result\fR
attribute.
.PP
To set the \fImariadb_use_result\fR attribute on statement
handle \f(CW$sth\fR, use either of the following:
.PP
.Vb 1
\& my $sth = $dbh\->prepare($sql, { mariadb_use_result => 1});
.Ve
.PP
or
.PP
.Vb 2
\& my $sth = $dbh\->prepare($sql);
\& $sth\->{mariadb_use_result} = 1;
.Ve
.PP
Column dependent attributes, for example \fINAME\fR, the column names, are returned
as a reference to an array. The array indices are corresponding to the indices
of the arrays returned by fetchrow and similar methods. For
example the following code will print a header of table names together with all
rows:
.PP
.Vb 2
\& my $sth = $dbh\->prepare(\*(AqSELECT * FROM t\*(Aq)
\& or die \*(AqError: \*(Aq . $dbh\->errstr() . "\en";
\&
\& $sth\->execute()
\& or die \*(AqError: \*(Aq . $sth\->errstr() . "\en";
\&
\& my $names = $sth\->{NAME};
\& my $numFields = $sth\->{\*(AqNUM_OF_FIELDS\*(Aq} \- 1;
\& for my $i ( 0..$numFields ) {
\& printf(\*(Aq%s%s\*(Aq, $i ? \*(Aq,\*(Aq : \*(Aq\*(Aq, $$names[$i]);
\& }
\& print "\en";
\& while (my $ref = $sth\->fetchrow_arrayref()) {
\& for my $i ( 0..$numFields ) {
\& printf(\*(Aq%s%s\*(Aq, $i ? \*(Aq,\*(Aq : \*(Aq\*(Aq, $$ref[$i]);
\& }
\& print "\en";
\& }
.Ve
.PP
For portable applications you should restrict yourself to attributes with
capitalized or mixed case names. Lower case attribute names are private to
DBD::MariaDB. The attribute list includes:
.IP ChopBlanks 4
.IX Item "ChopBlanks"
This attribute determines whether a fetchrow will chop preceding
and trailing blanks off the column values. Chopping blanks does not have impact
on the \fImariadb_max_length\fR attribute.
.IP mariadb_insertid 4
.IX Item "mariadb_insertid"
If the statement you executed performs an \f(CW\*(C`INSERT\*(C'\fR, and there is an
\&\f(CW\*(C`AUTO_INCREMENT\*(C'\fR column in the table you inserted in, this attribute holds the
value stored into the \f(CW\*(C`AUTO_INCREMENT\*(C'\fR column, if that value is automatically
generated, by storing \f(CW\*(C`NULL\*(C'\fR or \f(CW0\fR or was specified as an explicit value.
.Sp
Typically, you'd access the value via \f(CW\*(C`$sth\->{mariadb_insertid}\*(C'\fR. The value
can also be accessed via \f(CW\*(C`$dbh\->{mariadb_insertid}\*(C'\fR but this can easily
produce incorrect results in case one database handle is shared.
.Sp
Portable DBI applications should not use \fImariadb_insertid\fR. Instead they
should use DBI method \f(CW\*(C`$dbh\->last_insert_id()\*(C'\fR
or statement DBI method \f(CW\*(C`$sth\->last_insert_id()\*(C'\fR. Statement method was
introduced in DBI version 1.642, but DBD::MariaDB implements it also for older
DBI versions.
.IP mariadb_is_blob 4
.IX Item "mariadb_is_blob"
Reference to an array of boolean values; Logical true value indicates, that the
respective column is a blob.
.IP mariadb_is_key 4
.IX Item "mariadb_is_key"
Reference to an array of boolean values; Logical true value indicates, that the
respective column is a key.
.IP mariadb_is_num 4
.IX Item "mariadb_is_num"
Reference to an array of boolean values; Logical true value indicates, that the
respective column contains numeric values.
.IP mariadb_is_pri_key 4
.IX Item "mariadb_is_pri_key"
Reference to an array of boolean values; Logical true value indicates, that the
respective column is a primary key.
.IP mariadb_is_auto_increment 4
.IX Item "mariadb_is_auto_increment"
Reference to an array of boolean values; Logical true value indicates that the
respective column is an \f(CW\*(C`AUTO_INCREMENT\*(C'\fR column.
.IP mariadb_length 4
.IX Item "mariadb_length"
.PD 0
.IP mariadb_max_length 4
.IX Item "mariadb_max_length"
.PD
A reference to an array of maximum column sizes. The \fImariadb_max_length\fR is
the maximum physically present in the result table, \fImariadb_length\fR gives the
theoretically possible maximum.
.Sp
For string orientated variable types (char, varchar, text and similar types)
both attributes return value in bytes. If you are interested in number of
characters then instead of \fImariadb_length\fR use \f(CW\*(C`COLUMN_SIZE\*(C'\fR via standard DBI
method column_info and instead of \fImariadb_max_length\fR issue
SQL query \f(CW\*(C`SELECT MAX(CHAR_LENGTH(...))\*(C'\fR. Example:
.Sp
.Vb 3
\& my $ci_sth = $dbh\->column_info(undef, undef, $table, $column);
\& my $ci_ref = $ci_sth\->fetchall_arrayref({});
\& my $mariadb_char_length = $ci_ref\->[0]\->{COLUMN_SIZE};
\&
\& my $mariadb_char_max_length = $dbh\->selectrow_array(sprintf(
\& \*(AqSELECT MAX(CHAR_LENGTH(%s)) FROM %s\*(Aq,
\& $dbh\->quote_identifier($column),
\& $dbh\->quote_identifier($table),
\& ));
.Ve
.IP NAME 4
.IX Item "NAME"
A reference to an array of column names.
.IP NULLABLE 4
.IX Item "NULLABLE"
A reference to an array of boolean values; Logical true value indicates that
this column may contain \f(CW\*(C`NULL\*(C'\fR's.
.IP NUM_OF_FIELDS 4
.IX Item "NUM_OF_FIELDS"
Number of fields returned by a \f(CW\*(C`SELECT\*(C'\fR statement. You may use this for
checking whether a statement returned a result: A zero value indicates a
non\-\f(CW\*(C`SELECT\*(C'\fR statement like \f(CW\*(C`INSERT\*(C'\fR, \f(CW\*(C`DELETE\*(C'\fR or \f(CW\*(C`UPDATE\*(C'\fR.
.IP mariadb_table 4
.IX Item "mariadb_table"
A reference to an array of table names, useful in a \f(CW\*(C`JOIN\*(C'\fR result.
.IP TYPE 4
.IX Item "TYPE"
A reference to an array of column types. The engine's native column types are
mapped to portable types like \f(CWDBI::SQL_INTEGER()\fR or \f(CWDBI::SQL_VARCHAR()\fR, as
good as possible. Not all native types have a meaningful equivalent. If you
need the native column types, use \fImariadb_type\fR. See below.
.IP mariadb_type 4
.IX Item "mariadb_type"
A reference to an array of MySQL's native column types, for example
\&\f(CWDBD::MariaDB::TYPE_SHORT()\fR or \f(CWDBD::MariaDB::TYPE_STRING()\fR. Use the
\&\fITYPE\fR attribute, if you want portable types like
\&\f(CWDBI::SQL_SMALLINT()\fR or \f(CWDBI::SQL_VARCHAR()\fR.
.IP mariadb_type_name 4
.IX Item "mariadb_type_name"
Similar to \fImariadb_type\fR, but type names and not numbers are
returned. Whenever possible, the ANSI SQL name is preferred.
.IP mariadb_warning_count 4
.IX Item "mariadb_warning_count"
The number of warnings generated during execution of the SQL statement. This
attribute is available on both statement handles and database handles.
.SH "UNICODE SUPPORT"
.IX Header "UNICODE SUPPORT"
All string orientated variable types (char, varchar, text and similar types) are
represented by the DBD::MariaDB as Unicode strings according to the standard
Perl Unicode model. It means that Perl scalars contain Unicode code points and
not UTF\-8 bytes. Internally the DBD::MariaDB uses the MySQL's \f(CW\*(C`utf8mb4\*(C'\fR charset
for the network communication with MariaDB and MySQL servers. It automatically
transforms the network MySQL's \f(CW\*(C`utf8mb4\*(C'\fR charset to the Unicode Perl scalars
and vice-versa.
.PP
MySQL's \f(CW\*(C`utf8mb4\*(C'\fR charset for the network communication is configured by
\&\f(CW\*(C`MYSQL_SET_CHARSET_NAME\*(C'\fR libmariadb/libmysqlclient C library API which is a
requirement to have working quote method and an emulated client
side placeholders replacement.
.PP
Do not try to change network charset (e.g. via SQL command \f(CW\*(C`SET NAMES\*(C'\fR
manually) to anything different then UTF\-8 as it would confuse underlying C
library and DBD::MariaDB would misbehave (e.g. would lead to broken/insecure
quote method or an emulated client side placeholders replacement).
.PP
Using a non\-UTF\-8 charset for a column, table or database is fine because
MariaDB or MySQL server automatically transforms the storage charset to the
charset used by the network protocol (\f(CW\*(C`utf8mb4\*(C'\fR). Note that when DBD::MariaDB
is connecting to the MariaDB or MySQL server it calls SQL command
\&\f(CW\*(C`SET character_set_server = \*(Aqutf8mb4\*(Aq\*(C'\fR to ensure that the default charset for
new databases would be UTF\-8. Beware that a default charset for new tables is
set from a database charset.
.PP
In the case MySQL server does not support MySQL's \f(CW\*(C`utf8mb4\*(C'\fR charset for a
network protocol then DBD::MariaDB would try to use MySQL's \f(CW\*(C`utf8\*(C'\fR charset
which is a subset of UTF\-8 encoding restricted to the 3 byte UTF\-8 sequences.
Support for MySQL's \f(CW\*(C`utf8mb4\*(C'\fR charset was introduced in MySQL server version
5.5.3.
.SS "Working with binary data"
.IX Subsection "Working with binary data"
Perl scalars do not distinguish between binary \fIbyte\fR orientated buffers and
\&\fIUnicode\fR orientated strings. In Perl it is always up to the caller and the
callee to define in its API if functions and methods expect \fIbyte\fR buffers or
\&\fIUnicode\fR strings. It is not possible (or rather Perl application should not
try) to distinguish if Perl scalar contains a \fIbyte\fR buffer or \fIUnicode\fR
string.
.PP
When fetching data from MariaDB and MySQL servers, DBD::MariaDB treats all
fields marked with MySQL's charset \f(CW\*(C`utf8mb4\*(C'\fR (and also \f(CW\*(C`utf8\*(C'\fR) as \fIUnicode\fR
strings. Everything else is treated as binary \fIbyte\fR oriented buffers.
Therefore, the only difference is that UTF\-8 fields are automatically decoded to
Unicode. Binary blob fields remain untouched and corresponding Perl scalars
would contain just ordinals \f(CW0..255\fR (classic sequence of bytes). Unicode
string scalars would contain sequence of Unicode code points.
.PP
There is a small problem with input data, more preciously with SQL statements
and their bind parameters. By definition a SQL statement is a string and
therefore it is expected and handled by DBD::MariaDB as a \fIUnicode\fR string (not
\&\fIbyte\fR oriented buffer). There is no way to treat a SQL statement as a binary,
but this is not a problem. All SQL commands are encoded in ASCII and all ASCII
characters are invariants in UTF\-8 (have the same representation as a sequence
of Unicode code points and also when UTF\-8 encoded in a byte buffer). For the
remaining part of a SQL statement, placeholders with bind parameters can and
should be used.
.SS "Binary parameters"
.IX Subsection "Binary parameters"
Unfortunately, neither MariaDB nor MySQL server provide any type information for
prepared SQL statements; therefore, DBD::MariaDB has absolutely no way to know
if a particular bind parameter for a placeholder should be treated as \fIUnicode\fR
string or as \fIbyte\fR oriented buffer. So Perl applications which use
DBD::MariaDB must provide information about the correct type.
.PP
Moreover, DBI API for do, execute and all
select* methods binds all parameters as \f(CW\*(C`SQL_VARCHAR\*(C'\fR
type. Currently it is an API limitation which does not allow one to specify the
bind type. Varchar is a string and so DBD::MariaDB treats all of them as
\&\fIUnicode\fR strings.
.PP
The only way how to specify a type in DBI is via the
bind_param method. Its third argument takes \f(CW\*(C`SQL_*\*(C'\fR constant
which defines a type for the passed bind parameter.
.PP
Following type constants are treated as binary by DBD::MariaDB: \f(CW\*(C`SQL_BIT\*(C'\fR,
\&\f(CW\*(C`SQL_BLOB\*(C'\fR, \f(CW\*(C`SQL_BINARY\*(C'\fR, \f(CW\*(C`SQL_VARBINARY\*(C'\fR, \f(CW\*(C`SQL_LONGVARBINARY\*(C'\fR.
.PP
This approach of handling binary data was implemented in DBD::MariaDB because it
does not violate how Perl's Unicode model is working, follows exactly DBI API
documentation, and, more importantly, is how other DBI drivers (including
DBD::Pg and DBD::SQLite) in their recent versions
work. This ensures good compatibility for Perl applications which use multiple
database backends and several DBI drivers.
.PP
Please note that the old DBD::mysql driver in version 4.041 works
differently and has completely broken Unicode support.
.PP
To illustrate the usage, see the following example:
.PP
.Vb 4
\& # Prepare statement
\& my $sth = $dbh\->prepare(
\& \*(AqINSERT INTO users (id, name, picture) VALUES (?, ?, ?)\*(Aq
\& );
\&
\& # Bind number, 7\-bit ASCII values are always in Unicode and binary context
\& $sth\->bind_param(1, 10);
\&
\& # Bind name, may contains Unicode character, in this case U+00E9
\& $sth\->bind_param(2, "Andr\ex{E9}");
\&
\& # Bind picture, it is a sequence of binary bytes, not Unicode code points
\& $sth\->bind_param(3, "\ex{D8}\ex{A0}\ex{39}\ex{F8}", DBI::SQL_BINARY);
\&
\& # Execute statement with bind parameters
\& $sth\->execute();
.Ve
.PP
Explanation: In this case number \f(CW10\fR and name \f(CW"Andr\ex{E9}"\fR would be
automatically encoded from Perl Unicode string scalars to MySQL's \f(CW\*(C`utf8mb4\*(C'\fR
network charset and \fIpicture\fR would not be touched as it was bound with the
\&\f(CW\*(C`DBI::SQL_BINARY\*(C'\fR type. Note that 7\-bit ASCII values are invariants in UTF\-8,
they have the same representations in UTF\-8, so both the encoding and decoding
operations are just identity functions.
.PP
This is the preferred and safe way how to work with binary data. It is also
supported by other DBI drivers, including DBD::Pg and
DBD::SQLite (see above).
.PP
In DBD::MariaDB, there's another specific way how to create a SQL statement with
binary data: to call the quote method while specifying a binary
type. This method takes a bind parameter and properly quotes + escapes it. For
binary types it converts argument to MySQL's HEX syntax (\f(CW\*(C`X\*(Aq...\*(Aq\*(C'\fR) which is a
pure 7\-bit ASCII and therefore invariant for UTF\-8. See the following example:
.PP
.Vb 9
\& my $param1 = 10;
\& my $param2 = "Andr\ex{E9}";
\& my $param3 = "\ex{D8}\ex{A0}\ex{39}\ex{F8}";
\& my $query = \*(AqINSERT INTO users (id, name, picture) VALUES (\*(Aq .
\& $dbh\->quote($param1) . \*(Aq ,\*(Aq .
\& $dbh\->quote($param2) . \*(Aq ,\*(Aq .
\& $dbh\->quote($param3, DBI::SQL_BINARY) .
\& \*(Aq)\*(Aq;
\& $dbh\->do($query);
.Ve
.PP
The first two parameters are quoted and escaped for a later UTF\-8 encoding (to
MySQL's \f(CW\*(C`utf8mb4\*(C'\fR charset) and the third parameter is quoted and escaped as a
binary buffer to MySQL's HEX syntax for binary blobs.
.PP
This method is not recommended, because quoting, escaping and similar methods
can easily get written incorrectly and lead to SQL injections and other security
problems.
.SH "TRANSACTION SUPPORT"
.IX Header "TRANSACTION SUPPORT"
The transaction support works as follows:
.IP \(bu 4
By default AutoCommit mode is on, following the DBI
specifications.
.IP \(bu 4
If you execute
.Sp
.Vb 1
\& $dbh\->{AutoCommit} = 0;
.Ve
.Sp
or
.Sp
.Vb 1
\& $dbh\->{AutoCommit} = 1;
.Ve
.Sp
then the driver will set the MariaDB or MySQL server variable autocommit to \f(CW0\fR
or \f(CW1\fR, respectively. Switching from \f(CW0\fR to \f(CW1\fR will also issue a \f(CW\*(C`COMMIT\*(C'\fR,
following the DBI specifications.
.IP \(bu 4
The methods
.Sp
.Vb 2
\& $dbh\->rollback();
\& $dbh\->commit();
.Ve
.Sp
will issue the commands \f(CW\*(C`ROLLBACK\*(C'\fR and \f(CW\*(C`COMMIT\*(C'\fR, respectively. A \f(CW\*(C`ROLLBACK\*(C'\fR
will also be issued if AutoCommit mode is off and the database
handles DESTROY method is called. Again, this is following the DBI
specifications.
.PP
Given the above, you should note the following:
.IP \(bu 4
You should never change the server variable AutoCommit
manually, unless you are ignoring DBI's transaction support.
.IP \(bu 4
Switching AutoCommit mode from on to off or vice versa may
fail. You should always check for errors when changing
AutoCommit mode. The suggested way of doing so is using the
DBI flag RaiseError. If you don't like
RaiseError, you have to use code like the following:
.Sp
.Vb 4
\& $dbh\->{AutoCommit} = 0;
\& if ($dbh\->{AutoCommit}) {
\& # An error occurred!
\& }
.Ve
.IP \(bu 4
If you detect an error while changing the AutoCommit mode, you
should no longer use the database handle. In other words, you should disconnect
and reconnect again, because the transaction mode is unpredictable.
Alternatively you may verify the transaction mode by checking the value of the
server variable autocommit. However, such behaviour isn't portable.
.IP \(bu 4
DBD::MariaDB has a \fIreconnect\fR feature that handles the so-called MySQL
\&\fImorning bug\fR: If the server has disconnected, most probably due to a timeout,
then by default the driver will reconnect and attempt to execute the same SQL
statement again. However, this behaviour is disabled when AutoCommit is off:
Otherwise the transaction state would be completely unpredictable after a
reconnect.
.IP \(bu 4
The \fIreconnect\fR feature of DBD::MariaDB can be toggled by using the
\&\fImariadb_auto_reconnect\fR attribute. This behaviour
should be turned off in code that uses LOCK TABLE because if the database server
time out and DBD::MariaDB reconnect, table locks will be lost without any
indication of such loss.
.SH "MULTIPLE RESULT SETS"
.IX Header "MULTIPLE RESULT SETS"
DBD::MariaDB supports multiple result sets, thanks to Guy Harrison!
.PP
The basic usage of multiple result sets is
.PP
.Vb 5
\& do {
\& while (my @row = $sth\->fetchrow_array()) {
\& do stuff;
\& }
\& } while ($sth\->more_results);
.Ve
.PP
An example would be:
.PP
.Vb 2
\& $dbh\->do(\*(Aqdrop procedure if exists someproc\*(Aq)
\& or print $DBI::errstr;
\&
\& $dbh\->do(\*(Aqcreate procedure someproc() deterministic
\& begin
\& declare a,b,c,d int;
\& set a=1;
\& set b=2;
\& set c=3;
\& set d=4;
\& select a, b, c, d;
\& select d, c, b, a;
\& select b, a, c, d;
\& select c, b, d, a;
\& end\*(Aq
\& ) or die "$DBI::err: $DBI::errstr";
\&
\& my $sth = $dbh\->prepare(\*(Aqcall someproc()\*(Aq)
\& or die "$DBI::err: $DBI::errstr";
\&
\& $sth\->execute()
\& or die "$DBI::err: $DBI::errstr";
\&
\& my $i=0;
\& do {
\& print "\enRowset ".++$i."\en\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en\en";
\& foreach my $colno (0..$sth\->{NUM_OF_FIELDS}\-1) {
\& print $sth\->{NAME}\->[$colno]."\et";
\& }
\& print "\en";
\& while (my @row = $sth\->fetchrow_array()) {
\& foreach $field (0..$#row) {
\& print $row[$field]."\et";
\& }
\& print "\en";
\& }
\& } while ($sth\->more_results);
.Ve
.SS "Issues with multiple result sets"
.IX Subsection "Issues with multiple result sets"
Please be aware there could be issues if your result sets are \fIjagged\fR, meaning
the number of columns of your results vary. Varying numbers of columns could
result in your script crashing.
.SH MULTITHREADING
.IX Header "MULTITHREADING"
The multithreading capabilities of DBD::MariaDB depend completely on the
underlying C libraries. The modules are working with handle data only, no global
variables are accessed or (to the best of my knowledge) thread unsafe functions
are called. Thus DBD::MariaDB is believed to be completely thread safe, if the C
libraries are thread safe and you don't share handles among threads.
.PP
The obvious question is: Are the C libraries thread safe? In the case of MySQL
the answer is yes, since MySQL 5.5 it is. Older versions C library needs to be
compiled with \f(CW\*(C`\-\-with\-thread\-safe\-client\*(C'\fR or \f(CW\*(C`\-\-enable\-thread\-safe\-client\*(C'\fR
configure options.
.SH "ASYNCHRONOUS QUERIES"
.IX Header "ASYNCHRONOUS QUERIES"
You can make a single asynchronous query per MySQL connection; this allows you
to submit a long-running query to the server and have an event loop inform you
when it's ready. An asynchronous query is started by either setting the
\&\fImariadb_async\fR attribute to a true value in the do method, or in the
prepare method. Statements created with \fImariadb_async\fR set to
true in prepare always run their queries asynchronously when
execute is called. The driver also offers three additional
methods: \f(CWmariadb_async_result()\fR, \f(CWmariadb_async_ready()\fR, and
\&\f(CWmariadb_sockfd()\fR. \f(CWmariadb_async_result()\fR returns what do or execute would
have; that is, the number of rows affected. \f(CWmariadb_async_ready()\fR returns
true if \f(CWmariadb_async_result()\fR will not block, and zero otherwise. They both
return \f(CW\*(C`undef\*(C'\fR if that handle was not created with \fImariadb_async\fR set to true
or if an asynchronous query was not started yet. \f(CWmariadb_sockfd()\fR returns
the file descriptor number for the MySQL connection; you can use this in an
event loop.
.PP
Here's an example of how to use the asynchronous query interface:
.PP
.Vb 7
\& use feature \*(Aqsay\*(Aq;
\& $dbh\->do(\*(AqSELECT SLEEP(10)\*(Aq, { mariadb_async => 1 });
\& until($dbh\->mariadb_async_ready()) {
\& say \*(Aqnot ready yet!\*(Aq;
\& sleep 1;
\& }
\& my $rows = $dbh\->mariadb_async_result();
.Ve
.PP
And another example:
.PP
.Vb 1
\& my $fd = $dbh\->mariadb_sockfd();
\&
\& my $sth = $dbh\->prepare(\*(AqSELECT 1, SLEEP(10), t.id FROM t\*(Aq, { mariadb_async => 1 });
\& $sth\->execute();
\&
\& my $bits = \*(Aq\*(Aq;
\& vec($bits, $fd, 1) = 1;
\& select($bits, undef, $bits, undef);
\&
\& my $num_rows = $sth\->mariadb_async_result();
\& print "num rows: $num_rows\en";
\& while (my @array = $sth\->fetchrow_array()) {
\& print "row:\en";
\& print "\etvalue: $_\en" foreach @array;
\& }
.Ve
.SH INSTALLATION
.IX Header "INSTALLATION"
See DBD::MariaDB::INSTALL.
.SH AUTHORS
.IX Header "AUTHORS"
Originally, there was a non-DBI driver, Mysql, which was much like PHP drivers
such as mysql and mysqli. The \fBMysql\fR module was originally written by Andreas
König (\fIkoenig@kulturbox.de\fR) who still, to this day, contributes patches to
DBD::mysql. An emulated version of Mysql was provided to DBD::mysql from Jochen
Wiedmann, but eventually deprecated as it was another bundle of code to
maintain.
.PP
The first incarnation of DBD::mysql was developed by Alligator Descartes, who
was also aided and abetted by Gary Shea, Andreas König and Tim Bunce.
.PP
The current incarnation of DBD::mysql was written by Jochen
Wiedmann, then numerous changes and bug-fixes were added by Rudy Lippan. Next,
prepared statement support was added by Patrick Galbraith and Alexy Stroganov
(who also solely added embedded server support).
.PP
Since 2004 DBD::mysql has been maintained by Patrick Galbraith
(\fIpatg@patg.net\fR), and since 2013 with the great help of Michiel Beijen
(\fImichiel.beijen@gmail.com\fR), along with the entire community of Perl
developers who keep sending patches to help continue improving DBD::mysql.
.PP
In 2018 unreleased version 4.042_01 of DBD::mysql was forked and DBD::MariaDB
was created to fix long standing Unicode bugs and MariaDB support. Development
at that time was supported by GoodData. Currently it is hosted on Github as
part of the big perl5\-dbi project and maintained by Pali (\fIpali@cpan.org\fR).
.SH CONTRIBUTIONS
.IX Header "CONTRIBUTIONS"
Anyone who desires to contribute to this project is encouraged to do so.
Currently, the source code for this project can be found at Github:
.PP
.PP
Either fork this repository and produce a branch with your changeset that the
maintainer can merge to his tree, or create a diff with git. The maintainer is
more than glad to take contributions from the community as many features and
fixes from DBD::MariaDB have come from the community.
.SH COPYRIGHT
.IX Header "COPYRIGHT"
This module is
.IP \(bu 4
Large Portions Copyright (c) 2018 GoodData Corporation
.IP \(bu 4
Large Portions Copyright (c) 2015\-2022 Pali Rohár
.IP \(bu 4
Large Portions Copyright (c) 2004\-2017 Patrick Galbraith
.IP \(bu 4
Large Portions Copyright (c) 2013\-2017 Michiel Beijen
.IP \(bu 4
Large Portions Copyright (c) 2004\-2007 Alexey Stroganov
.IP \(bu 4
Large Portions Copyright (c) 2003\-2005 Rudolf Lippan
.IP \(bu 4
Large Portions Copyright (c) 1997\-2003 Jochen Wiedmann, with code portions
.IP \(bu 4
Copyright (c)1994\-1997 their original authors
.SH LICENSE
.IX Header "LICENSE"
This module is released under the same license as Perl itself. See
Perl Licensing for details.
.SH "MAILING LIST SUPPORT"
.IX Header "MAILING LIST SUPPORT"
This module is maintained and supported on a mailing list, dbi-users.
.PP
To subscribe to this list, send an email to
.PP
\&\f(CW\*(C`dbi\-users\-subscribe@perl.org\*(C'\fR
.PP
Mailing list archives are at
.PP
.SH "ADDITIONAL DBI INFORMATION"
.IX Header "ADDITIONAL DBI INFORMATION"
Additional information on the DBI project can be found on the World Wide Web at
the following URL:
.PP
.PP
where documentation, pointers to the mailing lists and mailing list archives and
pointers to the most current versions of the modules can be used.
.PP
Information on the DBI interface itself can be gained by typing:
.PP
.Vb 1
\& perldoc DBI
.Ve
.PP
Information on DBD::MariaDB specifically can be gained by typing:
.PP
.Vb 1
\& perldoc DBD::MariaDB
.Ve
.PP
(this will display the document you're currently reading)
.SH "BUG REPORTING, ENHANCEMENT/FEATURE REQUESTS"
.IX Header "BUG REPORTING, ENHANCEMENT/FEATURE REQUESTS"
Please report bugs, including all the information needed such as DBD::MariaDB
version, MariaDB/MySQL version, OS type/version, etc to this link:
.PP
.PP
In past for DBD::mysql, MySQL/Sun/Oracle responded to bugs and assisted in
fixing bugs which many thanks should be given for their help! This driver is
outside the realm of the numerous components they support, and the maintainer
and community solely support DBD::mysql and DBD::MariaDB.