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