Lose another dependency: Class::Inspector
[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';
ddcc02d1 8use DBIx::Class::_Util 'dbic_internal_try';
52b420dd 9use Try::Tiny;
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;
a3483a58 49 my ($source, $colinfo, $to_insert) = @_;
db8f81cf 50
e366f807 51 my $values = $self->next::method(@_);
52
6298a324 53 my $identity_col =
e366f807 54 first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
db8f81cf 55
cea43436 56# user might have an identity PK without is_auto_increment
fabbd5cc 57#
58# FIXME we probably should not have supported the above, see what
59# does it take to move away from it
cea43436 60 if (not $identity_col) {
61 foreach my $pk_col ($source->primary_columns) {
52416317 62 if (
63 ! exists $to_insert->{$pk_col}
64 and
65 $colinfo->{$pk_col}{data_type}
66 and
67 $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
68 ) {
cea43436 69 $identity_col = $pk_col;
70 last;
71 }
72 }
73 }
74
b0267fb7 75 if ($identity_col && (not exists $to_insert->{$identity_col})) {
db8f81cf 76 my $dbh = $self->_get_dbh;
77 my $table_name = $source->from;
78 $table_name = $$table_name if ref $table_name;
79
ddcc02d1 80 my ($identity) = dbic_internal_try {
9780718f 81 $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
548d1627 82 };
83
84 if (defined $identity) {
e366f807 85 $values->{$identity_col} = $identity;
548d1627 86 $self->_identity($identity);
87 }
88 }
89
e366f807 90 return $values;
548d1627 91}
92
4b3515a6 93sub _uuid_to_str {
94 my ($self, $data) = @_;
95
96 $data = unpack 'H*', $data;
97
98 for my $pos (8, 13, 18, 23) {
99 substr($data, $pos, 0) = '-';
100 }
101
102 return $data;
103}
104
105# select_single does not invoke a cursor object at all, hence UUID decoding happens
106# here if the proper cursor class is set
107sub select_single {
548d1627 108 my $self = shift;
4b3515a6 109
110 my @row = $self->next::method(@_);
111
112 return @row
113 unless $self->cursor_class->isa('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
114
548d1627 115 my ($ident, $select) = @_;
116
7e5fec1c 117 my $col_info = $self->_resolve_column_info($ident);
548d1627 118
119 for my $select_idx (0..$#$select) {
120 my $selected = $select->[$select_idx];
f200d74b 121
548d1627 122 next if ref $selected;
db8f81cf 123
4b3515a6 124 my $data_type = $col_info->{$selected}{data_type}
125 or next;
126
127 if ($self->_is_guid_type($data_type)) {
128 my $returned = $row[$select_idx];
548d1627 129
4b3515a6 130 if (length $returned == 16) {
131 $row[$select_idx] = $self->_uuid_to_str($returned);
132 }
548d1627 133 }
f200d74b 134 }
db8f81cf 135
4b3515a6 136 return @row;
db8f81cf 137}
138
2b0076be 139# this sub stolen from MSSQL
140
141sub build_datetime_parser {
142 my $self = shift;
ddcc02d1 143 dbic_internal_try {
63a18cfe 144 require DateTime::Format::Strptime;
9780718f 145 }
146 catch {
63a18cfe 147 $self->throw_exception("Couldn't load DateTime::Format::Strptime: $_");
9780718f 148 };
149
63a18cfe 150 return DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
2b0076be 151}
152
153=head2 connect_call_datetime_setup
154
155Used as:
156
157 on_connect_call => 'datetime_setup'
158
7df295ec 159In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
160timestamp formats (as temporary options for the session) for use with
2b0076be 161L<DBIx::Class::InflateColumn::DateTime>.
162
163The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
164second precision. The full precision is used.
165
166The C<DATE> data type supposedly stores hours and minutes too, according to the
167documentation, but I could not get that to work. It seems to only store the
168date.
169
170You will need the L<DateTime::Format::Strptime> module for inflation to work.
171
172=cut
173
174sub connect_call_datetime_setup {
175 my $self = shift;
176
177 $self->_do_query(
178 "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
179 );
180 $self->_do_query(
181 "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
182 );
183}
184
90d7422f 185sub _exec_svp_begin {
9cf3db6f 186 my ($self, $name) = @_;
187
90d7422f 188 $self->_dbh->do("SAVEPOINT $name");
9cf3db6f 189}
190
191# can't release savepoints that have been rolled back
90d7422f 192sub _exec_svp_release { 1 }
9cf3db6f 193
90d7422f 194sub _exec_svp_rollback {
9cf3db6f 195 my ($self, $name) = @_;
196
90d7422f 197 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
9cf3db6f 198}
199
f200d74b 2001;
db8f81cf 201
270eecac 202=head1 MAXIMUM CURSORS
203
7df295ec 204A L<DBIx::Class> application can use a lot of cursors, due to the usage of
205L<prepare_cached|DBI/prepare_cached>.
270eecac 206
207The default cursor maximum is C<50>, which can be a bit too low. This limit can
208be turned off (or increased) by the DBA by executing:
209
210 set option max_statement_count = 0
211 set option max_cursor_count = 0
212
213Highly recommended.
214
a2bd3796 215=head1 FURTHER QUESTIONS?
db8f81cf 216
a2bd3796 217Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
db8f81cf 218
a2bd3796 219=head1 COPYRIGHT AND LICENSE
db8f81cf 220
a2bd3796 221This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
222by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
223redistribute it and/or modify it under the same terms as the
224L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.