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