Switch cursor accessor to CAG's component_class type for autoloading
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ADO / Microsoft_SQL_Server.pm
CommitLineData
4ffa5700 1package DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server;
2
3use strict;
4use warnings;
5
6use base qw/
7 DBIx::Class::Storage::DBI::ADO
8 DBIx::Class::Storage::DBI::MSSQL
9/;
10use mro 'c3';
11
56dca25f 12=head1 NAME
13
14DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server - Support for Microsoft
15SQL Server via DBD::ADO
16
17=head1 SYNOPSIS
18
19This subclass supports MSSQL server connections via L<DBD::ADO>.
20
21=head1 DESCRIPTION
22
23The MSSQL specific functionality is provided by
24L<DBIx::Class::Storage::DBI::MSSQL>.
25
26=head1 EXAMPLE DSN
27
28 dbi:ADO:provider=sqlncli10;server=EEEBOX\SQLEXPRESS
29
30=head1 CAVEATS
31
32=head2 identities
33
34C<_identity_method> is set to C<@@identity>, as C<SCOPE_IDENTITY()> doesn't work
35with L<DBD::ADO>. See L<DBIx::Class::Storage::DBI::MSSQL/IMPLEMENTATION NOTES>
36for caveats regarding this.
37
38=head2 truncation bug
39
40There is a bug with MSSQL ADO providers where data gets truncated based on the
41size of the bind sizes in the first prepare call:
42
43L<https://rt.cpan.org/Ticket/Display.html?id=52048>
44
45The C<ado_size> workaround is used (see L<DBD::ADO/"ADO Providers">) with the
46approximate maximum size of the data_type of the bound column, or 8000 (maximum
47VARCHAR size) if the data_type is not available.
48
49This code is incomplete and may be buggy. Particularly, C<VARCHAR(MAX)> is not
50supported yet. The data_type list for other DBs is also incomplete. Please
51report problems (and send patches.)
52
53=head2 fractional seconds
54
55Fractional seconds with L<DBIx::Class::InflateColumn::DateTime> are not
56currently supported, datetimes are truncated at the second.
57
58=cut
59
4ffa5700 60sub _rebless {
61 my $self = shift;
62 $self->_identity_method('@@identity');
63}
64
b3857e35 65sub source_bind_attributes {
335b0f6c 66 my $self = shift;
67 my ($source) = @_;
8bcd9ece 68
335b0f6c 69 my $bind_attributes = $self->next::method(@_);
8bcd9ece 70
335b0f6c 71 foreach my $column ($source->columns) {
b3857e35 72 $bind_attributes->{$column}{ado_size} ||= 8000; # max VARCHAR
73 }
74
75 return $bind_attributes;
76}
77
78sub bind_attribute_by_data_type {
79 my ($self, $data_type) = @_;
80
48012f35 81 ($data_type = lc($data_type)) =~ s/\s+.*//;
82
b3857e35 83 my $max_size =
84 $self->_mssql_max_data_type_representation_size_in_bytes->{$data_type};
85
86 my $res = {};
87 $res->{ado_size} = $max_size if $max_size;
88
89 return $res;
90}
91
92# approximate
748eb620 93# XXX needs to support varchar(max) and varbinary(max)
b3857e35 94sub _mssql_max_data_type_representation_size_in_bytes {
95 my $self = shift;
96
97 my $blob_max = $self->_get_dbh->{LongReadLen} || 32768;
98
99 return +{
48012f35 100# MSSQL types
b3857e35 101 char => 8000,
102 varchar => 8000,
103 binary => 8000,
104 varbinary => 8000,
105 nchar => 8000,
106 nvarchar => 8000,
107 numeric => 100,
108 smallint => 100,
109 tinyint => 100,
110 smallmoney => 100,
111 bigint => 100,
112 bit => 100,
113 decimal => 100,
48012f35 114 integer => 100,
b3857e35 115 int => 100,
116 money => 100,
117 float => 100,
118 real => 100,
748eb620 119 uniqueidentifier => 100,
b3857e35 120 ntext => $blob_max,
121 text => $blob_max,
122 image => $blob_max,
123 date => 100,
124 datetime => 100,
125 datetime2 => 100,
126 datetimeoffset => 100,
127 smalldatetime => 100,
128 time => 100,
129 timestamp => 100,
48012f35 130 cursor => 100,
131 hierarchyid => 100,
132 sql_variant => 100,
133 table => 100,
134 xml => $blob_max, # ???
135
136# some non-MSSQL types
137 serial => 100,
138 bigserial => 100,
139 varchar2 => 8000,
140 blob => $blob_max,
141 clob => $blob_max,
b3857e35 142 }
8bcd9ece 143}
144
56dca25f 145sub datetime_parser_type {
146 'DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server::DateTime::Format'
147}
4ffa5700 148
56dca25f 149package # hide from PAUSE
150 DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server::DateTime::Format;
48012f35 151
56dca25f 152my $datetime_format = '%m/%d/%Y %I:%M:%S %p';
153my $datetime_parser;
48012f35 154
56dca25f 155sub parse_datetime {
156 shift;
157 require DateTime::Format::Strptime;
158 $datetime_parser ||= DateTime::Format::Strptime->new(
159 pattern => $datetime_format,
160 on_error => 'croak',
161 );
162 return $datetime_parser->parse_datetime(shift);
163}
48012f35 164
56dca25f 165sub format_datetime {
166 shift;
167 require DateTime::Format::Strptime;
168 $datetime_parser ||= DateTime::Format::Strptime->new(
169 pattern => $datetime_format,
170 on_error => 'croak',
171 );
172 return $datetime_parser->format_datetime(shift);
173}
48012f35 174
56dca25f 1751;
48012f35 176
4ffa5700 177=head1 AUTHOR
178
56dca25f 179See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
4ffa5700 180
181=head1 LICENSE
182
183You may distribute this code under the same terms as Perl itself.
184
185=cut
56dca25f 186# vim:sts=2 sw=2: