3d054bb192d5ae8f6e17111684c309304cd2ba14
[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 DBIx::Class::_Util 'dbic_internal_try';
9 use Try::Tiny;
10 use namespace::clean;
11
12 __PACKAGE__->mk_group_accessors(simple => qw/_identity/);
13 __PACKAGE__->sql_limit_dialect ('RowNumberOver');
14 __PACKAGE__->sql_quote_char ('"');
15
16 __PACKAGE__->new_guid('UUIDTOSTR(NEWID())');
17
18 # default to the UUID decoding cursor, overridable by the user
19 __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
20
21 =head1 NAME
22
23 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for SQL Anywhere
24
25 =head1 DESCRIPTION
26
27 This class implements autoincrements for SQL Anywhere and provides
28 L<DBIx::Class::InflateColumn::DateTime> support and support for the
29 C<uniqueidentifier> type (via
30 L<DBIx::Class::Storage::DBI::SQLAnywhere::Cursor>.)
31
32 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
33 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
34
35   /opt/sqlanywhere11/sdk/perl
36
37 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
38
39   on_connect_call => 'datetime_setup'
40
41 =head1 METHODS
42
43 =cut
44
45 sub last_insert_id { shift->_identity }
46
47 sub _prefetch_autovalues {
48   my $self = shift;
49   my ($source, $colinfo, $to_insert) = @_;
50
51   my $values = $self->next::method(@_);
52
53   my $identity_col =
54     first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
55
56 # user might have an identity PK without is_auto_increment
57 #
58 # FIXME we probably should not have supported the above, see what
59 # does it take to move away from it
60   if (not $identity_col) {
61     foreach my $pk_col ($source->primary_columns) {
62       if (
63         ! exists $to_insert->{$pk_col}
64           and
65         $colinfo->{$pk_col}{data_type}
66           and
67         $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
68       ) {
69         $identity_col = $pk_col;
70         last;
71       }
72     }
73   }
74
75   if ($identity_col && (not exists $to_insert->{$identity_col})) {
76     my $dbh = $self->_get_dbh;
77     my $table_name = $source->from;
78     $table_name    = $$table_name if ref $table_name;
79
80     my ($identity) = dbic_internal_try {
81       $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
82     };
83
84     if (defined $identity) {
85       $values->{$identity_col} = $identity;
86       $self->_identity($identity);
87     }
88   }
89
90   return $values;
91 }
92
93 sub _uuid_to_str {
94   my ($self, $data) = @_;
95
96   $data = unpack 'H*', $data;
97
98   for my $pos (8, 13, 18, 23) {
99     substr($data, $pos, 0) = '-';
100   }
101
102   return $data;
103 }
104
105 # select_single does not invoke a cursor object at all, hence UUID decoding happens
106 # here if the proper cursor class is set
107 sub select_single {
108   my $self = shift;
109
110   my @row = $self->next::method(@_);
111
112   return @row
113     unless $self->cursor_class->isa('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
114
115   my ($ident, $select) = @_;
116
117   my $col_info = $self->_resolve_column_info($ident);
118
119   for my $select_idx (0..$#$select) {
120     my $selected = $select->[$select_idx];
121
122     next if ref $selected;
123
124     my $data_type = $col_info->{$selected}{data_type}
125       or next;
126
127     if ($self->_is_guid_type($data_type)) {
128       my $returned = $row[$select_idx];
129
130       if (length $returned == 16) {
131         $row[$select_idx] = $self->_uuid_to_str($returned);
132       }
133     }
134   }
135
136   return @row;
137 }
138
139 # this sub stolen from MSSQL
140
141 sub build_datetime_parser {
142   my $self = shift;
143   dbic_internal_try {
144     require DateTime::Format::Strptime;
145   }
146   catch {
147     $self->throw_exception("Couldn't load DateTime::Format::Strptime: $_");
148   };
149
150   return DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
151 }
152
153 =head2 connect_call_datetime_setup
154
155 Used as:
156
157     on_connect_call => 'datetime_setup'
158
159 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
160 timestamp formats (as temporary options for the session) for use with
161 L<DBIx::Class::InflateColumn::DateTime>.
162
163 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
164 second precision. The full precision is used.
165
166 The C<DATE> data type supposedly stores hours and minutes too, according to the
167 documentation, but I could not get that to work. It seems to only store the
168 date.
169
170 You will need the L<DateTime::Format::Strptime> module for inflation to work.
171
172 =cut
173
174 sub connect_call_datetime_setup {
175   my $self = shift;
176
177   $self->_do_query(
178     "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
179   );
180   $self->_do_query(
181     "set temporary option date_format      = 'yyyy-mm-dd hh:mm:ss.ssssss'"
182   );
183 }
184
185 sub _exec_svp_begin {
186     my ($self, $name) = @_;
187
188     $self->_dbh->do("SAVEPOINT $name");
189 }
190
191 # can't release savepoints that have been rolled back
192 sub _exec_svp_release { 1 }
193
194 sub _exec_svp_rollback {
195     my ($self, $name) = @_;
196
197     $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
198 }
199
200 1;
201
202 =head1 MAXIMUM CURSORS
203
204 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
205 L<prepare_cached|DBI/prepare_cached>.
206
207 The default cursor maximum is C<50>, which can be a bit too low. This limit can
208 be turned off (or increased) by the DBA by executing:
209
210   set option max_statement_count = 0
211   set option max_cursor_count    = 0
212
213 Highly recommended.
214
215 =head1 FURTHER QUESTIONS?
216
217 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
218
219 =head1 COPYRIGHT AND LICENSE
220
221 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
222 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
223 redistribute it and/or modify it under the same terms as the
224 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.