use namespace::cleaned out imports for some common utilities
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / ASE / NoBindVars.pm
CommitLineData
057db5ce 1package DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars;
5608593e 2
3use base qw/
4 DBIx::Class::Storage::DBI::NoBindVars
057db5ce 5 DBIx::Class::Storage::DBI::Sybase::ASE
5608593e 6/;
322b7a6b 7use mro 'c3';
6298a324 8use List::Util 'first';
9use Scalar::Util 'looks_like_number';
10use namespace::clean;
5608593e 11
37b17a93 12sub _init {
9b3dabe0 13 my $self = shift;
14 $self->disable_sth_caching(1);
322b7a6b 15 $self->_identity_method('@@IDENTITY');
37b17a93 16 $self->next::method (@_);
9b3dabe0 17}
18
322b7a6b 19sub _fetch_identity_sql { 'SELECT ' . $_[0]->_identity_method }
6b1f5ef7 20
6298a324 21my $number = sub { looks_like_number $_[0] };
0c449973 22
b88bf40a 23my $decimal = sub { $_[0] =~ /^ [-+]? \d+ (?:\.\d*)? \z/x };
24
b55e97a7 25my %noquote = (
b88bf40a 26 int => sub { $_[0] =~ /^ [-+]? \d+ \z/x },
0c449973 27 bit => => sub { $_[0] =~ /^[01]\z/ },
b88bf40a 28 money => sub { $_[0] =~ /^\$ \d+ (?:\.\d*)? \z/x },
0c449973 29 float => $number,
30 real => $number,
31 double => $number,
b88bf40a 32 decimal => $decimal,
33 numeric => $decimal,
b55e97a7 34);
0c1bedfc 35
80007f97 36sub interpolate_unquoted {
0c1bedfc 37 my $self = shift;
38 my ($type, $value) = @_;
39
7d17f469 40 return $self->next::method(@_) if not defined $value or not defined $type;
bbdc039b 41
6298a324 42 if (my $key = first { $type =~ /$_/i } keys %noquote) {
80007f97 43 return 1 if $noquote{$key}->($value);
44 }
45 elsif ($self->is_datatype_numeric($type) && $number->($value)) {
46 return 1;
17d750d7 47 }
7d17f469 48
0c1bedfc 49 return $self->next::method(@_);
50}
51
166c6561 52sub _prep_interpolated_value {
e06ad5d5 53 my ($self, $type, $value) = @_;
54
55 if ($type =~ /money/i && defined $value) {
0ac07712 56 # change a ^ not followed by \$ to a \$
57 $value =~ s/^ (?! \$) /\$/x;
e06ad5d5 58 }
59
60 return $value;
61}
62
5608593e 631;
7e8cecc1 64
65=head1 NAME
66
057db5ce 67DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars - Storage::DBI subclass for
68Sybase ASE without placeholder support
7e8cecc1 69
70=head1 DESCRIPTION
71
48580715 72If you're using this driver then your version of Sybase or the libraries you
73use to connect to it do not support placeholders.
22b3249c 74
7e8cecc1 75You can also enable this driver explicitly using:
76
77 my $schema = SchemaClass->clone;
057db5ce 78 $schema->storage_type('::DBI::Sybase::ASE::NoBindVars');
7e8cecc1 79 $schema->connect($dsn, $user, $pass, \%opts);
80
81See the discussion in L<< DBD::Sybase/Using ? Placeholders & bind parameters to
82$sth->execute >> for details on the pros and cons of using placeholders.
83
84One advantage of not using placeholders is that C<select @@identity> will work
48580715 85for obtaining the last insert id of an C<IDENTITY> column, instead of having to
e97a6ee2 86do C<select max(col)> in a transaction as the base Sybase driver does.
7e8cecc1 87
88When using this driver, bind variables will be interpolated (properly quoted of
89course) into the SQL query itself, without using placeholders.
90
91The caching of prepared statements is also explicitly disabled, as the
92interpolation renders it useless.
93
94=head1 AUTHORS
95
96See L<DBIx::Class/CONTRIBUTORS>.
97
98=head1 LICENSE
99
100You may distribute this code under the same terms as Perl itself.
101
102=cut
103# vim:sts=2 sw=2: