Commit | Line | Data |
c1cac633 |
1 | package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server; |
2 | use strict; |
3 | use warnings; |
4 | |
eb0323df |
5 | use base qw/DBIx::Class::Storage::DBI::MSSQL/; |
2ad62d97 |
6 | use mro 'c3'; |
7 | |
7b1b2582 |
8 | use Carp::Clan qw/^DBIx::Class/; |
9 | use List::Util(); |
10 | use Scalar::Util (); |
c1cac633 |
11 | |
7b1b2582 |
12 | __PACKAGE__->mk_group_accessors(simple => qw/ |
13 | _using_dynamic_cursors |
14 | /); |
c1cac633 |
15 | |
16 | =head1 NAME |
17 | |
a89c6fc0 |
18 | DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific |
19 | to Microsoft SQL Server over ODBC |
c1cac633 |
20 | |
21 | =head1 DESCRIPTION |
22 | |
5a77aa8b |
23 | This class implements support specific to Microsoft SQL Server over ODBC. It is |
24 | loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it detects a |
25 | MSSQL back-end. |
c1cac633 |
26 | |
5a77aa8b |
27 | Most of the functionality is provided from the superclass |
28 | L<DBIx::Class::Storage::DBI::MSSQL>. |
c1cac633 |
29 | |
7b1b2582 |
30 | =head1 MULTIPLE ACTIVE STATEMENTS |
31 | |
32 | The following options are alternative ways to enable concurrent executing |
33 | statement support. Each has its own advantages and drawbacks. |
34 | |
35 | =head2 connect_call_use_dynamic_cursors |
36 | |
37 | Use as: |
38 | |
39 | on_connect_call => 'use_dynamic_cursors' |
40 | |
41 | in your L<DBIx::Class::Storage::DBI/connect_info> as one way to enable multiple |
42 | concurrent statements. |
43 | |
44 | Will add C<< odbc_cursortype => 2 >> to your DBI connection attributes. See |
45 | L<DBD::ODBC/odbc_cursortype> for more information. |
46 | |
41dd5d30 |
47 | Alternatively, you can add it yourself and dynamic cursor support will be |
48 | automatically enabled. |
7b1b2582 |
49 | |
41dd5d30 |
50 | If you're using FreeTDS, C<tds_version> must be set to at least C<8.0>. |
51 | |
52 | This will not work with CODE ref connect_info's. |
7b1b2582 |
53 | |
54 | B<WARNING:> this will break C<SCOPE_IDENTITY()>, and C<SELECT @@IDENTITY> will |
55 | be used instead, which on SQL Server 2005 and later will return erroneous |
56 | results on tables which have an on insert trigger that inserts into another |
57 | table with an C<IDENTITY> column. |
58 | |
59 | =cut |
60 | |
61 | sub connect_call_use_dynamic_cursors { |
62 | my $self = shift; |
63 | |
64 | if (ref($self->_dbi_connect_info->[0]) eq 'CODE') { |
65 | croak 'cannot set DBI attributes on a CODE ref connect_info'; |
66 | } |
67 | |
68 | my $dbi_attrs = $self->_dbi_connect_info->[-1]; |
69 | |
70 | unless (ref($dbi_attrs) && Scalar::Util::reftype($dbi_attrs) eq 'HASH') { |
71 | $dbi_attrs = {}; |
72 | push @{ $self->_dbi_connect_info }, $dbi_attrs; |
73 | } |
74 | |
75 | if (not exists $dbi_attrs->{odbc_cursortype}) { |
76 | # turn on support for multiple concurrent statements, unless overridden |
77 | $dbi_attrs->{odbc_cursortype} = 2; |
78 | my $connected = defined $self->_dbh; |
79 | $self->disconnect; |
80 | $self->ensure_connected if $connected; |
81 | $self->_set_dynamic_cursors; |
82 | } |
83 | } |
84 | |
85 | sub _set_dynamic_cursors { |
86 | my $self = shift; |
41dd5d30 |
87 | my $dbh = $self->_dbh; |
88 | |
89 | eval { |
90 | local $dbh->{RaiseError} = 1; |
91 | local $dbh->{PrintError} = 0; |
92 | $dbh->do('SELECT @@IDENTITY'); |
93 | }; |
94 | if ($@) { |
95 | croak <<'EOF'; |
96 | |
97 | Your drivers do not seem to support dynamic cursors (odbc_cursortype => 2), |
98 | if you're using FreeTDS, make sure to set tds_version to 8.0 or greater. |
99 | EOF |
100 | } |
101 | |
7b1b2582 |
102 | $self->_using_dynamic_cursors(1); |
103 | $self->_identity_method('@@identity'); |
104 | } |
105 | |
106 | sub _rebless { |
107 | no warnings 'uninitialized'; |
108 | my $self = shift; |
109 | |
110 | if (ref($self->_dbi_connect_info->[0]) ne 'CODE' && |
111 | eval { $self->_dbi_connect_info->[-1]{odbc_cursortype} } == 2) { |
112 | $self->_set_dynamic_cursors; |
113 | return; |
114 | } |
115 | |
116 | $self->_using_dynamic_cursors(0); |
117 | } |
118 | |
119 | =head2 connect_call_use_server_cursors |
120 | |
121 | Use as: |
122 | |
123 | on_connect_call => 'use_server_cursors' |
124 | |
125 | May allow multiple active select statements. See |
126 | L<DBD::ODBC/odbc_SQL_ROWSET_SIZE> for more information. |
127 | |
128 | Takes an optional parameter for the value to set the attribute to, default is |
129 | C<2>. |
130 | |
131 | B<WARNING>: this does not work on all versions of SQL Server, and may lock up |
132 | your database! |
133 | |
134 | =cut |
135 | |
136 | sub connect_call_use_server_cursors { |
137 | my $self = shift; |
138 | my $sql_rowset_size = shift || 2; |
139 | |
140 | $self->_dbh->{odbc_SQL_ROWSET_SIZE} = $sql_rowset_size; |
141 | } |
142 | |
143 | =head2 connect_call_use_mars |
144 | |
145 | Use as: |
146 | |
147 | on_connect_call => 'use_mars' |
148 | |
149 | Use to enable a feature of SQL Server 2005 and later, "Multiple Active Result |
150 | Sets". See L<DBD::ODBC::FAQ/Does DBD::ODBC support Multiple Active Statements?> |
151 | for more information. |
152 | |
153 | B<WARNING>: This has implications for the way transactions are handled. |
154 | |
155 | =cut |
156 | |
157 | sub connect_call_use_mars { |
158 | my $self = shift; |
159 | |
160 | my $dsn = $self->_dbi_connect_info->[0]; |
161 | |
162 | if (ref($dsn) eq 'CODE') { |
163 | croak 'cannot change the DBI DSN on a CODE ref connect_info'; |
164 | } |
165 | |
166 | if ($dsn !~ /MARS_Connection=/) { |
167 | $self->_dbi_connect_info->[0] = "$dsn;MARS_Connection=Yes"; |
168 | my $connected = defined $self->_dbh; |
169 | $self->disconnect; |
170 | $self->ensure_connected if $connected; |
171 | } |
172 | } |
173 | |
174 | 1; |
175 | |
5a77aa8b |
176 | =head1 AUTHOR |
c1cac633 |
177 | |
5a77aa8b |
178 | See L<DBIx::Class/CONTRIBUTORS>. |
c1cac633 |
179 | |
180 | =head1 LICENSE |
181 | |
182 | You may distribute this code under the same terms as Perl itself. |
183 | |
184 | =cut |
259c0e40 |
185 | # vim: sw=2 sts=2 |