Unbreak devrel version checks
[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 DBIx::Class::Carp;
13
14 =head1 NAME
15
16 DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server - Support for Microsoft
17 SQL Server via DBD::Sybase
18
19 =head1 SYNOPSIS
20
21 This subclass supports MSSQL server connections via L<DBD::Sybase>.
22
23 =head1 DESCRIPTION
24
25 This driver tries to determine whether your version of L<DBD::Sybase> and
26 supporting libraries (usually FreeTDS) support using placeholders, if not the
27 storage will be reblessed to
28 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars>.
29
30 The MSSQL specific functionality is provided by
31 L<DBIx::Class::Storage::DBI::MSSQL>.
32
33 =head1 METHODS
34
35 =cut
36
37 __PACKAGE__->datetime_parser_type(
38   'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format'
39 );
40
41 sub _rebless {
42   my $self = shift;
43   my $dbh  = $self->_get_dbh;
44
45   return if ref $self ne __PACKAGE__;
46   if (not $self->_use_typeless_placeholders) {
47     carp_once <<'EOF' unless $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN};
48 Placeholders do not seem to be supported in your configuration of
49 DBD::Sybase/FreeTDS.
50
51 This means you are taking a large performance hit, as caching of prepared
52 statements is disabled.
53
54 Make sure to configure your server with "tds version" of 8.0 or 7.0 in
55 /etc/freetds/freetds.conf .
56
57 To turn off this warning, set the DBIC_MSSQL_FREETDS_LOWVER_NOWARN environment
58 variable.
59 EOF
60     require
61       DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars;
62     bless $self,
63       'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::NoBindVars';
64     $self->_rebless;
65   }
66 }
67
68 sub _init {
69   my $self = shift;
70
71   $self->next::method(@_);
72
73   # work around massively broken freetds versions after 0.82
74   # - explicitly no scope_identity
75   # - no sth caching
76   #
77   # warn about the fact as well, do not provide a mechanism to shut it up
78   if ($self->_using_freetds and (my $ver = $self->_using_freetds_version||999) > 0.82) {
79     carp_once(
80       "Your DBD::Sybase was compiled against buggy FreeTDS version $ver. "
81     . 'Statement caching does not work and will be disabled.'
82     );
83
84     $self->_identity_method('@@identity');
85     $self->_no_scope_identity_query(1);
86     $self->disable_sth_caching(1);
87   }
88 }
89
90 # invoked only if DBD::Sybase is compiled against FreeTDS
91 sub _set_autocommit_stmt {
92   my ($self, $on) = @_;
93
94   return 'SET IMPLICIT_TRANSACTIONS ' . ($on ? 'OFF' : 'ON');
95 }
96
97 sub _get_server_version {
98   my $self = shift;
99
100   my $product_version = $self->_get_dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion');
101
102   if ((my $version = $product_version->{Character_Value}) =~ /^(\d+)\./) {
103     return $version;
104   }
105   else {
106     $self->throw_exception(
107       "MSSQL Version Retrieval Failed, Your ProductVersion's Character_Value is missing or malformed!"
108     );
109   }
110 }
111
112 =head2 connect_call_datetime_setup
113
114 Used as:
115
116   on_connect_call => 'datetime_setup'
117
118 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set:
119
120   $dbh->syb_date_fmt('ISO_strict'); # output fmt: 2004-08-21T14:36:48.080Z
121
122 On connection for use with L<DBIx::Class::InflateColumn::DateTime>
123
124 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
125 C<SMALLDATETIME> columns only have minute precision.
126
127 =cut
128
129 sub connect_call_datetime_setup {
130   my $self = shift;
131   my $dbh = $self->_get_dbh;
132
133   if ($dbh->can('syb_date_fmt')) {
134     # amazingly, this works with FreeTDS
135     $dbh->syb_date_fmt('ISO_strict');
136   }
137   else{
138     carp_once
139       'Your DBD::Sybase is too old to support '
140     . 'DBIx::Class::InflateColumn::DateTime, please upgrade!';
141   }
142 }
143
144
145 package # hide from PAUSE
146   DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format;
147
148 my $datetime_parse_format  = '%Y-%m-%dT%H:%M:%S.%3NZ';
149 my $datetime_format_format = '%Y-%m-%d %H:%M:%S.%3N'; # %F %T
150
151 my ($datetime_parser, $datetime_formatter);
152
153 sub parse_datetime {
154   shift;
155   require DateTime::Format::Strptime;
156   $datetime_parser ||= DateTime::Format::Strptime->new(
157     pattern  => $datetime_parse_format,
158     on_error => 'croak',
159   );
160   return $datetime_parser->parse_datetime(shift);
161 }
162
163 sub format_datetime {
164   shift;
165   require DateTime::Format::Strptime;
166   $datetime_formatter ||= DateTime::Format::Strptime->new(
167     pattern  => $datetime_format_format,
168     on_error => 'croak',
169   );
170   return $datetime_formatter->format_datetime(shift);
171 }
172
173 1;
174
175 =head1 AUTHOR
176
177 See L<DBIx::Class/CONTRIBUTORS>.
178
179 =head1 LICENSE
180
181 You may distribute this code under the same terms as Perl itself.
182
183 =cut