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