85898fa97da750fb460a7192dc4d21be37274d31
[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 sub _rebless {
13   my $self = shift;
14   $self->_identity_method('@@identity');
15 }
16
17 sub source_bind_attributes {
18   my ($self, $source) = @_;
19
20   my $bind_attributes;
21   foreach my $column ($source->columns) {
22
23     my $data_type = $source->column_info($column)->{data_type} || '';
24     $bind_attributes->{$column} = $self->bind_attribute_by_data_type($data_type)
25       if $data_type;
26     $bind_attributes->{$column}{ado_size} ||= 8000; # max VARCHAR
27   }
28
29   return $bind_attributes;
30 }
31
32 sub bind_attribute_by_data_type {
33   my ($self, $data_type) = @_;
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)
46 sub _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     char => 8000,
53     varchar => 8000,
54     binary => 8000,
55     varbinary => 8000,
56     nchar => 8000,
57     nvarchar => 8000,
58     numeric => 100,
59     smallint => 100,
60     tinyint => 100,
61     smallmoney => 100,
62     bigint => 100,
63     bit => 100,
64     decimal => 100,
65     int => 100,
66     money => 100,
67     float => 100,
68     real => 100,
69     uniqueidentifier => 100,
70     ntext => $blob_max,
71     text => $blob_max,
72     image => $blob_max,
73     date => 100,
74     datetime => 100,
75     datetime2 => 100,
76     datetimeoffset => 100,
77     smalldatetime => 100,
78     time => 100,
79     timestamp => 100,
80   }
81 }
82
83 1;
84
85 =head1 NAME
86
87 DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server - Support for Microsoft
88 SQL Server via DBD::ADO
89
90 =head1 SYNOPSIS
91
92 This subclass supports MSSQL server connections via L<DBD::ADO>.
93
94 =head1 DESCRIPTION
95
96 The MSSQL specific functionality is provided by
97 L<DBIx::Class::Storage::DBI::MSSQL>.
98
99 C<_identity_method> is set to C<@@identity>, as C<SCOPE_IDENTITY()> doesn't work
100 with L<DBD::ADO>. See L<DBIx::Class::Storage::DBI::MSSQL/IMPLEMENTATION NOTES>
101 for caveats regarding this.
102
103 =head1 AUTHOR
104
105 See L<DBIx::Class/CONTRIBUTORS>.
106
107 =head1 LICENSE
108
109 You may distribute this code under the same terms as Perl itself.
110
111 =cut