todo tests for text/image columns in 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
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;
683f73ec 26 } else {
27 # real Sybase
28 if (not $self->dbh->{syb_dynamic_supported}) {
29 bless $self, 'DBIx::Class::Storage:DBI::Sybase::NoBindVars';
30 $self->_rebless;
31 }
9539eeb1 32 $self->connect_call_datetime_setup;
47d9646a 33 }
c5ce7cd6 34 }
b50a5275 35}
36
683f73ec 37sub _populate_dbh {
38 my $self = shift;
39 $self->next::method(@_);
9539eeb1 40 $self->connect_call_datetime_setup;
683f73ec 41 1;
42}
43
6b1f5ef7 44{
45 my $old_dbd_warned = 0;
46
9539eeb1 47=head2 connect_call_datetime_setup
48
49Used as:
50
51 on_connect_call => 'datetime_setup'
52
53In L<DBIx::Class::Storage::DBI/connect_info> to set:
54
3abafb11 55 $dbh->syb_date_fmt('ISO_strict'); # output fmt: 2004-08-21T14:36:48.080Z
56 $dbh->do('set dateformat mdy'); # input fmt: 08/13/1979 18:08:55.080
9539eeb1 57
58On connection for use with L<DBIx::Class::InflateColumn::DateTime>, using
3abafb11 59L<DateTime::Format::Sybase>, which you will need to install.
60
61This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
62C<SMALLDATETIME> columns only have minute precision.
9539eeb1 63
64=cut
65
66 sub connect_call_datetime_setup {
6b1f5ef7 67 my $self = shift;
6b1f5ef7 68 my $dbh = $self->_dbh;
69
70 if ($dbh->can('syb_date_fmt')) {
71 $dbh->syb_date_fmt('ISO_strict');
72 } elsif (not $old_dbd_warned) {
73 carp "Your DBD::Sybase is too old to support ".
74 "DBIx::Class::InflateColumn::DateTime, please upgrade!";
75 $old_dbd_warned = 1;
76 }
77
78 $dbh->do('set dateformat mdy');
c5ce7cd6 79
6b1f5ef7 80 1;
c5ce7cd6 81 }
6b1f5ef7 82}
83
84sub _dbh_last_insert_id {
85 my ($self, $dbh, $source, $col) = @_;
c5ce7cd6 86
87 # sorry, there's no other way!
88 my $sth = $dbh->prepare_cached("select max($col) from ".$source->from);
89 return ($dbh->selectrow_array($sth))[0];
a964a928 90}
91
b7505130 92sub count {
aa56ff9a 93 my $self = shift;
94 my ($source, $attrs) = @_;
b7505130 95
6537ddaa 96 if (not exists $attrs->{rows}) {
97 return $self->next::method(@_);
98 }
b7505130 99
6537ddaa 100 my $offset = $attrs->{offset} || 0;
101 my $total = $attrs->{rows} + $offset;
b7505130 102
8cebbed0 103 my $new_attrs = $self->_copy_attributes_for_count($source, $attrs);
104
105 my $first_pk = ($source->primary_columns)[0];
106
107 $new_attrs->{select} = $first_pk ? "me.$first_pk" : 1;
a0348159 108
6537ddaa 109 my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
b7505130 110
6537ddaa 111 $self->dbh->{syb_rowcount} = $total;
b7505130 112
6537ddaa 113 my $count = 0;
114 $count++ while $tmp_rs->cursor->next;
b7505130 115
6537ddaa 116 $self->dbh->{syb_rowcount} = 0;
b7505130 117
6537ddaa 118 return $count - $offset;
b7505130 119}
120
c21c1eb6 121sub datetime_parser_type { "DateTime::Format::Sybase" }
c5ce7cd6 122
3885cff6 1231;
124
125=head1 NAME
126
127DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
128
129=head1 SYNOPSIS
130
7e8cecc1 131This subclass supports L<DBD::Sybase> for real Sybase databases. If you are
132using an MSSQL database via L<DBD::Sybase>, your storage will be reblessed to
133L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>.
3885cff6 134
7e8cecc1 135=head1 DESCRIPTION
d4483998 136
7e8cecc1 137If your version of Sybase does not support placeholders, then your storage
138will be reblessed to L<DBIx::Class::Storage::DBI::Sybase::NoBindVars>. You can
139also enable that driver explicitly, see the documentation for more details.
c5ce7cd6 140
7e8cecc1 141With this driver there is unfortunately no way to get the C<last_insert_id>
142without doing a C<select max(col)>.
c5ce7cd6 143
144But your queries will be cached.
145
146=head1 DATES
147
3abafb11 148See L</connect_call_datetime_setup> to setup date formats
149for L<DBIx::Class::InflateColumn::DateTime>.
c5ce7cd6 150
3885cff6 151=head1 AUTHORS
152
7e8cecc1 153See L<DBIx::Class/CONTRIBUTORS>.
c5ce7cd6 154
3885cff6 155=head1 LICENSE
156
157You may distribute this code under the same terms as Perl itself.
158
159=cut
c5ce7cd6 160# vim:sts=2 sw=2: