use DBIx::Class::Storage::DBI::Cursor;
use DBIx::Class::Storage::Statistics;
use IO::File;
-use Carp::Clan qw/DBIx::Class/;
__PACKAGE__->mk_group_accessors(
'simple' =>
$self;
}
+sub _RowNumberOver {
+ my ($self, $sql, $order, $rows, $offset ) = @_;
+
+ $offset += 1;
+ my $last = $rows + $offset;
+ my ( $order_by ) = $self->_order_by( $order );
+
+ $sql = <<"";
+SELECT * FROM
+(
+ SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
+ $sql
+ $order_by
+ ) Q1
+) Q2
+WHERE ROW_NUM BETWEEN $offset AND $last
+
+ return $sql;
+}
+
+
# While we're at it, this should make LIMIT queries more efficient,
# without digging into things too deeply
sub _find_syntax {
my ($self, $syntax) = @_;
+ my $dbhname = ref $syntax eq 'HASH' ? $syntax->{Driver}{Name} : '';
+ if(ref($self) && $dbhname && $dbhname eq 'DB2') {
+ return 'RowNumberOver';
+ }
+
$self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax);
}
sub dbh_do {
my $self = shift;
- my $todo = shift;
+ my $coderef = shift;
+
+ return $coderef->($self->_dbh, @_) if $self->{_in_txn_do};
+
+ ref $coderef eq 'CODE' or $self->throw_exception
+ ('$coderef must be a CODE reference');
my @result;
my $want_array = wantarray;
eval {
$self->_verify_pid if $self->_dbh;
$self->_populate_dbh if !$self->_dbh;
- my $dbh = $self->_dbh;
if($want_array) {
- @result = $todo->($dbh, @_);
+ @result = $coderef->($self->_dbh, @_);
}
elsif(defined $want_array) {
- $result[0] = $todo->($dbh, @_);
+ $result[0] = $coderef->($self->_dbh, @_);
}
else {
- $todo->($dbh, @_);
+ $coderef->($self->_dbh, @_);
}
};
- if($@) {
- my $exception = $@;
- $self->connected
- ? $self->throw_exception($exception)
- : $self->_populate_dbh;
+ my $exception = $@;
+ if(!$exception) { return $want_array ? @result : $result[0] }
+
+ $self->throw_exception($exception) if $self->connected;
+
+ # We were not connected - reconnect and retry, but let any
+ # exception fall right through this time
+ $self->_populate_dbh;
+ $coderef->($self->_dbh, @_);
+}
+
+# This is basically a blend of dbh_do above and DBIx::Class::Storage::txn_do.
+# It also informs dbh_do to bypass itself while under the direction of txn_do,
+# via $self->{_in_txn_do} (this saves some redundant eval and errorcheck, etc)
+sub txn_do {
+ my $self = shift;
+ my $coderef = shift;
+
+ ref $coderef eq 'CODE' or $self->throw_exception
+ ('$coderef must be a CODE reference');
+
+ local $self->{_in_txn_do} = 1;
+
+ my $tried = 0;
+
+ my @result;
+ my $want_array = wantarray;
- my $dbh = $self->_dbh;
- return $todo->($dbh, @_);
+ START_TXN: eval {
+ $self->_verify_pid if $self->_dbh;
+ $self->_populate_dbh if !$self->_dbh;
+
+ $self->txn_begin;
+ if($want_array) {
+ @result = $coderef->(@_);
+ }
+ elsif(defined $want_array) {
+ $result[0] = $coderef->(@_);
+ }
+ else {
+ $coderef->(@_);
+ }
+ $self->txn_commit;
+ };
+
+ my $exception = $@;
+ if(!$exception) { return $want_array ? @result : $result[0] }
+
+ if($tried++ > 0 || $self->connected) {
+ eval { $self->txn_rollback };
+ my $rollback_exception = $@;
+ if($rollback_exception) {
+ my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
+ $self->throw_exception($exception) # propagate nested rollback
+ if $rollback_exception =~ /$exception_class/;
+
+ $self->throw_exception(
+ "Transaction aborted: ${exception}. "
+ . "Rollback failed: ${rollback_exception}"
+ );
+ }
+ $self->throw_exception($exception)
}
- return $want_array ? @result : $result[0];
+ # We were not connected, and was first try - reconnect and retry
+ # XXX I know, gotos are evil. If you can find a better way
+ # to write this that doesn't duplicate a lot of code/structure,
+ # and behaves identically, feel free...
+
+ $self->_populate_dbh;
+ goto START_TXN;
}
=head2 disconnect
$dbh;
}
-
sub __txn_begin {
my ($dbh, $self) = @_;
if ($dbh->{AutoCommit}) {
return $self->_execute(@args);
}
+=head2 select
+
+=over 4
+
+=item Arguments: $ident, $select, $condition, $attrs
+
+=back
+
+Handle a SQL select statement.
+
+=cut
+
sub select {
my $self = shift;
my ($ident, $select, $condition, $attrs) = @_;
=head2 sth
+=over 4
+
+=item Arguments: $sql
+
+=back
+
Returns a L<DBI> sth (statement handle) for the supplied SQL.
=cut
$result{$col_name} = \%column_info;
}
};
- return \%result if !$@;
+ return \%result if !$@ && scalar keys %result;
}
my %result;
=back
-Creates an SQL file based on the Schema, for each of the specified
+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
=head2 deployment_statements
-Create the statements for L</deploy> and
-L<DBIx::Class::Schema/deploy>.
+=over 4
+
+=item Arguments: $schema, $type, $version, $directory, $sqlt_args
+
+=back
+
+Returns the statements used by L</deploy> and L<DBIx::Class::Schema/deploy>.
+The database driver name is given by C<$type>, though the value from
+L</sqlt_type> is used if it is not specified.
+
+C<$directory> is used to return statements from files in a previously created
+L</create_ddl_dir> directory and is optional. The filenames are constructed
+from L<DBIx::Class::Schema/ddl_filename>, the schema name and the C<$version>.
+
+If no C<$directory> is specified then the statements are constructed on the
+fly using L<SQL::Translator> and C<$version> is ignored.
+
+See L<SQL::Translator/METHODS> for a list of values for C<$sqlt_args>.
=cut
}
sub deploy {
- my ($self, $schema, $type, $sqltargs) = @_;
- foreach my $statement ( $self->deployment_statements($schema, $type, undef, undef, { no_comments => 1, %{ $sqltargs || {} } } ) ) {
+ my ($self, $schema, $type, $sqltargs, $dir) = @_;
+ foreach my $statement ( $self->deployment_statements($schema, $type, undef, $dir, { no_comments => 1, %{ $sqltargs || {} } } ) ) {
for ( split(";\n", $statement)) {
next if($_ =~ /^--/);
next if(!$_);
sub DESTROY {
my $self = shift;
return if !$self->_dbh;
-
$self->_verify_pid;
$self->_dbh(undef);
}
You may distribute this code under the same terms as Perl itself.
=cut
-