Refactor UUID generation logic in ::Storage::DBI::UniqueIdentifier
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLAnywhere.pm
CommitLineData
db8f81cf 1package DBIx::Class::Storage::DBI::SQLAnywhere;
f200d74b 2
3use strict;
4use warnings;
548d1627 5use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
f200d74b 6use mro 'c3';
6298a324 7use List::Util 'first';
52b420dd 8use Try::Tiny;
fd323bf1 9use 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
db8f81cf 17=head1 NAME
18
2b0076be 19DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
db8f81cf 20
21=head1 DESCRIPTION
22
d5dedbd6 23This class implements autoincrements for Sybase SQL Anywhere and provides
4b8dd353 24L<DBIx::Class::InflateColumn::DateTime> support.
db8f81cf 25
26You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
27distribution, B<NOT> the one on CPAN. It is usually under a path such as:
28
2b0076be 29 /opt/sqlanywhere11/sdk/perl
30
7df295ec 31Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
2b0076be 32
33 on_connect_call => 'datetime_setup'
34
35=head1 METHODS
db8f81cf 36
37=cut
38
39sub last_insert_id { shift->_identity }
40
e366f807 41sub _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
87sub _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
110sub 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
125Used as:
126
127 on_connect_call => 'datetime_setup'
128
7df295ec 129In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
130timestamp formats (as temporary options for the session) for use with
2b0076be 131L<DBIx::Class::InflateColumn::DateTime>.
132
133The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
134second precision. The full precision is used.
135
136The C<DATE> data type supposedly stores hours and minutes too, according to the
137documentation, but I could not get that to work. It seems to only store the
138date.
139
140You will need the L<DateTime::Format::Strptime> module for inflation to work.
141
142=cut
143
144sub 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 155sub _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
162sub _svp_release { 1 }
163
164sub _svp_rollback {
165 my ($self, $name) = @_;
166
167 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
168}
169
f200d74b 1701;
db8f81cf 171
270eecac 172=head1 MAXIMUM CURSORS
173
7df295ec 174A L<DBIx::Class> application can use a lot of cursors, due to the usage of
175L<prepare_cached|DBI/prepare_cached>.
270eecac 176
177The default cursor maximum is C<50>, which can be a bit too low. This limit can
178be 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
183Highly recommended.
184
db8f81cf 185=head1 AUTHOR
186
187See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
188
189=head1 LICENSE
190
191You may distribute this code under the same terms as Perl itself.
192
193=cut