Clear up some legacy cruft and straighten inheritance
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / Common.pm
1 package DBIx::Class::Storage::DBI::Sybase::Common;
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::Common - 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   local $dbh->{PrintError} = 0;
32
33   if ($dbh->{syb_no_child_con}) {
34 # ping is impossible with an active statement, we return false if so
35     my $ping = eval { $dbh->ping };
36     return $@ ? 0 : $ping;
37   }
38
39   eval {
40 # XXX if the main connection goes stale, does opening another for this statement
41 # really determine anything?
42     $dbh->do('select 1');
43   };
44
45   return $@ ? 0 : 1;
46 }
47
48 sub _set_max_connect {
49   my $self = shift;
50   my $val  = shift || 256;
51
52   my $dsn = $self->_dbi_connect_info->[0];
53
54   return if ref($dsn) eq 'CODE';
55
56   if ($dsn !~ /maxConnect=/) {
57     $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
58     my $connected = defined $self->_dbh;
59     $self->disconnect;
60     $self->ensure_connected if $connected;
61   }
62 }
63
64 =head2 using_freetds
65
66 Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
67 the Sybase OpenClient libraries were used.
68
69 =cut
70
71 sub using_freetds {
72   my $self = shift;
73
74   return $self->_get_dbh->{syb_oc_version} =~ /freetds/i;
75 }
76
77 =head2 set_textsize
78
79 When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
80 use this function instead. It does:
81
82   $dbh->do("SET TEXTSIZE $bytes");
83
84 Takes the number of bytes, or uses the C<LongReadLen> value from your
85 L<DBIx::Class/connect_info> if omitted, lastly falls back to the C<32768> which
86 is the L<DBD::Sybase> default.
87
88 =cut
89
90 sub set_textsize {
91   my $self = shift;
92   my $text_size = shift ||
93     eval { $self->_dbi_connect_info->[-1]->{LongReadLen} } ||
94     32768; # the DBD::Sybase default
95
96   return unless defined $text_size;
97
98   $self->_dbh->do("SET TEXTSIZE $text_size");
99 }
100
101 1;
102
103 =head1 AUTHORS
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