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