0757a4ba868061a7b3e1f9345dc803611948bf02
[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
12 use Carp::Clan qw/^DBIx::Class/;
13 use namespace::clean;
14
15 __PACKAGE__->datetime_parser_type(
16   'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format'
17 );
18
19 sub _rebless {
20   my $self = shift;
21   my $dbh  = $self->_get_dbh;
22
23   return if ref $self ne __PACKAGE__;
24   if (not $self->_use_typeless_placeholders) {
25     require
26       DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars;
27     bless $self,
28       'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars';
29     $self->_rebless;
30   }
31 }
32
33 sub _run_connection_actions {
34   my $self = shift;
35
36   # LongReadLen doesn't work with MSSQL through DBD::Sybase, and the default is
37   # huge on some versions of SQL server and can cause memory problems, so we
38   # fix it up here (see ::DBI::Sybase.pm)
39   $self->set_textsize;
40
41   $self->next::method(@_);
42 }
43
44 sub _dbh_begin_work {
45   my $self = shift;
46
47   $self->_get_dbh->do('BEGIN TRAN');
48 }
49
50 sub _dbh_commit {
51   my $self = shift;
52   my $dbh  = $self->_dbh
53     or $self->throw_exception('cannot COMMIT on a disconnected handle');
54   $dbh->do('COMMIT');
55 }
56
57 sub _dbh_rollback {
58   my $self = shift;
59   my $dbh  = $self->_dbh
60     or $self->throw_exception('cannot ROLLBACK on a disconnected handle');
61   $dbh->do('ROLLBACK');
62 }
63
64 sub _get_server_version {
65   my $self = shift;
66
67   my $product_version = $self->_get_dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion');
68
69   if ((my $version = $product_version->{Character_Value}) =~ /^(\d+)\./) {
70     return $version;
71   }
72   else {
73     $self->throw_exception(
74       "MSSQL Version Retrieval Failed, Your ProductVersion's Character_Value is missing or malformed!"
75     );
76   }
77 }
78
79 =head2 connect_call_datetime_setup
80
81 Used as:
82
83   on_connect_call => 'datetime_setup'
84
85 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set:
86
87   $dbh->syb_date_fmt('ISO_strict'); # output fmt: 2004-08-21T14:36:48.080Z
88
89 On connection for use with L<DBIx::Class::InflateColumn::DateTime>
90
91 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
92 C<SMALLDATETIME> columns only have minute precision.
93
94 =cut
95
96 {
97   my $old_dbd_warned = 0;
98
99   sub connect_call_datetime_setup {
100     my $self = shift;
101     my $dbh = $self->_get_dbh;
102
103     if ($dbh->can('syb_date_fmt')) {
104       # amazingly, this works with FreeTDS
105       $dbh->syb_date_fmt('ISO_strict');
106     } elsif (not $old_dbd_warned) {
107       carp "Your DBD::Sybase is too old to support ".
108       "DBIx::Class::InflateColumn::DateTime, please upgrade!";
109       $old_dbd_warned = 1;
110     }
111   }
112 }
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