Protect DBIC as best we can from the failure mode in 7cb35852
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
CommitLineData
f68f4d44 1package DBIx::Class::Storage::DBI::Sybase;
2
3use strict;
4use warnings;
ddcc02d1 5use DBIx::Class::_Util 'dbic_internal_try';
ed7ab0f4 6use Try::Tiny;
fd323bf1 7use namespace::clean;
2ad62d97 8
057db5ce 9use base qw/DBIx::Class::Storage::DBI/;
d867eeda 10
11=head1 NAME
12
95787afe 13DBIx::Class::Storage::DBI::Sybase - Base class for drivers using
14L<DBD::Sybase>
d867eeda 15
16=head1 DESCRIPTION
17
057db5ce 18This is the base class/dispatcher for Storage's designed to work with
19L<DBD::Sybase>
d867eeda 20
21=head1 METHODS
22
23=cut
f68f4d44 24
b0c42bbc 25sub _rebless { shift->_determine_connector_driver('Sybase') }
26
27sub _get_rdbms_name {
d867eeda 28 my $self = shift;
d29565e0 29
ddcc02d1 30 dbic_internal_try {
b0c42bbc 31 my $name = $self->_get_dbh->selectrow_arrayref('sp_server_info @attribute_id=1')->[2];
d867eeda 32
b0c42bbc 33 if ($name) {
34 $name =~ s/\W/_/gi;
057db5ce 35
b0c42bbc 36 # saner class name
37 $name = 'ASE' if $name eq 'SQL_Server';
d867eeda 38 }
b0c42bbc 39
40 $name; # RV
41 } catch {
42 $self->throw_exception("Unable to establish connection to determine database type: $_")
43 };
d867eeda 44}
45
c1e5a9ac 46sub _init {
47 # once the driver is determined see if we need to insert the DBD::Sybase w/ FreeTDS fixups
48 # this is a dirty version of "instance role application", \o/ DO WANT Moo \o/
49 my $self = shift;
aca3b4c3 50 if (! $self->isa('DBIx::Class::Storage::DBI::Sybase::FreeTDS') and $self->_using_freetds) {
c1e5a9ac 51 require DBIx::Class::Storage::DBI::Sybase::FreeTDS;
52
53 my @isa = @{mro::get_linear_isa(ref $self)};
54 my $class = shift @isa; # this is our current ref
55
56 my $trait_class = $class . '::FreeTDS';
57 mro::set_mro ($trait_class, 'c3');
58 no strict 'refs';
59 @{"${trait_class}::ISA"} = ($class, 'DBIx::Class::Storage::DBI::Sybase::FreeTDS', @isa);
60
61 bless ($self, $trait_class);
62
63 Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
64
65 $self->_init(@_);
66 }
67
68 $self->next::method(@_);
69}
70
057db5ce 71sub _ping {
d867eeda 72 my $self = shift;
d867eeda 73
057db5ce 74 my $dbh = $self->_dbh or return 0;
0a9a9955 75
057db5ce 76 local $dbh->{RaiseError} = 1;
77 local $dbh->{PrintError} = 0;
0a9a9955 78
c1e5a9ac 79# FIXME if the main connection goes stale, does opening another for this statement
80# really determine anything?
ddcc02d1 81# FIXME (2) THIS MAKES 0 SENSE!!! Need to test later
057db5ce 82 if ($dbh->{syb_no_child_con}) {
ddcc02d1 83 return dbic_internal_try {
37b5ab51 84 $self->_connect->do('select 1');
c1e5a9ac 85 1;
86 }
87 catch {
88 0;
89 };
057db5ce 90 }
d867eeda 91
ddcc02d1 92 return (
93 (dbic_internal_try {
94 $dbh->do('select 1');
95 1;
96 })
97 ? 1
98 : 0
99 );
0a9a9955 100}
101
057db5ce 102sub _set_max_connect {
d867eeda 103 my $self = shift;
057db5ce 104 my $val = shift || 256;
d867eeda 105
057db5ce 106 my $dsn = $self->_dbi_connect_info->[0];
d867eeda 107
057db5ce 108 return if ref($dsn) eq 'CODE';
81a10d8d 109
057db5ce 110 if ($dsn !~ /maxConnect=/) {
111 $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
112 my $connected = defined $self->_dbh;
113 $self->disconnect;
114 $self->ensure_connected if $connected;
d867eeda 115 }
116}
117
aca3b4c3 118# Whether or not DBD::Sybase was compiled against FreeTDS. If false, it means
119# the Sybase OpenClient libraries were used.
120sub _using_freetds {
d867eeda 121 my $self = shift;
c1e5a9ac 122 return ($self->_get_dbh->{syb_oc_version}||'') =~ /freetds/i;
a964a928 123}
124
aca3b4c3 125# Either returns the FreeTDS version against which DBD::Sybase was compiled,
126# 0 if can't be determined, or undef otherwise
127sub _using_freetds_version {
128 my $inf = shift->_get_dbh->{syb_oc_version};
129 return undef unless ($inf||'') =~ /freetds/i;
130 return $inf =~ /v([0-9\.]+)/ ? $1 : 0;
131}
132
a2bd3796 133=head1 FURTHER QUESTIONS?
f68f4d44 134
a2bd3796 135Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
47d9646a 136
a2bd3796 137=head1 COPYRIGHT AND LICENSE
f68f4d44 138
a2bd3796 139This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
140by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
141redistribute it and/or modify it under the same terms as the
142L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
f68f4d44 143
144=cut
a2bd3796 145
1461;
147