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