1 package DBIx::Class::Storage::DBI::SQLAnywhere;
5 use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
7 use List::Util 'first';
9 use DBIx::Class::Storage::DBI::SQLAnywhere::Cursor ();
12 __PACKAGE__->mk_group_accessors(simple => qw/_identity/);
13 __PACKAGE__->sql_limit_dialect ('RowNumberOver');
14 __PACKAGE__->sql_quote_char ('"');
16 __PACKAGE__->new_guid('UUIDTOSTR(NEWID())');
18 # default to the UUID decoding cursor, overridable by the user
19 __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
23 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for SQL Anywhere
27 This class implements autoincrements for SQL Anywhere and provides
28 L<DBIx::Class::InflateColumn::DateTime> support and support for the
29 C<uniqueidentifier> type (via
30 L<DBIx::Class::Storage::DBI::SQLAnywhere::Cursor>.)
32 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
33 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
35 /opt/sqlanywhere11/sdk/perl
37 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
39 on_connect_call => 'datetime_setup'
45 sub last_insert_id { shift->_identity }
47 sub _prefetch_autovalues {
49 my ($source, $to_insert) = @_;
51 my $values = $self->next::method(@_);
53 my $colinfo = $source->columns_info;
56 first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
58 # user might have an identity PK without is_auto_increment
60 # FIXME we probably should not have supported the above, see what
61 # does it take to move away from it
62 if (not $identity_col) {
63 foreach my $pk_col ($source->primary_columns) {
65 ! exists $to_insert->{$pk_col}
67 $colinfo->{$pk_col}{data_type}
69 $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
71 $identity_col = $pk_col;
77 if ($identity_col && (not exists $to_insert->{$identity_col})) {
78 my $dbh = $self->_get_dbh;
79 my $table_name = $source->from;
80 $table_name = $$table_name if ref $table_name;
82 my ($identity) = try {
83 $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
86 if (defined $identity) {
87 $values->{$identity_col} = $identity;
88 $self->_identity($identity);
96 my ($self, $data) = @_;
98 $data = unpack 'H*', $data;
100 for my $pos (8, 13, 18, 23) {
101 substr($data, $pos, 0) = '-';
107 # select_single does not invoke a cursor object at all, hence UUID decoding happens
108 # here if the proper cursor class is set
112 my @row = $self->next::method(@_);
115 unless $self->cursor_class->isa('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
117 my ($ident, $select) = @_;
119 my $col_info = $self->_resolve_column_info($ident);
121 for my $select_idx (0..$#$select) {
122 my $selected = $select->[$select_idx];
124 next if ref $selected;
126 my $data_type = $col_info->{$selected}{data_type}
129 if ($self->_is_guid_type($data_type)) {
130 my $returned = $row[$select_idx];
132 if (length $returned == 16) {
133 $row[$select_idx] = $self->_uuid_to_str($returned);
141 # this sub stolen from MSSQL
143 sub build_datetime_parser {
145 my $type = "DateTime::Format::Strptime";
147 eval "require ${type}"
150 $self->throw_exception("Couldn't load ${type}: $_");
153 return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
156 =head2 connect_call_datetime_setup
160 on_connect_call => 'datetime_setup'
162 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
163 timestamp formats (as temporary options for the session) for use with
164 L<DBIx::Class::InflateColumn::DateTime>.
166 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
167 second precision. The full precision is used.
169 The C<DATE> data type supposedly stores hours and minutes too, according to the
170 documentation, but I could not get that to work. It seems to only store the
173 You will need the L<DateTime::Format::Strptime> module for inflation to work.
177 sub connect_call_datetime_setup {
181 "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
184 "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
188 sub _exec_svp_begin {
189 my ($self, $name) = @_;
191 $self->_dbh->do("SAVEPOINT $name");
194 # can't release savepoints that have been rolled back
195 sub _exec_svp_release { 1 }
197 sub _exec_svp_rollback {
198 my ($self, $name) = @_;
200 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
205 =head1 MAXIMUM CURSORS
207 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
208 L<prepare_cached|DBI/prepare_cached>.
210 The default cursor maximum is C<50>, which can be a bit too low. This limit can
211 be turned off (or increased) by the DBA by executing:
213 set option max_statement_count = 0
214 set option max_cursor_count = 0
220 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
224 You may distribute this code under the same terms as Perl itself.