|
NAMEDBIx::Class::Storage::DBI - DBI storage handlerSYNOPSISmy $schema = MySchema->connect('dbi:SQLite:my.db'); $schema->storage->debug(1); my @stuff = $schema->storage->dbh_do( sub { my ($storage, $dbh, @args) = @_; $dbh->do("DROP TABLE authors"); }, @column_list ); $schema->resultset('Book')->search({ written_on => $schema->storage->datetime_parser->format_datetime(DateTime->now) }); DESCRIPTIONThis class represents the connection to an RDBMS via DBI. See DBIx::Class::Storage for general information. This pod only documents DBI-specific methods and behaviors.METHODSconnect_infoThis method is normally called by "connection" in DBIx::Class::Schema, which encapsulates its argument list in an arrayref before passing them here.The argument list may contain:
Please note that the DBI docs recommend that you always explicitly set "AutoCommit" to either 0 or 1. DBIx::Class further recommends that it be set to 1, and that you perform transactions via our "txn_do" in DBIx::Class::Schema method. DBIx::Class will set it to 1 if you do not do explicitly set it to zero. This is the default for most DBDs. See "DBIx::Class and AutoCommit" for details. DBIx::Class specific connection attributes In addition to the standard DBI connection attributes, DBIx::Class recognizes the following connection options. These options can be mixed in with your other DBI connection attributes, or placed in a separate hashref ("\%extra_attributes") as shown above. 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".
Some predefined storage methods you may use:
Some real-life examples of arguments to "connect_info" and "connect" in DBIx::Class::Schema # Simple SQLite connection ->connect_info([ 'dbi:SQLite:./foo.db' ]); # Connect via subref ->connect_info([ sub { DBI->connect(...) } ]); # Connect via subref in hashref ->connect_info([{ dbh_maker => sub { DBI->connect(...) }, on_connect_do => 'alter session ...', }]); # A bit more complicated ->connect_info( [ 'dbi:Pg:dbname=foo', 'postgres', 'my_pg_password', { AutoCommit => 1 }, { quote_char => q{"} }, ] ); # Equivalent to the previous example ->connect_info( [ 'dbi:Pg:dbname=foo', 'postgres', 'my_pg_password', { AutoCommit => 1, quote_char => q{"}, name_sep => q{.} }, ] ); # Same, but with hashref as argument # See parse_connect_info for explanation ->connect_info( [{ dsn => 'dbi:Pg:dbname=foo', user => 'postgres', password => 'my_pg_password', AutoCommit => 1, quote_char => q{"}, name_sep => q{.}, }] ); # Subref + DBIx::Class-specific connection options ->connect_info( [ sub { DBI->connect(...) }, { quote_char => q{`}, name_sep => q{@}, on_connect_do => ['SET search_path TO myschema,otherschema,public'], disable_sth_caching => 1, }, ] ); on_connect_doThis method is deprecated in favour of setting via "connect_info".on_disconnect_doThis method is deprecated in favour of setting via "connect_info".dbh_doArguments: ($subref | $method_name), @extra_coderef_args?Execute the given $subref or $method_name using the new exception-based connection management. The first two arguments will be the storage object that "dbh_do" was called on and a database handle to use. Any additional arguments will be passed verbatim to the called subref as arguments 2 and onwards. Using this (instead of $self->_dbh or $self->dbh) ensures correct exception handling and reconnection (or failover in future subclasses). Your subref should have no side-effects outside of the database, as there is the potential for your subref to be partially double-executed if the database connection was stale/dysfunctional. Example: my @stuff = $schema->storage->dbh_do( sub { my ($storage, $dbh, @cols) = @_; my $cols = join(q{, }, @cols); $dbh->selectrow_array("SELECT $cols FROM foo"); }, @column_list ); disconnectOur "disconnect" method also performs a rollback first if the database is not in "AutoCommit" mode.with_deferred_fk_checks
Storage specific method to run the code ref with FK checks deferred or in MySQL's case disabled entirely. connected
Verifies that the current database handle is active and ready to execute an SQL statement (e.g. the connection did not get stale, server is still answering, etc.) This method is used internally by "dbh". dbhReturns a $dbh - a data base handle of class DBI. The returned handle is guaranteed to be healthy by implicitly calling "connected", and if necessary performing a reconnection before returning. Keep in mind that this is very expensive on some database engines. Consider using "dbh_do" instead.connect_call_datetime_setupA no-op stub method, provided so that one can always safely supply the connection optionon_connect_call => 'datetime_setup' This way one does not need to know in advance whether the underlying storage requires any sort of hand-holding when dealing with calendar data. connect_call_rebase_sqlmakerThis on-connect call takes as a single argument the name of a class to "rebase" the SQLMaker inheritance hierarchy upon. For this to work properly the target class MUST inherit from DBIx::Class::SQLMaker::ClassicExtensions and SQL::Abstract::Classic as shown below.This infrastructure is provided to aid recent activity around experimental new aproaches to SQL generation within DBIx::Class. You can (and are encouraged to) mix and match old and new within the same codebase as follows: package DBIx::Class::Awesomer::SQLMaker; # you MUST inherit in this order to get the composition right # you are free to override-without-next::method any part you need use base qw( DBIx::Class::SQLMaker::ClassicExtensions << OPTIONAL::AWESOME::Class::Implementing::ExtraRainbowSauce >> SQL::Abstract::Classic ); << your new code goes here >> ... and then ... my $experimental_schema = $original_schema->connect( sub { $original_schema->storage->dbh }, { # the nested arrayref is important, as per # https://metacpan.org/pod/DBIx::Class::Storage::DBI#on_connect_call on_connect_call => [ [ rebase_sqlmaker => 'DBIx::Class::Awesomer::SQLMaker' ] ], }, ); select
Handle a SQL select statement. sql_limit_dialectThis is an accessor for the default SQL limit dialect used by a particular storage driver. Can be overridden by supplying an explicit "limit_dialect" to "connect" in DBIx::Class::Schema. For a list of available limit dialects see DBIx::Class::SQLMaker::LimitDialects.last_insert_idReturn the row id of the last insert._native_data_type
This API is EXPERIMENTAL, will almost definitely change in the future, and currently only used by ::AutoCast and ::Sybase::ASE. The default implementation returns "undef", implement in your Storage driver if you need this functionality. Should map types from other databases to the native RDBMS type, for example "VARCHAR2" to "VARCHAR". Types with modifiers should map to the underlying data type. For example, "INTEGER AUTO_INCREMENT" should become "INTEGER". Composite types should map to the container type, for example "ENUM(foo,bar,baz)" becomes "ENUM". sqlt_typeReturns the database driver name.bind_attribute_by_data_typeGiven a datatype from column info, returns a database specific bind attribute for "$dbh->bind_param($val,$attribute)" or nothing if we will let the database planner just handle it.This method is always called after the driver has been determined and a DBI connection has been established. Therefore you can refer to "DBI::$constant" and/or "DBD::$driver::$constant" directly, without worrying about loading the correct modules. is_datatype_numericGiven a datatype from column_info, returns a boolean value indicating if the current RDBMS considers it a numeric value. This controls how "set_column" in DBIx::Class::Row decides whether to mark the column as dirty - when the datatype is deemed numeric a "!=" comparison will be performed instead of the usual "eq".create_ddl_dir
Creates a SQL file based on the Schema, for each of the specified database engines in "\@databases" in the given directory. (note: specify SQL::Translator names, not DBI driver names). Given a previous version number, this will also create a file containing the ALTER TABLE statements to transform the previous schema into the current one. Note that these statements may contain "DROP TABLE" or "DROP COLUMN" statements that can potentially destroy data. The file names are created using the "ddl_filename" method below, please override this method in your schema if you would like a different file name format. For the ALTER file, the same format is used, replacing $version in the name with "$preversion-$version". See "METHODS" in SQL::Translator for a list of values for "\%sqlt_args". The most common value for this would be "{ add_drop_table => 1 }" to have the SQL produced include a "DROP TABLE" statement for each table created. For quoting purposes supply "quote_identifiers". If no arguments are passed, then the following default values are assumed:
By default, "\%sqlt_args" will have { add_drop_table => 1, ignore_constraint_names => 1, ignore_index_names => 1 } merged with the hash passed in. To disable any of those features, pass in a hashref like the following { ignore_constraint_names => 0, # ... other options } WARNING: You are strongly advised to check all SQL files created, before applying them. deployment_statements
Returns the statements used by "deploy" in DBIx::Class::Storage and "deploy" in DBIx::Class::Schema. The SQL::Translator (not DBI) database driver name can be explicitly provided in $type, otherwise the result of "sqlt_type" is used as default. $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. datetime_parserReturns the datetime parser classdatetime_parser_typeDefines the datetime parser class - currently defaults to DateTime::Format::MySQLbuild_datetime_parserSee "datetime_parser"is_replicatingA boolean that reports if a particular DBIx::Class::Storage::DBI is set to replicate from a master database. Default is undef, which is the result returned by databases that don't support replication.lag_behind_masterReturns a number that represents a certain amount of lag behind a master db when a given storage is replicating. The number is database dependent, but starts at zero and increases with the amount of lag. Default in undefrelname_to_table_alias
DBIx::Class uses DBIx::Class::Relationship names as table aliases in queries. This hook is to allow specific DBIx::Class::Storage drivers to change the way these aliases are named. The default behavior is ""$relname_$join_count" if $join_count > 1", otherwise "$relname". USAGE NOTESDBIx::Class and AutoCommitDBIx::Class can do some wonderful magic with handling exceptions, disconnections, and transactions when you use "AutoCommit => 1" (the default) combined with txn_do for transaction support.If you set "AutoCommit => 0" in your connect info, then you are always in an assumed transaction between commits, and you're telling us you'd like to manage that manually. A lot of the magic protections offered by this module will go away. We can't protect you from exceptions due to database disconnects because we don't know anything about how to restart your transactions. You're on your own for handling all sorts of exceptional cases if you choose the "AutoCommit => 0" path, just as you would be with raw DBI. FURTHER QUESTIONS?Check the list of additional DBIC resources.COPYRIGHT AND LICENSEThis module is free software copyright by the DBIx::Class (DBIC) authors. You can redistribute it and/or modify it under the same terms as the DBIx::Class library.
Visit the GSP FreeBSD Man Page Interface. |