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