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