remove unsafe_insert
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / Common.pm
1 package DBIx::Class::Storage::DBI::Sybase::Common;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Storage::DBI/;
7 use mro 'c3';
8
9 =head1 NAME
10
11 DBIx::Class::Storage::DBI::Sybase::Common - Common functionality for drivers using
12 DBD::Sybase
13
14 =head1 DESCRIPTION
15
16 This is the base class for L<DBIx::Class::Storage::DBI::Sybase> and
17 L<DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server>. It provides some
18 utility methods related to L<DBD::Sybase> and the supported functions of the
19 database you are connecting to.
20
21 =head1 METHODS
22
23 =cut
24
25 sub _ping {
26   my $self = shift;
27
28   my $dbh = $self->_dbh or return 0;
29
30   local $dbh->{RaiseError} = 1;
31   local $dbh->{PrintError} = 0;
32   local $@;
33
34   if ($dbh->{syb_no_child_con}) {
35 # ping is impossible with an active statement, we return false if so
36     my $ping = eval { $dbh->ping };
37     return $@ ? 0 : $ping;
38   }
39
40   eval {
41 # XXX if the main connection goes stale, does opening another for this statement
42 # really determine anything?
43     $dbh->do('select 1');
44   };
45
46   return $@ ? 0 : 1;
47 }
48
49 sub _set_max_connect {
50   my $self = shift;
51   my $val  = shift || 256;
52
53   my $dsn = $self->_dbi_connect_info->[0];
54
55   return if ref($dsn) eq 'CODE';
56
57   if ($dsn !~ /maxConnect=/) {
58     $self->_dbi_connect_info->[0] = "$dsn;maxConnect=$val";
59     my $connected = defined $self->_dbh;
60     $self->disconnect;
61     $self->ensure_connected if $connected;
62   }
63 }
64
65 =head2 using_freetds
66
67 Whether or not L<DBD::Sybase> was compiled against FreeTDS. If false, it means
68 the Sybase OpenClient libraries were used.
69
70 =cut
71
72 sub using_freetds {
73   my $self = shift;
74
75   return $self->_dbh->{syb_oc_version} =~ /freetds/i;
76 }
77
78 =head2 set_textsize
79
80 When using FreeTDS and/or MSSQL, C<< $dbh->{LongReadLen} >> is not available,
81 use this function instead. It does:
82
83   $dbh->do("SET TEXTSIZE $bytes");
84
85 Takes the number of bytes, or uses the C<LongReadLen> value from your
86 L<DBIx::Class/connect_info> if omitted, lastly falls back to the C<32768> which
87 is the L<DBD::Sybase> default.
88
89 =cut
90
91 sub set_textsize {
92   my $self = shift;
93   my $text_size = shift ||
94     eval { $self->_dbi_connect_info->[-1]->{LongReadLen} } ||
95     32768; # the DBD::Sybase default
96
97   return unless defined $text_size;
98
99   $self->_dbh->do("SET TEXTSIZE $text_size");
100 }
101
102 1;
103
104 =head1 AUTHORS
105
106 See L<DBIx::Class/CONTRIBUTORS>.
107
108 =head1 LICENSE
109
110 You may distribute this code under the same terms as Perl itself.
111
112 =cut