623a8e1c29dcf539f7d887b6f7e1344144c46756
[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/;
6 use mro 'c3';
7 use List::Util ();
8
9 __PACKAGE__->mk_group_accessors(simple => qw/
10   _identity
11 /);
12
13 =head1 NAME
14
15 DBIx::Class::Storage::DBI::SQLAnywhere - Driver for Sybase SQL Anywhere
16
17 =head1 DESCRIPTION
18
19 This class implements autoincrements for Sybase SQL Anywhere and selects the
20 RowNumberOver limit implementation.
21
22 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
23 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
24
25   /opt/sqlanywhere11/sdk/perl
26
27 Recommended L<DBIx::Class::Storage::DBI/connect_info> settings:
28
29   on_connect_call => 'datetime_setup'
30
31 =head1 METHODS
32
33 =cut
34
35 sub last_insert_id { shift->_identity }
36
37 sub insert {
38   my $self = shift;
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')");
57
58     $to_insert->{$identity_col} = $identity;
59
60     $self->_identity($identity);
61   }
62
63   return $self->next::method(@_);
64 }
65
66 # this sub stolen from DB2
67
68 sub _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}||{}} };
76 }
77
78 # this sub stolen from MSSQL
79
80 sub 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
90 Used as:
91
92     on_connect_call => 'datetime_setup'
93
94 In L<DBIx::Class::Storage::DBI/connect_info> to set the date and timestamp
95 formats (as temporary options for the session) for use with
96 L<DBIx::Class::InflateColumn::DateTime>.
97
98 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
99 second precision. The full precision is used.
100
101 The C<DATE> data type supposedly stores hours and minutes too, according to the
102 documentation, but I could not get that to work. It seems to only store the
103 date.
104
105 You will need the L<DateTime::Format::Strptime> module for inflation to work.
106
107 =cut
108
109 sub 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
120 1;
121
122 =head1 AUTHOR
123
124 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
125
126 =head1 LICENSE
127
128 You may distribute this code under the same terms as Perl itself.
129
130 =cut