fix typo
[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 establish 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 _init {
49   # once the driver is determined see if we need to insert the DBD::Sybase w/ FreeTDS fixups
50   # this is a dirty version of "instance role application", \o/ DO WANT Moo \o/
51   my $self = shift;
52   if (! $self->isa('DBIx::Class::Storage::DBI::Sybase::FreeTDS') and $self->_using_freetds) {
53     require DBIx::Class::Storage::DBI::Sybase::FreeTDS;
54
55     my @isa = @{mro::get_linear_isa(ref $self)};
56     my $class = shift @isa; # this is our current ref
57
58     my $trait_class = $class . '::FreeTDS';
59     mro::set_mro ($trait_class, 'c3');
60     no strict 'refs';
61     @{"${trait_class}::ISA"} = ($class, 'DBIx::Class::Storage::DBI::Sybase::FreeTDS', @isa);
62
63     bless ($self, $trait_class);
64
65     Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
66
67     $self->_init(@_);
68   }
69
70   $self->next::method(@_);
71 }
72
73 sub _ping {
74   my $self = shift;
75
76   my $dbh = $self->_dbh or return 0;
77
78   local $dbh->{RaiseError} = 1;
79   local $dbh->{PrintError} = 0;
80
81 # FIXME if the main connection goes stale, does opening another for this statement
82 # really determine anything?
83
84   if ($dbh->{syb_no_child_con}) {
85     return try {
86       $self->_connect(@{$self->_dbi_connect_info || [] })
87         ->do('select 1');
88       1;
89     }
90     catch {
91       0;
92     };
93   }
94
95   return try {
96     $dbh->do('select 1');
97     1;
98   }
99   catch {
100     0;
101   };
102 }
103
104 sub _set_max_connect {
105   my $self = shift;
106   my $val  = shift || 256;
107
108   my $dsn = $self->_dbi_connect_info->[0];
109
110   return if ref($dsn) eq 'CODE';
111
112   if ($dsn !~ /maxConnect=/) {
113     $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
114     my $connected = defined $self->_dbh;
115     $self->disconnect;
116     $self->ensure_connected if $connected;
117   }
118 }
119
120 # Whether or not DBD::Sybase was compiled against FreeTDS. If false, it means
121 # the Sybase OpenClient libraries were used.
122 sub _using_freetds {
123   my $self = shift;
124   return ($self->_get_dbh->{syb_oc_version}||'') =~ /freetds/i;
125 }
126
127 # Either returns the FreeTDS version against which DBD::Sybase was compiled,
128 # 0 if can't be determined, or undef otherwise
129 sub _using_freetds_version {
130   my $inf = shift->_get_dbh->{syb_oc_version};
131   return undef unless ($inf||'') =~ /freetds/i;
132   return $inf =~ /v([0-9\.]+)/ ? $1 : 0;
133 }
134
135 1;
136
137 =head1 AUTHORS
138
139 See L<DBIx::Class/CONTRIBUTORS>.
140
141 =head1 LICENSE
142
143 You may distribute this code under the same terms as Perl itself.
144
145 =cut