better way to find minimal dbms version in ::Replicated
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLAnywhere.pm
1 package DBIx::Class::Storage::DBI::SQLAnywhere;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
6 use mro 'c3';
7 use List::Util ();
8
9 __PACKAGE__->mk_group_accessors(simple => qw/
10   _identity
11 /);
12
13 =head1 NAME
14
15 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
16
17 =head1 DESCRIPTION
18
19 This class implements autoincrements for Sybase SQL Anywhere, selects the
20 RowNumberOver limit implementation and provides
21 L<DBIx::Class::InflateColumn::DateTime> support.
22
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:
25
26   /opt/sqlanywhere11/sdk/perl
27
28 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
29
30   on_connect_call => 'datetime_setup'
31
32 =head1 METHODS
33
34 =cut
35
36 sub last_insert_id { shift->_identity }
37
38 sub _new_uuid { 'UUIDTOSTR(NEWID())' }
39
40 sub insert {
41   my $self = shift;
42   my ($source, $to_insert) = @_;
43
44   my $identity_col = List::Util::first {
45       $source->column_info($_)->{is_auto_increment} 
46   } $source->columns;
47
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)
53       {
54         $identity_col = $pk_col;
55         last;
56       }
57     }
58   }
59
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;
64
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
79 sub _select_args {
80   my $self = shift;
81   my ($ident, $select) = @_;
82
83   my $col_info = $self->_resolve_column_info($ident);
84
85   for my $select_idx (0..$#$select) {
86     my $selected = $select->[$select_idx];
87
88     next if ref $selected;
89
90     my $data_type = $col_info->{$selected}{data_type};
91
92     if ($data_type && lc($data_type) eq 'uniqueidentifier') {
93       $select->[$select_idx] = { UUIDTOSTR => $selected };
94     }
95   }
96
97   return $self->next::method(@_);
98 }
99
100 # this sub stolen from DB2
101
102 sub _sql_maker_opts {
103   my ( $self, $opts ) = @_;
104
105   if ( $opts ) {
106     $self->{_sql_maker_opts} = { %$opts };
107   }
108
109   return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} };
110 }
111
112 # this sub stolen from MSSQL
113
114 sub build_datetime_parser {
115   my $self = shift;
116   my $type = "DateTime::Format::Strptime";
117   eval "use ${type}";
118   $self->throw_exception("Couldn't load ${type}: $@") if $@;
119   return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
120 }
121
122 =head2 connect_call_datetime_setup
123
124 Used as:
125
126     on_connect_call => 'datetime_setup'
127
128 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
129 timestamp formats (as temporary options for the session) for use with
130 L<DBIx::Class::InflateColumn::DateTime>.
131
132 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
133 second precision. The full precision is used.
134
135 The C<DATE> data type supposedly stores hours and minutes too, according to the
136 documentation, but I could not get that to work. It seems to only store the
137 date.
138
139 You will need the L<DateTime::Format::Strptime> module for inflation to work.
140
141 =cut
142
143 sub connect_call_datetime_setup {
144   my $self = shift;
145
146   $self->_do_query(
147     "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
148   );
149   $self->_do_query(
150     "set temporary option date_format      = 'yyyy-mm-dd hh:mm:ss.ssssss'"
151   );
152 }
153
154 sub _svp_begin {
155     my ($self, $name) = @_;
156
157     $self->_get_dbh->do("SAVEPOINT $name");
158 }
159
160 # can't release savepoints that have been rolled back
161 sub _svp_release { 1 }
162
163 sub _svp_rollback {
164     my ($self, $name) = @_;
165
166     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
167 }
168
169 1;
170
171 =head1 MAXIMUM CURSORS
172
173 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
174 L<prepare_cached|DBI/prepare_cached>.
175
176 The default cursor maximum is C<50>, which can be a bit too low. This limit can
177 be turned off (or increased) by the DBA by executing:
178
179   set option max_statement_count = 0
180   set option max_cursor_count    = 0
181
182 Highly recommended.
183
184 =head1 AUTHOR
185
186 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
187
188 =head1 LICENSE
189
190 You may distribute this code under the same terms as Perl itself.
191
192 =cut