The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Class::Storage::DBI - DBI storage handler

SYNOPSIS

DESCRIPTION

This class represents the connection to the database

METHODS

new

throw_exception

Throws an exception - croaks.

connect_info

The arguments of connect_info are always a single array reference.

This is normally accessed via "connection" in DBIx::Class::Schema, which encapsulates its argument list in an arrayref before calling connect_info here.

The arrayref can either contain the same set of arguments one would normally pass to "connect" in DBI, or a lone code reference which returns a connected database handle.

In either case, if the final argument in your connect_info happens to be a hashref, connect_info will look there for several connection-specific options:

on_connect_do

This can be set to an arrayref of literal sql statements, which will be executed immediately after making the connection to the database every time we [re-]connect.

limit_dialect

Sets the limit dialect. This is useful for JDBC-bridge among others where the remote SQL-dialect cannot be determined by the name of the driver alone.

quote_char

Specifies what characters to use to quote table and column names. If you use this you will want to specify name_sep as well.

quote_char expects either a single character, in which case is it is placed on either side of the table/column, or an arrayref of length 2 in which case the table/column name is placed between the elements.

For example under MySQL you'd use quote_char => '`', and user SQL Server you'd use quote_char => [qw/[ ]/].

name_sep

This only needs to be used in conjunction with quote_char, and is used to specify the charecter that seperates elements (schemas, tables, columns) from each other. In most cases this is simply a ..

These options can be mixed in with your other DBI connection attributes, or placed in a seperate hashref after all other normal DBI connection arguments.

Every time connect_info is invoked, any previous settings for these options will be cleared before setting the new ones, regardless of whether any options are specified in the new connect_info.

Examples:

  # Simple SQLite connection
  ->connect_info([ 'dbi:SQLite:./foo.db' ]);

  # Connect via subref
  ->connect_info([ sub { DBI->connect(...) } ]);

  # A bit more complicated
  ->connect_info(
    [
      'dbi:Pg:dbname=foo',
      'postgres',
      'my_pg_password',
      { AutoCommit => 0 },
      { quote_char => q{"}, name_sep => q{.} },
    ]
  );

  # Equivalent to the previous example
  ->connect_info(
    [
      'dbi:Pg:dbname=foo',
      'postgres',
      'my_pg_password',
      { AutoCommit => 0, quote_char => q{"}, name_sep => q{.} },
    ]
  );

  # Subref + DBIC-specific connection options
  ->connect_info(
    [
      sub { DBI->connect(...) },
      {
          quote_char => q{`},
          name_sep => q{@},
          on_connect_do => ['SET search_path TO myschema,otherschema,public'],
      },
    ]
  );

on_connect_do

This method is deprecated in favor of setting via "connect_info".

debug

Causes SQL trace information to be emitted on the debugobj object. (or STDERR if debugobj has not specifically been set).

This is the equivalent to setting "DBIC_TRACE" in your shell environment.

debugfh

Set or retrieve the filehandle used for trace/debug output. This should be an IO::Handle compatible ojbect (only the print method is used. Initially set to be STDERR - although see information on the DBIC_TRACE environment variable.

debugobj

Sets or retrieves the object used for metric collection. Defaults to an instance of DBIx::Class::Storage::Statistics that is campatible with the original method of using a coderef as a callback. See the aforementioned Statistics class for more information.

debugcb

Sets a callback to be executed each time a statement is run; takes a sub reference. Callback is executed as $sub->($op, $info) where $op is SELECT/INSERT/UPDATE/DELETE and $info is what would normally be printed.

See debugobj for a better way.

disconnect

Disconnect the DBI handle, performing a rollback first if the database is not in AutoCommit mode.

connected

Check if the DBI handle is connected. Returns true if the handle is connected.

ensure_connected

Check whether the database handle is connected - if not then make a connection.

dbh

Returns the dbh - a data base handle of class DBI.

sql_maker

Returns a sql_maker object - normally an object of class DBIC::SQL::Abstract.

txn_begin

Calls begin_work on the current dbh.

See DBIx::Class::Schema for the txn_do() method, which allows for an entire code block to be executed transactionally.

txn_commit

Issues a commit against the current dbh.

txn_rollback

Issues a rollback against the current dbh. A nested rollback will throw a DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION exception, which allows the rollback to propagate to the outermost transaction.

select

Arguments: $ident, $select, $condition, $attrs

Handle a SQL select statement.

select_single

Performs a select, fetch and return of data - handles a single row only.

sth

Arguments: $sql

Returns a DBI sth (statement handle) for the supplied SQL.

columns_info_for

Returns database type info for a given table column.

last_insert_id

Return the row id of the last insert.

sqlt_type

Returns the database driver name.

create_ddl_dir (EXPERIMENTAL)

Arguments: $schema \@databases, $version, $directory, $sqlt_args

Creates a SQL file based on the Schema, for each of the specified database types, in the given directory.

Note that this feature is currently EXPERIMENTAL and may not work correctly across all databases, or fully handle complex relationships.

deployment_statements

Arguments: $schema, $type, $version, $directory, $sqlt_args

Returns the statements used by "deploy" and "deploy" in DBIx::Class::Schema. The database driver name is given by $type, though the value from "sqlt_type" is used if it is not specified.

$directory is used to return statements from files in a previously created "create_ddl_dir" directory and is optional. The filenames are constructed from "ddl_filename" in DBIx::Class::Schema, the schema name and the $version.

If no $directory is specified then the statements are constructed on the fly using SQL::Translator and $version is ignored.

See "METHODS" in SQL::Translator for a list of values for $sqlt_args.

deploy

Sends the appropriate statements to create or modify tables to the db. This would normally be called through "deploy" in DBIx::Class::Schema.

datetime_parser

Returns the datetime parser class

datetime_parser_type

Defines (returns) the datetime parser class - currently hardwired to DateTime::Format::MySQL

build_datetime_parser

See "datetime_parser"

SQL METHODS

The module defines a set of methods within the DBIC::SQL::Abstract namespace. These build on SQL::Abstract::Limit to provide the SQL query functions.

The following methods are extended:-

delete
insert
select
update
limit_dialect

See "connect_info" for details. For setting, this method is deprecated in favor of "connect_info".

quote_char

See "connect_info" for details. For setting, this method is deprecated in favor of "connect_info".

name_sep

See "connect_info" for details. For setting, this method is deprecated in favor of "connect_info".

ENVIRONMENT VARIABLES

DBIC_TRACE

If DBIC_TRACE is set then SQL trace information is produced (as when the debug method is set).

If the value is of the form 1=/path/name then the trace output is written to the file /path/name.

This environment variable is checked when the storage object is first created (when you call connect on your schema). So, run-time changes to this environment variable will not take effect unless you also re-connect on your schema.

DBIX_CLASS_STORAGE_DBI_DEBUG

Old name for DBIC_TRACE

AUTHORS

Matt S. Trout <mst@shadowcatsystems.co.uk>

Andy Grundman <andy@hybridized.org>

LICENSE

You may distribute this code under the same terms as Perl itself.