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