fix Sybase DT stuff and storage bases
[dbsrgits/DBIx-Class-Historic.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 base qw/DBIx::Class::Storage::DBI/;
7
8 use Carp::Clan qw/^DBIx::Class/;
9
10 sub _rebless {
11   my $self = shift;
12
13   if (ref($self) eq 'DBIx::Class::Storage::DBI::Sybase') {
14     my $dbtype = eval {
15       @{$self->dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
16     } || '';
17
18     my $exception = $@;
19     $dbtype =~ s/\W/_/gi;
20     my $subclass = "DBIx::Class::Storage::DBI::Sybase::${dbtype}";
21
22     if (!$exception && $dbtype && $self->load_optional_class($subclass)) {
23       bless $self, $subclass;
24       $self->_rebless;
25     } elsif (not $self->dbh->{syb_dynamic_supported}) {
26 # probably real Sybase
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 datetime_parser_type { "DBIx::Class::Storage::DBI::Sybase::DateTime" }
64
65 1;
66
67 =head1 NAME
68
69 DBIx::Class::Storage::DBI::Sybase - Storage::DBI subclass for Sybase
70
71 =head1 SYNOPSIS
72
73 This subclass supports L<DBD::Sybase> for real Sybase databases.  If
74 you are using an MSSQL database via L<DBD::Sybase>, see
75 L<DBIx::Class::Storage::DBI::Sybase::MSSQL>.
76
77 =head1 CAVEATS
78
79 If your version of Sybase does not support placeholders, then this storage
80 driver uses L<DBIx::Class::Storage::DBI::NoBindVars> as a base,
81
82 In which case, bind variables will be interpolated (properly quoted of course)
83 into the SQL query itself, without using bind placeholders.
84
85 More importantly this means that caching of prepared statements is explicitly
86 disabled, as the interpolation renders it useless.
87
88 If your version of Sybase B<DOES> support placeholders (check
89 C<<$dbh->{syb_dynamic_supported}>> then unfortunately there's no way to get the
90 C<last_insert_id> without doing a C<select max(col)>.
91
92 But your queries will be cached.
93
94 =head1 DATES
95
96 On connection C<syb_date_fmt> is set to C<ISO_strict>, e.g.:
97 C<2004-08-21T14:36:48.080Z> and C<dateformat> is set to C<mdy>, e.g.:
98 C<08/13/1979>.
99
100 You will need the L<DateTime::Format::Strptime> module if you are going to use
101 L<DBIx::Class::InflateColumn::DateTime>.
102
103 =head1 AUTHORS
104
105 Brandon L Black <blblack@gmail.com>
106
107 Justin Hunter <justin.d.hunter@gmail.com>
108
109 Rafael Kitover <rkitover@cpan.org>
110
111 =head1 LICENSE
112
113 You may distribute this code under the same terms as Perl itself.
114
115 =cut
116 # vim:sts=2 sw=2: