DESCRIPTION

MySQL-specific extension (subclass) to DBI in order to improve convenience, security, and error handling. Supports easy use of credential (cnf) files, akin to

mysql --defaults-file=credentials.cnf

It aims to reduce boilerplate, verbosity, mistakes, and parameter overload, but above all it tries to make it quick and easy to Do The Right Thing.

As the name implies, the class provides connector objects -- containers for storing and updating your connection parameters. When you call connect, the connector returns a handle created using its retained parameters plus any call-time parameters passed. You don't however have to use connectors; for simple usage it can be easier to use connect directly from the class.

You can use a DSN tuple if you want to, but it's more readable and less error-prone to specify your parameters either as a hash or by setting individual attributes. Each call to connect will then construct the DSN for you.

You can optionally import a helper method, called dbh (or whatever name you choose) so that you can focus even less on the connector/connection and more on your code. The helper will cache your database handle and create a new one automatically if the old one is destroyed or goes stale.

The fourth layer of convenience is provided by the added database handle methods. Changing session variables (session_var) is easy, listing only genuine tables (real_tables) is easy, and there's more.

CLASS METHODS

new

Constructor for a connector, based on class defaults. Takes a (possibly empty) list of parameters. Returns a connector (Mojar::Mysql::Connector object) the defaults of which are those of the class overlaid with those passed to the constructor.

Constructor for a connection (db handle). If the first element passed has prefix DBI: then it is a DSN string (the traditional route) and so is passed straight to DBI::connect ("DBI Class Methods" in DBI). Otherwise a DSN is first constructed. (The DSN tuple does not persist and is constructed fresh on each call to connect.)

In the examples above, $dbh1 and $dbh2 are not equivalent because the second connector would also incorporate module defaults and use-time parameters, in addition to the passed parameters. So, for instance, mysql_enable_utf8 might be included in the second connector.

dsn

A convenience method used internally by connect. Takes a (possibly empty) parameter hash. Returns a four-element array to pass to DBI::connect, constructed from the default values of the constructing class overlaid with any additional parameters passed. The main reason for using this method is when you want to use DBI (or another DSN-consumer) directly but want to avoid the inconvenience of assembling sensible parameters yourself.

dsn_to_dump

warn(Mojar::Mysql::Connector->dsn_to_dump(@dsn));

A convenience method used internally to chop up the four-element array (particularly the fourth element, the hash ref) into something more readable, for error reporting and debugging. Tries to occlude any password within.

Defaults

say Mojar::Util::dumper(Mojar::Mysql::Connector->Defaults);

Provides access to the class defaults in order to help debugging.

OBJECT METHODS

new

$connector->new(label => 'transaction', AutoCommit => 0);

Constructor for a connector based on an existing connector. Takes a (possibly empty) parameter hash. Returns a connector (Mojar::Mysql::Connector object) the defaults of which are those of the given connector overlaid with any arguments passed in.

connect

Constructor for a connection (db handle). If the first element passed has prefix DBI: then it is a DSN string (the traditional route) and so is passed straight to DBI::connect ("DBI Class Methods" in DBI) without consideration of the connector's existing parameters. Otherwise a DSN is first constructed. (The DSN tuple does not persist and is constructed fresh on each call to connect.)

Attributes

All connector parameters are implemented as attributes with exactly the same spelling. So for example you can

Returns an arrayref of hashrefs, each hashref being a record of the resultset. The keys of the hashref are the column/field names of the record. This is simply minimal sugar on the selectall_arrayref method provided by DBI; it saves the little boilerplate of "Slice => {}". If you want to pass bound values then you need undef as the second argument.

current_schema

session_var

Getter/setter for session variables. To get a value, simply pass the variable's name.

$value = $dbh->session_var('date_format');

In list context returns the old value and the new value; in scalar context returns the handle to facilitate chaining.

$dbh->session_var(var1 => ...)
->session_var(var2 => ...);

disable_quotes

my @ddl = $dbh->disable_quotes->selectrow_array(q{SHOW CREATE ...});

Disable optional quotes around identifiers. Currently only affects output of SHOW CREATE TABLE. If you have unsafe identifiers (eg spaces or keywords) then those will still be quoted. Lasts the lifetime of the connection.

SUPPORT

Homepage

Wiki

RATIONALE

This class was first used in production in 2002. Before then, connecting to databases was ugly and annoying. Setting RaiseError upon every connect was clumsy and irritating. In development teams it was tricky checking that all code was using sensible parameters and awkward ensuring use of risky parameters (eg disable_fk_checks) was kept local. As use of this class spread, it had to be useful in persistent high performance applications as well as many small scripts and the occasional commandline. More recently I discovered the Joy of Mojolicious and employed Mojo::Base to remove unwanted complexity and eliminate a long-standing bug. The ensuing fun motivated an extensive rewrite, fixing broken documentation, improved the tests (thank you travis), and we have, finally, its public release. As noted below there are now quite a few smart alternatives out there but I'm still surprised how little support there is for keeping passwords out of your codebase and helping you manage multiple connections.