Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / Base.pm
1 package DBIx::Class::Storage::DBI::Sybase::Base;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Storage::DBI/;
7 use mro 'c3';
8
9 =head1 NAME
10
11 DBIx::Class::Storage::DBI::Sybase::Base - Common functionality for drivers using
12 DBD::Sybase
13
14 =head1 DESCRIPTION
15
16 This is the base class for L<DBIx::Class::Storage::DBI::Sybase> and
17 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>. It provides some
18 utility methods related to L<DBD::Sybase> and the supported functions of the
19 database you are connecting to.
20
21 =head1 METHODS
22
23 =cut
24
25 sub _ping {
26   my $self = shift;
27
28   my $dbh = $self->_dbh or return 0;
29
30   local $dbh->{RaiseError} = 1;
31   eval {
32     $dbh->do('select 1');
33   };
34
35   return $@ ? 0 : 1;
36 }
37
38 =head2 placeholders_supported
39
40 Whether or not string placeholders work. Does not check for implicit conversion
41 errors, see L</placeholders_with_type_conversion_supported>.
42
43 =cut
44
45 sub placeholders_supported {
46   my $self = shift;
47   my $dbh  = $self->_get_dbh;
48
49   return eval {
50 # There's also $dbh->{syb_dynamic_supported} but it can be inaccurate for this
51 # purpose.
52     local $dbh->{PrintError} = 0;
53     local $dbh->{RaiseError} = 1;
54     $dbh->selectrow_array('select ?', {}, 1);
55   };
56 }
57
58 =head2 placeholders_with_type_conversion_supported 
59
60 Checks if placeholders bound to non-string types throw implicit type conversion
61 errors or not.
62
63 See L<DBIx::Class::Storage::DBI::Sybase/connect_call_set_auto_cast>.
64
65 =cut
66
67 sub placeholders_with_type_conversion_supported {
68   my $self = shift;
69   my $dbh  = $self->_dbh;
70
71   return eval {
72     local $dbh->{PrintError} = 0;
73     local $dbh->{RaiseError} = 1;
74 # this specifically tests a bind that is NOT a string
75     $dbh->selectrow_array('select 1 where 1 = ?', {}, 1);
76   };
77 }
78
79 sub _set_max_connect {
80   my $self = shift;
81   my $val  = shift || 256;
82
83   my $dsn = $self->_dbi_connect_info->[0];
84
85   return if ref($dsn) eq 'CODE';
86
87   if ($dsn !~ /maxConnect=/) {
88     $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
89     my $connected = defined $self->_dbh;
90     $self->disconnect;
91     $self->ensure_connected if $connected;
92   }
93 }
94
95 =head2 using_freetds
96
97 Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
98 the Sybase OpenClient libraries were used.
99
100 =cut
101
102 sub using_freetds {
103   my $self = shift;
104
105   return $self->_dbh->{syb_oc_version} =~ /freetds/i;
106 }
107
108 =head2 set_textsize
109
110 When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
111 use this function instead. It does:
112
113   $dbh->do("SET TEXTSIZE $bytes");
114
115 Takes the number of bytes, or uses the C<LongReadLen> value from your
116 L<DBIx::Class/connect_info> if omitted.
117
118 =cut
119
120 sub set_textsize {
121   my $self = shift;
122   my $text_size = shift ||
123     eval { $self->_dbi_connect_info->[-1]->{LongReadLen} };
124
125   return unless defined $text_size;
126
127   $self->_dbh->do("SET TEXTSIZE $text_size");
128 }
129
130 1;
131
132 =head1 AUTHORS
133
134 See L<DBIx::Class/CONTRIBUTORS>.
135
136 =head1 LICENSE
137
138 You may distribute this code under the same terms as Perl itself.
139
140 =cut