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