Commit | Line | Data |
db8f81cf |
1 | package DBIx::Class::Storage::DBI::SQLAnywhere; |
f200d74b |
2 | |
3 | use strict; |
4 | use warnings; |
548d1627 |
5 | use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/; |
f200d74b |
6 | use mro 'c3'; |
6298a324 |
7 | use List::Util 'first'; |
52b420dd |
8 | use Try::Tiny; |
fd323bf1 |
9 | use namespace::clean; |
f200d74b |
10 | |
6a247f33 |
11 | __PACKAGE__->mk_group_accessors(simple => qw/_identity/); |
12 | __PACKAGE__->sql_limit_dialect ('RowNumberOver'); |
2b8cc2f2 |
13 | __PACKAGE__->sql_quote_char ('"'); |
db8f81cf |
14 | |
40d8d018 |
15 | __PACKAGE__->new_guid('UUIDTOSTR(NEWID())'); |
16 | |
4b3515a6 |
17 | # default to the UUID decoding cursor, overridable by the user |
18 | __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor'); |
19 | |
db8f81cf |
20 | =head1 NAME |
21 | |
4b3515a6 |
22 | DBIx::Class::Storage::DBI::SQLAnywhere - Driver for SQL Anywhere |
db8f81cf |
23 | |
24 | =head1 DESCRIPTION |
25 | |
4b3515a6 |
26 | This class implements autoincrements for SQL Anywhere and provides |
27 | L<DBIx::Class::InflateColumn::DateTime> support and support for the |
28 | C<uniqueidentifier> type (via |
29 | L<DBIx::Class::Storage::DBI::SQLAnywhere::Cursor>.) |
db8f81cf |
30 | |
31 | You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere |
32 | distribution, B<NOT> the one on CPAN. It is usually under a path such as: |
33 | |
2b0076be |
34 | /opt/sqlanywhere11/sdk/perl |
35 | |
7df295ec |
36 | Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings: |
2b0076be |
37 | |
38 | on_connect_call => 'datetime_setup' |
39 | |
40 | =head1 METHODS |
db8f81cf |
41 | |
42 | =cut |
43 | |
44 | sub last_insert_id { shift->_identity } |
45 | |
e366f807 |
46 | sub _prefetch_autovalues { |
f200d74b |
47 | my $self = shift; |
a3483a58 |
48 | my ($source, $colinfo, $to_insert) = @_; |
db8f81cf |
49 | |
e366f807 |
50 | my $values = $self->next::method(@_); |
51 | |
6298a324 |
52 | my $identity_col = |
e366f807 |
53 | first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo; |
db8f81cf |
54 | |
cea43436 |
55 | # user might have an identity PK without is_auto_increment |
fabbd5cc |
56 | # |
57 | # FIXME we probably should not have supported the above, see what |
58 | # does it take to move away from it |
cea43436 |
59 | if (not $identity_col) { |
60 | foreach my $pk_col ($source->primary_columns) { |
52416317 |
61 | if ( |
62 | ! exists $to_insert->{$pk_col} |
63 | and |
64 | $colinfo->{$pk_col}{data_type} |
65 | and |
66 | $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i |
67 | ) { |
cea43436 |
68 | $identity_col = $pk_col; |
69 | last; |
70 | } |
71 | } |
72 | } |
73 | |
b0267fb7 |
74 | if ($identity_col && (not exists $to_insert->{$identity_col})) { |
db8f81cf |
75 | my $dbh = $self->_get_dbh; |
76 | my $table_name = $source->from; |
77 | $table_name = $$table_name if ref $table_name; |
78 | |
9780718f |
79 | my ($identity) = try { |
80 | $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')") |
548d1627 |
81 | }; |
82 | |
83 | if (defined $identity) { |
e366f807 |
84 | $values->{$identity_col} = $identity; |
548d1627 |
85 | $self->_identity($identity); |
86 | } |
87 | } |
88 | |
e366f807 |
89 | return $values; |
548d1627 |
90 | } |
91 | |
4b3515a6 |
92 | sub _uuid_to_str { |
93 | my ($self, $data) = @_; |
94 | |
95 | $data = unpack 'H*', $data; |
96 | |
97 | for my $pos (8, 13, 18, 23) { |
98 | substr($data, $pos, 0) = '-'; |
99 | } |
100 | |
101 | return $data; |
102 | } |
103 | |
104 | # select_single does not invoke a cursor object at all, hence UUID decoding happens |
105 | # here if the proper cursor class is set |
106 | sub select_single { |
548d1627 |
107 | my $self = shift; |
4b3515a6 |
108 | |
109 | my @row = $self->next::method(@_); |
110 | |
111 | return @row |
112 | unless $self->cursor_class->isa('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor'); |
113 | |
548d1627 |
114 | my ($ident, $select) = @_; |
115 | |
7e5fec1c |
116 | my $col_info = $self->_resolve_column_info($ident); |
548d1627 |
117 | |
118 | for my $select_idx (0..$#$select) { |
119 | my $selected = $select->[$select_idx]; |
f200d74b |
120 | |
548d1627 |
121 | next if ref $selected; |
db8f81cf |
122 | |
4b3515a6 |
123 | my $data_type = $col_info->{$selected}{data_type} |
124 | or next; |
125 | |
126 | if ($self->_is_guid_type($data_type)) { |
127 | my $returned = $row[$select_idx]; |
548d1627 |
128 | |
4b3515a6 |
129 | if (length $returned == 16) { |
130 | $row[$select_idx] = $self->_uuid_to_str($returned); |
131 | } |
548d1627 |
132 | } |
f200d74b |
133 | } |
db8f81cf |
134 | |
4b3515a6 |
135 | return @row; |
db8f81cf |
136 | } |
137 | |
2b0076be |
138 | # this sub stolen from MSSQL |
139 | |
140 | sub build_datetime_parser { |
141 | my $self = shift; |
142 | my $type = "DateTime::Format::Strptime"; |
9780718f |
143 | try { |
52b420dd |
144 | eval "require ${type}" |
9780718f |
145 | } |
146 | catch { |
147 | $self->throw_exception("Couldn't load ${type}: $_"); |
148 | }; |
149 | |
2b0076be |
150 | return $type->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 | |
7df295ec |
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 |
2b0076be |
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 | |
90d7422f |
185 | sub _exec_svp_begin { |
9cf3db6f |
186 | my ($self, $name) = @_; |
187 | |
90d7422f |
188 | $self->_dbh->do("SAVEPOINT $name"); |
9cf3db6f |
189 | } |
190 | |
191 | # can't release savepoints that have been rolled back |
90d7422f |
192 | sub _exec_svp_release { 1 } |
9cf3db6f |
193 | |
90d7422f |
194 | sub _exec_svp_rollback { |
9cf3db6f |
195 | my ($self, $name) = @_; |
196 | |
90d7422f |
197 | $self->_dbh->do("ROLLBACK TO SAVEPOINT $name") |
9cf3db6f |
198 | } |
199 | |
f200d74b |
200 | 1; |
db8f81cf |
201 | |
270eecac |
202 | =head1 MAXIMUM CURSORS |
203 | |
7df295ec |
204 | A L<DBIx::Class> application can use a lot of cursors, due to the usage of |
205 | L<prepare_cached|DBI/prepare_cached>. |
270eecac |
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 | |
db8f81cf |
215 | =head1 AUTHOR |
216 | |
217 | See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>. |
218 | |
219 | =head1 LICENSE |
220 | |
221 | You may distribute this code under the same terms as Perl itself. |
222 | |
223 | =cut |