8c5f988dcfaf00cf57cc28708a40f6a37cde6076
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
1 package DBIx::Class::Storage::DBI::Sybase;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Storage::DBI/;
7
8 =head1 NAME
9
10 DBIx::Class::Storage::DBI::Sybase - Base class for drivers using
11 L<DBD::Sybase>
12
13 =head1 DESCRIPTION
14
15 This is the base class/dispatcher for Storage's designed to work with
16 L<DBD::Sybase>
17
18 =head1 METHODS
19
20 =cut
21
22 sub _rebless {
23   my $self = shift;
24
25   my $dbtype = eval {
26     @{$self->_get_dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
27   };
28
29   $self->throw_exception("Unable to estable connection to determine database type: $@")
30     if $@;
31
32   if ($dbtype) {
33     $dbtype =~ s/\W/_/gi;
34
35     # saner class name
36     $dbtype = 'ASE' if $dbtype eq 'SQL_Server';
37
38     my $subclass = __PACKAGE__ . "::$dbtype";
39     if ($self->load_optional_class($subclass)) {
40       bless $self, $subclass;
41       $self->_rebless;
42     }
43   }
44 }
45
46 sub _ping {
47   my $self = shift;
48
49   my $dbh = $self->_dbh or return 0;
50
51   local $dbh->{RaiseError} = 1;
52   local $dbh->{PrintError} = 0;
53
54   if ($dbh->{syb_no_child_con}) {
55 # if extra connections are not allowed, then ->ping is reliable
56     my $ping = eval { $dbh->ping };
57     return $@ ? 0 : $ping;
58   }
59
60   eval {
61 # XXX if the main connection goes stale, does opening another for this statement
62 # really determine anything?
63     $dbh->do('select 1');
64   };
65
66   return $@ ? 0 : 1;
67 }
68
69 sub _set_max_connect {
70   my $self = shift;
71   my $val  = shift || 256;
72
73   my $dsn = $self->_dbi_connect_info->[0];
74
75   return if ref($dsn) eq 'CODE';
76
77   if ($dsn !~ /maxConnect=/) {
78     $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
79     my $connected = defined $self->_dbh;
80     $self->disconnect;
81     $self->ensure_connected if $connected;
82   }
83 }
84
85 =head2 using_freetds
86
87 Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
88 the Sybase OpenClient libraries were used.
89
90 =cut
91
92 sub using_freetds {
93   my $self = shift;
94
95   return $self->_get_dbh->{syb_oc_version} =~ /freetds/i;
96 }
97
98 =head2 set_textsize
99
100 When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
101 use this function instead. It does:
102
103   $dbh->do("SET TEXTSIZE $bytes");
104
105 Takes the number of bytes, or uses the C<LongReadLen> value from your
106 L<connect_info|DBIx::Class::Storage::DBI/connect_info> if omitted, lastly falls
107 back to the C<32768> which is the L<DBD::Sybase> default.
108
109 =cut
110
111 sub set_textsize {
112   my $self = shift;
113   my $text_size = shift ||
114     eval { $self->_dbi_connect_info->[-1]->{LongReadLen} } ||
115     32768; # the DBD::Sybase default
116
117   return unless defined $text_size;
118
119   $self->_dbh->do("SET TEXTSIZE $text_size");
120 }
121
122 1;
123
124 =head1 AUTHORS
125
126 See L<DBIx::Class/CONTRIBUTORS>.
127
128 =head1 LICENSE
129
130 You may distribute this code under the same terms as Perl itself.
131
132 =cut