add money type support
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Sybase / NoBindVars.pm
1 package DBIx::Class::Storage::DBI::Sybase::NoBindVars;
2
3 use Class::C3;
4 use base qw/
5   DBIx::Class::Storage::DBI::NoBindVars
6   DBIx::Class::Storage::DBI::Sybase
7 /;
8 use List::Util ();
9 use Scalar::Util ();
10
11 sub _rebless {
12   my $self = shift;
13   $self->disable_sth_caching(1);
14 }
15
16 # this works when NOT using placeholders
17 sub _fetch_identity_sql { 'SELECT @@IDENTITY' }
18
19 my $number = sub { Scalar::Util::looks_like_number($_[0]) };
20
21 my $decimal = sub { $_[0] =~ /^ [-+]? \d+ (?:\.\d*)? \z/x };
22
23 my %noquote = (
24     int => sub { $_[0] =~ /^ [-+]? \d+ \z/x },
25     bit => => sub { $_[0] =~ /^[01]\z/ },
26     money => sub { $_[0] =~ /^\$ \d+ (?:\.\d*)? \z/x },
27     float => $number,
28     real => $number,
29     double => $number,
30     decimal => $decimal,
31     numeric => $decimal,
32 );
33
34 sub should_quote_value {
35   my $self = shift;
36   my ($type, $value) = @_;
37
38   return $self->next::method(@_) if not defined $value or not defined $type;
39
40   if (my $key = List::Util::first { $type =~ /$_/i } keys %noquote) {
41     return 0 if $noquote{$key}->($value);
42   } elsif ($self->is_datatype_numeric($type) && $number->($value)) {
43     return 0;
44   }
45
46 ## try to guess based on value
47 #  elsif (not $type) {
48 #    return 0 if $number->($value) || $noquote->{money}->($value);
49 #  }
50
51   return $self->next::method(@_);
52 }
53
54 sub transform_unbound_value {
55   my ($self, $type, $value) = @_;
56
57   if ($type =~ /money/i && defined $value) {
58     $value =~ s/^\$//;
59     $value = '$' . $value;
60   }
61
62   return $value;
63 }
64
65 1;
66
67 =head1 NAME
68
69 DBIx::Class::Storage::DBI::Sybase::NoBindVars - Storage::DBI subclass for Sybase
70 without placeholder support
71
72 =head1 DESCRIPTION
73
74 If you're using this driver than your version of Sybase does not support
75 placeholders, or your version of L<DBD::Sybase> was compiled with FreeTDS rather
76 than the Sybase OpenClient libraries. You can check with:
77
78   $dbh->{syb_dynamic_supported}
79
80 To see if you are using FreeTDS, run:
81
82   perl -MDBI -le 'my $dbh = DBI->connect($dsn, $user, $pass); print $dbh->{syb_oc_version}'
83
84 You will get a warning on startup if you're using FreeTDS in any case.
85
86 You can also enable this driver explicitly using:
87
88   my $schema = SchemaClass->clone;
89   $schema->storage_type('::DBI::Sybase::NoBindVars');
90   $schema->connect($dsn, $user, $pass, \%opts);
91
92 See the discussion in L<< DBD::Sybase/Using ? Placeholders & bind parameters to
93 $sth->execute >> for details on the pros and cons of using placeholders.
94
95 One advantage of not using placeholders is that C<select @@identity> will work
96 for obtainging the last insert id of an C<IDENTITY> column, instead of having to
97 do C<select max(col)> as the base Sybase driver does.
98
99 When using this driver, bind variables will be interpolated (properly quoted of
100 course) into the SQL query itself, without using placeholders.
101
102 The caching of prepared statements is also explicitly disabled, as the
103 interpolation renders it useless.
104
105 =head1 AUTHORS
106
107 See L<DBIx::Class/CONTRIBUTORS>.
108
109 =head1 LICENSE
110
111 You may distribute this code under the same terms as Perl itself.
112
113 =cut
114 # vim:sts=2 sw=2: