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