df301d2bb574c509564d348b2217340decbc280f
[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 'first';
8 use Try::Tiny;
9 use namespace::clean;
10
11 __PACKAGE__->mk_group_accessors(simple => qw/_identity/);
12 __PACKAGE__->sql_limit_dialect ('RowNumberOver');
13
14 =head1 NAME
15
16 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
17
18 =head1 DESCRIPTION
19
20 This class implements autoincrements for Sybase SQL Anywhere 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 $colinfo = $source->columns_info;
45
46   my $identity_col =
47     first { $_->{is_auto_increment} } values %$colinfo;
48
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 (
53         ! exists $to_insert->{$pk_col}
54           and
55         $colinfo->{$pk_col}{data_type}
56           and
57         $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
58       ) {
59         $identity_col = $pk_col;
60         last;
61       }
62     }
63   }
64
65   if ($identity_col && (not exists $to_insert->{$identity_col})) {
66     my $dbh = $self->_get_dbh;
67     my $table_name = $source->from;
68     $table_name    = $$table_name if ref $table_name;
69
70     my ($identity) = try {
71       $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
72     };
73
74     if (defined $identity) {
75       $to_insert->{$identity_col} = $identity;
76       $self->_identity($identity);
77     }
78   }
79
80   return $self->next::method(@_);
81 }
82
83 # convert UUIDs to strings in selects
84 sub _select_args {
85   my $self = shift;
86   my ($ident, $select) = @_;
87
88   my $col_info = $self->_resolve_column_info($ident);
89
90   for my $select_idx (0..$#$select) {
91     my $selected = $select->[$select_idx];
92
93     next if ref $selected;
94
95     my $data_type = $col_info->{$selected}{data_type};
96
97     if ($data_type && lc($data_type) eq 'uniqueidentifier') {
98       $select->[$select_idx] = { UUIDTOSTR => $selected };
99     }
100   }
101
102   return $self->next::method(@_);
103 }
104
105 # this sub stolen from MSSQL
106
107 sub build_datetime_parser {
108   my $self = shift;
109   my $type = "DateTime::Format::Strptime";
110   try {
111     eval "require ${type}"
112   }
113   catch {
114     $self->throw_exception("Couldn't load ${type}: $_");
115   };
116
117   return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
118 }
119
120 =head2 connect_call_datetime_setup
121
122 Used as:
123
124     on_connect_call => 'datetime_setup'
125
126 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
127 timestamp formats (as temporary options for the session) for use with
128 L<DBIx::Class::InflateColumn::DateTime>.
129
130 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
131 second precision. The full precision is used.
132
133 The C<DATE> data type supposedly stores hours and minutes too, according to the
134 documentation, but I could not get that to work. It seems to only store the
135 date.
136
137 You will need the L<DateTime::Format::Strptime> module for inflation to work.
138
139 =cut
140
141 sub connect_call_datetime_setup {
142   my $self = shift;
143
144   $self->_do_query(
145     "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
146   );
147   $self->_do_query(
148     "set temporary option date_format      = 'yyyy-mm-dd hh:mm:ss.ssssss'"
149   );
150 }
151
152 sub _svp_begin {
153     my ($self, $name) = @_;
154
155     $self->_get_dbh->do("SAVEPOINT $name");
156 }
157
158 # can't release savepoints that have been rolled back
159 sub _svp_release { 1 }
160
161 sub _svp_rollback {
162     my ($self, $name) = @_;
163
164     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
165 }
166
167 1;
168
169 =head1 MAXIMUM CURSORS
170
171 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
172 L<prepare_cached|DBI/prepare_cached>.
173
174 The default cursor maximum is C<50>, which can be a bit too low. This limit can
175 be turned off (or increased) by the DBA by executing:
176
177   set option max_statement_count = 0
178   set option max_cursor_count    = 0
179
180 Highly recommended.
181
182 =head1 AUTHOR
183
184 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
185
186 =head1 LICENSE
187
188 You may distribute this code under the same terms as Perl itself.
189
190 =cut