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