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/_identity/);
12 __PACKAGE__->sql_limit_dialect ('RowNumberOver');
13 __PACKAGE__->sql_quote_char ('"');
15 __PACKAGE__->new_guid('UUIDTOSTR(NEWID())');
17 # default to the UUID decoding cursor, overridable by the user
18 __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
22 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for SQL Anywhere
26 This class implements autoincrements for SQL Anywhere and provides
27 L<DBIx::Class::InflateColumn::DateTime> support and support for the
28 C<uniqueidentifier> type (via
29 L<DBIx::Class::Storage::DBI::SQLAnywhere::Cursor>.)
31 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
32 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
34 /opt/sqlanywhere11/sdk/perl
36 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
38 on_connect_call => 'datetime_setup'
44 sub last_insert_id { shift->_identity }
46 sub _prefetch_autovalues {
48 my ($source, $colinfo, $to_insert) = @_;
50 my $values = $self->next::method(@_);
53 first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
55 # user might have an identity PK without is_auto_increment
57 # FIXME we probably should not have supported the above, see what
58 # does it take to move away from it
59 if (not $identity_col) {
60 foreach my $pk_col ($source->primary_columns) {
62 ! exists $to_insert->{$pk_col}
64 $colinfo->{$pk_col}{data_type}
66 $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
68 $identity_col = $pk_col;
74 if ($identity_col && (not exists $to_insert->{$identity_col})) {
75 my $dbh = $self->_get_dbh;
76 my $table_name = $source->from;
77 $table_name = $$table_name if ref $table_name;
79 my ($identity) = try {
80 $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
83 if (defined $identity) {
84 $values->{$identity_col} = $identity;
85 $self->_identity($identity);
93 my ($self, $data) = @_;
95 $data = unpack 'H*', $data;
97 for my $pos (8, 13, 18, 23) {
98 substr($data, $pos, 0) = '-';
104 # select_single does not invoke a cursor object at all, hence UUID decoding happens
105 # here if the proper cursor class is set
109 my @row = $self->next::method(@_);
112 unless $self->cursor_class->isa('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
114 my ($ident, $select) = @_;
116 my $col_info = $self->_resolve_column_info($ident);
118 for my $select_idx (0..$#$select) {
119 my $selected = $select->[$select_idx];
121 next if ref $selected;
123 my $data_type = $col_info->{$selected}{data_type}
126 if ($self->_is_guid_type($data_type)) {
127 my $returned = $row[$select_idx];
129 if (length $returned == 16) {
130 $row[$select_idx] = $self->_uuid_to_str($returned);
138 # this sub stolen from MSSQL
140 sub build_datetime_parser {
142 my $type = "DateTime::Format::Strptime";
144 eval "require ${type}"
147 $self->throw_exception("Couldn't load ${type}: $_");
150 return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
153 =head2 connect_call_datetime_setup
157 on_connect_call => 'datetime_setup'
159 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
160 timestamp formats (as temporary options for the session) for use with
161 L<DBIx::Class::InflateColumn::DateTime>.
163 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
164 second precision. The full precision is used.
166 The C<DATE> data type supposedly stores hours and minutes too, according to the
167 documentation, but I could not get that to work. It seems to only store the
170 You will need the L<DateTime::Format::Strptime> module for inflation to work.
174 sub connect_call_datetime_setup {
178 "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
181 "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
185 sub _exec_svp_begin {
186 my ($self, $name) = @_;
188 $self->_dbh->do("SAVEPOINT $name");
191 # can't release savepoints that have been rolled back
192 sub _exec_svp_release { 1 }
194 sub _exec_svp_rollback {
195 my ($self, $name) = @_;
197 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
202 =head1 MAXIMUM CURSORS
204 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
205 L<prepare_cached|DBI/prepare_cached>.
207 The default cursor maximum is C<50>, which can be a bit too low. This limit can
208 be turned off (or increased) by the DBA by executing:
210 set option max_statement_count = 0
211 set option max_cursor_count = 0
217 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
221 You may distribute this code under the same terms as Perl itself.