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