/;
use mro 'c3';
use Carp::Clan qw/^DBIx::Class/;
+use List::Util ();
+
+__PACKAGE__->mk_group_accessors('simple' =>
+ qw/_identity _blob_log_on_update/
+);
=head1 NAME
-DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
+DBIx::Class::Storage::DBI::Sybase - Sybase support for DBIx::Class
=head1 SYNOPSIS
also enable that driver explicitly, see the documentation for more details.
With this driver there is unfortunately no way to get the C<last_insert_id>
-without doing a C<select max(col)>.
+without doing a C<SELECT MAX(col)>.
But your queries will be cached.
-You need at least version C<1.09> of L<DBD::Sybase> for placeholder support.
-Otherwise your storage will be automatically reblessed into C<::NoBindVars>.
+You need a version of L<DBD::Sybase> compiled with the Sybase OpenClient
+libraries, B<NOT> FreeTDS, for placeholder support. Otherwise your storage will
+be automatically reblessed into C<::NoBindVars>.
-A recommended L<DBIx::Class::Storage::DBI/connect_info> settings:
+A recommended L<DBIx::Class::Storage::DBI/connect_info> setting:
- on_connect_call => [['datetime_setup'], [blob_setup => log_on_update => 0]]
+ on_connect_call => [['datetime_setup'], ['blob_setup', log_on_update => 0]]
=head1 METHODS
=cut
-__PACKAGE__->mk_group_accessors('simple' =>
- qw/_blob_log_on_update/
-);
-
sub _rebless {
my $self = shift;
} else { # real Sybase
my $no_bind_vars = 'DBIx::Class::Storage::DBI::Sybase::NoBindVars';
- if (not $self->dbh->{syb_dynamic_supported}) {
- $self->ensure_class_loaded($no_bind_vars);
- bless $self, $no_bind_vars;
- $self->_rebless;
- }
-
- if ($DBD::Sybase::VERSION < 1.09) {
+ if ($self->_using_freetds) {
carp <<'EOF';
Your version of Sybase potentially supports placeholders and query caching,
-however your version of DBD::Sybase is too old to do this properly. Please
-upgrade to at least version 1.09 if you want this feature.
+however you seem to be using FreeTDS which does not (yet?) support this.
-TEXT/IMAGE column support will also not work in older versions of DBD::Sybase.
+Please recompile DBD::Sybase with the Sybase OpenClient libraries if you want
+these features.
+
+TEXT/IMAGE column support will also not work under FreeTDS.
See perldoc DBIx::Class::Storage::DBI::Sybase for more details.
EOF
bless $self, $no_bind_vars;
$self->_rebless;
}
+
+ if (not $self->dbh->{syb_dynamic_supported}) {
+ $self->ensure_class_loaded($no_bind_vars);
+ bless $self, $no_bind_vars;
+ $self->_rebless;
+ }
+
$self->_set_maxConnect;
}
}
}
+# Make sure we have CHAINED mode turned on, we don't know how DBD::Sybase was
+# compiled.
+sub _populate_dbh {
+ my $self = shift;
+ $self->next::method(@_);
+ $self->_dbh->{syb_chained_txn} = 1;
+}
+
+sub _using_freetds {
+ my $self = shift;
+
+ return $self->_dbh->{syb_oc_version} =~ /freetds/i;
+}
+
sub _set_maxConnect {
my $self = shift;
Used as:
- on_connect_call => [ [ blob_setup => log_on_update => 0 ] ]
+ on_connect_call => [ [ 'blob_setup', log_on_update => 0 ] ]
Does C<< $dbh->{syb_binary_images} = 1; >> to return C<IMAGE> data as raw binary
instead of as a hex string.
## need to use the API, but for now it isn't.
#
#sub order_columns_for_select {
-# my ($self, $source) = @_;
+# my ($self, $source, $columns) = @_;
#
# my (@non_blobs, @blobs);
#
-# for my $col ($source->columns) {
+# for my $col (@$columns) {
# if ($self->_is_lob_type($source->column_info($col)->{data_type})) {
# push @blobs, $col;
# } else {
# return (@non_blobs, @blobs);
#}
-# override to handle TEXT/IMAGE
+# the select-piggybacking-on-insert trick stolen from odbc/mssql
+sub _prep_for_execute {
+ my $self = shift;
+ my ($op, $extra_bind, $ident, $args) = @_;
+
+ my ($sql, $bind) = $self->next::method (@_);
+
+ if ($op eq 'insert') {
+ my $table = $ident->from;
+
+ my $bind_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
+ my $identity_col =
+List::Util::first { $bind_info->{$_}{is_auto_increment} } (keys %$bind_info);
+
+ if ($identity_col) {
+ $sql =
+"SET IDENTITY_INSERT $table ON\n" .
+"$sql\n" .
+"SET IDENTITY_INSERT $table OFF"
+ } else {
+ $identity_col = List::Util::first {
+ $ident->column_info($_)->{is_auto_increment}
+ } $ident->columns;
+ }
+
+ if ($identity_col) {
+ $sql =
+ "$sql\n" .
+ $self->_fetch_identity_sql($ident, $identity_col) . "\n";
+ }
+ }
+
+ return ($sql, $bind);
+}
+
+sub _fetch_identity_sql {
+ my ($self, $source, $col) = @_;
+
+ return "SELECT MAX($col) FROM ".$source->from;
+}
+
+sub _execute {
+ my $self = shift;
+ my ($op) = @_;
+
+ my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
+
+ if ($op eq 'insert') {
+ $self->_identity($sth->fetchrow_array);
+ $sth->finish;
+ }
+
+ return wantarray ? ($rv, $sth, @bind) : $rv;
+}
+
+sub last_insert_id { shift->_identity }
+
+# override to handle TEXT/IMAGE and nested txn
sub insert {
my ($self, $source, $to_insert) = splice @_, 0, 3;
+ my $dbh = $self->_dbh;
my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
- my $updated_cols = $self->next::method($source, $to_insert, @_);
+# Sybase has nested transactions fortunately, because we have to do the insert
+# in a transaction to avoid race conditions with the SELECT MAX(COL) identity
+# method used when placeholders are enabled.
+ my $updated_cols = do {
+ local $self->{auto_savepoint} = 1;
+ my $args = \@_;
+ my $method = $self->next::can;
+ $self->txn_do(
+ sub { $self->$method($source, $to_insert, @$args) }
+ );
+ };
$self->_insert_blobs($source, $blob_cols, $to_insert) if %$blob_cols;
sub datetime_parser_type { "DateTime::Format::Sybase" }
-sub _dbh_last_insert_id {
- my ($self, $dbh, $source, $col) = @_;
+# savepoint support using ASE syntax
+
+sub _svp_begin {
+ my ($self, $name) = @_;
+
+ $self->dbh->do("SAVE TRANSACTION $name");
+}
+
+# A new SAVE TRANSACTION with the same name releases the previous one.
+sub _svp_release { 1 }
- # sorry, there's no other way!
- my $sth = $self->sth("select max($col) from ".$source->from);
- my ($id) = $dbh->selectrow_array($sth);
- $sth->finish;
+sub _svp_rollback {
+ my ($self, $name) = @_;
- return $id;
+ $self->dbh->do("ROLLBACK TRANSACTION $name");
}
1;
=head1 IMAGE AND TEXT COLUMNS
-You need at least version C<1.09> of L<DBD::Sybase> for C<TEXT/IMAGE> column
-support.
+L<DBD::Sybase> compiled with FreeTDS will B<NOT> work with C<TEXT/IMAGE>
+columns.
See L</connect_call_blob_setup> for a L<DBIx::Class::Storage::DBI/connect_info>
setting you need to work with C<IMAGE> columns.