Centralize (and privatize) handling of freetds driver/version detection
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
CommitLineData
c1cac633 1package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
2use strict;
3use warnings;
4
aca3b4c3 5use base qw/
6 DBIx::Class::Storage::DBI::ODBC
7 DBIx::Class::Storage::DBI::MSSQL
8/;
2ad62d97 9use mro 'c3';
6298a324 10use Scalar::Util 'reftype';
ed7ab0f4 11use Try::Tiny;
70c28808 12use DBIx::Class::Carp;
fd323bf1 13use namespace::clean;
c1cac633 14
7b1b2582 15__PACKAGE__->mk_group_accessors(simple => qw/
16 _using_dynamic_cursors
17/);
c1cac633 18
19=head1 NAME
20
a89c6fc0 21DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
22to Microsoft SQL Server over ODBC
c1cac633 23
24=head1 DESCRIPTION
25
5a77aa8b 26This class implements support specific to Microsoft SQL Server over ODBC. It is
27loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it detects a
28MSSQL back-end.
c1cac633 29
5a77aa8b 30Most of the functionality is provided from the superclass
31L<DBIx::Class::Storage::DBI::MSSQL>.
c1cac633 32
dcc7ddff 33=head1 USAGE NOTES
34
35=head2 Basic Linux Setup (Debian)
36
37 sudo aptitude install tdsodbc libdbd-odbc-perl unixodbc
38
39In case it is not already there put the following in C</etc/odbcinst.ini>:
40
41 [FreeTDS]
42 Description = FreeTDS
43 Driver = /usr/lib/odbc/libtdsodbc.so
44 Setup = /usr/lib/odbc/libtdsS.so
45 UsageCount = 1
46
47Set your C<$dsn> in L<connect_info|DBIx::Class::Storage::DBI/connect_info> as follows:
48
49 dbi:ODBC:server=<my.host.name>;port=1433;driver=FreeTDS;tds_version=8.0
50
51If you use the EasySoft driver (L<http://www.easysoft.com>):
52
53 dbi:ODBC:server=<my.host.name>;port=1433;driver=Easysoft ODBC-SQL Server
54
55=head2 Basic Windows Setup
56
57Use the following C<$dsn> for the Microsoft ODBC driver:
58
59 dbi:ODBC:driver={SQL Server};server=SERVER\SQL_SERVER_INSTANCE_NAME
60
61And for the Native Client:
62
63 dbi:ODBC:driver={SQL Server Native Client 10.0};server=SERVER\SQL_SERVER_INSTANCE_NAME
64
65Go into Control Panel -> System and Security -> Administrative Tools -> Data
66Sources (ODBC) to check driver names and to set up data sources.
67
68Use System DSNs, not User DSNs if you want to use DSNs.
69
70If you set up a DSN, use the following C<$dsn> for
71L<connect_info|DBIx::Class::Storage::DBI/connect_info>:
72
73 dbi:ODBC:dsn=MY_DSN
74
7b1b2582 75=head1 MULTIPLE ACTIVE STATEMENTS
76
77The following options are alternative ways to enable concurrent executing
384b8bce 78statement support. Each has its own advantages and drawbacks and works on
79different platforms. Read each section carefully.
80
81In order of preference, they are:
82
83=over 8
84
1db83fb9 85=item * L<mars|/connect_call_use_mars>
384b8bce 86
1db83fb9 87=item * L<dynamic_cursors|/connect_call_use_dynamic_cursors>
384b8bce 88
1db83fb9 89=item * L<server_cursors|/connect_call_use_server_cursors>
384b8bce 90
91=back
92
93=head1 METHODS
94
95=head2 connect_call_use_mars
96
97Use as:
98
99 on_connect_call => 'use_mars'
100
1db83fb9 101in your connection info, or alternatively specify it directly:
102
103 Your::Schema->connect (
104 $original_dsn . '; MARS_Connection=Yes',
105 $user,
106 $pass,
107 \%attrs,
108 )
109
384b8bce 110Use to enable a feature of SQL Server 2005 and later, "Multiple Active Result
111Sets". See L<DBD::ODBC::FAQ/Does DBD::ODBC support Multiple Active Statements?>
112for more information.
113
114This does not work on FreeTDS drivers at the time of this writing, and only
115works with the Native Client, later versions of the Windows MS ODBC driver, and
116the Easysoft driver.
117
118=cut
119
120sub connect_call_use_mars {
121 my $self = shift;
122
123 my $dsn = $self->_dbi_connect_info->[0];
124
125 if (ref($dsn) eq 'CODE') {
126 $self->throw_exception('cannot change the DBI DSN on a CODE ref connect_info');
127 }
128
129 if ($dsn !~ /MARS_Connection=/) {
aca3b4c3 130 if ($self->_using_freetds) {
384b8bce 131 $self->throw_exception('FreeTDS does not support MARS at the time of '
132 .'writing.');
133 }
134
135 if (exists $self->_server_info->{normalized_dbms_version} &&
136 $self->_server_info->{normalized_dbms_version} < 9) {
137 $self->throw_exception('SQL Server 2005 or later required to use MARS.');
138 }
139
140 if (my ($data_source) = $dsn =~ /^dbi:ODBC:([\w-]+)\z/i) { # prefix with DSN
a469c045 141 warn "Bare DSN in ODBC connect string, rewriting as 'dsn=$data_source'"
142 ." for MARS\n";
143 $dsn = "dbi:ODBC:dsn=$data_source";
384b8bce 144 }
145
146 $self->_dbi_connect_info->[0] = "$dsn;MARS_Connection=Yes";
147 $self->disconnect;
148 $self->ensure_connected;
149 }
150}
151
152sub connect_call_use_MARS {
153 carp "'connect_call_use_MARS' has been deprecated, use "
154 ."'connect_call_use_mars' instead.";
155 shift->connect_call_use_mars(@_)
156}
7b1b2582 157
158=head2 connect_call_use_dynamic_cursors
159
160Use as:
161
162 on_connect_call => 'use_dynamic_cursors'
163
1db83fb9 164Which will add C<< odbc_cursortype => 2 >> to your DBI connection
165attributes, or alternatively specify the necessary flag directly:
7b1b2582 166
1db83fb9 167 Your::Schema->connect (@dsn, { ... odbc_cursortype => 2 })
7b1b2582 168
1db83fb9 169See L<DBD::ODBC/odbc_cursortype> for more information.
7b1b2582 170
41dd5d30 171If you're using FreeTDS, C<tds_version> must be set to at least C<8.0>.
172
173This will not work with CODE ref connect_info's.
7b1b2582 174
1db83fb9 175B<WARNING:> on FreeTDS (and maybe some other drivers) this will break
176C<SCOPE_IDENTITY()>, and C<SELECT @@IDENTITY> will be used instead, which on SQL
177Server 2005 and later will return erroneous results on tables which have an on
178insert trigger that inserts into another table with an C<IDENTITY> column.
7b1b2582 179
9ffaf8a3 180B<WARNING:> on FreeTDS, changes made in one statement (e.g. an insert) may not
181be visible from a following statement (e.g. a select.)
182
25d3127d 183B<WARNING:> FreeTDS versions > 0.82 seem to have completely broken the ODBC
184protocol. DBIC will not allow dynamic cursor support with such versions to
185protect your data. Please hassle the authors of FreeTDS to act on the bugs that
186make their driver not overly usable with DBD::ODBC.
187
7b1b2582 188=cut
189
190sub connect_call_use_dynamic_cursors {
191 my $self = shift;
192
9ffaf8a3 193 if (($self->_dbic_connect_attributes->{odbc_cursortype} || 0) < 2) {
7b1b2582 194
9ffaf8a3 195 my $dbi_inf = $self->_dbi_connect_info;
196
197 $self->throw_exception ('Cannot set DBI attributes on a CODE ref connect_info')
198 if ref($dbi_inf->[0]) eq 'CODE';
7b1b2582 199
1db83fb9 200 # reenter connection information with the attribute re-set
9ffaf8a3 201 $dbi_inf->[3] = {} if @$dbi_inf <= 3;
202 $dbi_inf->[3]{odbc_cursortype} = 2;
203
204 $self->_dbi_connect_info($dbi_inf);
205
75517ea9 206 $self->disconnect; # resetting dbi attrs, so have to reconnect
207 $self->ensure_connected;
7b1b2582 208 }
209}
210
1db83fb9 211sub _run_connection_actions {
7b1b2582 212 my $self = shift;
213
1db83fb9 214 # keep the dynamic_cursors_support and driver-state in sync
215 # on every reconnect
216 my $use_dyncursors = ($self->_dbic_connect_attributes->{odbc_cursortype} || 0) > 1;
1a58752c 217 if (
1db83fb9 218 $use_dyncursors
219 xor
220 !!$self->_using_dynamic_cursors
1a58752c 221 ) {
1db83fb9 222 if ($use_dyncursors) {
223 try {
224 my $dbh = $self->_dbh;
225 local $dbh->{RaiseError} = 1;
226 local $dbh->{PrintError} = 0;
227 $dbh->do('SELECT @@IDENTITY');
228 } catch {
229 $self->throw_exception (
230 'Your drivers do not seem to support dynamic cursors (odbc_cursortype => 2).'
231 . (
aca3b4c3 232 $self->_using_freetds
1db83fb9 233 ? ' If you are using FreeTDS, make sure to set tds_version to 8.0 or greater.'
234 : ''
235 )
236 );
237 };
238
239 $self->_using_dynamic_cursors(1);
240 $self->_identity_method('@@identity');
aca3b4c3 241 $self->_no_scope_identity_query($self->_using_freetds);
1db83fb9 242 }
243 else {
244 $self->_using_dynamic_cursors(0);
245 $self->_identity_method(undef);
25d3127d 246 $self->_no_scope_identity_query(undef);
1db83fb9 247 }
384b8bce 248 }
1db83fb9 249
250 $self->next::method (@_);
25d3127d 251
252 # freetds is too damn broken, some fixups
aca3b4c3 253 if ($self->_using_freetds) {
25d3127d 254
255 # no dynamic cursors starting from 0.83
256 if ($self->_using_dynamic_cursors) {
aca3b4c3 257 my $fv = $self->_using_freetds_version || 999; # assume large if can't be determined
25d3127d 258 $self->throw_exception(
259 'Dynamic cursors (odbc_cursortype => 2) are not supported with FreeTDS > 0.82 '
260 . "(you have $fv). Please hassle FreeTDS authors to fix the outstanding bugs in "
261 . 'their driver.'
262 ) if $fv > 0.82
263 }
264
265 # FreeTDS is too broken wrt execute_for_fetch batching
266 # just disable it outright until things quiet down
267 $self->_get_dbh->{odbc_disable_array_operations} = 1;
268 }
7b1b2582 269}
270
271=head2 connect_call_use_server_cursors
272
273Use as:
274
275 on_connect_call => 'use_server_cursors'
276
277May allow multiple active select statements. See
278L<DBD::ODBC/odbc_SQL_ROWSET_SIZE> for more information.
279
280Takes an optional parameter for the value to set the attribute to, default is
281C<2>.
282
283B<WARNING>: this does not work on all versions of SQL Server, and may lock up
284your database!
285
384b8bce 286At the time of writing, this option only works on Microsoft's Windows drivers,
287later versions of the ODBC driver and the Native Client driver.
288
7b1b2582 289=cut
290
291sub connect_call_use_server_cursors {
292 my $self = shift;
293 my $sql_rowset_size = shift || 2;
294
384b8bce 295 if ($^O !~ /win32|cygwin/i) {
296 $self->throw_exception('Server cursors only work on Windows platforms at '
297 .'the time of writing.');
298 }
299
9ae966b9 300 $self->_get_dbh->{odbc_SQL_ROWSET_SIZE} = $sql_rowset_size;
7b1b2582 301}
302
7b1b2582 3031;
304
5a77aa8b 305=head1 AUTHOR
c1cac633 306
9ffaf8a3 307See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
c1cac633 308
309=head1 LICENSE
310
311You may distribute this code under the same terms as Perl itself.
312
313=cut
9ffaf8a3 314# vim:sw=2 sts=2 et