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