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