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