add note about where to get Schema::Loader
[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->_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 =cut
61
62 sub placeholders_with_type_conversion_supported {
63   my $self = shift;
64   my $dbh  = $self->_dbh;
65
66   return eval {
67     local $dbh->{PrintError} = 0;
68     local $dbh->{RaiseError} = 1;
69 # this specifically tests a bind that is NOT a string
70     $dbh->selectrow_array('select 1 where 1 = ?', {}, 1);
71   };
72 }
73
74 sub _set_max_connect {
75   my $self = shift;
76   my $val  = shift || 256;
77
78   my $dsn = $self->_dbi_connect_info->[0];
79
80   return if ref($dsn) eq 'CODE';
81
82   if ($dsn !~ /maxConnect=/) {
83     $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
84     my $connected = defined $self->_dbh;
85     $self->disconnect;
86     $self->ensure_connected if $connected;
87   }
88 }
89
90 =head2 using_freetds
91
92 Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
93 the Sybase OpenClient libraries were used.
94
95 =cut
96
97 sub using_freetds {
98   my $self = shift;
99
100   return $self->_dbh->{syb_oc_version} =~ /freetds/i;
101 }
102
103 =head2 set_textsize
104
105 When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
106 use this function instead. It does:
107
108   $dbh->do("SET TEXTSIZE $bytes");
109
110 Takes the number of bytes, or uses the C<LongReadLen> value from your
111 L<DBIx::Class/connect_info> if omitted.
112
113 =cut
114
115 sub set_textsize {
116   my $self = shift;
117   my $text_size = shift ||
118     eval { $self->_dbi_connect_info->[-1]->{LongReadLen} };
119
120   return unless defined $text_size;
121
122   $self->_dbh->do("SET TEXTSIZE $text_size");
123 }
124
125 1;
126
127 =head1 AUTHORS
128
129 See L<DBIx::Class/CONTRIBUTORS>.
130
131 =head1 LICENSE
132
133 You may distribute this code under the same terms as Perl itself.
134
135 =cut