Patch up weird MRO fail on 5.8 perls
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / Microsoft_SQL_Server.pm
1 package DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server;
2
3 use strict;
4 use warnings;
5
6 use base qw/
7   DBIx::Class::Storage::DBI::Sybase
8   DBIx::Class::Storage::DBI::MSSQL
9 /;
10 use mro 'c3';
11 use Carp::Clan qw/^DBIx::Class/;
12
13 # Temporary fix for mysterious MRO fail on 5.8 perls
14 Class::C3::reinitialize if $] < '5.01';
15
16 sub _rebless {
17   my $self = shift;
18   my $dbh  = $self->_get_dbh;
19
20   return if ref $self ne __PACKAGE__;
21   if (not $self->_use_typeless_placeholders) {
22     require
23       DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars;
24     bless $self,
25       'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars';
26     $self->_rebless;
27   }
28 }
29
30 sub _run_connection_actions {
31   my $self = shift;
32
33   # LongReadLen doesn't work with MSSQL through DBD::Sybase, and the default is
34   # huge on some versions of SQL server and can cause memory problems, so we
35   # fix it up here (see ::DBI::Sybase.pm)
36   $self->set_textsize;
37
38   $self->next::method(@_);
39 }
40
41 sub _dbh_begin_work {
42   my $self = shift;
43
44   $self->_get_dbh->do('BEGIN TRAN');
45 }
46
47 sub _dbh_commit {
48   my $self = shift;
49   my $dbh  = $self->_dbh
50     or $self->throw_exception('cannot COMMIT on a disconnected handle');
51   $dbh->do('COMMIT');
52 }
53
54 sub _dbh_rollback {
55   my $self = shift;
56   my $dbh  = $self->_dbh
57     or $self->throw_exception('cannot ROLLBACK on a disconnected handle');
58   $dbh->do('ROLLBACK');
59 }
60
61 sub _get_server_version {
62   my $self = shift;
63
64   my $product_version = $self->_get_dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion');
65
66   if ((my $version = $product_version->{Character_Value}) =~ /^(\d+)\./) {
67     return $version;
68   }
69   else {
70     $self->throw_exception(
71       "MSSQL Version Retrieval Failed, Your ProductVersion's Character_Value is missing or malformed!"
72     );
73   }
74 }
75
76 =head2 connect_call_datetime_setup
77
78 Used as:
79
80   on_connect_call => 'datetime_setup'
81
82 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set:
83
84   $dbh->syb_date_fmt('ISO_strict'); # output fmt: 2004-08-21T14:36:48.080Z
85
86 On connection for use with L<DBIx::Class::InflateColumn::DateTime>
87
88 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
89 C<SMALLDATETIME> columns only have minute precision.
90
91 =cut
92
93 {
94   my $old_dbd_warned = 0;
95
96   sub connect_call_datetime_setup {
97     my $self = shift;
98     my $dbh = $self->_get_dbh;
99
100     if ($dbh->can('syb_date_fmt')) {
101       # amazingly, this works with FreeTDS
102       $dbh->syb_date_fmt('ISO_strict');
103     } elsif (not $old_dbd_warned) {
104       carp "Your DBD::Sybase is too old to support ".
105       "DBIx::Class::InflateColumn::DateTime, please upgrade!";
106       $old_dbd_warned = 1;
107     }
108   }
109 }
110
111 sub datetime_parser_type {
112   'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format'
113
114
115 package # hide from PAUSE
116   DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format;
117
118 my $datetime_parse_format  = '%Y-%m-%dT%H:%M:%S.%3NZ';
119 my $datetime_format_format = '%Y-%m-%d %H:%M:%S.%3N'; # %F %T 
120
121 my ($datetime_parser, $datetime_formatter);
122
123 sub parse_datetime {
124   shift;
125   require DateTime::Format::Strptime;
126   $datetime_parser ||= DateTime::Format::Strptime->new(
127     pattern  => $datetime_parse_format,
128     on_error => 'croak',
129   );
130   return $datetime_parser->parse_datetime(shift);
131 }
132
133 sub format_datetime {
134   shift;
135   require DateTime::Format::Strptime;
136   $datetime_formatter ||= DateTime::Format::Strptime->new(
137     pattern  => $datetime_format_format,
138     on_error => 'croak',
139   );
140   return $datetime_formatter->format_datetime(shift);
141 }
142
143 1;
144
145 =head1 NAME
146
147 DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server - Support for Microsoft
148 SQL Server via DBD::Sybase
149
150 =head1 SYNOPSIS
151
152 This subclass supports MSSQL server connections via L<DBD::Sybase>.
153
154 =head1 DESCRIPTION
155
156 This driver tries to determine whether your version of L<DBD::Sybase> and
157 supporting libraries (usually FreeTDS) support using placeholders, if not the
158 storage will be reblessed to
159 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars>.
160
161 The MSSQL specific functionality is provided by
162 L<DBIx::Class::Storage::DBI::MSSQL>.
163
164 =head1 AUTHOR
165
166 See L<DBIx::Class/CONTRIBUTORS>.
167
168 =head1 LICENSE
169
170 You may distribute this code under the same terms as Perl itself.
171
172 =cut