Commit | Line | Data |
db8f81cf |
1 | package DBIx::Class::Storage::DBI::SQLAnywhere; |
f200d74b |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use base qw/DBIx::Class::Storage::DBI/; |
6 | use mro 'c3'; |
db8f81cf |
7 | use List::Util (); |
f200d74b |
8 | |
db8f81cf |
9 | __PACKAGE__->mk_group_accessors(simple => qw/ |
10 | _identity |
11 | /); |
12 | |
13 | =head1 NAME |
14 | |
2b0076be |
15 | DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere |
db8f81cf |
16 | |
17 | =head1 DESCRIPTION |
18 | |
4b8dd353 |
19 | This class implements autoincrements for Sybase SQL Anywhere, selects the |
20 | RowNumberOver limit implementation and provides |
21 | L<DBIx::Class::InflateColumn::DateTime> support. |
db8f81cf |
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 | |
2b0076be |
26 | /opt/sqlanywhere11/sdk/perl |
27 | |
28 | Recommended L<DBIx::Class::Storage::DBI/connect_info> settings: |
29 | |
30 | on_connect_call => 'datetime_setup' |
31 | |
32 | =head1 METHODS |
db8f81cf |
33 | |
34 | =cut |
35 | |
36 | sub last_insert_id { shift->_identity } |
37 | |
38 | sub insert { |
f200d74b |
39 | my $self = shift; |
db8f81cf |
40 | my ($source, $to_insert) = @_; |
41 | |
db8f81cf |
42 | my $identity_col = List::Util::first { |
43 | $source->column_info($_)->{is_auto_increment} |
44 | } $source->columns; |
45 | |
b0267fb7 |
46 | if ($identity_col && (not exists $to_insert->{$identity_col})) { |
db8f81cf |
47 | my $dbh = $self->_get_dbh; |
48 | my $table_name = $source->from; |
49 | $table_name = $$table_name if ref $table_name; |
50 | |
51 | my ($identity) = $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')"); |
f200d74b |
52 | |
db8f81cf |
53 | $to_insert->{$identity_col} = $identity; |
54 | |
55 | $self->_identity($identity); |
f200d74b |
56 | } |
db8f81cf |
57 | |
58 | return $self->next::method(@_); |
59 | } |
60 | |
2b0076be |
61 | # this sub stolen from DB2 |
db8f81cf |
62 | |
63 | sub _sql_maker_opts { |
64 | my ( $self, $opts ) = @_; |
65 | |
66 | if ( $opts ) { |
67 | $self->{_sql_maker_opts} = { %$opts }; |
68 | } |
69 | |
70 | return { limit_dialect => 'RowNumberOver', %{$self->{_sql_maker_opts}||{}} }; |
f200d74b |
71 | } |
72 | |
2b0076be |
73 | # this sub stolen from MSSQL |
74 | |
75 | sub build_datetime_parser { |
76 | my $self = shift; |
77 | my $type = "DateTime::Format::Strptime"; |
78 | eval "use ${type}"; |
79 | $self->throw_exception("Couldn't load ${type}: $@") if $@; |
80 | return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' ); |
81 | } |
82 | |
83 | =head2 connect_call_datetime_setup |
84 | |
85 | Used as: |
86 | |
87 | on_connect_call => 'datetime_setup' |
88 | |
89 | In L<DBIx::Class::Storage::DBI/connect_info> to set the date and timestamp |
90 | formats (as temporary options for the session) for use with |
91 | L<DBIx::Class::InflateColumn::DateTime>. |
92 | |
93 | The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for |
94 | second precision. The full precision is used. |
95 | |
96 | The C<DATE> data type supposedly stores hours and minutes too, according to the |
97 | documentation, but I could not get that to work. It seems to only store the |
98 | date. |
99 | |
100 | You will need the L<DateTime::Format::Strptime> module for inflation to work. |
101 | |
102 | =cut |
103 | |
104 | sub connect_call_datetime_setup { |
105 | my $self = shift; |
106 | |
107 | $self->_do_query( |
108 | "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'" |
109 | ); |
110 | $self->_do_query( |
111 | "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'" |
112 | ); |
113 | } |
114 | |
f200d74b |
115 | 1; |
db8f81cf |
116 | |
117 | =head1 AUTHOR |
118 | |
119 | See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>. |
120 | |
121 | =head1 LICENSE |
122 | |
123 | You may distribute this code under the same terms as Perl itself. |
124 | |
125 | =cut |