DB/SQLite.pm

filter: Statementstmt 25.6% 59 / 230 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 35.9% 14 / 39 Podpod 31.8% 7 / 22 totaltotal 17.5% 80 / 456 CC 57 SCAR 48.8
CC
57
Cov
16%
_info_from_sql
507.9
list_tables
42.0
refine_dbi_column_info
20.4
CRAP 132.0
« DB/Pg.pm ↑ Summary DB/MariaDB.pm »
1 package Rose::DB::SQLite;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 16 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use Rose::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use SQL::ReservedWords::SQLite();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 our $VERSION = '0.765';
11  
12 #our $Debug = 0;
13  
14 use Rose::Class::MakeMethods::Generic
15 16 ▶(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 inheritable_scalar => 'coerce_autoincrement_to_serial',
17 16 );
18  
19 16 __PACKAGE__->coerce_autoincrement_to_serial(1);
20  
21 #
22 # Object methods
23 #
24  
25 sub build_dsn
26 0 ▶{
Subroutine build_dsn: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my($self_or_class, %args) = @_;
28  
29 0 my %info;
30  
31 0 ▶ $info{'dbname'} = $args{'db'} || $args{'database'};
Condition0%0/3
$args{'db'} || $args{'database'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'db'} || $args{'database'}
$args{'db'} $args{'database'}
Truth table0%0/3
A || B
A= $args{'db'}B= $args{'database'}
ABresult
1X1
011
000
32  
33 return
34 "dbi:SQLite:" .
35 0 join(';', map { "$_=$info{$_}" } grep { defined $info{$_} } qw(dbname));
36 }
37  
38 4 ▶sub dbi_driver { 'SQLite' }
Subroutine dbi_driver: called CC=1 SCAR=0.0
Pod undocumented
39  
40 0 ▶sub sqlite_unicode { shift->dbh_attribute_boolean('sqlite_unicode', @_) }
Subroutine sqlite_unicode: not called CC=1 SCAR=6.9
Pod documented
41  
42 sub init_dbh
43 0 ▶{
Subroutine init_dbh: not called CC=3 SCAR=20.0
Pod undocumented
44 0 my($self) = shift;
45  
46 0 my $database = $self->database;
47  
48 0 ▶ unless($self->auto_create || -e $database)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->auto_create or -e $database) 0 0 0
Condition0%0/3
$self->auto_create or -e $database
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->auto_create or -e $database
$self->auto_create -e $database
Truth table0%0/3
A or B
A= $self->auto_createB= -e $database
ABresult
1X1
011
000
49 {
50 0 Carp::croak "Refusing to create non-existent SQLite database ",
51 "file: '$database'";
52 }
53  
54 0 my $method = ref($self)->parent_class . '::init_dbh';
55  
56 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 0 return $self->$method(@_);
58 }
59  
60 0 ▶sub last_insertid_from_sth { shift->dbh->func('last_insert_rowid') }
Subroutine last_insertid_from_sth: not called CC=1 SCAR=6.9
Pod undocumented
61  
62 0 ▶sub supports_multi_column_count_distinct { 0 }
Subroutine supports_multi_column_count_distinct: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 sub validate_date_keyword
65 0 ▶{
Subroutine validate_date_keyword: not called CC=4 SCAR=25.0
Pod documented
66 16 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 0 ▶ !ref $_[1] && (lc $_[1] eq 'current_timestamp' ||
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[1] and lc $_[1] eq "current_timestamp" || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/ 0 0 0
Condition0%0/9
!ref($_[1]) && (lc $_[1] eq 'current_timestamp' || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/)
!ll&&!rl&&r
000
lc $_[1] eq "current_timestamp" or $_[0]->keyword_function_calls and $_[1] =~ /^\w+\(.*\)$/
l!l&&r!l&&!r
000
$_[0]->keyword_function_calls and $_[1] =~ /^\w+\(.*\)$/
!ll&&!rl&&r
000
MC/DC0%0/4
!ref($_[1]) && (lc $_[1] eq 'current_timestamp' || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/)
!ref($_[1]) lc $_[1] eq "current_timestamp" $_[0]->keyword_function_calls $_[1] =~ /^\w+\(.*\)$/
Truth table0%0/5
not(A) && B or C and D
A= !ref($_[1])B= lc $_[1] eq "current_timestamp"C= $_[0]->keyword_function_callsD= $_[1] =~ /^\w+\(.*\)$/
ABCDresult
0XXX0
100X0
10100
11XX1
10111
68 ($_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/));
69 }
70  
71 sub validate_datetime_keyword
72 0 ▶{
Subroutine validate_datetime_keyword: not called CC=4 SCAR=25.0
Pod documented
73 16 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 0 ▶ !ref $_[1] && (lc $_[1] eq 'current_timestamp' ||
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[1] and lc $_[1] eq "current_timestamp" || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/ 0 0 0
Condition0%0/9
!ref($_[1]) && (lc $_[1] eq 'current_timestamp' || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/)
!ll&&!rl&&r
000
lc $_[1] eq "current_timestamp" or $_[0]->keyword_function_calls and $_[1] =~ /^\w+\(.*\)$/
l!l&&r!l&&!r
000
$_[0]->keyword_function_calls and $_[1] =~ /^\w+\(.*\)$/
!ll&&!rl&&r
000
MC/DC0%0/4
!ref($_[1]) && (lc $_[1] eq 'current_timestamp' || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/)
!ref($_[1]) lc $_[1] eq "current_timestamp" $_[0]->keyword_function_calls $_[1] =~ /^\w+\(.*\)$/
Truth table0%0/5
not(A) && B or C and D
A= !ref($_[1])B= lc $_[1] eq "current_timestamp"C= $_[0]->keyword_function_callsD= $_[1] =~ /^\w+\(.*\)$/
ABCDresult
0XXX0
100X0
10100
11XX1
10111
75 ($_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/));
76 }
77  
78 sub validate_timestamp_keyword
79 0 ▶{
Subroutine validate_timestamp_keyword: not called CC=4 SCAR=25.0
Pod documented
80 16 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 0 ▶ !ref $_[1] && (lc $_[1] eq 'current_timestamp' ||
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[1] and lc $_[1] eq "current_timestamp" || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/ 0 0 0
Condition0%0/9
!ref($_[1]) && (lc $_[1] eq 'current_timestamp' || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/)
!ll&&!rl&&r
000
lc $_[1] eq "current_timestamp" or $_[0]->keyword_function_calls and $_[1] =~ /^\w+\(.*\)$/
l!l&&r!l&&!r
000
$_[0]->keyword_function_calls and $_[1] =~ /^\w+\(.*\)$/
!ll&&!rl&&r
000
MC/DC0%0/4
!ref($_[1]) && (lc $_[1] eq 'current_timestamp' || $_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/)
!ref($_[1]) lc $_[1] eq "current_timestamp" $_[0]->keyword_function_calls $_[1] =~ /^\w+\(.*\)$/
Truth table0%0/5
not(A) && B or C and D
A= !ref($_[1])B= lc $_[1] eq "current_timestamp"C= $_[0]->keyword_function_callsD= $_[1] =~ /^\w+\(.*\)$/
ABCDresult
0XXX0
100X0
10100
11XX1
10111
82 ($_[0]->keyword_function_calls && $_[1] =~ /^\w+\(.*\)$/));
83 }
84  
85 0 ▶sub should_inline_date_keyword { 1 }
Subroutine should_inline_date_keyword: not called CC=1 SCAR=6.9
Pod undocumented
86 0 ▶sub should_inline_datetime_keyword { 1 }
Subroutine should_inline_datetime_keyword: not called CC=1 SCAR=6.9
Pod undocumented
87 0 ▶sub should_inline_time_keyword { 1 }
Subroutine should_inline_time_keyword: not called CC=1 SCAR=6.9
Pod undocumented
88 0 ▶sub should_inline_timestamp_keyword { 1 }
Subroutine should_inline_timestamp_keyword: not called CC=1 SCAR=6.9
Pod undocumented
89  
90 sub parse_date
91 0 ▶{
Subroutine parse_date: not called CC=4 SCAR=30.0
Pod documented
92 0 my($self, $value) = @_;
93  
94 0 ▶ if(UNIVERSAL::isa($value, 'DateTime') ||
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "DateTime") or $self->validate_date_keyword($value)) 0 0 0
Condition0%0/3
UNIVERSAL::isa($value, "DateTime") or $self->validate_date_keyword($value)
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($value, "DateTime") or $self->validate_date_keyword($value)
UNIVERSAL::isa($value, "DateTime") $self->validate_date_keyword($value)
Truth table0%0/3
A or B
A= UNIVERSAL::isa($value, "DateTime")B= $self->validate_date_keyword($value)
ABresult
1X1
011
000
95 $self->validate_date_keyword($value))
96 {
97 0 return $value;
98 }
99  
100 0 my($dt, $error);
101  
102 TRY:
103 {
104 0 local $@;
105 0 eval { $dt = Rose::DateTime::Util::parse_date($value) };
106 0 $error = $@;
107 }
108  
109 0 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
110 {
111 0 $self->error("Could not parse date '$value' - $error");
112 0 return undef;
113 }
114  
115 0 return $dt;
116 }
117  
118 sub parse_datetime
119 0 ▶{
Subroutine parse_datetime: not called CC=4 SCAR=30.0
Pod documented
120 0 my($self, $value) = @_;
121  
122 0 ▶ if(UNIVERSAL::isa($value, 'DateTime') ||
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "DateTime") or $self->validate_datetime_keyword($value)) 0 0 0
Condition0%0/3
UNIVERSAL::isa($value, "DateTime") or $self->validate_datetime_keyword($value)
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($value, "DateTime") or $self->validate_datetime_keyword($value)
UNIVERSAL::isa($value, "DateTime") $self->validate_datetime_keyword($value)
Truth table0%0/3
A or B
A= UNIVERSAL::isa($value, "DateTime")B= $self->validate_datetime_keyword($value)
ABresult
1X1
011
000
123 $self->validate_datetime_keyword($value))
124 {
125 0 return $value;
126 }
127  
128 0 my($dt, $error);
129  
130 TRY:
131 {
132 0 local $@;
133 0 eval { $dt = Rose::DateTime::Util::parse_date($value) };
134 0 $error = $@;
135 }
136  
137 0 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
138 {
139 0 $self->error("Could not parse datetime '$value' - $error");
140 0 return undef;
141 }
142  
143 0 return $dt;
144 }
145  
146 sub parse_timestamp
147 0 ▶{
Subroutine parse_timestamp: not called CC=4 SCAR=30.0
Pod documented
148 0 my($self, $value) = @_;
149  
150 0 ▶ if(UNIVERSAL::isa($value, 'DateTime') ||
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "DateTime") or $self->validate_timestamp_keyword($value)) 0 0 0
Condition0%0/3
UNIVERSAL::isa($value, "DateTime") or $self->validate_timestamp_keyword($value)
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($value, "DateTime") or $self->validate_timestamp_keyword($value)
UNIVERSAL::isa($value, "DateTime") $self->validate_timestamp_keyword($value)
Truth table0%0/3
A or B
A= UNIVERSAL::isa($value, "DateTime")B= $self->validate_timestamp_keyword($value)
ABresult
1X1
011
000
151 $self->validate_timestamp_keyword($value))
152 {
153 0 return $value;
154 }
155  
156 0 my($dt, $error);
157  
158 TRY:
159 {
160 0 local $@;
161 0 eval { $dt = Rose::DateTime::Util::parse_date($value) };
162 0 $error = $@;
163 }
164  
165 0 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
166 {
167 0 $self->error("Could not parse timestamp '$value' - $error");
168 0 return undef;
169 }
170  
171 0 return $dt;
172 }
173  
174 sub format_bitfield
175 0 ▶{
Subroutine format_bitfield: not called CC=2 SCAR=17.9
Pod undocumented
176 0 my($self, $vec, $size) = @_;
177 0 ▶ $vec = Bit::Vector->new_Bin($size, $vec->to_Bin) if($size);
Branch: 1 branch
BranchTrueFalseTotal
if $size 0 0 0
178 0 return q(b') . $vec->to_Bin . q(');
179 }
180  
181 sub refine_dbi_column_info
182 0 ▶{
Subroutine refine_dbi_column_info: not called CC=5 SCAR=30.1
Pod undocumented
183 0 my($self, $col_info) = @_;
184  
185 0 my $method = ref($self)->parent_class . '::refine_dbi_column_info';
186  
187 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
188 0 $self->$method($col_info);
189  
190 0 ▶ if($col_info->{'TYPE_NAME'} eq 'bit')
Branch: 2 branches
BranchTrueFalseTotal
if ($col_info->{'TYPE_NAME'} eq 'bit') { } 0 0 0
elsif ($col_info->{'TYPE_NAME'} eq 'datetime' and defined $col_info->{'COLUMN_DEF'}) { } 0 0 0
191 {
192 0 $col_info->{'TYPE_NAME'} = 'bits';
193 }
194  
195 ▶ elsif($col_info->{'TYPE_NAME'} eq 'datetime' && defined $col_info->{'COLUMN_DEF'})
Condition0%0/3
$col_info->{"TYPE_NAME"} eq "datetime" and defined $col_info->{"COLUMN_DEF"}
!ll&&!rl&&r
000
MC/DC0%0/2
$col_info->{"TYPE_NAME"} eq "datetime" and defined $col_info->{"COLUMN_DEF"}
$col_info->{"TYPE_NAME"} eq "datetime" defined $col_info->{"COLUMN_DEF"}
Truth table0%0/3
A and B
A= $col_info->{"TYPE_NAME"} eq "datetime"B= defined $col_info->{"COLUMN_DEF"}
ABresult
0X0
100
111
196 {
197 0 ▶ if(lc $col_info->{'COLUMN_DEF'} eq 'current_timestamp')
Branch: 1 branch
BranchTrueFalseTotal
if (lc $col_info->{"COLUMN_DEF"} eq "current_timestamp") 0 0 0
198 {
199 # Translate "current time" value into something that our date parser
200 # will understand.
201 0 $col_info->{'COLUMN_DEF'} = 'now';
202  
203 # ...or let the database handle this?
204 #$col_info->{'COLUMN_DEF'} = undef;
205 }
206 }
207  
208 0 return;
209 }
210  
211 0 ▶sub likes_implicit_joins { 1 }
Subroutine likes_implicit_joins: not called CC=1 SCAR=6.9
Pod undocumented
212  
213 16 *is_reserved_word = \&SQL::ReservedWords::SQLite::is_reserved;
214  
215 sub quote_column_name
216 0 ▶{
Subroutine quote_column_name: not called CC=1 SCAR=6.9
Pod undocumented
217 0 my $name = $_[1];
218 0 $name =~ s/"/""/g;
219 0 return qq("$name");
220 }
221  
222 sub quote_table_name
223 0 ▶{
Subroutine quote_table_name: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my $name = $_[1];
225 0 $name =~ s/"/""/g;
226 0 return qq("$name");
227 }
228  
229 #
230 # Introspection
231 #
232  
233 sub list_tables
234 0 ▶{
Subroutine list_tables: not called CC=6 SCAR=37.4
Pod undocumented
235 0 my($self, %args) = @_;
236  
237 0 ▶ my $types = $args{'include_views'} ? q('table', 'view') : q('table');
Branch: 1 branch
BranchTrueFalseTotal
$args{'include_views'} ? : 0 0 0
238  
239 0 my(@tables, $error);
240  
241 TRY:
242 {
243 0 local $@;
244  
245 eval
246 0 {
247 0 ▶ my $dbh = $self->dbh or die $self->error;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->dbh or die $self->error 0 0 0
248  
249 0 local $dbh->{'RaiseError'} = 1;
250  
251 0 my $sth = $dbh->prepare("SELECT name FROM sqlite_master WHERE type IN($types)");
252 0 $sth->execute;
253  
254 0 my $name;
255 0 $sth->bind_columns(\$name);
256  
257 0 ▶ while($sth->fetch)
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 0 0 0
258 {
259 0 push(@tables, $name);
260 }
261 };
262  
263 0 $error = $@;
264 }
265  
266 0 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
267 {
268 0 Carp::croak "Could not list tables from ", $self->dsn, " - $error";
269 }
270  
271 0 ▶ return wantarray ? @tables : \@tables;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
272 }
273  
274 sub _get_primary_key_column_names
275 0 ▶{
Subroutine _get_primary_key_column_names: not called CC=2 SCAR=17.9
276 0 my($self, $catalog, $schema, $table) = @_;
277 0 ▶ my $pk_columns = ($self->_table_info($table))[1] || [];
Condition0%0/2
($self->_table_info($table))[1] || []
l!l
00
MC/DC0%0/1
($self->_table_info($table))[1] || []
($self->_table_info($table))[1]
Truth table0%0/2
A
A= ($self->_table_info($table))[1]
Aresult
11
00
278 0 return $pk_columns;
279 }
280  
281 sub _table_info
282 0 ▶{
Subroutine _table_info: not called CC=2 SCAR=17.9
283 0 my($self, $table) = @_;
284  
285 0 ▶ my $dbh = $self->dbh or Carp::croak $self->error;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->dbh or Carp::croak($self->error) 0 0 0
286  
287 0 my $table_unquoted = $self->unquote_table_name($table);
288  
289 0 my $sth = $dbh->prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?");
290 0 my $sql;
291  
292 0 $sth->execute($table_unquoted);
293 0 $sth->bind_columns(\$sql);
294 0 $sth->fetch;
295 0 $sth->finish;
296  
297 0 return $self->_info_from_sql($sql);
298 }
299  
300 ## Yay! A Giant Wad o' Regexes "parser"! Yeah, this is lame, but I really
301 ## don't want to load an actual parser, or even a regex lib or helper...
302  
303 16 our $Paren_Depth = 15;
304 16 our $Nested_Parens = '\(' . '([^()]|\(' x $Paren_Depth . '[^()]*' . '\))*' x $Paren_Depth . '\)';
305  
306 # This doesn't seem to work...
307 #$Nested_Parens = qr{\( (?: (?> [^()]+ ) | (??{ $Nested_Parens }) )* \)}x;
308  
309 16 our $Quoted =
310 qr{(?: ' (?: [^'] | '' )+ '
311 | " (?: [^"] | "" )+ "
312 | ` (?: [^`] | `` )+ `)}six;
313  
314 16 our $Name = qr{(?: $Quoted | \w+ )}six;
315  
316 16 our $Type =
317 qr{\w+ (?: \s* \( \s* \d+ \s* (?: , \s* \d+ \s*)? \) )?}six;
318  
319 16 our $Conflict_Algorithm =
320 qr{(?: ROLLBACK | ABORT | FAIL | IGNORE | REPLACE )}six;
321  
322 16 our $Conflict_Clause =
323 qr{(?: ON \s+ CONFLICT \s+ $Conflict_Algorithm )}six;
324  
325 16 our $Sort_Order =
326 qr{(?: COLLATE \s+ \S+ \s+)? (?:ASC | DESC)}six;
327  
328 16 our $Column_Constraint =
329 qr{(?: NOT \s+ NULL (?: \s+ $Conflict_Clause)?
330 | PRIMARY \s+ KEY (?: \s+ $Sort_Order)? (?: \s+ $Conflict_Clause)? (?: \s+ AUTOINCREMENT)?
331 | UNIQUE (?: \s+ $Conflict_Clause)?
332 | CHECK \s* $Nested_Parens (?: \s+ $Conflict_Clause)?
333 | REFERENCES \s+ $Name \s* \( \s* $Name \s* \)
334 | DEFAULT \s+ (?: $Name | \w+ \s* $Nested_Parens | [^,)]+ )
335 | COLLATE \s+ \S+)}six;
336  
337 16 our $Table_Constraint =
338 qr{(?: (?: PRIMARY \s+ KEY | UNIQUE | CHECK ) \s* $Nested_Parens
339 | FOREIGN \s+ KEY \s+ (?: $Name \s+ )? $Nested_Parens \s+ REFERENCES \s+ $Name \s+ $Nested_Parens )}six;
340  
341 16 our $Column_Def =
342 qr{($Name) (?:\s+ ($Type))? ( (?: \s+ (?:CONSTRAINT \s+ $Name \s+)? $Column_Constraint )* )}six;
343  
344 # SQLite allows C comments to be unterminated if they're at the end of the
345 # input stream. Crazy, but true: http://www.sqlite.org/lang_comment.html
346 16 our $C_Comment_Cont = qr{/\*.*$}six;
347 16 our $C_Comment = qr{/\*[^*]*\*+(?:[^/*][^*]*\*+)*/}six;
348 16 our $SQL_Comment = qr{--[^\r\n]*(\r?\n)}six;
349 16 our $Comment = qr{($Quoted)|($C_Comment|$SQL_Comment|$C_Comment_Cont)}six;
350  
351 # These constants are from the DBI documentation. Is there somewhere
352 # I can load these from?
353 16 ▶use constant SQL_NO_NULLS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
354 16 ▶use constant SQL_NULLABLE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
355  
356 sub _info_from_sql
357 0 ▶{
Subroutine _info_from_sql: not called CC=23 SCAR=62.3
358 0 my($self, $sql) = @_;
359  
360 0 my(@col_info, @pk_columns, @uk_info);
361  
362 0 my($new_sql, $pos);
363  
364 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
365  
366 # Remove comments
367 0 ▶ while($sql =~ /\G((.*?)$Comment)/sgix)
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\G((.*?)$Rose::DB::SQLite::Comment)/gisx) 0 0 0
368 {
369 0 $pos = pos($sql);
370  
371 0 ▶ if(defined $4) # caught comment
Branch: 1 branch
BranchTrueFalseTotal
if (defined $4) { } 0 0 0
372 {
373 16 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
374 0 $new_sql .= "$2$3";
375 }
376 else
377 {
378 0 $new_sql .= $1;
379 }
380 }
381  
382 0 ▶ $sql = $new_sql . substr($sql, $pos) if(defined $new_sql);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_sql 0 0 0
383  
384 # Remove the start and end
385 0 $sql =~ s/^\s* CREATE \s+ (?:TEMP(?:ORARY)? \s+)? TABLE \s+ $Name \s*\(\s*//sgix;
386 0 $sql =~ s/\s*\)\s*$//six;
387  
388 # Remove leading space from lines
389 0 $sql =~ s/^\s+//mg;
390  
391 0 my $i = 1;
392  
393 # Column definitions
394 0 ▶ while($sql =~ s/^$Column_Def (?:\s*,\s*|\s*$)//six)
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ s/^$Rose::DB::SQLite::Column_Def (?:\s*,\s*|\s*$)//six) 0 0 0
395 {
396 0 my $col_name = _unquote_name($1);
397 0 ▶ my $col_type = $2 || 'scalar';
Condition0%0/2
$2 || 'scalar'
l!l
00
MC/DC0%0/1
$2 || 'scalar'
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
398 0 my $constraints = $3;
399  
400 0 ▶ unless(defined $col_name)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col_name) 0 0 0
401 {
402 0 Carp::croak "Could not extract column name from SQL: $sql";
403 }
404  
405 0 my %col_info =
406 (
407 COLUMN_NAME => $col_name,
408 TYPE_NAME => $col_type,
409 ORDINAL_POSITION => $i++,
410 );
411  
412 0 ▶ if($col_type =~ /^(\w+) \s* \( \s* (\d+) \s* \)$/x)
Branch: 2 branches
BranchTrueFalseTotal
if ($col_type =~ /^(\w+) \s* \( \s* (\d+) \s* \)$/x) { } 0 0 0
elsif ($col_type =~ /^\s* (\w+) \s* \( \s* (\d+) \s* , \s* (\d+) \s* \) \s*$/x) { } 0 0 0
413 {
414 0 $col_info{'TYPE_NAME'} = $1;
415 0 $col_info{'COLUMN_SIZE'} = $2;
416 0 $col_info{'CHAR_OCTET_LENGTH'} = $2;
417 }
418 elsif($col_type =~ /^\s* (\w+) \s* \( \s* (\d+) \s* , \s* (\d+) \s* \) \s*$/x)
419 {
420 0 $col_info{'TYPE_NAME'} = $1;
421 0 $col_info{'DECIMAL_DIGITS'} = $2;
422 0 $col_info{'COLUMN_SIZE'} = $3;
423 }
424  
425 0 ▶ while($constraints =~ s/^\s* (?:CONSTRAINT \s+ $Name \s+)? ($Column_Constraint) \s*//six)
Branch: 1 branch
BranchTrueFalseTotal
if ($constraints =~ s/^\s* (?:CONSTRAINT \s+ $Rose::DB::SQLite::Name \s+)? ($Rose::DB::SQLite::Column_Constraint) \s*//six) 0 0 0
426 {
427 0 local $_ = $1;
428  
429 0 ▶ if(/^DEFAULT \s+ ( $Name | \w+ \s* $Nested_Parens | [^,)]+ )/six)
Branch: 4 branches
BranchTrueFalseTotal
if (/^DEFAULT \s+ ( $Rose::DB::SQLite::Name | \w+ \s* $Rose::DB::SQLite::Nested_Parens | [^,)]+ )/six) { } 0 0 0
elsif (/^PRIMARY (?: \s+ KEY )? \b (?: .*? (AUTOINCREMENT) )?/six) { } 0 0 0
elsif (/^\s* UNIQUE (?: \s+ KEY)? \b/six) { } 0 0 0
elsif (/^NOT \s+ NULL \b/six) { } 0 0 0
430 {
431 0 $col_info{'COLUMN_DEF'} = _unquote_name($1);
432 }
433 elsif(/^PRIMARY (?: \s+ KEY )? \b (?: .*? (AUTOINCREMENT) )?/six)
434 {
435 0 push(@pk_columns, $col_name);
436  
437 0 ▶ if($1 && $class->coerce_autoincrement_to_serial)
Branch: 1 branch
BranchTrueFalseTotal
if ($1 and $class->coerce_autoincrement_to_serial) 0 0 0
Condition0%0/3
$1 and $class->coerce_autoincrement_to_serial
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $class->coerce_autoincrement_to_serial
$1 $class->coerce_autoincrement_to_serial
Truth table0%0/3
A and B
A= $1B= $class->coerce_autoincrement_to_serial
ABresult
0X0
100
111
438 {
439 0 $col_info{'TYPE_NAME'} = 'serial';
440 }
441 }
442 elsif(/^\s* UNIQUE (?: \s+ KEY)? \b/six)
443 {
444 0 push(@uk_info, [ $col_name ]);
445 }
446 elsif(/^NOT \s+ NULL \b/six)
447 {
448 0 $col_info{'NULLABLE'} = SQL_NO_NULLS;
449 }
450 }
451  
452 0 ▶ $col_info{'NULLABLE'} = SQL_NULLABLE unless(defined $col_info{'NULLABLE'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_info{"NULLABLE"} 0 0 0
453  
454 0 push(@col_info, \%col_info);
455 }
456  
457 0 ▶ while($sql =~ s/^($Table_Constraint) (?:\s*,\s*|\s*$)//six)
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ s/^($Rose::DB::SQLite::Table_Constraint) (?:\s*,\s*|\s*$)//six) 0 0 0
458 {
459 0 my $constraint = $1;
460  
461 0 ▶ if($constraint =~ /^\s* PRIMARY \s+ KEY \s* ($Nested_Parens)/six)
Branch: 2 branches
BranchTrueFalseTotal
if ($constraint =~ /^\s* PRIMARY \s+ KEY \s* ($Rose::DB::SQLite::Nested_Parens)/six) { } 0 0 0
elsif ($constraint =~ /^\s* UNIQUE \s* ($Rose::DB::SQLite::Nested_Parens)/six) { } 0 0 0
462 {
463 0 @pk_columns = ();
464  
465 0 my $columns = $1;
466 0 $columns =~ s/^\(\s*//;
467 0 $columns =~ s/\s*\)\s*$//;
468  
469 0 ▶ while($columns =~ s/^\s* ($Name) (?:\s*,\s*|\s*$)//six)
Branch: 1 branch
BranchTrueFalseTotal
if ($columns =~ s/^\s* ($Rose::DB::SQLite::Name) (?:\s*,\s*|\s*$)//six) 0 0 0
470 {
471 0 push(@pk_columns, _unquote_name($1));
472 }
473 }
474 elsif($constraint =~ /^\s* UNIQUE \s* ($Nested_Parens)/six)
475 {
476 0 my $columns = $1;
477 0 $columns =~ s/^\(\s*//;
478 0 $columns =~ s/\s*\)\s*$//;
479  
480 0 my @uk_columns;
481  
482 0 ▶ while($columns =~ s/^\s* ($Name) (?:\s*,\s*|\s*$)//six)
Branch: 1 branch
BranchTrueFalseTotal
if ($columns =~ s/^\s* ($Rose::DB::SQLite::Name) (?:\s*,\s*|\s*$)//six) 0 0 0
483 {
484 0 push(@uk_columns, _unquote_name($1));
485 }
486  
487 0 push(@uk_info, \@uk_columns);
488 }
489 }
490  
491 0 return(\@col_info, \@pk_columns, \@uk_info);
492 }
493  
494 sub _unquote_name
495 0 ▶{
Subroutine _unquote_name: not called CC=2 SCAR=17.9
496 0 my $name = shift;
497  
498 0 ▶ if($name =~ s/^(['`"]) ( (?: [^\1]+ | \1\1 )+ ) \1 $/$2/six)
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^(['`"]) ( (?: [^\1]+ | \1\1 )+ ) \1 $/$2/six) 0 0 0
499 {
500 0 my $q = $1;
501 0 $name =~ s/$q$q/$q/g;
502 }
503  
504 0 return $name;
505 }
506  
507 16 1;
508  
509 __END__
« DB/Pg.pm ↑ Summary DB/MariaDB.pm »