Commit | Line | Data |
---|---|---|
f68f4d44 | 1 | package DBIx::Class::Storage::DBI::Sybase; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
ed7ab0f4 | 5 | use Try::Tiny; |
fd323bf1 | 6 | use namespace::clean; |
2ad62d97 | 7 | |
057db5ce | 8 | use base qw/DBIx::Class::Storage::DBI/; |
d867eeda | 9 | |
10 | =head1 NAME | |
11 | ||
95787afe | 12 | DBIx::Class::Storage::DBI::Sybase - Base class for drivers using |
13 | L<DBD::Sybase> | |
d867eeda | 14 | |
15 | =head1 DESCRIPTION | |
16 | ||
057db5ce | 17 | This is the base class/dispatcher for Storage's designed to work with |
18 | L<DBD::Sybase> | |
d867eeda | 19 | |
20 | =head1 METHODS | |
21 | ||
22 | =cut | |
f68f4d44 | 23 | |
47d9646a | 24 | sub _rebless { |
d867eeda | 25 | my $self = shift; |
d29565e0 | 26 | |
ed7ab0f4 | 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: $_") | |
057db5ce | 32 | }; |
d867eeda | 33 | |
057db5ce | 34 | if ($dbtype) { |
d867eeda | 35 | $dbtype =~ s/\W/_/gi; |
d867eeda | 36 | |
057db5ce | 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)) { | |
d867eeda | 42 | bless $self, $subclass; |
43 | $self->_rebless; | |
d867eeda | 44 | } |
45 | } | |
46 | } | |
47 | ||
c1e5a9ac | 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 | ||
057db5ce | 73 | sub _ping { |
d867eeda | 74 | my $self = shift; |
d867eeda | 75 | |
057db5ce | 76 | my $dbh = $self->_dbh or return 0; |
0a9a9955 | 77 | |
057db5ce | 78 | local $dbh->{RaiseError} = 1; |
79 | local $dbh->{PrintError} = 0; | |
0a9a9955 | 80 | |
c1e5a9ac | 81 | # FIXME if the main connection goes stale, does opening another for this statement |
82 | # really determine anything? | |
83 | ||
057db5ce | 84 | if ($dbh->{syb_no_child_con}) { |
c1e5a9ac | 85 | return try { |
86 | $self->_connect(@{$self->_dbi_connect_info || [] }) | |
87 | ->do('select 1'); | |
88 | 1; | |
89 | } | |
90 | catch { | |
91 | 0; | |
92 | }; | |
057db5ce | 93 | } |
d867eeda | 94 | |
52b420dd | 95 | return try { |
057db5ce | 96 | $dbh->do('select 1'); |
52b420dd | 97 | 1; |
c1e5a9ac | 98 | } |
99 | catch { | |
52b420dd | 100 | 0; |
d867eeda | 101 | }; |
0a9a9955 | 102 | } |
103 | ||
057db5ce | 104 | sub _set_max_connect { |
d867eeda | 105 | my $self = shift; |
057db5ce | 106 | my $val = shift || 256; |
d867eeda | 107 | |
057db5ce | 108 | my $dsn = $self->_dbi_connect_info->[0]; |
d867eeda | 109 | |
057db5ce | 110 | return if ref($dsn) eq 'CODE'; |
81a10d8d | 111 | |
057db5ce | 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; | |
d867eeda | 117 | } |
118 | } | |
119 | ||
057db5ce | 120 | =head2 using_freetds |
d867eeda | 121 | |
057db5ce | 122 | Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means |
123 | the Sybase OpenClient libraries were used. | |
d867eeda | 124 | |
125 | =cut | |
126 | ||
057db5ce | 127 | sub using_freetds { |
d867eeda | 128 | my $self = shift; |
d867eeda | 129 | |
c1e5a9ac | 130 | return ($self->_get_dbh->{syb_oc_version}||'') =~ /freetds/i; |
a964a928 | 131 | } |
132 | ||
f68f4d44 | 133 | 1; |
134 | ||
057db5ce | 135 | =head1 AUTHORS |
f68f4d44 | 136 | |
d867eeda | 137 | See L<DBIx::Class/CONTRIBUTORS>. |
47d9646a | 138 | |
f68f4d44 | 139 | =head1 LICENSE |
140 | ||
141 | You may distribute this code under the same terms as Perl itself. | |
142 | ||
143 | =cut |