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