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