Try::Tiny conversion finished
[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 use Try::Tiny;
9
10 __PACKAGE__->mk_group_accessors(simple => qw/
11   _identity
12 /);
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, selects the
21 RowNumberOver limit implementation and provides
22 L<DBIx::Class::InflateColumn::DateTime> support.
23
24 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
25 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
26
27   /opt/sqlanywhere11/sdk/perl
28
29 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
30
31   on_connect_call => 'datetime_setup'
32
33 =head1 METHODS
34
35 =cut
36
37 sub last_insert_id { shift->_identity }
38
39 sub _new_uuid { 'UUIDTOSTR(NEWID())' }
40
41 sub insert {
42   my $self = shift;
43   my ($source, $to_insert) = @_;
44
45   my $identity_col = List::Util::first {
46       $source->column_info($_)->{is_auto_increment} 
47   } $source->columns;
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 (not exists $to_insert->{$pk_col} &&
53           $source->column_info($pk_col)->{data_type} !~ /^uniqueidentifier/i)
54       {
55         $identity_col = $pk_col;
56         last;
57       }
58     }
59   }
60
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;
65
66     my ($identity) = try {
67       $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
68     };
69
70     if (defined $identity) {
71       $to_insert->{$identity_col} = $identity;
72       $self->_identity($identity);
73     }
74   }
75
76   return $self->next::method(@_);
77 }
78
79 # convert UUIDs to strings in selects
80 sub _select_args {
81   my $self = shift;
82   my ($ident, $select) = @_;
83
84   my $col_info = $self->_resolve_column_info($ident);
85
86   for my $select_idx (0..$#$select) {
87     my $selected = $select->[$select_idx];
88
89     next if ref $selected;
90
91     my $data_type = $col_info->{$selected}{data_type};
92
93     if ($data_type && lc($data_type) eq 'uniqueidentifier') {
94       $select->[$select_idx] = { UUIDTOSTR => $selected };
95     }
96   }
97
98   return $self->next::method(@_);
99 }
100
101 # this sub stolen from DB2
102
103 sub _sql_maker_opts {
104   my ( $self, $opts ) = @_;
105
106   if ( $opts ) {
107     $self->{_sql_maker_opts} = { %$opts };
108   }
109
110   return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} };
111 }
112
113 # this sub stolen from MSSQL
114
115 sub build_datetime_parser {
116   my $self = shift;
117   my $type = "DateTime::Format::Strptime";
118   try {
119     eval "require ${type}"
120   }
121   catch {
122     $self->throw_exception("Couldn't load ${type}: $_");
123   };
124
125   return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
126 }
127
128 =head2 connect_call_datetime_setup
129
130 Used as:
131
132     on_connect_call => 'datetime_setup'
133
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>.
137
138 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
139 second precision. The full precision is used.
140
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
143 date.
144
145 You will need the L<DateTime::Format::Strptime> module for inflation to work.
146
147 =cut
148
149 sub connect_call_datetime_setup {
150   my $self = shift;
151
152   $self->_do_query(
153     "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
154   );
155   $self->_do_query(
156     "set temporary option date_format      = 'yyyy-mm-dd hh:mm:ss.ssssss'"
157   );
158 }
159
160 sub _svp_begin {
161     my ($self, $name) = @_;
162
163     $self->_get_dbh->do("SAVEPOINT $name");
164 }
165
166 # can't release savepoints that have been rolled back
167 sub _svp_release { 1 }
168
169 sub _svp_rollback {
170     my ($self, $name) = @_;
171
172     $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
173 }
174
175 1;
176
177 =head1 MAXIMUM CURSORS
178
179 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
180 L<prepare_cached|DBI/prepare_cached>.
181
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:
184
185   set option max_statement_count = 0
186   set option max_cursor_count    = 0
187
188 Highly recommended.
189
190 =head1 AUTHOR
191
192 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
193
194 =head1 LICENSE
195
196 You may distribute this code under the same terms as Perl itself.
197
198 =cut