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