Multiple code/test/doc improvements for MSSQL over DBD::ADO
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ADO / Microsoft_SQL_Server.pm
1 package DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server;
2
3 use strict;
4 use warnings;
5
6 use base qw/
7   DBIx::Class::Storage::DBI::ADO
8   DBIx::Class::Storage::DBI::MSSQL
9 /;
10 use mro 'c3';
11
12 =head1 NAME
13
14 DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server - Support for Microsoft
15 SQL Server via DBD::ADO
16
17 =head1 SYNOPSIS
18
19 This subclass supports MSSQL server connections via L<DBD::ADO>.
20
21 =head1 DESCRIPTION
22
23 The MSSQL specific functionality is provided by
24 L<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
34 C<_identity_method> is set to C<@@identity>, as C<SCOPE_IDENTITY()> doesn't work
35 with L<DBD::ADO>. See L<DBIx::Class::Storage::DBI::MSSQL/IMPLEMENTATION NOTES>
36 for caveats regarding this.
37
38 =head2 truncation bug
39
40 There is a bug with MSSQL ADO providers where data gets truncated based on the
41 size of the bind sizes in the first prepare call:
42
43 L<https://rt.cpan.org/Ticket/Display.html?id=52048>
44
45 The C<ado_size> workaround is used (see L<DBD::ADO/"ADO Providers">) with the
46 approximate maximum size of the data_type of the bound column, or 8000 (maximum
47 VARCHAR size) if the data_type is not available.
48
49 This code is incomplete and may be buggy. Particularly, C<VARCHAR(MAX)> is not
50 supported yet. The data_type list for other DBs is also incomplete. Please
51 report problems (and send patches.)
52
53 =head2 fractional seconds
54
55 Fractional seconds with L<DBIx::Class::InflateColumn::DateTime> are not
56 currently supported, datetimes are truncated at the second.
57
58 =cut
59
60 sub _rebless {
61   my $self = shift;
62   $self->_identity_method('@@identity');
63 }
64
65 sub source_bind_attributes {
66   my $self = shift;
67   my ($source) = @_;
68
69   my $bind_attributes = $self->next::method(@_);
70
71   foreach my $column ($source->columns) {
72     $bind_attributes->{$column}{ado_size} ||= 8000; # max VARCHAR
73   }
74
75   return $bind_attributes;
76 }
77
78 sub bind_attribute_by_data_type {
79   my ($self, $data_type) = @_;
80
81   ($data_type = lc($data_type)) =~ s/\s+.*//;
82
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
93 # XXX needs to support varchar(max) and varbinary(max)
94 sub _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 +{
100 # MSSQL types
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,
114     integer => 100,
115     int => 100,
116     money => 100,
117     float => 100,
118     real => 100,
119     uniqueidentifier => 100,
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,
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,
142   }
143 }
144
145 sub datetime_parser_type {
146   'DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server::DateTime::Format'
147 }
148
149 package # hide from PAUSE
150   DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server::DateTime::Format;
151
152 my $datetime_format = '%m/%d/%Y %I:%M:%S %p';
153 my $datetime_parser;
154
155 sub 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 }
164
165 sub 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 }
174
175 1;
176
177 =head1 AUTHOR
178
179 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
180
181 =head1 LICENSE
182
183 You may distribute this code under the same terms as Perl itself.
184
185 =cut
186 # vim:sts=2 sw=2: