Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
CommitLineData
3885cff6 1package DBIx::Class::Storage::DBI::Sybase;
2
3use strict;
4use warnings;
5
c5ce7cd6 6use base qw/DBIx::Class::Storage::DBI/;
3885cff6 7
6b1f5ef7 8use Carp::Clan qw/^DBIx::Class/;
9
47d9646a 10sub _rebless {
b50a5275 11 my $self = shift;
c5ce7cd6 12
13 if (ref($self) eq 'DBIx::Class::Storage::DBI::Sybase') {
14 my $dbtype = eval {
15 @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
16 } || '';
17
18 my $exception = $@;
19 $dbtype =~ s/\W/_/gi;
20 my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}";
21
22 if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
23 bless $self, $subclass;
24 $self->_rebless;
6b1f5ef7 25 } elsif (not $self->dbh->{syb_dynamic_supported}) {
6b1f5ef7 26 bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
27 $self->_rebless;
47d9646a 28 }
c5ce7cd6 29 }
b50a5275 30}
31
6b1f5ef7 32{
33 my $old_dbd_warned = 0;
34
35 sub _populate_dbh {
36 my $self = shift;
37 $self->next::method(@_);
38 my $dbh = $self->_dbh;
39
40 if ($dbh->can('syb_date_fmt')) {
41 $dbh->syb_date_fmt('ISO_strict');
42 } elsif (not $old_dbd_warned) {
43 carp "Your DBD::Sybase is too old to support ".
44 "DBIx::Class::InflateColumn::DateTime, please upgrade!";
45 $old_dbd_warned = 1;
46 }
47
48 $dbh->do('set dateformat mdy');
c5ce7cd6 49
6b1f5ef7 50 1;
c5ce7cd6 51 }
6b1f5ef7 52}
53
54sub _dbh_last_insert_id {
55 my ($self, $dbh, $source, $col) = @_;
c5ce7cd6 56
57 # sorry, there's no other way!
58 my $sth = $dbh->prepare_cached("select max($col) from ".$source->from);
59 return ($dbh->selectrow_array($sth))[0];
a964a928 60}
61
b7505130 62sub count {
63 my ($self, $source, $attrs) = @_;
64
65 if (exists $attrs->{rows}) {
66 my $new_attrs = $self->_trim_attributes_for_count($source, $attrs);
67
68 $new_attrs->{select} = '1';
69 $new_attrs->{as} = ['dummy'];
70
71# speed things up at least *a little*
72 $new_attrs->{result_class} = 'DBIx::Class::ResultClass::HashRefInflator';
73
74 my $offset = $attrs->{offset} || 0;
75 my $total = $attrs->{rows} + $offset;
76
77 $self->dbh->do("set rowcount $total");
78
79 my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
80
81 my $count = 0;
82 $count++ while $tmp_rs->cursor->next;
83
84 $self->dbh->do("set rowcount 0");
85
86 return $count;
87 }
88
89 return $self->next::method(@_);
90}
91
c5ce7cd6 92sub datetime_parser_type { "DBIx::Class::Storage::DBI::Sybase::DateTime" }
93
3885cff6 941;
95
96=head1 NAME
97
98DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
99
100=head1 SYNOPSIS
101
7e8cecc1 102This subclass supports L<DBD::Sybase> for real Sybase databases. If you are
103using an MSSQL database via L<DBD::Sybase>, your storage will be reblessed to
104L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.
3885cff6 105
7e8cecc1 106=head1 DESCRIPTION
d4483998 107
7e8cecc1 108If your version of Sybase does not support placeholders, then your storage
109will be reblessed to L<DBIx::Class::Storage::DBI::Sybase::NoBindVars>. You can
110also enable that driver explicitly, see the documentation for more details.
c5ce7cd6 111
7e8cecc1 112With this driver there is unfortunately no way to get the C<last_insert_id>
113without doing a C<select max(col)>.
c5ce7cd6 114
115But your queries will be cached.
116
117=head1 DATES
118
119On connection C<syb_date_fmt> is set to C<ISO_strict>, e.g.:
120C<2004-08-21T14:36:48.080Z> and C<dateformat> is set to C<mdy>, e.g.:
7e8cecc1 121C<08/13/1979 18:08:55.080>.
c5ce7cd6 122
b7505130 123This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
124C<SMALLDATETIME> columns only have minute precision.
125
c5ce7cd6 126You will need the L<DateTime::Format::Strptime> module if you are going to use
127L<DBIx::Class::InflateColumn::DateTime>.
128
3885cff6 129=head1 AUTHORS
130
7e8cecc1 131See L<DBIx::Class/CONTRIBUTORS>.
c5ce7cd6 132
3885cff6 133=head1 LICENSE
134
135You may distribute this code under the same terms as Perl itself.
136
137=cut
c5ce7cd6 138# vim:sts=2 sw=2: