use TOP for sybase limit count thanks to refactored count
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
1 package DBIx::Class::Storage::DBI::Sybase;
2
3 use strict;
4 use warnings;
5
6 use Class::C3;
7 use base qw/DBIx::Class::Storage::DBI/;
8
9 use Carp::Clan qw/^DBIx::Class/;
10
11 sub _rebless {
12   my $self = shift;
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;
26     } elsif (not $self->dbh->{syb_dynamic_supported}) {
27       bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
28       $self->_rebless;
29     }
30   }
31 }
32
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');
50
51     1;
52   }
53 }
54
55 sub _dbh_last_insert_id {
56   my ($self, $dbh, $source, $col) = @_;
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];
61 }
62
63 sub count {
64   my ($self, $source, $attrs) = @_;
65
66   if (exists $attrs->{rows}) {
67     my $offset = $attrs->{offset} || 0;
68     my $total  = $attrs->{rows} + $offset;
69
70     my $new_attrs = $self->_trim_attributes_for_count($source, $attrs);
71
72     my $query = $source->resultset_class->new($source, $new_attrs)->as_query;
73
74     my $top_attrs = {};
75     $top_attrs->{from} = [{
76       top_subq => $query
77     }];
78     $top_attrs->{select} = "TOP $total 1";
79     $top_attrs->{as}     = ['total'];
80
81     my $top_query = $source->resultset_class->new($source, $top_attrs)->as_query;
82
83     my $count_attrs = {};
84     $count_attrs->{from} = [{
85       count_subq => $top_query
86     }];
87     $count_attrs->{select} = { count => '*' };
88     $count_attrs->{as}     = ['count'];
89
90     my $tmp_rs = $source->resultset_class->new($source, $count_attrs);
91     my ($count) = $tmp_rs->cursor->next;
92
93     $count -= $offset;
94     $count  = $attrs->{rows} if $count > $attrs->{rows};
95
96     return $count;
97   }
98
99   return $self->next::method(@_);
100 }
101
102 sub datetime_parser_type { "DBIx::Class::Storage::DBI::Sybase::DateTime" }
103
104 1;
105
106 =head1 NAME
107
108 DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
109
110 =head1 SYNOPSIS
111
112 This subclass supports L<DBD::Sybase> for real Sybase databases.  If you are
113 using an MSSQL database via L<DBD::Sybase>, your storage will be reblessed to
114 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.
115
116 =head1 DESCRIPTION
117
118 If your version of Sybase does not support placeholders, then your storage
119 will be reblessed to L<DBIx::Class::Storage::DBI::Sybase::NoBindVars>. You can
120 also enable that driver explicitly, see the documentation for more details.
121
122 With this driver there is unfortunately no way to get the C<last_insert_id>
123 without doing a C<select max(col)>.
124
125 But your queries will be cached.
126
127 =head1 DATES
128
129 On connection C<syb_date_fmt> is set to C<ISO_strict>, e.g.:
130 C<2004-08-21T14:36:48.080Z> and C<dateformat> is set to C<mdy>, e.g.:
131 C<08/13/1979 18:08:55.080>.
132
133 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
134 C<SMALLDATETIME> columns only have minute precision.
135
136 You will need the L<DateTime::Format::Strptime> module if you are going to use
137 L<DBIx::Class::InflateColumn::DateTime>.
138
139 =head1 AUTHORS
140
141 See L<DBIx::Class/CONTRIBUTORS>.
142
143 =head1 LICENSE
144
145 You may distribute this code under the same terms as Perl itself.
146
147 =cut
148 # vim:sts=2 sw=2: