File Coverage

blib/lib/DBIx/Class/Storage/DBI/SQLAnywhere.pm
Criterion Covered Total %
statement 21 71 29.5
branch 0 20 0.0
condition 0 9 0.0
subroutine 7 17 41.1
pod 4 4 100.0
total 32 121 26.4


line stmt bran cond sub pod time code
1             package DBIx::Class::Storage::DBI::SQLAnywhere;
2              
3 3     3   732 use strict;
  3         7  
  3         90  
4 3     3   17 use warnings;
  3         7  
  3         85  
5 3     3   19 use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
  3         7  
  3         319  
6 3     3   22 use mro 'c3';
  3         6  
  3         28  
7 3     3   104 use DBIx::Class::_Util 'dbic_internal_try';
  3         9  
  3         138  
8 3     3   19 use DBIx::Class::ResultSource::FromSpec::Util 'fromspec_columns_info';
  3         6  
  3         122  
9 3     3   14 use namespace::clean;
  3         9  
  3         13  
10              
11             __PACKAGE__->mk_group_accessors(simple => qw/_identity/);
12             __PACKAGE__->sql_limit_dialect ('RowNumberOver');
13             __PACKAGE__->sql_quote_char ('"');
14              
15             __PACKAGE__->new_guid('UUIDTOSTR(NEWID())');
16              
17             # default to the UUID decoding cursor, overridable by the user
18             __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
19              
20             =head1 NAME
21              
22             DBIx::Class::Storage::DBI::SQLAnywhere - Driver for SQL Anywhere
23              
24             =head1 DESCRIPTION
25              
26             This class implements autoincrements for SQL Anywhere and provides
27             L support and support for the
28             C type (via
29             L.)
30              
31             You need the C driver that comes with the SQL Anywhere
32             distribution, B the one on CPAN. It is usually under a path such as:
33              
34             /opt/sqlanywhere11/sdk/perl
35              
36             Recommended L settings:
37              
38             on_connect_call => 'datetime_setup'
39              
40             =head1 METHODS
41              
42             =cut
43              
44 0     0 1   sub last_insert_id { shift->_identity }
45              
46             sub _prefetch_autovalues {
47 0     0     my $self = shift;
48 0           my ($source, $colinfo, $to_insert) = @_;
49              
50 0           my $values = $self->next::method(@_);
51              
52             my ($identity_col) =
53 0           grep { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
  0            
54              
55             # user might have an identity PK without is_auto_increment
56             #
57             # FIXME we probably should not have supported the above, see what
58             # does it take to move away from it
59 0 0         if (not $identity_col) {
60 0           foreach my $pk_col ($source->primary_columns) {
61 0 0 0       if (
      0        
62             ! exists $to_insert->{$pk_col}
63             and
64             $colinfo->{$pk_col}{data_type}
65             and
66             $colinfo->{$pk_col}{data_type} !~ /^uniqueidentifier/i
67             ) {
68 0           $identity_col = $pk_col;
69 0           last;
70             }
71             }
72             }
73              
74 0 0 0       if ($identity_col && (not exists $to_insert->{$identity_col})) {
75 0           my $dbh = $self->_get_dbh;
76 0           my $table_name = $source->from;
77 0 0         $table_name = $$table_name if ref $table_name;
78              
79             my ($identity) = dbic_internal_try {
80 0     0     $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
81 0           };
82              
83 0 0         if (defined $identity) {
84 0           $values->{$identity_col} = $identity;
85 0           $self->_identity($identity);
86             }
87             }
88              
89 0           return $values;
90             }
91              
92             sub _uuid_to_str {
93 0     0     my ($self, $data) = @_;
94              
95 0           $data = unpack 'H*', $data;
96              
97 0           for my $pos (8, 13, 18, 23) {
98 0           substr($data, $pos, 0) = '-';
99             }
100              
101 0           return $data;
102             }
103              
104             # select_single does not invoke a cursor object at all, hence UUID decoding happens
105             # here if the proper cursor class is set
106             sub select_single {
107 0     0 1   my $self = shift;
108              
109 0           my @row = $self->next::method(@_);
110              
111             return @row
112 0 0         unless $self->cursor_class->isa('DBIx::Class::Storage::DBI::SQLAnywhere::Cursor');
113              
114 0           my ($ident, $select) = @_;
115              
116 0           my $col_info = fromspec_columns_info($ident);
117              
118 0           for my $select_idx (0..$#$select) {
119 0           my $selected = $select->[$select_idx];
120              
121 0 0         next if ref $selected;
122              
123             my $data_type = $col_info->{$selected}{data_type}
124 0 0         or next;
125              
126 0 0         if ($self->_is_guid_type($data_type)) {
127 0           my $returned = $row[$select_idx];
128              
129 0 0         if (length $returned == 16) {
130 0           $row[$select_idx] = $self->_uuid_to_str($returned);
131             }
132             }
133             }
134              
135 0           return @row;
136             }
137              
138             sub build_datetime_parser {
139              
140 0     0 1   require DateTime::Format::Strptime;
141              
142 0           DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
143             }
144              
145             =head2 connect_call_datetime_setup
146              
147             Used as:
148              
149             on_connect_call => 'datetime_setup'
150              
151             In L to set the date and
152             timestamp formats (as temporary options for the session) for use with
153             L.
154              
155             The C data type supports up to 6 digits after the decimal point for
156             second precision. The full precision is used.
157              
158             The C data type supposedly stores hours and minutes too, according to the
159             documentation, but I could not get that to work. It seems to only store the
160             date.
161              
162             You will need the L module for inflation to work.
163              
164             =cut
165              
166             sub connect_call_datetime_setup {
167 0     0 1   my $self = shift;
168              
169 0           $self->_do_query(
170             "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
171             );
172 0           $self->_do_query(
173             "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
174             );
175             }
176              
177             sub _exec_svp_begin {
178 0     0     my ($self, $name) = @_;
179              
180 0           $self->_dbh->do("SAVEPOINT $name");
181             }
182              
183             # can't release savepoints that have been rolled back
184 0     0     sub _exec_svp_release { 1 }
185              
186             sub _exec_svp_rollback {
187 0     0     my ($self, $name) = @_;
188              
189 0           $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
190             }
191              
192             1;
193              
194             =head1 MAXIMUM CURSORS
195              
196             A L application can use a lot of cursors, due to the usage of
197             L.
198              
199             The default cursor maximum is C<50>, which can be a bit too low. This limit can
200             be turned off (or increased) by the DBA by executing:
201              
202             set option max_statement_count = 0
203             set option max_cursor_count = 0
204              
205             Highly recommended.
206              
207             =head1 FURTHER QUESTIONS?
208              
209             Check the list of L.
210              
211             =head1 COPYRIGHT AND LICENSE
212              
213             This module is free software L
214             by the L. You can
215             redistribute it and/or modify it under the same terms as the
216             L.