Set name_sep by default (even if unused). Simplify raw-sql scanner code
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLAnywhere.pm
CommitLineData
db8f81cf 1package DBIx::Class::Storage::DBI::SQLAnywhere;
f200d74b 2
3use strict;
4use warnings;
548d1627 5use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
f200d74b 6use mro 'c3';
6298a324 7use List::Util 'first';
52b420dd 8use Try::Tiny;
fd323bf1 9use namespace::clean;
f200d74b 10
6a247f33 11__PACKAGE__->mk_group_accessors(simple => qw/_identity/);
12__PACKAGE__->sql_limit_dialect ('RowNumberOver');
db8f81cf 13
14=head1 NAME
15
2b0076be 16DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
db8f81cf 17
18=head1 DESCRIPTION
19
d5dedbd6 20This class implements autoincrements for Sybase SQL Anywhere and provides
4b8dd353 21L<DBIx::Class::InflateColumn::DateTime> support.
db8f81cf 22
23You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
24distribution, B<NOT> the one on CPAN. It is usually under a path such as:
25
2b0076be 26 /opt/sqlanywhere11/sdk/perl
27
7df295ec 28Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
2b0076be 29
30 on_connect_call => 'datetime_setup'
31
32=head1 METHODS
db8f81cf 33
34=cut
35
36sub last_insert_id { shift->_identity }
37
548d1627 38sub _new_uuid { 'UUIDTOSTR(NEWID())' }
39
db8f81cf 40sub insert {
f200d74b 41 my $self = shift;
db8f81cf 42 my ($source, $to_insert) = @_;
43
6298a324 44 my $identity_col =
45 first { $source->column_info($_)->{is_auto_increment} } $source->columns;
db8f81cf 46
cea43436 47# user might have an identity PK without is_auto_increment
48 if (not $identity_col) {
49 foreach my $pk_col ($source->primary_columns) {
548d1627 50 if (not exists $to_insert->{$pk_col} &&
51 $source->column_info($pk_col)->{data_type} !~ /^uniqueidentifier/i)
52 {
cea43436 53 $identity_col = $pk_col;
54 last;
55 }
56 }
57 }
58
b0267fb7 59 if ($identity_col && (not exists $to_insert->{$identity_col})) {
db8f81cf 60 my $dbh = $self->_get_dbh;
61 my $table_name = $source->from;
62 $table_name = $$table_name if ref $table_name;
63
9780718f 64 my ($identity) = try {
65 $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
548d1627 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
78sub _select_args {
79 my $self = shift;
80 my ($ident, $select) = @_;
81
7e5fec1c 82 my $col_info = $self->_resolve_column_info($ident);
548d1627 83
84 for my $select_idx (0..$#$select) {
85 my $selected = $select->[$select_idx];
f200d74b 86
548d1627 87 next if ref $selected;
db8f81cf 88
7e5fec1c 89 my $data_type = $col_info->{$selected}{data_type};
548d1627 90
7da56142 91 if ($data_type && lc($data_type) eq 'uniqueidentifier') {
548d1627 92 $select->[$select_idx] = { UUIDTOSTR => $selected };
93 }
f200d74b 94 }
db8f81cf 95
96 return $self->next::method(@_);
97}
98
2b0076be 99# this sub stolen from MSSQL
100
101sub build_datetime_parser {
102 my $self = shift;
103 my $type = "DateTime::Format::Strptime";
9780718f 104 try {
52b420dd 105 eval "require ${type}"
9780718f 106 }
107 catch {
108 $self->throw_exception("Couldn't load ${type}: $_");
109 };
110
2b0076be 111 return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
112}
113
114=head2 connect_call_datetime_setup
115
116Used as:
117
118 on_connect_call => 'datetime_setup'
119
7df295ec 120In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
121timestamp formats (as temporary options for the session) for use with
2b0076be 122L<DBIx::Class::InflateColumn::DateTime>.
123
124The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
125second precision. The full precision is used.
126
127The C<DATE> data type supposedly stores hours and minutes too, according to the
128documentation, but I could not get that to work. It seems to only store the
129date.
130
131You will need the L<DateTime::Format::Strptime> module for inflation to work.
132
133=cut
134
135sub 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
9cf3db6f 146sub _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
153sub _svp_release { 1 }
154
155sub _svp_rollback {
156 my ($self, $name) = @_;
157
158 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
159}
160
f200d74b 1611;
db8f81cf 162
270eecac 163=head1 MAXIMUM CURSORS
164
7df295ec 165A L<DBIx::Class> application can use a lot of cursors, due to the usage of
166L<prepare_cached|DBI/prepare_cached>.
270eecac 167
168The default cursor maximum is C<50>, which can be a bit too low. This limit can
169be 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
174Highly recommended.
175
db8f81cf 176=head1 AUTHOR
177
178See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
179
180=head1 LICENSE
181
182You may distribute this code under the same terms as Perl itself.
183
184=cut