1 package DBIx::Class::Storage::DBI;
2 # -*- mode: cperl; cperl-indent-level: 2 -*-
7 use base qw/DBIx::Class::Storage::DBIHacks DBIx::Class::Storage/;
10 use Carp::Clan qw/^DBIx::Class|^Try::Tiny/;
12 use DBIx::Class::Storage::DBI::Cursor;
13 use Scalar::Util qw/refaddr weaken reftype blessed/;
14 use List::Util qw/first/;
15 use Sub::Name 'subname';
21 # default cursor class, overridable in connect_info attributes
22 __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::Cursor');
24 __PACKAGE__->mk_group_accessors('inherited' => qw/
25 sql_limit_dialect sql_quote_char sql_name_sep
28 __PACKAGE__->mk_group_accessors('component_class' => qw/sql_maker_class datetime_parser_type/);
30 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker');
31 __PACKAGE__->datetime_parser_type('DateTime::Format::MySQL'); # historic default
33 __PACKAGE__->sql_name_sep('.');
35 __PACKAGE__->mk_group_accessors('simple' => qw/
36 _connect_info _dbi_connect_info _dbic_connect_attributes _driver_determined
37 _dbh _dbh_details _conn_pid _sql_maker _sql_maker_opts
38 transaction_depth _dbh_autocommit savepoints
41 # the values for these accessors are picked out (and deleted) from
42 # the attribute hashref passed to connect_info
43 my @storage_options = qw/
44 on_connect_call on_disconnect_call on_connect_do on_disconnect_do
45 disable_sth_caching unsafe auto_savepoint
47 __PACKAGE__->mk_group_accessors('simple' => @storage_options);
50 # capability definitions, using a 2-tiered accessor system
53 # A driver/user may define _use_X, which blindly without any checks says:
54 # "(do not) use this capability", (use_dbms_capability is an "inherited"
57 # If _use_X is undef, _supports_X is then queried. This is a "simple" style
58 # accessor, which in turn calls _determine_supports_X, and stores the return
59 # in a special slot on the storage object, which is wiped every time a $dbh
60 # reconnection takes place (it is not guaranteed that upon reconnection we
61 # will get the same rdbms version). _determine_supports_X does not need to
62 # exist on a driver, as we ->can for it before calling.
64 my @capabilities = (qw/
66 insert_returning_bound
71 __PACKAGE__->mk_group_accessors( dbms_capability => map { "_supports_$_" } @capabilities );
72 __PACKAGE__->mk_group_accessors( use_dbms_capability => map { "_use_$_" } (@capabilities ) );
74 # on by default, not strictly a capability (pending rewrite)
75 __PACKAGE__->_use_join_optimizer (1);
76 sub _determine_supports_join_optimizer { 1 };
78 # Each of these methods need _determine_driver called before itself
79 # in order to function reliably. This is a purely DRY optimization
81 # get_(use)_dbms_capability need to be called on the correct Storage
82 # class, as _use_X may be hardcoded class-wide, and _supports_X calls
83 # _determine_supports_X which obv. needs a correct driver as well
84 my @rdbms_specific_methods = qw/
98 get_use_dbms_capability
105 for my $meth (@rdbms_specific_methods) {
107 my $orig = __PACKAGE__->can ($meth)
108 or die "$meth is not a ::Storage::DBI method!";
111 no warnings qw/redefine/;
112 *{__PACKAGE__ ."::$meth"} = subname $meth => sub {
114 # only fire when invoked on an instance, a valid class-based invocation
115 # would e.g. be setting a default for an inherited accessor
118 ! $_[0]->_driver_determined
120 ! $_[0]->{_in_determine_driver}
122 $_[0]->_determine_driver;
124 # This for some reason crashes and burns on perl 5.8.1
125 # IFF the method ends up throwing an exception
126 #goto $_[0]->can ($meth);
128 my $cref = $_[0]->can ($meth);
139 DBIx::Class::Storage::DBI - DBI storage handler
143 my $schema = MySchema->connect('dbi:SQLite:my.db');
145 $schema->storage->debug(1);
147 my @stuff = $schema->storage->dbh_do(
149 my ($storage, $dbh, @args) = @_;
150 $dbh->do("DROP TABLE authors");
155 $schema->resultset('Book')->search({
156 written_on => $schema->storage->datetime_parser->format_datetime(DateTime->now)
161 This class represents the connection to an RDBMS via L<DBI>. See
162 L<DBIx::Class::Storage> for general information. This pod only
163 documents DBI-specific methods and behaviors.
170 my $new = shift->next::method(@_);
172 $new->transaction_depth(0);
173 $new->_sql_maker_opts({});
174 $new->_dbh_details({});
175 $new->{savepoints} = [];
176 $new->{_in_dbh_do} = 0;
177 $new->{_dbh_gen} = 0;
179 # read below to see what this does
180 $new->_arm_global_destructor;
185 # This is hack to work around perl shooting stuff in random
186 # order on exit(). If we do not walk the remaining storage
187 # objects in an END block, there is a *small but real* chance
188 # of a fork()ed child to kill the parent's shared DBI handle,
189 # *before perl reaches the DESTROY in this package*
190 # Yes, it is ugly and effective.
191 # Additionally this registry is used by the CLONE method to
192 # make sure no handles are shared between threads
194 my %seek_and_destroy;
196 sub _arm_global_destructor {
198 my $key = refaddr ($self);
199 $seek_and_destroy{$key} = $self;
200 weaken ($seek_and_destroy{$key});
204 local $?; # just in case the DBI destructor changes it somehow
206 # destroy just the object if not native to this process/thread
207 $_->_verify_pid for (grep
209 values %seek_and_destroy
214 # As per DBI's recommendation, DBIC disconnects all handles as
215 # soon as possible (DBIC will reconnect only on demand from within
217 for (values %seek_and_destroy) {
219 $_->{_dbh_gen}++; # so that existing cursors will drop as well
228 # some databases spew warnings on implicit disconnect
229 local $SIG{__WARN__} = sub {};
232 # this op is necessary, since the very last perl runtime statement
233 # triggers a global destruction shootout, and the $SIG localization
234 # may very well be destroyed before perl actually gets to do the
239 # handle pid changes correctly - do not destroy parent's connection
243 my $pid = $self->_conn_pid;
244 if( defined $pid and $pid != $$ and my $dbh = $self->_dbh ) {
245 $dbh->{InactiveDestroy} = 1;
255 This method is normally called by L<DBIx::Class::Schema/connection>, which
256 encapsulates its argument list in an arrayref before passing them here.
258 The argument list may contain:
264 The same 4-element argument set one would normally pass to
265 L<DBI/connect>, optionally followed by
266 L<extra attributes|/DBIx::Class specific connection attributes>
267 recognized by DBIx::Class:
269 $connect_info_args = [ $dsn, $user, $password, \%dbi_attributes?, \%extra_attributes? ];
273 A single code reference which returns a connected
274 L<DBI database handle|DBI/connect> optionally followed by
275 L<extra attributes|/DBIx::Class specific connection attributes> recognized
278 $connect_info_args = [ sub { DBI->connect (...) }, \%extra_attributes? ];
282 A single hashref with all the attributes and the dsn/user/password
285 $connect_info_args = [{
293 $connect_info_args = [{
294 dbh_maker => sub { DBI->connect (...) },
299 This is particularly useful for L<Catalyst> based applications, allowing the
300 following config (L<Config::General> style):
305 dsn dbi:mysql:database=test
312 The C<dsn>/C<user>/C<password> combination can be substituted by the
313 C<dbh_maker> key whose value is a coderef that returns a connected
314 L<DBI database handle|DBI/connect>
318 Please note that the L<DBI> docs recommend that you always explicitly
319 set C<AutoCommit> to either I<0> or I<1>. L<DBIx::Class> further
320 recommends that it be set to I<1>, and that you perform transactions
321 via our L<DBIx::Class::Schema/txn_do> method. L<DBIx::Class> will set it
322 to I<1> if you do not do explicitly set it to zero. This is the default
323 for most DBDs. See L</DBIx::Class and AutoCommit> for details.
325 =head3 DBIx::Class specific connection attributes
327 In addition to the standard L<DBI|DBI/ATTRIBUTES_COMMON_TO_ALL_HANDLES>
328 L<connection|DBI/Database_Handle_Attributes> attributes, DBIx::Class recognizes
329 the following connection options. These options can be mixed in with your other
330 L<DBI> connection attributes, or placed in a separate hashref
331 (C<\%extra_attributes>) as shown above.
333 Every time C<connect_info> is invoked, any previous settings for
334 these options will be cleared before setting the new ones, regardless of
335 whether any options are specified in the new C<connect_info>.
342 Specifies things to do immediately after connecting or re-connecting to
343 the database. Its value may contain:
349 This contains one SQL statement to execute.
351 =item an array reference
353 This contains SQL statements to execute in order. Each element contains
354 a string or a code reference that returns a string.
356 =item a code reference
358 This contains some code to execute. Unlike code references within an
359 array reference, its return value is ignored.
363 =item on_disconnect_do
365 Takes arguments in the same form as L</on_connect_do> and executes them
366 immediately before disconnecting from the database.
368 Note, this only runs if you explicitly call L</disconnect> on the
371 =item on_connect_call
373 A more generalized form of L</on_connect_do> that calls the specified
374 C<connect_call_METHOD> methods in your storage driver.
376 on_connect_do => 'select 1'
380 on_connect_call => [ [ do_sql => 'select 1' ] ]
382 Its values may contain:
388 Will call the C<connect_call_METHOD> method.
390 =item a code reference
392 Will execute C<< $code->($storage) >>
394 =item an array reference
396 Each value can be a method name or code reference.
398 =item an array of arrays
400 For each array, the first item is taken to be the C<connect_call_> method name
401 or code reference, and the rest are parameters to it.
405 Some predefined storage methods you may use:
411 Executes a SQL string or a code reference that returns a SQL string. This is
412 what L</on_connect_do> and L</on_disconnect_do> use.
420 Will execute the scalar as SQL.
424 Taken to be arguments to L<DBI/do>, the SQL string optionally followed by the
425 attributes hashref and bind values.
427 =item a code reference
429 Will execute C<< $code->($storage) >> and execute the return array refs as
436 Execute any statements necessary to initialize the database session to return
437 and accept datetime/timestamp values used with
438 L<DBIx::Class::InflateColumn::DateTime>.
440 Only necessary for some databases, see your specific storage driver for
441 implementation details.
445 =item on_disconnect_call
447 Takes arguments in the same form as L</on_connect_call> and executes them
448 immediately before disconnecting from the database.
450 Calls the C<disconnect_call_METHOD> methods as opposed to the
451 C<connect_call_METHOD> methods called by L</on_connect_call>.
453 Note, this only runs if you explicitly call L</disconnect> on the
456 =item disable_sth_caching
458 If set to a true value, this option will disable the caching of
459 statement handles via L<DBI/prepare_cached>.
463 Sets a specific SQL::Abstract::Limit-style limit dialect, overriding the
464 default L</sql_limit_dialect> setting of the storage (if any). For a list
465 of available limit dialects see L<DBIx::Class::SQLMaker::LimitDialects>.
469 When true automatically sets L</quote_char> and L</name_sep> to the characters
470 appropriate for your particular RDBMS. This option is preferred over specifying
471 L</quote_char> directly.
475 Specifies what characters to use to quote table and column names.
477 C<quote_char> expects either a single character, in which case is it
478 is placed on either side of the table/column name, or an arrayref of length
479 2 in which case the table/column name is placed between the elements.
481 For example under MySQL you should use C<< quote_char => '`' >>, and for
482 SQL Server you should use C<< quote_char => [qw/[ ]/] >>.
486 This parameter is only useful in conjunction with C<quote_char>, and is used to
487 specify the character that separates elements (schemas, tables, columns) from
488 each other. If unspecified it defaults to the most commonly used C<.>.
492 This Storage driver normally installs its own C<HandleError>, sets
493 C<RaiseError> and C<ShowErrorStatement> on, and sets C<PrintError> off on
494 all database handles, including those supplied by a coderef. It does this
495 so that it can have consistent and useful error behavior.
497 If you set this option to a true value, Storage will not do its usual
498 modifications to the database handle's attributes, and instead relies on
499 the settings in your connect_info DBI options (or the values you set in
500 your connection coderef, in the case that you are connecting via coderef).
502 Note that your custom settings can cause Storage to malfunction,
503 especially if you set a C<HandleError> handler that suppresses exceptions
504 and/or disable C<RaiseError>.
508 If this option is true, L<DBIx::Class> will use savepoints when nesting
509 transactions, making it possible to recover from failure in the inner
510 transaction without having to abort all outer transactions.
514 Use this argument to supply a cursor class other than the default
515 L<DBIx::Class::Storage::DBI::Cursor>.
519 Some real-life examples of arguments to L</connect_info> and
520 L<DBIx::Class::Schema/connect>
522 # Simple SQLite connection
523 ->connect_info([ 'dbi:SQLite:./foo.db' ]);
526 ->connect_info([ sub { DBI->connect(...) } ]);
528 # Connect via subref in hashref
530 dbh_maker => sub { DBI->connect(...) },
531 on_connect_do => 'alter session ...',
534 # A bit more complicated
541 { quote_char => q{"} },
545 # Equivalent to the previous example
551 { AutoCommit => 1, quote_char => q{"}, name_sep => q{.} },
555 # Same, but with hashref as argument
556 # See parse_connect_info for explanation
559 dsn => 'dbi:Pg:dbname=foo',
561 password => 'my_pg_password',
568 # Subref + DBIx::Class-specific connection options
571 sub { DBI->connect(...) },
575 on_connect_do => ['SET search_path TO myschema,otherschema,public'],
576 disable_sth_caching => 1,
586 my ($self, $info) = @_;
588 return $self->_connect_info if !$info;
590 $self->_connect_info($info); # copy for _connect_info
592 $info = $self->_normalize_connect_info($info)
593 if ref $info eq 'ARRAY';
595 for my $storage_opt (keys %{ $info->{storage_options} }) {
596 my $value = $info->{storage_options}{$storage_opt};
598 $self->$storage_opt($value);
601 # Kill sql_maker/_sql_maker_opts, so we get a fresh one with only
602 # the new set of options
603 $self->_sql_maker(undef);
604 $self->_sql_maker_opts({});
606 for my $sql_maker_opt (keys %{ $info->{sql_maker_options} }) {
607 my $value = $info->{sql_maker_options}{$sql_maker_opt};
609 $self->_sql_maker_opts->{$sql_maker_opt} = $value;
613 %{ $self->_default_dbi_connect_attributes || {} },
614 %{ $info->{attributes} || {} },
617 my @args = @{ $info->{arguments} };
619 if (keys %attrs and ref $args[0] ne 'CODE') {
621 'You provided explicit AutoCommit => 0 in your connection_info. '
622 . 'This is almost universally a bad idea (see the footnotes of '
623 . 'DBIx::Class::Storage::DBI for more info). If you still want to '
624 . 'do this you can set $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} to disable '
626 if ! $attrs{AutoCommit} and ! $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK};
628 push @args, \%attrs if keys %attrs;
630 $self->_dbi_connect_info(\@args);
633 # save attributes them in a separate accessor so they are always
634 # introspectable, even in case of a CODE $dbhmaker
635 $self->_dbic_connect_attributes (\%attrs);
637 return $self->_connect_info;
640 sub _normalize_connect_info {
641 my ($self, $info_arg) = @_;
644 my @args = @$info_arg; # take a shallow copy for further mutilation
646 # combine/pre-parse arguments depending on invocation style
649 if (ref $args[0] eq 'CODE') { # coderef with optional \%extra_attributes
650 %attrs = %{ $args[1] || {} };
653 elsif (ref $args[0] eq 'HASH') { # single hashref (i.e. Catalyst config)
654 %attrs = %{$args[0]};
656 if (my $code = delete $attrs{dbh_maker}) {
659 my @ignored = grep { delete $attrs{$_} } (qw/dsn user password/);
662 'Attribute(s) %s in connect_info were ignored, as they can not be applied '
663 . "to the result of 'dbh_maker'",
665 join (', ', map { "'$_'" } (@ignored) ),
670 @args = delete @attrs{qw/dsn user password/};
673 else { # otherwise assume dsn/user/password + \%attrs + \%extra_attrs
675 % { $args[3] || {} },
676 % { $args[4] || {} },
678 @args = @args[0,1,2];
681 $info{arguments} = \@args;
683 my @storage_opts = grep exists $attrs{$_},
684 @storage_options, 'cursor_class';
686 @{ $info{storage_options} }{@storage_opts} =
687 delete @attrs{@storage_opts} if @storage_opts;
689 my @sql_maker_opts = grep exists $attrs{$_},
690 qw/limit_dialect quote_char name_sep quote_names/;
692 @{ $info{sql_maker_options} }{@sql_maker_opts} =
693 delete @attrs{@sql_maker_opts} if @sql_maker_opts;
695 $info{attributes} = \%attrs if %attrs;
700 sub _default_dbi_connect_attributes () {
705 ShowErrorStatement => 1,
711 This method is deprecated in favour of setting via L</connect_info>.
715 =head2 on_disconnect_do
717 This method is deprecated in favour of setting via L</connect_info>.
721 sub _parse_connect_do {
722 my ($self, $type) = @_;
724 my $val = $self->$type;
725 return () if not defined $val;
730 push @res, [ 'do_sql', $val ];
731 } elsif (ref($val) eq 'CODE') {
733 } elsif (ref($val) eq 'ARRAY') {
734 push @res, map { [ 'do_sql', $_ ] } @$val;
736 $self->throw_exception("Invalid type for $type: ".ref($val));
744 Arguments: ($subref | $method_name), @extra_coderef_args?
746 Execute the given $subref or $method_name using the new exception-based
747 connection management.
749 The first two arguments will be the storage object that C<dbh_do> was called
750 on and a database handle to use. Any additional arguments will be passed
751 verbatim to the called subref as arguments 2 and onwards.
753 Using this (instead of $self->_dbh or $self->dbh) ensures correct
754 exception handling and reconnection (or failover in future subclasses).
756 Your subref should have no side-effects outside of the database, as
757 there is the potential for your subref to be partially double-executed
758 if the database connection was stale/dysfunctional.
762 my @stuff = $schema->storage->dbh_do(
764 my ($storage, $dbh, @cols) = @_;
765 my $cols = join(q{, }, @cols);
766 $dbh->selectrow_array("SELECT $cols FROM foo");
777 my $dbh = $self->_get_dbh;
779 return $self->$code($dbh, @_)
780 if ( $self->{_in_dbh_do} || $self->{transaction_depth} );
782 local $self->{_in_dbh_do} = 1;
784 # take a ref instead of a copy, to preserve coderef @_ aliasing semantics
787 $self->$code ($dbh, @$args);
789 $self->throw_exception($_) if $self->connected;
791 # We were not connected - reconnect and retry, but let any
792 # exception fall right through this time
793 carp "Retrying $code after catching disconnected exception: $_"
794 if $ENV{DBIC_DBIRETRY_DEBUG};
796 $self->_populate_dbh;
797 $self->$code($self->_dbh, @$args);
801 # This is basically a blend of dbh_do above and DBIx::Class::Storage::txn_do.
802 # It also informs dbh_do to bypass itself while under the direction of txn_do,
803 # via $self->{_in_dbh_do} (this saves some redundant eval and errorcheck, etc)
808 ref $coderef eq 'CODE' or $self->throw_exception
809 ('$coderef must be a CODE reference');
811 local $self->{_in_dbh_do} = 1;
814 my $want = wantarray;
820 # take a ref instead of a copy, to preserve coderef @_ aliasing semantics
825 my $txn_start_depth = $self->transaction_depth;
827 @result = $coderef->(@$args);
829 elsif(defined $want) {
830 $result[0] = $coderef->(@$args);
836 my $delta_txn = $txn_start_depth - $self->transaction_depth;
837 if ($delta_txn == 0) {
840 elsif ($delta_txn != 1) {
841 # an off-by-one would mean we fired a rollback
842 carp "Unexpected reduction of transaction depth by $delta_txn after execution of $coderef";
848 if(! defined $exception) { return wantarray ? @result : $result[0] }
850 if($self->transaction_depth > 1 || $tried++ || $self->connected) {
851 my $rollback_exception;
852 try { $self->txn_rollback } catch { $rollback_exception = shift };
853 if(defined $rollback_exception) {
854 my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
855 $self->throw_exception($exception) # propagate nested rollback
856 if $rollback_exception =~ /$exception_class/;
858 $self->throw_exception(
859 "Transaction aborted: ${exception}. "
860 . "Rollback failed: ${rollback_exception}"
863 $self->throw_exception($exception)
866 # We were not connected, and was first try - reconnect and retry
868 carp "Retrying $coderef after catching disconnected exception: $exception"
869 if $ENV{DBIC_TXNRETRY_DEBUG};
870 $self->_populate_dbh;
876 Our C<disconnect> method also performs a rollback first if the
877 database is not in C<AutoCommit> mode.
887 push @actions, ( $self->on_disconnect_call || () );
888 push @actions, $self->_parse_connect_do ('on_disconnect_do');
890 $self->_do_connection_actions(disconnect_call_ => $_) for @actions;
892 $self->_dbh_rollback unless $self->_dbh_autocommit;
894 %{ $self->_dbh->{CachedKids} } = ();
895 $self->_dbh->disconnect;
901 =head2 with_deferred_fk_checks
905 =item Arguments: C<$coderef>
907 =item Return Value: The return value of $coderef
911 Storage specific method to run the code ref with FK checks deferred or
912 in MySQL's case disabled entirely.
916 # Storage subclasses should override this
917 sub with_deferred_fk_checks {
918 my ($self, $sub) = @_;
926 =item Arguments: none
928 =item Return Value: 1|0
932 Verifies that the current database handle is active and ready to execute
933 an SQL statement (e.g. the connection did not get stale, server is still
934 answering, etc.) This method is used internally by L</dbh>.
940 return 0 unless $self->_seems_connected;
943 local $self->_dbh->{RaiseError} = 1;
948 sub _seems_connected {
953 my $dbh = $self->_dbh
956 return $dbh->FETCH('Active');
962 my $dbh = $self->_dbh or return 0;
967 sub ensure_connected {
970 unless ($self->connected) {
971 $self->_populate_dbh;
977 Returns a C<$dbh> - a data base handle of class L<DBI>. The returned handle
978 is guaranteed to be healthy by implicitly calling L</connected>, and if
979 necessary performing a reconnection before returning. Keep in mind that this
980 is very B<expensive> on some database engines. Consider using L</dbh_do>
988 if (not $self->_dbh) {
989 $self->_populate_dbh;
991 $self->ensure_connected;
996 # this is the internal "get dbh or connect (don't check)" method
1000 $self->_populate_dbh unless $self->_dbh;
1006 unless ($self->_sql_maker) {
1007 my $sql_maker_class = $self->sql_maker_class;
1009 my %opts = %{$self->_sql_maker_opts||{}};
1011 $opts{limit_dialect}
1013 $self->sql_limit_dialect
1016 my $s_class = (ref $self) || $self;
1018 "Your storage class ($s_class) does not set sql_limit_dialect and you "
1019 . 'have not supplied an explicit limit_dialect in your connection_info. '
1020 . 'DBIC will attempt to use the GenericSubQ dialect, which works on most '
1021 . 'databases but can be (and often is) painfully slow. '
1022 . "Please file an RT ticket against '$s_class' ."
1029 my ($quote_char, $name_sep);
1031 if ($opts{quote_names}) {
1032 $quote_char = (delete $opts{quote_char}) || $self->sql_quote_char || do {
1033 my $s_class = (ref $self) || $self;
1035 "You requested 'quote_names' but your storage class ($s_class) does "
1036 . 'not explicitly define a default sql_quote_char and you have not '
1037 . 'supplied a quote_char as part of your connection_info. DBIC will '
1038 .q{default to the ANSI SQL standard quote '"', which works most of }
1039 . "the time. Please file an RT ticket against '$s_class'."
1045 $name_sep = (delete $opts{name_sep}) || $self->sql_name_sep;
1048 $self->_sql_maker($sql_maker_class->new(
1049 bindtype=>'columns',
1050 array_datatypes => 1,
1051 limit_dialect => $dialect,
1052 ($quote_char ? (quote_char => $quote_char) : ()),
1053 name_sep => ($name_sep || '.'),
1057 return $self->_sql_maker;
1060 # nothing to do by default
1067 my @info = @{$self->_dbi_connect_info || []};
1068 $self->_dbh(undef); # in case ->connected failed we might get sent here
1069 $self->_dbh_details({}); # reset everything we know
1071 $self->_dbh($self->_connect(@info));
1073 $self->_conn_pid($$) if $^O ne 'MSWin32'; # on win32 these are in fact threads
1075 $self->_determine_driver;
1077 # Always set the transaction depth on connect, since
1078 # there is no transaction in progress by definition
1079 $self->{transaction_depth} = $self->_dbh_autocommit ? 0 : 1;
1081 $self->_run_connection_actions unless $self->{_in_determine_driver};
1084 sub _run_connection_actions {
1088 push @actions, ( $self->on_connect_call || () );
1089 push @actions, $self->_parse_connect_do ('on_connect_do');
1091 $self->_do_connection_actions(connect_call_ => $_) for @actions;
1096 sub set_use_dbms_capability {
1097 $_[0]->set_inherited ($_[1], $_[2]);
1100 sub get_use_dbms_capability {
1101 my ($self, $capname) = @_;
1103 my $use = $self->get_inherited ($capname);
1106 : do { $capname =~ s/^_use_/_supports_/; $self->get_dbms_capability ($capname) }
1110 sub set_dbms_capability {
1111 $_[0]->_dbh_details->{capability}{$_[1]} = $_[2];
1114 sub get_dbms_capability {
1115 my ($self, $capname) = @_;
1117 my $cap = $self->_dbh_details->{capability}{$capname};
1119 unless (defined $cap) {
1120 if (my $meth = $self->can ("_determine$capname")) {
1121 $cap = $self->$meth ? 1 : 0;
1127 $self->set_dbms_capability ($capname, $cap);
1137 unless ($info = $self->_dbh_details->{info}) {
1141 my $server_version = try { $self->_get_server_version };
1143 if (defined $server_version) {
1144 $info->{dbms_version} = $server_version;
1146 my ($numeric_version) = $server_version =~ /^([\d\.]+)/;
1147 my @verparts = split (/\./, $numeric_version);
1153 # consider only up to 3 version parts, iff not more than 3 digits
1155 while (@verparts && @use_parts < 3) {
1156 my $p = shift @verparts;
1158 push @use_parts, $p;
1160 push @use_parts, 0 while @use_parts < 3;
1162 $info->{normalized_dbms_version} = sprintf "%d.%03d%03d", @use_parts;
1166 $self->_dbh_details->{info} = $info;
1172 sub _get_server_version {
1173 shift->_dbh_get_info(18);
1177 my ($self, $info) = @_;
1179 return try { $self->_get_dbh->get_info($info) } || undef;
1182 sub _determine_driver {
1185 if ((not $self->_driver_determined) && (not $self->{_in_determine_driver})) {
1186 my $started_connected = 0;
1187 local $self->{_in_determine_driver} = 1;
1189 if (ref($self) eq __PACKAGE__) {
1191 if ($self->_dbh) { # we are connected
1192 $driver = $self->_dbh->{Driver}{Name};
1193 $started_connected = 1;
1195 # if connect_info is a CODEREF, we have no choice but to connect
1196 if (ref $self->_dbi_connect_info->[0] &&
1197 reftype $self->_dbi_connect_info->[0] eq 'CODE') {
1198 $self->_populate_dbh;
1199 $driver = $self->_dbh->{Driver}{Name};
1202 # try to use dsn to not require being connected, the driver may still
1203 # force a connection in _rebless to determine version
1204 # (dsn may not be supplied at all if all we do is make a mock-schema)
1205 my $dsn = $self->_dbi_connect_info->[0] || $ENV{DBI_DSN} || '';
1206 ($driver) = $dsn =~ /dbi:([^:]+):/i;
1207 $driver ||= $ENV{DBI_DRIVER};
1212 my $storage_class = "DBIx::Class::Storage::DBI::${driver}";
1213 if ($self->load_optional_class($storage_class)) {
1214 mro::set_mro($storage_class, 'c3');
1215 bless $self, $storage_class;
1221 $self->_driver_determined(1);
1223 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
1225 $self->_init; # run driver-specific initializations
1227 $self->_run_connection_actions
1228 if !$started_connected && defined $self->_dbh;
1232 sub _do_connection_actions {
1234 my $method_prefix = shift;
1237 if (not ref($call)) {
1238 my $method = $method_prefix . $call;
1240 } elsif (ref($call) eq 'CODE') {
1242 } elsif (ref($call) eq 'ARRAY') {
1243 if (ref($call->[0]) ne 'ARRAY') {
1244 $self->_do_connection_actions($method_prefix, $_) for @$call;
1246 $self->_do_connection_actions($method_prefix, @$_) for @$call;
1249 $self->throw_exception (sprintf ("Don't know how to process conection actions of type '%s'", ref($call)) );
1255 sub connect_call_do_sql {
1257 $self->_do_query(@_);
1260 sub disconnect_call_do_sql {
1262 $self->_do_query(@_);
1265 # override in db-specific backend when necessary
1266 sub connect_call_datetime_setup { 1 }
1269 my ($self, $action) = @_;
1271 if (ref $action eq 'CODE') {
1272 $action = $action->($self);
1273 $self->_do_query($_) foreach @$action;
1276 # Most debuggers expect ($sql, @bind), so we need to exclude
1277 # the attribute hash which is the second argument to $dbh->do
1278 # furthermore the bind values are usually to be presented
1279 # as named arrayref pairs, so wrap those here too
1280 my @do_args = (ref $action eq 'ARRAY') ? (@$action) : ($action);
1281 my $sql = shift @do_args;
1282 my $attrs = shift @do_args;
1283 my @bind = map { [ undef, $_ ] } @do_args;
1285 $self->_query_start($sql, @bind);
1286 $self->_get_dbh->do($sql, $attrs, @do_args);
1287 $self->_query_end($sql, @bind);
1294 my ($self, @info) = @_;
1296 $self->throw_exception("You failed to provide any connection info")
1299 my ($old_connect_via, $dbh);
1301 if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
1302 $old_connect_via = $DBI::connect_via;
1303 $DBI::connect_via = 'connect';
1307 if(ref $info[0] eq 'CODE') {
1308 $dbh = $info[0]->();
1311 $dbh = DBI->connect(@info);
1318 unless ($self->unsafe) {
1320 $self->throw_exception(
1321 'Refusing clobbering of {HandleError} installed on externally supplied '
1322 ."DBI handle $dbh. Either remove the handler or use the 'unsafe' attribute."
1323 ) if $dbh->{HandleError} and ref $dbh->{HandleError} ne '__DBIC__DBH__ERROR__HANDLER__';
1325 # Default via _default_dbi_connect_attributes is 1, hence it was an explicit
1326 # request, or an external handle. Complain and set anyway
1327 unless ($dbh->{RaiseError}) {
1328 carp( ref $info[0] eq 'CODE'
1330 ? "The 'RaiseError' of the externally supplied DBI handle is set to false. "
1331 ."DBIx::Class will toggle it back to true, unless the 'unsafe' connect "
1332 .'attribute has been supplied'
1334 : 'RaiseError => 0 supplied in your connection_info, without an explicit '
1335 .'unsafe => 1. Toggling RaiseError back to true'
1338 $dbh->{RaiseError} = 1;
1341 # this odd anonymous coderef dereference is in fact really
1342 # necessary to avoid the unwanted effect described in perl5
1345 my $weak_self = $_[0];
1348 # the coderef is blessed so we can distinguish it from externally
1349 # supplied handles (which must be preserved)
1350 $_[1]->{HandleError} = bless sub {
1352 $weak_self->throw_exception("DBI Exception: $_[0]");
1355 # the handler may be invoked by something totally out of
1357 croak ("DBI Exception (unhandled by DBIC, ::Schema GCed): $_[0]");
1359 }, '__DBIC__DBH__ERROR__HANDLER__';
1364 $self->throw_exception("DBI Connection failed: $_")
1367 $DBI::connect_via = $old_connect_via if $old_connect_via;
1370 $self->_dbh_autocommit($dbh->{AutoCommit});
1375 my ($self, $name) = @_;
1377 $name = $self->_svp_generate_name
1378 unless defined $name;
1380 $self->throw_exception ("You can't use savepoints outside a transaction")
1381 if $self->{transaction_depth} == 0;
1383 $self->throw_exception ("Your Storage implementation doesn't support savepoints")
1384 unless $self->can('_svp_begin');
1386 push @{ $self->{savepoints} }, $name;
1388 $self->debugobj->svp_begin($name) if $self->debug;
1390 return $self->_svp_begin($name);
1394 my ($self, $name) = @_;
1396 $self->throw_exception ("You can't use savepoints outside a transaction")
1397 if $self->{transaction_depth} == 0;
1399 $self->throw_exception ("Your Storage implementation doesn't support savepoints")
1400 unless $self->can('_svp_release');
1402 if (defined $name) {
1403 $self->throw_exception ("Savepoint '$name' does not exist")
1404 unless grep { $_ eq $name } @{ $self->{savepoints} };
1406 # Dig through the stack until we find the one we are releasing. This keeps
1407 # the stack up to date.
1410 do { $svp = pop @{ $self->{savepoints} } } while $svp ne $name;
1412 $name = pop @{ $self->{savepoints} };
1415 $self->debugobj->svp_release($name) if $self->debug;
1417 return $self->_svp_release($name);
1421 my ($self, $name) = @_;
1423 $self->throw_exception ("You can't use savepoints outside a transaction")
1424 if $self->{transaction_depth} == 0;
1426 $self->throw_exception ("Your Storage implementation doesn't support savepoints")
1427 unless $self->can('_svp_rollback');
1429 if (defined $name) {
1430 # If they passed us a name, verify that it exists in the stack
1431 unless(grep({ $_ eq $name } @{ $self->{savepoints} })) {
1432 $self->throw_exception("Savepoint '$name' does not exist!");
1435 # Dig through the stack until we find the one we are releasing. This keeps
1436 # the stack up to date.
1437 while(my $s = pop(@{ $self->{savepoints} })) {
1438 last if($s eq $name);
1440 # Add the savepoint back to the stack, as a rollback doesn't remove the
1441 # named savepoint, only everything after it.
1442 push(@{ $self->{savepoints} }, $name);
1444 # We'll assume they want to rollback to the last savepoint
1445 $name = $self->{savepoints}->[-1];
1448 $self->debugobj->svp_rollback($name) if $self->debug;
1450 return $self->_svp_rollback($name);
1453 sub _svp_generate_name {
1455 return 'savepoint_'.scalar(@{ $self->{'savepoints'} });
1461 # this means we have not yet connected and do not know the AC status
1462 # (e.g. coderef $dbh)
1463 if (! defined $self->_dbh_autocommit) {
1464 $self->ensure_connected;
1466 # otherwise re-connect on pid changes, so
1467 # that the txn_depth is adjusted properly
1468 # the lightweight _get_dbh is good enoug here
1469 # (only superficial handle check, no pings)
1474 if($self->transaction_depth == 0) {
1475 $self->debugobj->txn_begin()
1477 $self->_dbh_begin_work;
1479 elsif ($self->auto_savepoint) {
1482 $self->{transaction_depth}++;
1485 sub _dbh_begin_work {
1488 # if the user is utilizing txn_do - good for him, otherwise we need to
1489 # ensure that the $dbh is healthy on BEGIN.
1490 # We do this via ->dbh_do instead of ->dbh, so that the ->dbh "ping"
1491 # will be replaced by a failure of begin_work itself (which will be
1492 # then retried on reconnect)
1493 if ($self->{_in_dbh_do}) {
1494 $self->_dbh->begin_work;
1496 $self->dbh_do(sub { $_[1]->begin_work });
1502 if (! $self->_dbh) {
1503 $self->throw_exception('cannot COMMIT on a disconnected handle');
1505 elsif ($self->{transaction_depth} == 1) {
1506 $self->debugobj->txn_commit()
1509 $self->{transaction_depth} = 0
1510 if $self->_dbh_autocommit;
1512 elsif($self->{transaction_depth} > 1) {
1513 $self->{transaction_depth}--;
1515 if $self->auto_savepoint;
1517 elsif (! $self->_dbh->FETCH('AutoCommit') ) {
1519 carp "Storage transaction_depth $self->{transaction_depth} does not match "
1520 ."false AutoCommit of $self->{_dbh}, attempting COMMIT anyway";
1522 $self->debugobj->txn_commit()
1525 $self->{transaction_depth} = 0
1526 if $self->_dbh_autocommit;
1529 $self->throw_exception( 'Refusing to commit without a started transaction' );
1535 my $dbh = $self->_dbh
1536 or $self->throw_exception('cannot COMMIT on a disconnected handle');
1542 my $dbh = $self->_dbh;
1544 if ($self->{transaction_depth} == 1) {
1545 $self->debugobj->txn_rollback()
1547 $self->{transaction_depth} = 0
1548 if $self->_dbh_autocommit;
1549 $self->_dbh_rollback;
1551 elsif($self->{transaction_depth} > 1) {
1552 $self->{transaction_depth}--;
1553 if ($self->auto_savepoint) {
1554 $self->svp_rollback;
1559 die DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION->new;
1563 my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
1565 if ($_ !~ /$exception_class/) {
1566 # ensure that a failed rollback resets the transaction depth
1567 $self->{transaction_depth} = $self->_dbh_autocommit ? 0 : 1;
1570 $self->throw_exception($_)
1576 my $dbh = $self->_dbh
1577 or $self->throw_exception('cannot ROLLBACK on a disconnected handle');
1581 # This used to be the top-half of _execute. It was split out to make it
1582 # easier to override in NoBindVars without duping the rest. It takes up
1583 # all of _execute's args, and emits $sql, @bind.
1584 sub _prep_for_execute {
1585 my ($self, $op, $extra_bind, $ident, $args) = @_;
1587 if( blessed $ident && $ident->isa("DBIx::Class::ResultSource") ) {
1588 $ident = $ident->from();
1591 my ($sql, @bind) = $self->sql_maker->$op($ident, @$args);
1594 map { ref $_ eq 'ARRAY' ? $_ : [ '!!dummy', $_ ] } @$extra_bind)
1596 return ($sql, \@bind);
1600 sub _fix_bind_params {
1601 my ($self, @bind) = @_;
1603 ### Turn @bind from something like this:
1604 ### ( [ "artist", 1 ], [ "cdid", 1, 3 ] )
1606 ### ( "'1'", "'1'", "'3'" )
1609 if ( defined( $_ && $_->[1] ) ) {
1610 map { qq{'$_'}; } @{$_}[ 1 .. $#$_ ];
1617 my ( $self, $sql, @bind ) = @_;
1619 if ( $self->debug ) {
1620 @bind = $self->_fix_bind_params(@bind);
1622 $self->debugobj->query_start( $sql, @bind );
1627 my ( $self, $sql, @bind ) = @_;
1629 if ( $self->debug ) {
1630 @bind = $self->_fix_bind_params(@bind);
1631 $self->debugobj->query_end( $sql, @bind );
1636 my ($self, $dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
1638 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
1640 $self->_query_start( $sql, @$bind );
1642 my $sth = $self->sth($sql,$op);
1644 my $placeholder_index = 1;
1646 foreach my $bound (@$bind) {
1647 my $attributes = {};
1648 my($column_name, @data) = @$bound;
1650 if ($bind_attributes) {
1651 $attributes = $bind_attributes->{$column_name}
1652 if defined $bind_attributes->{$column_name};
1655 foreach my $data (@data) {
1656 my $ref = ref $data;
1658 if ($ref and overload::Method($data, '""') ) {
1661 elsif ($ref eq 'SCALAR') { # any scalarrefs are assumed to be bind_inouts
1662 $sth->bind_param_inout(
1663 $placeholder_index++,
1665 $self->_max_column_bytesize($ident, $column_name),
1671 $sth->bind_param($placeholder_index++, $data, $attributes);
1675 # Can this fail without throwing an exception anyways???
1676 my $rv = $sth->execute();
1677 $self->throw_exception(
1678 $sth->errstr || $sth->err || 'Unknown error: execute() returned false, but error flags were not set...'
1681 $self->_query_end( $sql, @$bind );
1683 return (wantarray ? ($rv, $sth, @$bind) : $rv);
1688 $self->dbh_do('_dbh_execute', @_); # retry over disconnects
1691 sub _prefetch_autovalues {
1692 my ($self, $source, $to_insert) = @_;
1694 my $colinfo = $source->columns_info;
1697 for my $col (keys %$colinfo) {
1699 $colinfo->{$col}{auto_nextval}
1702 ! exists $to_insert->{$col}
1704 ref $to_insert->{$col} eq 'SCALAR'
1707 $values{$col} = $self->_sequence_fetch(
1709 ( $colinfo->{$col}{sequence} ||=
1710 $self->_dbh_get_autoinc_seq($self->_get_dbh, $source, $col)
1720 my ($self, $source, $to_insert) = @_;
1722 my $prefetched_values = $self->_prefetch_autovalues($source, $to_insert);
1725 $to_insert = { %$to_insert, %$prefetched_values };
1727 # list of primary keys we try to fetch from the database
1728 # both not-exsists and scalarrefs are considered
1730 for ($source->primary_columns) {
1731 $fetch_pks{$_} = scalar keys %fetch_pks # so we can preserve order for prettyness
1732 if ! exists $to_insert->{$_} or ref $to_insert->{$_} eq 'SCALAR';
1735 my ($sqla_opts, @ir_container);
1736 if ($self->_use_insert_returning) {
1738 # retain order as declared in the resultsource
1739 for (sort { $fetch_pks{$a} <=> $fetch_pks{$b} } keys %fetch_pks ) {
1740 push @{$sqla_opts->{returning}}, $_;
1741 $sqla_opts->{returning_container} = \@ir_container
1742 if $self->_use_insert_returning_bound;
1746 my $bind_attributes = $self->source_bind_attributes($source);
1748 my ($rv, $sth) = $self->_execute('insert' => [], $source, $bind_attributes, $to_insert, $sqla_opts);
1752 if (my $retlist = $sqla_opts->{returning}) {
1753 @ir_container = try {
1754 local $SIG{__WARN__} = sub {};
1755 my @r = $sth->fetchrow_array;
1758 } unless @ir_container;
1760 @returned_cols{@$retlist} = @ir_container if @ir_container;
1763 return { %$prefetched_values, %returned_cols };
1767 ## Currently it is assumed that all values passed will be "normal", i.e. not
1768 ## scalar refs, or at least, all the same type as the first set, the statement is
1769 ## only prepped once.
1771 my ($self, $source, $cols, $data) = @_;
1774 @colvalues{@$cols} = (0..$#$cols);
1776 for my $i (0..$#$cols) {
1777 my $first_val = $data->[0][$i];
1778 next unless ref $first_val eq 'SCALAR';
1780 $colvalues{ $cols->[$i] } = $first_val;
1783 # check for bad data and stringify stringifiable objects
1784 my $bad_slice = sub {
1785 my ($msg, $col_idx, $slice_idx) = @_;
1786 $self->throw_exception(sprintf "%s for column '%s' in populate slice:\n%s",
1790 require Data::Dumper::Concise;
1791 local $Data::Dumper::Maxdepth = 1; # don't dump objects, if any
1792 Data::Dumper::Concise::Dumper ({
1793 map { $cols->[$_] => $data->[$slice_idx][$_] } (0 .. $#$cols)
1799 for my $datum_idx (0..$#$data) {
1800 my $datum = $data->[$datum_idx];
1802 for my $col_idx (0..$#$cols) {
1803 my $val = $datum->[$col_idx];
1804 my $sqla_bind = $colvalues{ $cols->[$col_idx] };
1805 my $is_literal_sql = (ref $sqla_bind) eq 'SCALAR';
1807 if ($is_literal_sql) {
1809 $bad_slice->('bind found where literal SQL expected', $col_idx, $datum_idx);
1811 elsif ((my $reftype = ref $val) ne 'SCALAR') {
1812 $bad_slice->("$reftype reference found where literal SQL expected",
1813 $col_idx, $datum_idx);
1815 elsif ($$val ne $$sqla_bind){
1816 $bad_slice->("inconsistent literal SQL value, expecting: '$$sqla_bind'",
1817 $col_idx, $datum_idx);
1820 elsif (my $reftype = ref $val) {
1822 if (overload::Method($val, '""')) {
1823 $datum->[$col_idx] = "".$val;
1826 $bad_slice->("$reftype reference found where bind expected",
1827 $col_idx, $datum_idx);
1833 return $self->_insert_bulk(
1834 $source, $cols, \%colvalues, $data,
1838 # Broken out so that it can be overridden in Storage/DBI/mysql.pm
1840 my ($self, $source, $cols, $colvalues, $data) = @_;
1842 my ($sql, $bind) = $self->_prep_for_execute (
1843 'insert', undef, $source, [$colvalues]
1847 # if the bindlist is empty - make sure all "values" are in fact
1848 # literal scalarrefs. If not the case this means the storage ate
1849 # them away (e.g. the NoBindVars component) and interpolated them
1850 # directly into the SQL. This obviosly can't be good for multi-inserts
1852 $self->throw_exception('Cannot insert_bulk without support for placeholders')
1853 if first { ref $_ ne 'SCALAR' } values %$colvalues;
1856 # neither _execute_array, nor _execute_inserts_with_no_binds are
1857 # atomic (even if _execute _array is a single call). Thus a safety
1859 my $guard = $self->txn_scope_guard;
1861 $self->_query_start( $sql, @$bind ? [ dummy => '__BULK_INSERT__' ] : () );
1862 my $sth = $self->sth($sql);
1865 #@bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
1866 $self->_execute_array( $source, $sth, $bind, $cols, $data );
1869 # bind_param_array doesn't work if there are no binds
1870 $self->_dbh_execute_inserts_with_no_binds( $sth, scalar @$data );
1874 $self->_query_end( $sql, @$bind ? [ dummy => '__BULK_INSERT__' ] : () );
1878 return (wantarray ? ($rv, $sth, @$bind) : $rv);
1881 sub _execute_array {
1882 my ($self, $source, $sth, $bind, $cols, $data, @extra) = @_;
1884 ## This must be an arrayref, else nothing works!
1885 my $tuple_status = [];
1887 ## Get the bind_attributes, if any exist
1888 my $bind_attributes = $self->source_bind_attributes($source);
1890 ## Bind the values and execute
1891 my $placeholder_index = 1;
1893 foreach my $bound (@$bind) {
1895 my $attributes = {};
1896 my ($column_name, $data_index) = @$bound;
1898 if( $bind_attributes ) {
1899 $attributes = $bind_attributes->{$column_name}
1900 if defined $bind_attributes->{$column_name};
1903 my @data = map { $_->[$data_index] } @$data;
1905 $sth->bind_param_array(
1908 (%$attributes ? $attributes : ()),
1910 $placeholder_index++;
1915 $rv = $self->_dbh_execute_array($sth, $tuple_status, @extra);
1921 # Not all DBDs are create equal. Some throw on error, some return
1922 # an undef $rv, and some set $sth->err - try whatever we can
1923 $err = ($sth->errstr || 'UNKNOWN ERROR ($sth->errstr is unset)') if (
1926 ( !defined $rv or $sth->err )
1929 # Statement must finish even if there was an exception.
1934 $err = shift unless defined $err
1939 ++$i while $i <= $#$tuple_status && !ref $tuple_status->[$i];
1941 $self->throw_exception("Unexpected populate error: $err")
1942 if ($i > $#$tuple_status);
1944 require Data::Dumper::Concise;
1945 $self->throw_exception(sprintf "%s for populate slice:\n%s",
1946 ($tuple_status->[$i][1] || $err),
1947 Data::Dumper::Concise::Dumper( { map { $cols->[$_] => $data->[$i][$_] } (0 .. $#$cols) } ),
1954 sub _dbh_execute_array {
1955 my ($self, $sth, $tuple_status, @extra) = @_;
1957 return $sth->execute_array({ArrayTupleStatus => $tuple_status});
1960 sub _dbh_execute_inserts_with_no_binds {
1961 my ($self, $sth, $count) = @_;
1965 my $dbh = $self->_get_dbh;
1966 local $dbh->{RaiseError} = 1;
1967 local $dbh->{PrintError} = 0;
1969 $sth->execute foreach 1..$count;
1975 # Make sure statement is finished even if there was an exception.
1980 $err = shift unless defined $err;
1983 $self->throw_exception($err) if defined $err;
1989 my ($self, $source, @args) = @_;
1991 my $bind_attrs = $self->source_bind_attributes($source);
1993 return $self->_execute('update' => [], $source, $bind_attrs, @args);
1998 my ($self, $source, @args) = @_;
2000 my $bind_attrs = $self->source_bind_attributes($source);
2002 return $self->_execute('delete' => [], $source, $bind_attrs, @args);
2005 # We were sent here because the $rs contains a complex search
2006 # which will require a subquery to select the correct rows
2007 # (i.e. joined or limited resultsets, or non-introspectable conditions)
2009 # Generating a single PK column subquery is trivial and supported
2010 # by all RDBMS. However if we have a multicolumn PK, things get ugly.
2011 # Look at _multipk_update_delete()
2012 sub _subq_update_delete {
2014 my ($rs, $op, $values) = @_;
2016 my $rsrc = $rs->result_source;
2018 # quick check if we got a sane rs on our hands
2019 my @pcols = $rsrc->_pri_cols;
2021 my $sel = $rs->_resolved_attrs->{select};
2022 $sel = [ $sel ] unless ref $sel eq 'ARRAY';
2025 join ("\x00", map { join '.', $rs->{attrs}{alias}, $_ } sort @pcols)
2027 join ("\x00", sort @$sel )
2029 $self->throw_exception (
2030 '_subq_update_delete can not be called on resultsets selecting columns other than the primary keys'
2037 $op eq 'update' ? $values : (),
2038 { $pcols[0] => { -in => $rs->as_query } },
2043 return $self->_multipk_update_delete (@_);
2047 # ANSI SQL does not provide a reliable way to perform a multicol-PK
2048 # resultset update/delete involving subqueries. So by default resort
2049 # to simple (and inefficient) delete_all style per-row opearations,
2050 # while allowing specific storages to override this with a faster
2053 sub _multipk_update_delete {
2054 return shift->_per_row_update_delete (@_);
2057 # This is the default loop used to delete/update rows for multi PK
2058 # resultsets, and used by mysql exclusively (because it can't do anything
2061 # We do not use $row->$op style queries, because resultset update/delete
2062 # is not expected to cascade (this is what delete_all/update_all is for).
2064 # There should be no race conditions as the entire operation is rolled
2067 sub _per_row_update_delete {
2069 my ($rs, $op, $values) = @_;
2071 my $rsrc = $rs->result_source;
2072 my @pcols = $rsrc->_pri_cols;
2074 my $guard = $self->txn_scope_guard;
2076 # emulate the return value of $sth->execute for non-selects
2077 my $row_cnt = '0E0';
2079 my $subrs_cur = $rs->cursor;
2080 my @all_pk = $subrs_cur->all;
2081 for my $pks ( @all_pk) {
2084 for my $i (0.. $#pcols) {
2085 $cond->{$pcols[$i]} = $pks->[$i];
2090 $op eq 'update' ? $values : (),
2104 $self->_execute($self->_select_args(@_));
2107 sub _select_args_to_query {
2110 # my ($op, $bind, $ident, $bind_attrs, $select, $cond, $rs_attrs, $rows, $offset)
2111 # = $self->_select_args($ident, $select, $cond, $attrs);
2112 my ($op, $bind, $ident, $bind_attrs, @args) =
2113 $self->_select_args(@_);
2115 # my ($sql, $prepared_bind) = $self->_prep_for_execute($op, $bind, $ident, [ $select, $cond, $rs_attrs, $rows, $offset ]);
2116 my ($sql, $prepared_bind) = $self->_prep_for_execute($op, $bind, $ident, \@args);
2117 $prepared_bind ||= [];
2120 ? ($sql, $prepared_bind, $bind_attrs)
2121 : \[ "($sql)", @$prepared_bind ]
2126 my ($self, $ident, $select, $where, $attrs) = @_;
2128 my $sql_maker = $self->sql_maker;
2129 my ($alias2source, $rs_alias) = $self->_resolve_ident_sources ($ident);
2136 $rs_alias && $alias2source->{$rs_alias}
2137 ? ( _rsroot_rsrc => $alias2source->{$rs_alias} )
2142 # calculate bind_attrs before possible $ident mangling
2143 my $bind_attrs = {};
2144 for my $alias (keys %$alias2source) {
2145 my $bindtypes = $self->source_bind_attributes ($alias2source->{$alias}) || {};
2146 for my $col (keys %$bindtypes) {
2148 my $fqcn = join ('.', $alias, $col);
2149 $bind_attrs->{$fqcn} = $bindtypes->{$col} if $bindtypes->{$col};
2151 # Unqialified column names are nice, but at the same time can be
2152 # rather ambiguous. What we do here is basically go along with
2153 # the loop, adding an unqualified column slot to $bind_attrs,
2154 # alongside the fully qualified name. As soon as we encounter
2155 # another column by that name (which would imply another table)
2156 # we unset the unqualified slot and never add any info to it
2157 # to avoid erroneous type binding. If this happens the users
2158 # only choice will be to fully qualify his column name
2160 if (exists $bind_attrs->{$col}) {
2161 $bind_attrs->{$col} = {};
2164 $bind_attrs->{$col} = $bind_attrs->{$fqcn};
2169 # Sanity check the attributes (SQLMaker does it too, but
2170 # in case of a software_limit we'll never reach there)
2171 if (defined $attrs->{offset}) {
2172 $self->throw_exception('A supplied offset attribute must be a non-negative integer')
2173 if ( $attrs->{offset} =~ /\D/ or $attrs->{offset} < 0 );
2175 $attrs->{offset} ||= 0;
2177 if (defined $attrs->{rows}) {
2178 $self->throw_exception("The rows attribute must be a positive integer if present")
2179 if ( $attrs->{rows} =~ /\D/ or $attrs->{rows} <= 0 );
2181 elsif ($attrs->{offset}) {
2182 # MySQL actually recommends this approach. I cringe.
2183 $attrs->{rows} = $sql_maker->__max_int;
2188 # see if we need to tear the prefetch apart otherwise delegate the limiting to the
2189 # storage, unless software limit was requested
2192 ( $attrs->{rows} && keys %{$attrs->{collapse}} )
2194 # grouped prefetch (to satisfy group_by == select)
2195 ( $attrs->{group_by}
2197 @{$attrs->{group_by}}
2199 $attrs->{_prefetch_selector_range}
2202 ($ident, $select, $where, $attrs)
2203 = $self->_adjust_select_args_for_complex_prefetch ($ident, $select, $where, $attrs);
2205 elsif (! $attrs->{software_limit} ) {
2206 push @limit, $attrs->{rows}, $attrs->{offset};
2209 # try to simplify the joinmap further (prune unreferenced type-single joins)
2210 $ident = $self->_prune_unused_joins ($ident, $select, $where, $attrs);
2213 # This would be the point to deflate anything found in $where
2214 # (and leave $attrs->{bind} intact). Problem is - inflators historically
2215 # expect a row object. And all we have is a resultsource (it is trivial
2216 # to extract deflator coderefs via $alias2source above).
2218 # I don't see a way forward other than changing the way deflators are
2219 # invoked, and that's just bad...
2222 return ('select', $attrs->{bind}, $ident, $bind_attrs, $select, $where, $attrs, @limit);
2225 # Returns a counting SELECT for a simple count
2226 # query. Abstracted so that a storage could override
2227 # this to { count => 'firstcol' } or whatever makes
2228 # sense as a performance optimization
2230 #my ($self, $source, $rs_attrs) = @_;
2231 return { count => '*' };
2235 sub source_bind_attributes {
2236 my ($self, $source) = @_;
2238 my $bind_attributes;
2240 my $colinfo = $source->columns_info;
2242 for my $col (keys %$colinfo) {
2243 if (my $dt = $colinfo->{$col}{data_type} ) {
2244 $bind_attributes->{$col} = $self->bind_attribute_by_data_type($dt)
2248 return $bind_attributes;
2255 =item Arguments: $ident, $select, $condition, $attrs
2259 Handle a SQL select statement.
2265 my ($ident, $select, $condition, $attrs) = @_;
2266 return $self->cursor_class->new($self, \@_, $attrs);
2271 my ($rv, $sth, @bind) = $self->_select(@_);
2272 my @row = $sth->fetchrow_array;
2273 my @nextrow = $sth->fetchrow_array if @row;
2274 if(@row && @nextrow) {
2275 carp "Query returned more than one row. SQL that returns multiple rows is DEPRECATED for ->find and ->single";
2277 # Need to call finish() to work round broken DBDs
2282 =head2 sql_limit_dialect
2284 This is an accessor for the default SQL limit dialect used by a particular
2285 storage driver. Can be overridden by supplying an explicit L</limit_dialect>
2286 to L<DBIx::Class::Schema/connect>. For a list of available limit dialects
2287 see L<DBIx::Class::SQLMaker::LimitDialects>.
2293 =item Arguments: $sql
2297 Returns a L<DBI> sth (statement handle) for the supplied SQL.
2302 my ($self, $dbh, $sql) = @_;
2304 # 3 is the if_active parameter which avoids active sth re-use
2305 my $sth = $self->disable_sth_caching
2306 ? $dbh->prepare($sql)
2307 : $dbh->prepare_cached($sql, {}, 3);
2309 # XXX You would think RaiseError would make this impossible,
2310 # but apparently that's not true :(
2311 $self->throw_exception(
2314 sprintf( "\$dbh->prepare() of '%s' through %s failed *silently* without "
2315 .'an exception and/or setting $dbh->errstr',
2317 ? substr($sql, 0, 20) . '...'
2320 'DBD::' . $dbh->{Driver}{Name},
2328 my ($self, $sql) = @_;
2329 $self->dbh_do('_dbh_sth', $sql); # retry over disconnects
2332 sub _dbh_columns_info_for {
2333 my ($self, $dbh, $table) = @_;
2335 if ($dbh->can('column_info')) {
2339 my ($schema,$tab) = $table =~ /^(.+?)\.(.+)$/ ? ($1,$2) : (undef,$table);
2340 my $sth = $dbh->column_info( undef,$schema, $tab, '%' );
2342 while ( my $info = $sth->fetchrow_hashref() ){
2344 $column_info{data_type} = $info->{TYPE_NAME};
2345 $column_info{size} = $info->{COLUMN_SIZE};
2346 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
2347 $column_info{default_value} = $info->{COLUMN_DEF};
2348 my $col_name = $info->{COLUMN_NAME};
2349 $col_name =~ s/^\"(.*)\"$/$1/;
2351 $result{$col_name} = \%column_info;
2356 return \%result if !$caught && scalar keys %result;
2360 my $sth = $dbh->prepare($self->sql_maker->select($table, undef, \'1 = 0'));
2362 my @columns = @{$sth->{NAME_lc}};
2363 for my $i ( 0 .. $#columns ){
2365 $column_info{data_type} = $sth->{TYPE}->[$i];
2366 $column_info{size} = $sth->{PRECISION}->[$i];
2367 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
2369 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
2370 $column_info{data_type} = $1;
2371 $column_info{size} = $2;
2374 $result{$columns[$i]} = \%column_info;
2378 foreach my $col (keys %result) {
2379 my $colinfo = $result{$col};
2380 my $type_num = $colinfo->{data_type};
2382 if(defined $type_num && $dbh->can('type_info')) {
2383 my $type_info = $dbh->type_info($type_num);
2384 $type_name = $type_info->{TYPE_NAME} if $type_info;
2385 $colinfo->{data_type} = $type_name if $type_name;
2392 sub columns_info_for {
2393 my ($self, $table) = @_;
2394 $self->_dbh_columns_info_for ($self->_get_dbh, $table);
2397 =head2 last_insert_id
2399 Return the row id of the last insert.
2403 sub _dbh_last_insert_id {
2404 my ($self, $dbh, $source, $col) = @_;
2406 my $id = try { $dbh->last_insert_id (undef, undef, $source->name, $col) };
2408 return $id if defined $id;
2410 my $class = ref $self;
2411 $self->throw_exception ("No storage specific _dbh_last_insert_id() method implemented in $class, and the generic DBI::last_insert_id() failed");
2414 sub last_insert_id {
2416 $self->_dbh_last_insert_id ($self->_dbh, @_);
2419 =head2 _native_data_type
2423 =item Arguments: $type_name
2427 This API is B<EXPERIMENTAL>, will almost definitely change in the future, and
2428 currently only used by L<::AutoCast|DBIx::Class::Storage::DBI::AutoCast> and
2429 L<::Sybase::ASE|DBIx::Class::Storage::DBI::Sybase::ASE>.
2431 The default implementation returns C<undef>, implement in your Storage driver if
2432 you need this functionality.
2434 Should map types from other databases to the native RDBMS type, for example
2435 C<VARCHAR2> to C<VARCHAR>.
2437 Types with modifiers should map to the underlying data type. For example,
2438 C<INTEGER AUTO_INCREMENT> should become C<INTEGER>.
2440 Composite types should map to the container type, for example
2441 C<ENUM(foo,bar,baz)> becomes C<ENUM>.
2445 sub _native_data_type {
2446 #my ($self, $data_type) = @_;
2450 # Check if placeholders are supported at all
2451 sub _determine_supports_placeholders {
2453 my $dbh = $self->_get_dbh;
2455 # some drivers provide a $dbh attribute (e.g. Sybase and $dbh->{syb_dynamic_supported})
2456 # but it is inaccurate more often than not
2458 local $dbh->{PrintError} = 0;
2459 local $dbh->{RaiseError} = 1;
2460 $dbh->do('select ?', {}, 1);
2468 # Check if placeholders bound to non-string types throw exceptions
2470 sub _determine_supports_typeless_placeholders {
2472 my $dbh = $self->_get_dbh;
2475 local $dbh->{PrintError} = 0;
2476 local $dbh->{RaiseError} = 1;
2477 # this specifically tests a bind that is NOT a string
2478 $dbh->do('select 1 where 1 = ?', {}, 1);
2488 Returns the database driver name.
2493 shift->_get_dbh->{Driver}->{Name};
2496 =head2 bind_attribute_by_data_type
2498 Given a datatype from column info, returns a database specific bind
2499 attribute for C<< $dbh->bind_param($val,$attribute) >> or nothing if we will
2500 let the database planner just handle it.
2502 Generally only needed for special case column types, like bytea in postgres.
2506 sub bind_attribute_by_data_type {
2510 =head2 is_datatype_numeric
2512 Given a datatype from column_info, returns a boolean value indicating if
2513 the current RDBMS considers it a numeric value. This controls how
2514 L<DBIx::Class::Row/set_column> decides whether to mark the column as
2515 dirty - when the datatype is deemed numeric a C<< != >> comparison will
2516 be performed instead of the usual C<eq>.
2520 sub is_datatype_numeric {
2521 my ($self, $dt) = @_;
2523 return 0 unless $dt;
2525 return $dt =~ /^ (?:
2526 numeric | int(?:eger)? | (?:tiny|small|medium|big)int | dec(?:imal)? | real | float | double (?: \s+ precision)? | (?:big)?serial
2531 =head2 create_ddl_dir
2535 =item Arguments: $schema \@databases, $version, $directory, $preversion, \%sqlt_args
2539 Creates a SQL file based on the Schema, for each of the specified
2540 database engines in C<\@databases> in the given directory.
2541 (note: specify L<SQL::Translator> names, not L<DBI> driver names).
2543 Given a previous version number, this will also create a file containing
2544 the ALTER TABLE statements to transform the previous schema into the
2545 current one. Note that these statements may contain C<DROP TABLE> or
2546 C<DROP COLUMN> statements that can potentially destroy data.
2548 The file names are created using the C<ddl_filename> method below, please
2549 override this method in your schema if you would like a different file
2550 name format. For the ALTER file, the same format is used, replacing
2551 $version in the name with "$preversion-$version".
2553 See L<SQL::Translator/METHODS> for a list of values for C<\%sqlt_args>.
2554 The most common value for this would be C<< { add_drop_table => 1 } >>
2555 to have the SQL produced include a C<DROP TABLE> statement for each table
2556 created. For quoting purposes supply C<quote_table_names> and
2557 C<quote_field_names>.
2559 If no arguments are passed, then the following default values are assumed:
2563 =item databases - ['MySQL', 'SQLite', 'PostgreSQL']
2565 =item version - $schema->schema_version
2567 =item directory - './'
2569 =item preversion - <none>
2573 By default, C<\%sqlt_args> will have
2575 { add_drop_table => 1, ignore_constraint_names => 1, ignore_index_names => 1 }
2577 merged with the hash passed in. To disable any of those features, pass in a
2578 hashref like the following
2580 { ignore_constraint_names => 0, # ... other options }
2583 WARNING: You are strongly advised to check all SQL files created, before applying
2588 sub create_ddl_dir {
2589 my ($self, $schema, $databases, $version, $dir, $preversion, $sqltargs) = @_;
2592 carp "No directory given, using ./\n";
2597 (require File::Path and File::Path::make_path ("$dir")) # make_path does not like objects (i.e. Path::Class::Dir)
2599 $self->throw_exception(
2600 "Failed to create '$dir': " . ($! || $@ || 'error unknown')
2604 $self->throw_exception ("Directory '$dir' does not exist\n") unless(-d $dir);
2606 $databases ||= ['MySQL', 'SQLite', 'PostgreSQL'];
2607 $databases = [ $databases ] if(ref($databases) ne 'ARRAY');
2609 my $schema_version = $schema->schema_version || '1.x';
2610 $version ||= $schema_version;
2613 add_drop_table => 1,
2614 ignore_constraint_names => 1,
2615 ignore_index_names => 1,
2619 unless (DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')) {
2620 $self->throw_exception("Can't create a ddl file without " . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') );
2623 my $sqlt = SQL::Translator->new( $sqltargs );
2625 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
2626 my $sqlt_schema = $sqlt->translate({ data => $schema })
2627 or $self->throw_exception ($sqlt->error);
2629 foreach my $db (@$databases) {
2631 $sqlt->{schema} = $sqlt_schema;
2632 $sqlt->producer($db);
2635 my $filename = $schema->ddl_filename($db, $version, $dir);
2636 if (-e $filename && ($version eq $schema_version )) {
2637 # if we are dumping the current version, overwrite the DDL
2638 carp "Overwriting existing DDL file - $filename";
2642 my $output = $sqlt->translate;
2644 carp("Failed to translate to $db, skipping. (" . $sqlt->error . ")");
2647 if(!open($file, ">$filename")) {
2648 $self->throw_exception("Can't open $filename for writing ($!)");
2651 print $file $output;
2654 next unless ($preversion);
2656 require SQL::Translator::Diff;
2658 my $prefilename = $schema->ddl_filename($db, $preversion, $dir);
2659 if(!-e $prefilename) {
2660 carp("No previous schema file found ($prefilename)");
2664 my $difffile = $schema->ddl_filename($db, $version, $dir, $preversion);
2666 carp("Overwriting existing diff file - $difffile");
2672 my $t = SQL::Translator->new($sqltargs);
2677 or $self->throw_exception ($t->error);
2679 my $out = $t->translate( $prefilename )
2680 or $self->throw_exception ($t->error);
2682 $source_schema = $t->schema;
2684 $source_schema->name( $prefilename )
2685 unless ( $source_schema->name );
2688 # The "new" style of producers have sane normalization and can support
2689 # diffing a SQL file against a DBIC->SQLT schema. Old style ones don't
2690 # And we have to diff parsed SQL against parsed SQL.
2691 my $dest_schema = $sqlt_schema;
2693 unless ( "SQL::Translator::Producer::$db"->can('preprocess_schema') ) {
2694 my $t = SQL::Translator->new($sqltargs);
2699 or $self->throw_exception ($t->error);
2701 my $out = $t->translate( $filename )
2702 or $self->throw_exception ($t->error);
2704 $dest_schema = $t->schema;
2706 $dest_schema->name( $filename )
2707 unless $dest_schema->name;
2710 my $diff = SQL::Translator::Diff::schema_diff($source_schema, $db,
2714 if(!open $file, ">$difffile") {
2715 $self->throw_exception("Can't write to $difffile ($!)");
2723 =head2 deployment_statements
2727 =item Arguments: $schema, $type, $version, $directory, $sqlt_args
2731 Returns the statements used by L</deploy> and L<DBIx::Class::Schema/deploy>.
2733 The L<SQL::Translator> (not L<DBI>) database driver name can be explicitly
2734 provided in C<$type>, otherwise the result of L</sqlt_type> is used as default.
2736 C<$directory> is used to return statements from files in a previously created
2737 L</create_ddl_dir> directory and is optional. The filenames are constructed
2738 from L<DBIx::Class::Schema/ddl_filename>, the schema name and the C<$version>.
2740 If no C<$directory> is specified then the statements are constructed on the
2741 fly using L<SQL::Translator> and C<$version> is ignored.
2743 See L<SQL::Translator/METHODS> for a list of values for C<$sqlt_args>.
2747 sub deployment_statements {
2748 my ($self, $schema, $type, $version, $dir, $sqltargs) = @_;
2749 $type ||= $self->sqlt_type;
2750 $version ||= $schema->schema_version || '1.x';
2752 my $filename = $schema->ddl_filename($type, $version, $dir);
2755 # FIXME replace this block when a proper sane sql parser is available
2757 open($file, "<$filename")
2758 or $self->throw_exception("Can't open $filename ($!)");
2761 return join('', @rows);
2764 unless (DBIx::Class::Optional::Dependencies->req_ok_for ('deploy') ) {
2765 $self->throw_exception("Can't deploy without a ddl_dir or " . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') );
2768 # sources needs to be a parser arg, but for simplicty allow at top level
2770 $sqltargs->{parser_args}{sources} = delete $sqltargs->{sources}
2771 if exists $sqltargs->{sources};
2773 my $tr = SQL::Translator->new(
2774 producer => "SQL::Translator::Producer::${type}",
2776 parser => 'SQL::Translator::Parser::DBIx::Class',
2782 @ret = $tr->translate;
2785 $ret[0] = $tr->translate;
2788 $self->throw_exception( 'Unable to produce deployment statements: ' . $tr->error)
2789 unless (@ret && defined $ret[0]);
2791 return wantarray ? @ret : $ret[0];
2794 # FIXME deploy() currently does not accurately report sql errors
2795 # Will always return true while errors are warned
2797 my ($self, $schema, $type, $sqltargs, $dir) = @_;
2801 return if($line =~ /^--/);
2802 # next if($line =~ /^DROP/m);
2803 return if($line =~ /^BEGIN TRANSACTION/m);
2804 return if($line =~ /^COMMIT/m);
2805 return if $line =~ /^\s+$/; # skip whitespace only
2806 $self->_query_start($line);
2808 # do a dbh_do cycle here, as we need some error checking in
2809 # place (even though we will ignore errors)
2810 $self->dbh_do (sub { $_[1]->do($line) });
2812 carp qq{$_ (running "${line}")};
2814 $self->_query_end($line);
2816 my @statements = $schema->deployment_statements($type, undef, $dir, { %{ $sqltargs || {} }, no_comments => 1 } );
2817 if (@statements > 1) {
2818 foreach my $statement (@statements) {
2819 $deploy->( $statement );
2822 elsif (@statements == 1) {
2823 # split on single line comments and end of statements
2824 foreach my $line ( split(/\s*--.*\n|;\n/, $statements[0])) {
2830 =head2 datetime_parser
2832 Returns the datetime parser class
2836 sub datetime_parser {
2838 return $self->{datetime_parser} ||= do {
2839 $self->build_datetime_parser(@_);
2843 =head2 datetime_parser_type
2845 Defines the datetime parser class - currently defaults to L<DateTime::Format::MySQL>
2847 =head2 build_datetime_parser
2849 See L</datetime_parser>
2853 sub build_datetime_parser {
2855 my $type = $self->datetime_parser_type(@_);
2860 =head2 is_replicating
2862 A boolean that reports if a particular L<DBIx::Class::Storage::DBI> is set to
2863 replicate from a master database. Default is undef, which is the result
2864 returned by databases that don't support replication.
2868 sub is_replicating {
2873 =head2 lag_behind_master
2875 Returns a number that represents a certain amount of lag behind a master db
2876 when a given storage is replicating. The number is database dependent, but
2877 starts at zero and increases with the amount of lag. Default in undef
2881 sub lag_behind_master {
2885 =head2 relname_to_table_alias
2889 =item Arguments: $relname, $join_count
2893 L<DBIx::Class> uses L<DBIx::Class::Relationship> names as table aliases in
2896 This hook is to allow specific L<DBIx::Class::Storage> drivers to change the
2897 way these aliases are named.
2899 The default behavior is C<< "$relname_$join_count" if $join_count > 1 >>,
2900 otherwise C<"$relname">.
2904 sub relname_to_table_alias {
2905 my ($self, $relname, $join_count) = @_;
2907 my $alias = ($join_count && $join_count > 1 ?
2908 join('_', $relname, $join_count) : $relname);
2913 # The size in bytes to use for DBI's ->bind_param_inout, this is the generic
2914 # version and it may be necessary to amend or override it for a specific storage
2915 # if such binds are necessary.
2916 sub _max_column_bytesize {
2917 my ($self, $source, $col) = @_;
2919 my $inf = $source->column_info($col);
2920 return $inf->{_max_bytesize} ||= do {
2924 if (my $data_type = $inf->{data_type}) {
2925 $data_type = lc($data_type);
2927 # String/sized-binary types
2928 if ($data_type =~ /^(?:l?(?:var)?char(?:acter)?(?:\s*varying)?
2929 |(?:var)?binary(?:\s*varying)?|raw)\b/x
2931 $max_size = $inf->{size};
2933 # Other charset/unicode types, assume scale of 4
2934 elsif ($data_type =~ /^(?:national\s*character(?:\s*varying)?|nchar
2938 $max_size = $inf->{size} * 4 if $inf->{size};
2941 elsif ($self->_is_lob_type($data_type)) {
2942 # default to longreadlen
2945 $max_size = 100; # for all other (numeric?) datatypes
2949 $max_size ||= $self->_get_dbh->{LongReadLen} || 8000;
2953 # Determine if a data_type is some type of BLOB
2954 # FIXME: these regexes are expensive, result of these checks should be cached in
2957 my ($self, $data_type) = @_;
2958 $data_type && ($data_type =~ /lob|bfile|text|image|bytea|memo/i
2959 || $data_type =~ /^long(?:\s+(?:raw|bit\s*varying|varbit|binary
2960 |varchar|character\s*varying|nvarchar
2961 |national\s*character\s*varying))?\z/xi);
2964 sub _is_binary_lob_type {
2965 my ($self, $data_type) = @_;
2966 $data_type && ($data_type =~ /blob|bfile|image|bytea/i
2967 || $data_type =~ /^long(?:\s+(?:raw|bit\s*varying|varbit|binary))?\z/xi);
2970 sub _is_text_lob_type {
2971 my ($self, $data_type) = @_;
2972 $data_type && ($data_type =~ /^(?:clob|memo)\z/i
2973 || $data_type =~ /^long(?:\s+(?:varchar|character\s*varying|nvarchar
2974 |national\s*character\s*varying))\z/xi);
2981 =head2 DBIx::Class and AutoCommit
2983 DBIx::Class can do some wonderful magic with handling exceptions,
2984 disconnections, and transactions when you use C<< AutoCommit => 1 >>
2985 (the default) combined with C<txn_do> for transaction support.
2987 If you set C<< AutoCommit => 0 >> in your connect info, then you are always
2988 in an assumed transaction between commits, and you're telling us you'd
2989 like to manage that manually. A lot of the magic protections offered by
2990 this module will go away. We can't protect you from exceptions due to database
2991 disconnects because we don't know anything about how to restart your
2992 transactions. You're on your own for handling all sorts of exceptional
2993 cases if you choose the C<< AutoCommit => 0 >> path, just as you would
2999 Matt S. Trout <mst@shadowcatsystems.co.uk>
3001 Andy Grundman <andy@hybridized.org>
3005 You may distribute this code under the same terms as Perl itself.