add quote_names connect_info option
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLAnywhere.pm
1 package DBIx::Class::Storage::DBI::SQLAnywhere;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
6 use mro 'c3';
7 use List::Util 'first';
8 use Try::Tiny;
9 use namespace::clean;
10
11 __PACKAGE__->mk_group_accessors(simple => qw/_identity/);
12 __PACKAGE__->sql_limit_dialect ('RowNumberOver');
13 __PACKAGE__->sql_quote_char ('"');
14
15 =head1 NAME
16
17 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
18
19 =head1 DESCRIPTION
20
21 This class implements autoincrements for Sybase SQL Anywhere and provides
22 L<DBIx::Class::InflateColumn::DateTime> support.
23
24 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
25 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
26
27   /opt/sqlanywhere11/sdk/perl
28
29 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
30
31   on_connect_call => 'datetime_setup'
32
33 =head1 METHODS
34
35 =cut
36
37 sub last_insert_id { shift->_identity }
38
39 sub _new_uuid { 'UUIDTOSTR(NEWID())' }
40
41 sub _prefetch_autovalues {
42   my $self = shift;
43   my ($source, $to_insert) = @_;
44
45   my $values = $self->next::method(@_);
46
47   my $colinfo = $source->columns_info;
48
49   my $identity_col =
50     first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
51
52 # user might have an identity PK without is_auto_increment
53   if (not $identity_col) {
54     foreach my $pk_col ($source->primary_columns) {
55       if (
56         ! exists $to_insert->{$pk_col}
57           and
58         $colinfo->{$pk_col}{data_type}
59           and
60         $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
61       ) {
62         $identity_col = $pk_col;
63         last;
64       }
65     }
66   }
67
68   if ($identity_col && (not exists $to_insert->{$identity_col})) {
69     my $dbh = $self->_get_dbh;
70     my $table_name = $source->from;
71     $table_name    = $$table_name if ref $table_name;
72
73     my ($identity) = try {
74       $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
75     };
76
77     if (defined $identity) {
78       $values->{$identity_col} = $identity;
79       $self->_identity($identity);
80     }
81   }
82
83   return $values;
84 }
85
86 # convert UUIDs to strings in selects
87 sub _select_args {
88   my $self = shift;
89   my ($ident, $select) = @_;
90
91   my $col_info = $self->_resolve_column_info($ident);
92
93   for my $select_idx (0..$#$select) {
94     my $selected = $select->[$select_idx];
95
96     next if ref $selected;
97
98     my $data_type = $col_info->{$selected}{data_type};
99
100     if ($data_type && lc($data_type) eq 'uniqueidentifier') {
101       $select->[$select_idx] = { UUIDTOSTR => $selected };
102     }
103   }
104
105   return $self->next::method(@_);
106 }
107
108 # this sub stolen from MSSQL
109
110 sub build_datetime_parser {
111   my $self = shift;
112   my $type = "DateTime::Format::Strptime";
113   try {
114     eval "require ${type}"
115   }
116   catch {
117     $self->throw_exception("Couldn't load ${type}: $_");
118   };
119
120   return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
121 }
122
123 =head2 connect_call_datetime_setup
124
125 Used as:
126
127     on_connect_call => 'datetime_setup'
128
129 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
130 timestamp formats (as temporary options for the session) for use with
131 L<DBIx::Class::InflateColumn::DateTime>.
132
133 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
134 second precision. The full precision is used.
135
136 The C<DATE> data type supposedly stores hours and minutes too, according to the
137 documentation, but I could not get that to work. It seems to only store the
138 date.
139
140 You will need the L<DateTime::Format::Strptime> module for inflation to work.
141
142 =cut
143
144 sub connect_call_datetime_setup {
145   my $self = shift;
146
147   $self->_do_query(
148     "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
149   );
150   $self->_do_query(
151     "set temporary option date_format      = 'yyyy-mm-dd hh:mm:ss.ssssss'"
152   );
153 }
154
155 sub _svp_begin {
156     my ($self, $name) = @_;
157
158     $self->_get_dbh->do("SAVEPOINT $name");
159 }
160
161 # can't release savepoints that have been rolled back
162 sub _svp_release { 1 }
163
164 sub _svp_rollback {
165     my ($self, $name) = @_;
166
167     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
168 }
169
170 1;
171
172 =head1 MAXIMUM CURSORS
173
174 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
175 L<prepare_cached|DBI/prepare_cached>.
176
177 The default cursor maximum is C<50>, which can be a bit too low. This limit can
178 be turned off (or increased) by the DBA by executing:
179
180   set option max_statement_count = 0
181   set option max_cursor_count    = 0
182
183 Highly recommended.
184
185 =head1 AUTHOR
186
187 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
188
189 =head1 LICENSE
190
191 You may distribute this code under the same terms as Perl itself.
192
193 =cut