1 package DBIx::Class::Storage::DBI::SQLAnywhere;
5 use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
7 use List::Util 'first';
11 __PACKAGE__->mk_group_accessors(simple => qw/
17 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
21 This class implements autoincrements for Sybase SQL Anywhere, selects the
22 RowNumberOver limit implementation and provides
23 L<DBIx::Class::InflateColumn::DateTime> support.
25 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
26 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
28 /opt/sqlanywhere11/sdk/perl
30 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
32 on_connect_call => 'datetime_setup'
38 sub last_insert_id { shift->_identity }
40 sub _new_uuid { 'UUIDTOSTR(NEWID())' }
44 my ($source, $to_insert) = @_;
47 first { $source->column_info($_)->{is_auto_increment} } $source->columns;
49 # user might have an identity PK without is_auto_increment
50 if (not $identity_col) {
51 foreach my $pk_col ($source->primary_columns) {
52 if (not exists $to_insert->{$pk_col} &&
53 $source->column_info($pk_col)->{data_type} !~ /^uniqueidentifier/i)
55 $identity_col = $pk_col;
61 if ($identity_col && (not exists $to_insert->{$identity_col})) {
62 my $dbh = $self->_get_dbh;
63 my $table_name = $source->from;
64 $table_name = $$table_name if ref $table_name;
66 my ($identity) = try {
67 $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
70 if (defined $identity) {
71 $to_insert->{$identity_col} = $identity;
72 $self->_identity($identity);
76 return $self->next::method(@_);
79 # convert UUIDs to strings in selects
82 my ($ident, $select) = @_;
84 my $col_info = $self->_resolve_column_info($ident);
86 for my $select_idx (0..$#$select) {
87 my $selected = $select->[$select_idx];
89 next if ref $selected;
91 my $data_type = $col_info->{$selected}{data_type};
93 if ($data_type && lc($data_type) eq 'uniqueidentifier') {
94 $select->[$select_idx] = { UUIDTOSTR => $selected };
98 return $self->next::method(@_);
101 # this sub stolen from DB2
103 sub _sql_maker_opts {
104 my ( $self, $opts ) = @_;
107 $self->{_sql_maker_opts} = { %$opts };
110 return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} };
113 # this sub stolen from MSSQL
115 sub build_datetime_parser {
117 my $type = "DateTime::Format::Strptime";
119 eval "require ${type}"
122 $self->throw_exception("Couldn't load ${type}: $_");
125 return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
128 =head2 connect_call_datetime_setup
132 on_connect_call => 'datetime_setup'
134 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
135 timestamp formats (as temporary options for the session) for use with
136 L<DBIx::Class::InflateColumn::DateTime>.
138 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
139 second precision. The full precision is used.
141 The C<DATE> data type supposedly stores hours and minutes too, according to the
142 documentation, but I could not get that to work. It seems to only store the
145 You will need the L<DateTime::Format::Strptime> module for inflation to work.
149 sub connect_call_datetime_setup {
153 "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
156 "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
161 my ($self, $name) = @_;
163 $self->_get_dbh->do("SAVEPOINT $name");
166 # can't release savepoints that have been rolled back
167 sub _svp_release { 1 }
170 my ($self, $name) = @_;
172 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
177 =head1 MAXIMUM CURSORS
179 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
180 L<prepare_cached|DBI/prepare_cached>.
182 The default cursor maximum is C<50>, which can be a bit too low. This limit can
183 be turned off (or increased) by the DBA by executing:
185 set option max_statement_count = 0
186 set option max_cursor_count = 0
192 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
196 You may distribute this code under the same terms as Perl itself.