DBIx/Class/Storage/DBI/SQLAnywhere.pm

filter: Statementstmt 33.3% 27 / 81 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 35.0% 7 / 20 Podpod 100% 4 / 4 totaltotal 27.3% 38 / 139 CC 17 SCAR 37.5
CC
17
Cov
25%
_prefetch_autovalues
110.0
select_single
56.0
_uuid_to_str
6.0
CRAP 42.5
« DBIx/Class/Optional/Dependencies.pm ↑ Summary DBIx/Class/Schema/Versioned.pm »
1 package DBIx::Class::Storage::DBI::SQLAnywhere;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw/DBIx::Class::Storage::DBI::UniqueIdentifier/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 __PACKAGE__->mk_group_accessors(simple => qw/_identity/);
12 2 __PACKAGE__->sql_limit_dialect ('RowNumberOver');
13 2 __PACKAGE__->sql_quote_char ('"');
14  
15 2 __PACKAGE__->new_guid('UUIDTOSTR(NEWID())');
16  
17 # default to the UUID decoding cursor, overridable by the user
18 2 __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<DBIx::Class::InflateColumn::DateTime> support and support for the
28 C<uniqueidentifier> type (via
29 L<DBIx::Class::Storage::DBI::SQLAnywhere::Cursor>.)
30  
31 You need the C<DBD::SQLAnywhere> driver that comes with the SQL Anywhere
32 distribution, B<NOT> the one on CPAN. It is usually under a path such as:
33  
34 /opt/sqlanywhere11/sdk/perl
35  
36 Recommended L<connect_info|DBIx::Class::Storage::DBI/connect_info> settings:
37  
38 on_connect_call => 'datetime_setup'
39  
40 =head1 METHODS
41  
42 =cut
43  
44 0 ▶sub last_insert_id { shift->_identity }
Subroutine last_insert_id: not called CC=1 SCAR=6.9
Pod documented
45  
46 sub _prefetch_autovalues {
47 0 ▶ my $self = shift;
Subroutine _prefetch_autovalues: not called CC=10 SCAR=47.0
48 0 my ($source, $colinfo, $to_insert) = @_;
49  
50 0 my $values = $self->next::method(@_);
51  
52 my $identity_col =
53 0 ▶ first { $colinfo->{$_}{is_auto_increment} } keys %$colinfo;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 ▶ if (not $identity_col) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($identity_col) 0 0 0
60 0 foreach my $pk_col ($source->primary_columns) {
61 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
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 ▶ 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
75 0 my $dbh = $self->_get_dbh;
76 0 my $table_name = $source->from;
77 0 ▶ $table_name = $$table_name if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 0 0
78  
79 my ($identity) = try {
80 0 ▶ $dbh->selectrow_array("SELECT GET_IDENTITY('$table_name')")
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 };
82  
83 0 ▶ if (defined $identity) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $identity) 0 0 0
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 ▶ my ($self, $data) = @_;
Subroutine _uuid_to_str: not called CC=2 SCAR=17.9
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 ▶ my $self = shift;
Subroutine select_single: not called CC=7 SCAR=40.3
Pod documented
108  
109 0 my @row = $self->next::method(@_);
110  
111 return @row
112 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
113  
114 0 my ($ident, $select) = @_;
115  
116 0 my $col_info = $self->_resolve_column_info($ident);
117  
118 0 for my $select_idx (0..$#$select) {
119 0 my $selected = $select->[$select_idx];
120  
121 0 ▶ next if ref $selected;
Branch: 1 branch
BranchTrueFalseTotal
if ref $selected 0 0 0
122  
123 my $data_type = $col_info->{$selected}{data_type}
124 0 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
my $data_type = $col_info->{$selected}{"data_type"} or next 0 0 0
125  
126 0 ▶ if ($self->_is_guid_type($data_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_guid_type($data_type)) 0 0 0
127 0 my $returned = $row[$select_idx];
128  
129 0 ▶ if (length $returned == 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $returned == 16) 0 0 0
130 0 $row[$select_idx] = $self->_uuid_to_str($returned);
131 }
132 }
133 }
134  
135 0 return @row;
136 }
137  
138 # this sub stolen from MSSQL
139  
140 sub build_datetime_parser {
141 0 ▶ my $self = shift;
Subroutine build_datetime_parser: not called CC=1 SCAR=6.9
Pod documented
142 0 my $type = "DateTime::Format::Strptime";
143 try {
144 0 ▶ eval "require ${type}"
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 }
146 catch {
147 0 ▶ $self->throw_exception("Couldn't load ${type}: $_");
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 };
149  
150 0 return $type->new( pattern => '%Y-%m-%d %H:%M:%S.%6N' );
151 }
152  
153 =head2 connect_call_datetime_setup
154  
155 Used as:
156  
157 on_connect_call => 'datetime_setup'
158  
159 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the date and
160 timestamp formats (as temporary options for the session) for use with
161 L<DBIx::Class::InflateColumn::DateTime>.
162  
163 The C<TIMESTAMP> data type supports up to 6 digits after the decimal point for
164 second precision. The full precision is used.
165  
166 The C<DATE> data type supposedly stores hours and minutes too, according to the
167 documentation, but I could not get that to work. It seems to only store the
168 date.
169  
170 You will need the L<DateTime::Format::Strptime> module for inflation to work.
171  
172 =cut
173  
174 sub connect_call_datetime_setup {
175 0 ▶ my $self = shift;
Subroutine connect_call_datetime_setup: not called CC=1 SCAR=6.9
Pod documented
176  
177 0 $self->_do_query(
178 "set temporary option timestamp_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
179 );
180 0 $self->_do_query(
181 "set temporary option date_format = 'yyyy-mm-dd hh:mm:ss.ssssss'"
182 );
183 }
184  
185 sub _exec_svp_begin {
186 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
187  
188 0 $self->_dbh->do("SAVEPOINT $name");
189 }
190  
191 # can't release savepoints that have been rolled back
192 0 ▶sub _exec_svp_release { 1 }
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
193  
194 sub _exec_svp_rollback {
195 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
196  
197 0 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
198 }
199  
200 2 1;
201  
202 =head1 MAXIMUM CURSORS
203  
204 A L<DBIx::Class> application can use a lot of cursors, due to the usage of
205 L<prepare_cached|DBI/prepare_cached>.
206  
207 The default cursor maximum is C<50>, which can be a bit too low. This limit can
208 be turned off (or increased) by the DBA by executing:
209  
210 set option max_statement_count = 0
211 set option max_cursor_count = 0
212  
213 Highly recommended.
214  
215 =head1 AUTHOR
216  
217 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
218  
219 =head1 LICENSE
220  
221 You may distribute this code under the same terms as Perl itself.
222  
223 =cut
« DBIx/Class/Optional/Dependencies.pm ↑ Summary DBIx/Class/Schema/Versioned.pm »