1 package DBIx::Class::Storage::DBI::SQLAnywhere;
5 use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
9 __PACKAGE__->mk_group_accessors(simple => qw/
15 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
19 This class implements autoincrements for Sybase SQL Anywhere, selects the
20 RowNumberOver limit implementation and provides
21 L<DBIx::Class::InflateColumn::DateTime> support.
23 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
24 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
26 /opt/sqlanywhere11/sdk/perl
28 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
30 on_connect_call => 'datetime_setup'
36 sub last_insert_id { shift->_identity }
38 sub _new_uuid { 'UUIDTOSTR(NEWID())' }
42 my ($source, $to_insert) = @_;
44 my $identity_col = List::Util::first {
45 $source->column_info($_)->{is_auto_increment}
48 # user might have an identity PK without is_auto_increment
49 if (not $identity_col) {
50 foreach my $pk_col ($source->primary_columns) {
51 if (not exists $to_insert->{$pk_col} &&
52 $source->column_info($pk_col)->{data_type} !~ /^uniqueidentifier/i)
54 $identity_col = $pk_col;
60 if ($identity_col && (not exists $to_insert->{$identity_col})) {
61 my $dbh = $self->_get_dbh;
62 my $table_name = $source->from;
63 $table_name = $$table_name if ref $table_name;
65 my ($identity) = eval {
66 local $@; $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
69 if (defined $identity) {
70 $to_insert->{$identity_col} = $identity;
71 $self->_identity($identity);
75 return $self->next::method(@_);
78 # convert UUIDs to strings in selects
81 my ($ident, $select) = @_;
83 my ($alias2source, $rs_alias) = $self->_resolve_ident_sources($ident);
85 for my $select_idx (0..$#$select) {
86 my $selected = $select->[$select_idx];
88 next if ref $selected;
90 my ($alias, $col) = split /\./, $selected;
91 ($alias, $col) = ($rs_alias, $selected) if not defined $col;
93 my $data_type = eval {
94 $alias2source->{$alias}->column_info($col)->{data_type}
97 if ($data_type && $data_type =~ /^uniqueidentifier\z/i) {
98 $select->[$select_idx] = { UUIDTOSTR => $selected };
102 return $self->next::method(@_);
105 # this sub stolen from DB2
107 sub _sql_maker_opts {
108 my ( $self, $opts ) = @_;
111 $self->{_sql_maker_opts} = { %$opts };
114 return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} };
117 # this sub stolen from MSSQL
119 sub build_datetime_parser {
121 my $type = "DateTime::Format::Strptime";
123 $self->throw_exception("Couldn't load ${type}: $@") if $@;
124 return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
127 =head2 connect_call_datetime_setup
131 on_connect_call => 'datetime_setup'
133 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
134 timestamp formats (as temporary options for the session) for use with
135 L<DBIx::Class::InflateColumn::DateTime>.
137 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
138 second precision. The full precision is used.
140 The C<DATE> data type supposedly stores hours and minutes too, according to the
141 documentation, but I could not get that to work. It seems to only store the
144 You will need the L<DateTime::Format::Strptime> module for inflation to work.
148 sub connect_call_datetime_setup {
152 "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
155 "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
160 my ($self, $name) = @_;
162 $self->_get_dbh->do("SAVEPOINT $name");
165 # can't release savepoints that have been rolled back
166 sub _svp_release { 1 }
169 my ($self, $name) = @_;
171 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
176 =head1 MAXIMUM CURSORS
178 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
179 L<prepare_cached|DBI/prepare_cached>.
181 The default cursor maximum is C<50>, which can be a bit too low. This limit can
182 be turned off (or increased) by the DBA by executing:
184 set option max_statement_count = 0
185 set option max_cursor_count = 0
191 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
195 You may distribute this code under the same terms as Perl itself.