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 | |
15 | =head1 NAME |
16 | |
2b0076be |
17 | DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere |
db8f81cf |
18 | |
19 | =head1 DESCRIPTION |
20 | |
d5dedbd6 |
21 | This class implements autoincrements for Sybase SQL Anywhere and provides |
4b8dd353 |
22 | L<DBIx::Class::InflateColumn::DateTime> support. |
db8f81cf |
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 | |
2b0076be |
27 | /opt/sqlanywhere11/sdk/perl |
28 | |
7df295ec |
29 | Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings: |
2b0076be |
30 | |
31 | on_connect_call => 'datetime_setup' |
32 | |
33 | =head1 METHODS |
db8f81cf |
34 | |
35 | =cut |
36 | |
37 | sub last_insert_id { shift->_identity } |
38 | |
548d1627 |
39 | sub _new_uuid { 'UUIDTOSTR(NEWID())' } |
40 | |
e366f807 |
41 | sub _prefetch_autovalues { |
f200d74b |
42 | my $self = shift; |
db8f81cf |
43 | my ($source, $to_insert) = @_; |
44 | |
e366f807 |
45 | my $values = $self->next::method(@_); |
46 | |
52416317 |
47 | my $colinfo = $source->columns_info; |
48 | |
6298a324 |
49 | my $identity_col = |
e366f807 |
50 | first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo; |
db8f81cf |
51 | |
cea43436 |
52 | # user might have an identity PK without is_auto_increment |
53 | if (not $identity_col) { |
54 | foreach my $pk_col ($source->primary_columns) { |
52416317 |
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 | ) { |
cea43436 |
62 | $identity_col = $pk_col; |
63 | last; |
64 | } |
65 | } |
66 | } |
67 | |
b0267fb7 |
68 | if ($identity_col && (not exists $to_insert->{$identity_col})) { |
db8f81cf |
69 | my $dbh = $self->_get_dbh; |
70 | my $table_name = $source->from; |
71 | $table_name = $$table_name if ref $table_name; |
72 | |
9780718f |
73 | my ($identity) = try { |
74 | $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')") |
548d1627 |
75 | }; |
76 | |
77 | if (defined $identity) { |
e366f807 |
78 | $values->{$identity_col} = $identity; |
548d1627 |
79 | $self->_identity($identity); |
80 | } |
81 | } |
82 | |
e366f807 |
83 | return $values; |
548d1627 |
84 | } |
85 | |
86 | # convert UUIDs to strings in selects |
87 | sub _select_args { |
88 | my $self = shift; |
89 | my ($ident, $select) = @_; |
90 | |
7e5fec1c |
91 | my $col_info = $self->_resolve_column_info($ident); |
548d1627 |
92 | |
93 | for my $select_idx (0..$#$select) { |
94 | my $selected = $select->[$select_idx]; |
f200d74b |
95 | |
548d1627 |
96 | next if ref $selected; |
db8f81cf |
97 | |
7e5fec1c |
98 | my $data_type = $col_info->{$selected}{data_type}; |
548d1627 |
99 | |
7da56142 |
100 | if ($data_type && lc($data_type) eq 'uniqueidentifier') { |
548d1627 |
101 | $select->[$select_idx] = { UUIDTOSTR => $selected }; |
102 | } |
f200d74b |
103 | } |
db8f81cf |
104 | |
105 | return $self->next::method(@_); |
106 | } |
107 | |
2b0076be |
108 | # this sub stolen from MSSQL |
109 | |
110 | sub build_datetime_parser { |
111 | my $self = shift; |
112 | my $type = "DateTime::Format::Strptime"; |
9780718f |
113 | try { |
52b420dd |
114 | eval "require ${type}" |
9780718f |
115 | } |
116 | catch { |
117 | $self->throw_exception("Couldn't load ${type}: $_"); |
118 | }; |
119 | |
2b0076be |
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 | |
7df295ec |
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 |
2b0076be |
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 | |
9cf3db6f |
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 | |
f200d74b |
170 | 1; |
db8f81cf |
171 | |
270eecac |
172 | =head1 MAXIMUM CURSORS |
173 | |
7df295ec |
174 | A L<DBIx::Class> application can use a lot of cursors, due to the usage of |
175 | L<prepare_cached|DBI/prepare_cached>. |
270eecac |
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 | |
db8f81cf |
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 |