use namespace::clean w/ Try::Tiny
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase.pm
CommitLineData
f68f4d44 1package DBIx::Class::Storage::DBI::Sybase;
2
3use strict;
4use warnings;
ed7ab0f4 5use Try::Tiny;
fd323bf1 6use namespace::clean;
2ad62d97 7
057db5ce 8use base qw/DBIx::Class::Storage::DBI/;
d867eeda 9
10=head1 NAME
11
95787afe 12DBIx::Class::Storage::DBI::Sybase - Base class for drivers using
13L<DBD::Sybase>
d867eeda 14
15=head1 DESCRIPTION
16
057db5ce 17This is the base class/dispatcher for Storage's designed to work with
18L<DBD::Sybase>
d867eeda 19
20=head1 METHODS
21
22=cut
f68f4d44 23
47d9646a 24sub _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
057db5ce 48sub _ping {
d867eeda 49 my $self = shift;
d867eeda 50
057db5ce 51 my $dbh = $self->_dbh or return 0;
0a9a9955 52
057db5ce 53 local $dbh->{RaiseError} = 1;
54 local $dbh->{PrintError} = 0;
0a9a9955 55
057db5ce 56 if ($dbh->{syb_no_child_con}) {
57# if extra connections are not allowed, then ->ping is reliable
52b420dd 58 return try { $dbh->ping } catch { 0; };
057db5ce 59 }
d867eeda 60
52b420dd 61 return try {
057db5ce 62# XXX if the main connection goes stale, does opening another for this statement
63# really determine anything?
64 $dbh->do('select 1');
52b420dd 65 1;
ed7ab0f4 66 } catch {
52b420dd 67 0;
d867eeda 68 };
0a9a9955 69}
70
057db5ce 71sub _set_max_connect {
d867eeda 72 my $self = shift;
057db5ce 73 my $val = shift || 256;
d867eeda 74
057db5ce 75 my $dsn = $self->_dbi_connect_info->[0];
d867eeda 76
057db5ce 77 return if ref($dsn) eq 'CODE';
81a10d8d 78
057db5ce 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;
d867eeda 84 }
85}
86
057db5ce 87=head2 using_freetds
d867eeda 88
057db5ce 89Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
90the Sybase OpenClient libraries were used.
d867eeda 91
92=cut
93
057db5ce 94sub using_freetds {
d867eeda 95 my $self = shift;
d867eeda 96
057db5ce 97 return $self->_get_dbh->{syb_oc_version} =~ /freetds/i;
d867eeda 98}
99
057db5ce 100=head2 set_textsize
db66bc3f 101
057db5ce 102When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
103use this function instead. It does:
0a9a9955 104
057db5ce 105 $dbh->do("SET TEXTSIZE $bytes");
0a9a9955 106
057db5ce 107Takes the number of bytes, or uses the C<LongReadLen> value from your
8384a713 108L<connect_info|DBIx::Class::Storage::DBI/connect_info> if omitted, lastly falls
109back to the C<32768> which is the L<DBD::Sybase> default.
d867eeda 110
111=cut
112
057db5ce 113sub set_textsize {
d867eeda 114 my $self = shift;
9780718f 115 my $text_size =
116 shift
117 ||
118 try { $self->_dbi_connect_info->[-1]->{LongReadLen} }
119 ||
057db5ce 120 32768; # the DBD::Sybase default
d867eeda 121
057db5ce 122 return unless defined $text_size;
d867eeda 123
057db5ce 124 $self->_dbh->do("SET TEXTSIZE $text_size");
a964a928 125}
126
f68f4d44 1271;
128
057db5ce 129=head1 AUTHORS
f68f4d44 130
d867eeda 131See L<DBIx::Class/CONTRIBUTORS>.
47d9646a 132
f68f4d44 133=head1 LICENSE
134
135You may distribute this code under the same terms as Perl itself.
136
137=cut