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