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