change FreeTDS related warnings to carp_once
[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 # invoked only if DBD::Sybase is compiled against FreeTDS
69 sub _set_autocommit_stmt {
70   my ($self, $on) = @_;
71
72   return 'SET IMPLICIT_TRANSACTIONS ' . ($on ? 'OFF' : 'ON');
73 }
74
75 sub _get_server_version {
76   my $self = shift;
77
78   my $product_version = $self->_get_dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion');
79
80   if ((my $version = $product_version->{Character_Value}) =~ /^(\d+)\./) {
81     return $version;
82   }
83   else {
84     $self->throw_exception(
85       "MSSQL Version Retrieval Failed, Your ProductVersion's Character_Value is missing or malformed!"
86     );
87   }
88 }
89
90 =head2 connect_call_datetime_setup
91
92 Used as:
93
94   on_connect_call => 'datetime_setup'
95
96 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set:
97
98   $dbh->syb_date_fmt('ISO_strict'); # output fmt: 2004-08-21T14:36:48.080Z
99
100 On connection for use with L<DBIx::Class::InflateColumn::DateTime>
101
102 This works for both C<DATETIME> and C<SMALLDATETIME> columns, although
103 C<SMALLDATETIME> columns only have minute precision.
104
105 =cut
106
107 sub connect_call_datetime_setup {
108   my $self = shift;
109   my $dbh = $self->_get_dbh;
110
111   if ($dbh->can('syb_date_fmt')) {
112     # amazingly, this works with FreeTDS
113     $dbh->syb_date_fmt('ISO_strict');
114   }
115   else{
116     carp_once
117       'Your DBD::Sybase is too old to support '
118     . 'DBIx::Class::InflateColumn::DateTime, please upgrade!';
119   }
120 }
121
122
123 package # hide from PAUSE
124   DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server::DateTime::Format;
125
126 my $datetime_parse_format  = '%Y-%m-%dT%H:%M:%S.%3NZ';
127 my $datetime_format_format = '%Y-%m-%d %H:%M:%S.%3N'; # %F %T 
128
129 my ($datetime_parser, $datetime_formatter);
130
131 sub parse_datetime {
132   shift;
133   require DateTime::Format::Strptime;
134   $datetime_parser ||= DateTime::Format::Strptime->new(
135     pattern  => $datetime_parse_format,
136     on_error => 'croak',
137   );
138   return $datetime_parser->parse_datetime(shift);
139 }
140
141 sub format_datetime {
142   shift;
143   require DateTime::Format::Strptime;
144   $datetime_formatter ||= DateTime::Format::Strptime->new(
145     pattern  => $datetime_format_format,
146     on_error => 'croak',
147   );
148   return $datetime_formatter->format_datetime(shift);
149 }
150
151 1;
152
153 =head1 AUTHOR
154
155 See L<DBIx::Class/CONTRIBUTORS>.
156
157 =head1 LICENSE
158
159 You may distribute this code under the same terms as Perl itself.
160
161 =cut