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