Switch sql_maker_class and datetime_parser_type to component_class accessors
[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 __PACKAGE__->datetime_parser_type (
61   'DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server::DateTime::Format'
62 );
63
64 sub _rebless {
65   my $self = shift;
66   $self->_identity_method('@@identity');
67 }
68
69 sub source_bind_attributes {
70   my $self = shift;
71   my ($source) = @_;
72
73   my $bind_attributes = $self->next::method(@_);
74
75   foreach my $column ($source->columns) {
76     $bind_attributes->{$column}{ado_size} ||= 8000; # max VARCHAR
77   }
78
79   return $bind_attributes;
80 }
81
82 sub bind_attribute_by_data_type {
83   my ($self, $data_type) = @_;
84
85   ($data_type = lc($data_type)) =~ s/\s+.*//;
86
87   my $max_size =
88     $self->_mssql_max_data_type_representation_size_in_bytes->{$data_type};
89
90   my $res = {};
91   $res->{ado_size} = $max_size if $max_size;
92
93   return $res;
94 }
95
96 # approximate
97 # XXX needs to support varchar(max) and varbinary(max)
98 sub _mssql_max_data_type_representation_size_in_bytes {
99   my $self = shift;
100
101   my $blob_max = $self->_get_dbh->{LongReadLen} || 32768;
102
103   return +{
104 # MSSQL types
105     char => 8000,
106     varchar => 8000,
107     binary => 8000,
108     varbinary => 8000,
109     nchar => 8000,
110     nvarchar => 8000,
111     numeric => 100,
112     smallint => 100,
113     tinyint => 100,
114     smallmoney => 100,
115     bigint => 100,
116     bit => 100,
117     decimal => 100,
118     integer => 100,
119     int => 100,
120     money => 100,
121     float => 100,
122     real => 100,
123     uniqueidentifier => 100,
124     ntext => $blob_max,
125     text => $blob_max,
126     image => $blob_max,
127     date => 100,
128     datetime => 100,
129     datetime2 => 100,
130     datetimeoffset => 100,
131     smalldatetime => 100,
132     time => 100,
133     timestamp => 100,
134     cursor => 100,
135     hierarchyid => 100,
136     sql_variant => 100,
137     table => 100,
138     xml => $blob_max, # ???
139
140 # some non-MSSQL types
141     serial => 100,
142     bigserial => 100,
143     varchar2 => 8000,
144     blob => $blob_max,
145     clob => $blob_max,
146   }
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: