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