DBIx/Class/Storage/DBI/SQLAnywhere.pm

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