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