ORM/Db/DBI/SQLite.pm

filter: Statementstmt 93.6% 74 / 79 Branchbran 67.6% 23 / 34 Conditioncond 25.0% 2 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 10 totaltotal 75.6% 112 / 148 CC 21 SCAR 22.8
CC
21
Cov
82%
table_struct
16.7
_lost_connection
12.0
insertid
2.1
CRAP 9.8
« ORM/Metaprop.pm ↑ Summary ORM/Order.pm »
1 #
2 # DESCRIPTION
3 # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl
4 # library that implements object-relational mapping. Its features are
5 # much similar to those of Java's Hibernate library, but interface is
6 # much different and easier to use.
7 #
8 # AUTHOR
9 # Alexey V. Akimov <akimov_alexey@sourceforge.net>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2005-2006 Alexey V. Akimov
13 #
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18 #
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23 #
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #
28  
29 package ORM::Db::DBI::SQLite;
30  
31 4 $VERSION = 0.8;
32  
33 4 ▶use base 'ORM::Db::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 ##
36 ## CONSTRUCTORS
37 ##
38  
39 sub new
40 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
41 4 my $class = shift;
42 4 my %arg = @_;
43  
44 4 $arg{driver} = 'SQLite';
45 4 $class->SUPER::new( %arg );
46 }
47  
48 ##
49 ## CLASS METHODS
50 ##
51  
52 sub qc
53 219 ▶{
Subroutine qc: called CC=2 SCAR=6.9
Pod undocumented
54 219 my $self = shift;
55 219 my $str = shift;
56  
57 219 ▶ if( defined $str )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 195 24 219
58 {
59 195 $str =~ s/\'/\'\'/g;
60 195 $str = "'$str'";
61 }
62 else
63 {
64 24 $str = 'NULL';
65 }
66  
67 219 return $str;
68 }
69  
70 sub qi
71 423 ▶{
Subroutine qi: called CC=1 SCAR=0.0
Pod undocumented
72 423 my $self = shift;
73 423 my $str = shift;
74  
75 423 $str =~ s/\[/\\\[/g;
76 423 $str =~ s/\]/\\\]/g;
77 423 $str = "[$str]";
78  
79 423 return $str;
80 }
81  
82 212 ▶sub qt { $_[0]->qi( $_[1] ); }
Subroutine qt: called CC=1 SCAR=0.0
Pod undocumented
83 195 ▶sub qf { $_[0]->qi( $_[1] ); }
Subroutine qf: called CC=1 SCAR=0.0
Pod undocumented
84  
85 ##
86 ## OBJECT METHODS
87 ##
88  
89  
90 sub begin_transaction
91 10 ▶{
Subroutine begin_transaction: called CC=1 SCAR=0.0
Pod undocumented
92 10 my $self = shift;
93 10 my %arg = @_;
94  
95 10 $self->{ta} = 1;
96 10 $self->do( query=>"BEGIN TRANSACTION", error=>$arg{error} );
97 }
98  
99 sub commit_transaction
100 9 ▶{
Subroutine commit_transaction: called CC=1 SCAR=0.0
Pod undocumented
101 9 my $self = shift;
102 9 my %arg = @_;
103  
104 9 delete $self->{ta};
105 9 $self->do( query=>"COMMIT TRANSACTION", error=>$arg{error} );
106 }
107  
108 sub rollback_transaction
109 1 ▶{
Subroutine rollback_transaction: called CC=2 SCAR=7.0
Pod undocumented
110 1 my $self = shift;
111 1 my %arg = @_;
112  
113 1 delete $self->{ta};
114 1 ▶ unless( $self->{lost_connection} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lost_connection"}) 1 0 1
115 {
116 1 $self->do( query=>"ROLLBACK TRANSACTION", error=>$arg{error} );
117 }
118 }
119  
120 ## use: $id = $db->insertid()
121 ##
122 sub insertid
123 21 ▶{
Subroutine insertid: called CC=2 SCAR=7.2
Pod undocumented
124 21 my $self = shift;
125 21 ▶ $self->_db_handler ? $self->_db_handler->func( 'last_insert_rowid' ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_db_handler ? : 21 0 21
126 }
127  
128 sub table_struct
129 10 ▶{
Subroutine table_struct: called CC=16 SCAR=28.1
Pod undocumented
130 10 my $self = shift;
131 10 my %arg = @_;
132 10 my $error = ORM::Error->new;
133 10 my %field;
134 my %defaults;
135 10 my $res;
136  
137 ## Fetch table structure
138 $res = $self->select
139 (
140 10 query => "SELECT sql FROM sqlite_master WHERE type='table' and name=".$self->qc($arg{table}),
141 error => $error,
142 );
143 10 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 10 0 10
144 {
145 10 my $data;
146  
147 10 $data = $res->next_row;
148 10 ▶ $data = $data ? $data->{sql} : '';
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 9 1 10
149 10 $data =~ /^CREATE TABLE [^\(]+\((.+)\)/ism;
150 10 ▶ $data = $1 || '';
Condition100%2/2
$1 || ''
l!l
91
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
151 10 $data =~ s/[\r\n]/ /g;
152  
153 10 ▶ my @rows = split /,/, $data if( $data );
Branch: 1 branch
BranchTrueFalseTotal
if $data 9 1 10
154  
155 10 for $row ( @rows )
156 {
157 43 ▶ if( $row =~ /^\s*([^\s]+)\s+([^\s]+)(.*?\s+default (NULL|\'[^\']*\'))?/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /^\s*([^\s]+)\s+([^\s]+)(.*?\s+default (NULL|\'[^\']*\'))?/i) { } 43 0 43
158 {
159 43 ▶ last if $1 =~ /PRIMARY|UNIQUE|CHECK/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 =~ /PRIMARY|UNIQUE|CHECK/ 0 43 43
160 43 my $name = $1;
161 43 my $type = $2;
162 43 my $def = $4;
163  
164 43 ▶ $name = $1 if( $name =~ /^\[(.+)\]$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\[(.+)\]$/ 43 0 43
165 43 ▶ $name = $2 if( $name =~ /^(['"])(.+)\1$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(['"])(.+)\1$/ 0 43 43
166  
167 43 ▶ if( ! defined $def )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $def) { } 9 34 43
elsif ($def eq 'NULL') { } 4 30 34
168 {
169 }
170 elsif( $def eq 'NULL' )
171 {
172 4 $def = undef;
173 }
174 else
175 {
176 30 $def = substr $def, 1, (length $def) - 2;
177 }
178  
179 43 $defaults{ $name } = $def;
180 43 $field{ $name } = $arg{class}->_db_type_to_class( $name, $type );
181 }
182 else
183 {
184 0 $error->add_fatal( "Can't detect columns for table '$arg{table}'" );
185 0 last;
186 }
187 }
188 }
189  
190 ## Fetch class references
191 10 ▶ if( scalar( %field ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %field) 9 1 10
192 {
193 $res = $self->select
194 (
195 error => $error,
196 9 query => 'SELECT * FROM '.$self->qt('_ORM_refs').' WHERE class='.$self->qc( $arg{class} ),
197 );
198 9 ▶ unless( $error->fatal )
Branch: 1 branch
BranchTrueFalseTotal
unless ($error->fatal) 9 0 9
199 {
200 9 ▶ while( $data = $res->next_row )
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $res->next_row) 2 9 11
201 {
202 2 ▶ if( exists $field{$data->{prop}} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field{$data->{"prop"}}) 2 0 2
203 {
204 2 $field{$data->{prop}} = $data->{ref_class};
205 }
206 }
207 }
208 }
209  
210 10 $error->upto( $arg{error} );
211 10 return \%field, \%defaults;
212 }
213  
214 2 ▶sub _ta_select { ''; }
Subroutine _ta_select: called CC=1 SCAR=0.0
215  
216 sub _lost_connection
217 0 ▶{
Subroutine _lost_connection: not called CC=3 SCAR=24.8
218 0 my $self = shift;
219 0 my $err = shift;
220  
221 0 ▶ defined $err && ( $err == 2006 || $err == 2013 );
Branch: 1 branch
BranchTrueFalseTotal
defined $err and $err == 2006 || $err == 2013 0 0 0
Condition0%0/6
defined $err && ($err == 2006 || $err == 2013)
!ll&&!rl&&r
000
$err == 2006 or $err == 2013
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $err && ($err == 2006 || $err == 2013)
defined $err $err == 2006 $err == 2013
Truth table0%0/4
A && B or C
A= defined $errB= $err == 2006C= $err == 2013
ABCresult
0XX0
1000
11X1
1011
222 }
223  
224 ##
225 ## SQL FUNCTIONS
226 ##
227  
« ORM/Metaprop.pm ↑ Summary ORM/Order.pm »