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
12sub _rebless {
13  my $self = shift;
14  $self->_identity_method('@@identity');
15}
16
17sub source_bind_attributes {
18  my $self = shift;
19  my ($source) = @_;
20
21  my $bind_attributes = $self->next::method(@_);
22
23  foreach my $column ($source->columns) {
24    $bind_attributes->{$column}{ado_size} ||= 8000; # max VARCHAR
25  }
26
27  return $bind_attributes;
28}
29
30sub bind_attribute_by_data_type {
31  my ($self, $data_type) = @_;
32
33  ($data_type = lc($data_type)) =~ s/\s+.*//;
34
35  my $max_size =
36    $self->_mssql_max_data_type_representation_size_in_bytes->{$data_type};
37
38  my $res = {};
39  $res->{ado_size} = $max_size if $max_size;
40
41  return $res;
42}
43
44# approximate
45# XXX needs to support varchar(max) and varbinary(max)
46sub _mssql_max_data_type_representation_size_in_bytes {
47  my $self = shift;
48
49  my $blob_max = $self->_get_dbh->{LongReadLen} || 32768;
50
51  return +{
52# MSSQL types
53    char => 8000,
54    varchar => 8000,
55    binary => 8000,
56    varbinary => 8000,
57    nchar => 8000,
58    nvarchar => 8000,
59    numeric => 100,
60    smallint => 100,
61    tinyint => 100,
62    smallmoney => 100,
63    bigint => 100,
64    bit => 100,
65    decimal => 100,
66    integer => 100,
67    int => 100,
68    money => 100,
69    float => 100,
70    real => 100,
71    uniqueidentifier => 100,
72    ntext => $blob_max,
73    text => $blob_max,
74    image => $blob_max,
75    date => 100,
76    datetime => 100,
77    datetime2 => 100,
78    datetimeoffset => 100,
79    smalldatetime => 100,
80    time => 100,
81    timestamp => 100,
82    cursor => 100,
83    hierarchyid => 100,
84    sql_variant => 100,
85    table => 100,
86    xml => $blob_max, # ???
87
88# some non-MSSQL types
89    serial => 100,
90    bigserial => 100,
91    varchar2 => 8000,
92    blob => $blob_max,
93    clob => $blob_max,
94  }
95}
96
971;
98
99=head1 NAME
100
101DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server - Support for Microsoft
102SQL Server via DBD::ADO
103
104=head1 SYNOPSIS
105
106This subclass supports MSSQL server connections via L<DBD::ADO>.
107
108=head1 DESCRIPTION
109
110The MSSQL specific functionality is provided by
111L<DBIx::Class::Storage::DBI::MSSQL>.
112
113=head2 CAVEATS
114
115=head3 identities
116
117C<_identity_method> is set to C<@@identity>, as C<SCOPE_IDENTITY()> doesn't work
118with L<DBD::ADO>. See L<DBIx::Class::Storage::DBI::MSSQL/IMPLEMENTATION NOTES>
119for caveats regarding this.
120
121=head3 truncation bug
122
123There is a bug with MSSQL ADO providers where data gets truncated based on the
124size of the bind sizes in the first prepare call:
125
126L<https://rt.cpan.org/Ticket/Display.html?id=52048>
127
128The C<ado_size> workaround is used (see L<DBD::ADO/"ADO Providers">) with the
129approximate maximum size of the data_type of the bound column, or 8000 (maximum
130VARCHAR size) if the data_type is not available.
131
132This code is incomplete and may be buggy. Particularly, C<VARCHAR(MAX)> is not
133supported yet. The data_type list for other DBs is also incomplete. Please
134report problems (and send patches.)
135
136=head1 AUTHOR
137
138See L<DBIx::Class/CONTRIBUTORS>.
139
140=head1 LICENSE
141
142You may distribute this code under the same terms as Perl itself.
143
144=cut
145