3fe093001230914d2bb82efb814eec1e7d4fdec4
[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 use Try::Tiny;
6
7 use base qw/DBIx::Class::Storage::DBI/;
8
9 =head1 NAME
10
11 DBIx::Class::Storage::DBI::Sybase - Base class for drivers using
12 L<DBD::Sybase>
13
14 =head1 DESCRIPTION
15
16 This is the base class/dispatcher for Storage's designed to work with
17 L<DBD::Sybase>
18
19 =head1 METHODS
20
21 =cut
22
23 sub _rebless {
24   my $self = shift;
25
26   my $dbtype;
27   try {
28     $dbtype = @{$self->_get_dbh->selectrow_arrayref(qq{sp_server_info \@attribute_id=1})}[2]
29   } catch {
30     $self->throw_exception("Unable to estable connection to determine database type: $_")
31   };
32
33   if ($dbtype) {
34     $dbtype =~ s/\W/_/gi;
35
36     # saner class name
37     $dbtype = 'ASE' if $dbtype eq 'SQL_Server';
38
39     my $subclass = __PACKAGE__ . "::$dbtype";
40     if ($self->load_optional_class($subclass)) {
41       bless $self, $subclass;
42       $self->_rebless;
43     }
44   }
45 }
46
47 sub _ping {
48   my $self = shift;
49
50   my $dbh = $self->_dbh or return 0;
51
52   local $dbh->{RaiseError} = 1;
53   local $dbh->{PrintError} = 0;
54
55   if ($dbh->{syb_no_child_con}) {
56 # if extra connections are not allowed, then ->ping is reliable
57     my $alive;
58     try { $alive = $dbh->ping } catch { $alive = 0 };
59     return $alive;
60   }
61
62   my $rc = 1;
63   try {
64 # XXX if the main connection goes stale, does opening another for this statement
65 # really determine anything?
66     $dbh->do('select 1');
67   } catch {
68     $rc = 0;
69   };
70
71   return $rc;
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->_get_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<connect_info|DBIx::Class::Storage::DBI/connect_info> if omitted, lastly falls
112 back to the C<32768> which is the L<DBD::Sybase> default.
113
114 =cut
115
116 sub set_textsize {
117   my $self = shift;
118   my $text_size =
119     shift
120       ||
121     try { $self->_dbi_connect_info->[-1]->{LongReadLen} }
122       ||
123     32768; # the DBD::Sybase default
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