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