| 1 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # This module provides an interface to the SQLite database used to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # cache data for File::Properties modules. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # Copyright © 2010,2011 Brendt Wohlberg <wohl@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # See distribution LICENSE file for license details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # Most recent modification: 5 November 2011 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | package File::Properties::Database; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | our $VERSION = 0.01; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use File::Properties::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | require 5.005; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use DBI qw(:sql_types); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use DBD::SQLite; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use Error qw(:try); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # Constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | my $clss = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | bless $self, $clss; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | $self->_init(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Initialiser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=8 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | my $dbfp = shift; # Database file path | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | my $opts = shift; # Options hash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | $opts = {} if undef $opts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Throw exception if DB file $dpfp does not exist and 'NoCreate' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # option is true. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | throw File::Properties::Error("DBI file $dbfp not found") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if $opts->{'NoCreate'} and (not defined $dbfp or not -f $dbfp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$opts->{"NoCreate"} and not defined $dbfp && -f $dbfp
defined $dbfp && -f $dbfp
MC/DC0%0/3
$opts->{"NoCreate"} and not defined $dbfp && -f $dbfp
$opts->{"NoCreate"}
defined $dbfp
-f $dbfp
Truth table25%1/4
A and not(B && C)
A= $opts->{"NoCreate"}B= defined $dbfpC= -f $dbfp
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # If DB file not specified, construct it in memory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | $dbfp = ':memory:' if (not defined $dbfp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ## Throw exception if error encountered opening DBI connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | my $dbh = _dbopen($dbfp, $opts->{'ReadOnly'}?1:0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | throw File::Properties::Error("Error opening DBI interface to file $dbfp") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | if not defined($dbh); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # Record constructor options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | $self->opts($opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # Record SQLite DBI interface | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | $self->dbi($dbh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # Table column specifications | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | $self->definedcolumns(undef,{}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # Destructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | _dbclose($self->dbi); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Get (or set) options specified at initialisation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | sub opts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opts: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | $self->{'opts'} = shift if (@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | return $self->{'opts'} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # Get (or set) dbi handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 86 | ▶ | sub dbi { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbi: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 86 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 86 | ▶ | $self->{'dbih'} = shift if (@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 86 | return $self->{'dbih'} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # Get (or set) table column specification hash or hash entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 30 | ▶ | sub definedcolumns { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
definedcolumns: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 30 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 30 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | ## If table name is defined, additional parameter specifies new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | ## value for that hash entry, otherwise the additional parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ## specifies new value for entire hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 30 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | if (defined $tbnm) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | $self->{'tblc'}->{$tbnm} = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | $self->{'tblc'} = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ## If table name is specified and the corresponding entry is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ## defined, obtain the column names from the SQL database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 30 | ▶ | if (defined $tbnm and not defined $self->{'tblc'}->{$tbnm}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $tbnm and not defined $self->{"tblc"}{$tbnm}
MC/DC0%0/2
defined $tbnm and not defined $self->{"tblc"}{$tbnm}
defined $tbnm
not defined $self->{"tblc"}{$tbnm}
Truth table66%2/3
A and not(B)
A= defined $tbnmB= not defined $self->{"tblc"}{$tbnm}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $self->{'tblc'}->{$tbnm} = $self->columns($tbnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # Return hash reference, or hash entry if table name specified | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 30 | ▶ | return (defined $tbnm)? $self->{'tblc'}->{$tbnm}:$self->{'tblc'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # Execute SQL command | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 9 | ▶ | sub sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 9 | my $sqlc = shift; # SQL command text | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 9 | return $self->dbi->do($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # Define (and initialise) table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 6 | ▶ | sub definetable { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
definetable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 6 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 6 | my $cols = shift; # Column specification | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | ## Create table if it doesn't exist. WARNING: when the table already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | ## exists, there is currently not a test to ensure that the existing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | ## layout matches the column specification in the arguments to this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | ## method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 6 | my $sqlc = "CREATE TABLE IF NOT EXISTS $tbnm (" . join(',',@$cols) . ');'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | my $drtv = $self->sql($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # Record column names for this table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 6 | $self->definedcolumns($tbnm, [map { /^[^\s]+/; $& } @$cols]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 6 | return $drtv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # Insert rows into table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 14 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 14 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 14 | my $opts = shift; # Insert options | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # Determine column names for insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 14 | my $clnm = $self->_optioncols($tbnm, $opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # Determine data for insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 14 | my $data = _optiondata($tbnm, $clnm, $opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Start transaction (autocommit off) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 14 | ▶ | $self->dbi->begin_work or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | throw File::Properties::Error("DBI error ".$self->dbi->errstr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # Construct string describing columns corresponding to row data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # using either specified array of column names, or recorded column | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # names for this table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 14 | my $clst = join(',',@$clnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # Construct insert statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | my $sqlc = "INSERT INTO $tbnm ($clst) VALUES (" . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 14 | join(',', map { '?' } @$clnm) . ');'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # Prepare for insertion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 14 | my $sth = $self->dbi->prepare($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # Execute insertion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 14 | return $self->_executedata($sth, $data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # Update rows in table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | my $opts = shift; # Update options | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # Determine column names for update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 1 | my $clnm = $self->_optioncols($tbnm, $opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # Determine data for update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | my $data = _optiondata($tbnm, $clnm, $opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # Start transaction (autocommit off) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | $self->dbi->begin_work or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | throw File::Properties::Error("DBI error ".$self->dbi->errstr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # Construct update statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | my $sqlc = "UPDATE $tbnm SET " . join(',', map { "$_=?" } @$clnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | $sqlc .= _optionwhere($opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # Prepare for update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | my $sth = $self->dbi->prepare($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # Execute update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | return $self->_executedata($sth, $data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # Select data from table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 19 | ▶ | sub retrieve { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=18 SCAR=33.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 19 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 19 | my $opts = shift; # Select options | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 19 | my $slc = 'SELECT '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # Select statement includes DISTINCT if 'Distint' option true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 19 | ▶ | $slc .= 'DISTINCT ' if (ref($opts) eq 'HASH' and $opts->{'Distinct'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $opts eq "HASH" and $opts->{"Distinct"}
MC/DC0%0/2
ref $opts eq "HASH" and $opts->{"Distinct"}
ref $opts eq "HASH"
$opts->{"Distinct"}
Truth table33%1/3
A and B
A= ref $opts eq "HASH"B= $opts->{"Distinct"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 19 | my ($ncl, $cln); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | ## List of returned columns is constructed from the array provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | ## with the 'Columns' option, otherwise all columns are returned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 19 | ▶ | if (ref($opts) eq 'HASH' and ref($opts->{'Columns'}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $opts eq "HASH" and ref $opts->{"Columns"} eq "ARRAY"
MC/DC0%0/2
ref $opts eq "HASH" and ref $opts->{"Columns"} eq "ARRAY"
ref $opts eq "HASH"
ref $opts->{"Columns"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $opts eq "HASH"B= ref $opts->{"Columns"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | $slc .= join(',',@{$opts->{'Columns'}}) . ' '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $cln = $opts->{'Columns'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $ncl = scalar @$cln; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 19 | $slc .= '* '; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 19 | $cln = $self->definedcolumns($tbnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 19 | $ncl = scalar @$cln; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Append FROM and WHERE clauses to select statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 19 | $slc .= "FROM $tbnm " . _optionwhere($opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # Append optional additional clauses to select statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 19 | ▶ | $slc .= $opts->{'Suffix'} if (ref($opts) eq 'HASH' and $opts->{'Suffix'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $opts eq "HASH" and $opts->{"Suffix"}
MC/DC0%0/2
ref $opts eq "HASH" and $opts->{"Suffix"}
ref $opts eq "HASH"
$opts->{"Suffix"}
Truth table33%1/3
A and B
A= ref $opts eq "HASH"B= $opts->{"Suffix"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # Check option 'ReturnType' for invalid values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | throw File::Properties::Error("Option 'ReturnType' may only have". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | "values 'Array' or 'Hash'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | if (ref($opts) eq 'HASH' and defined($opts->{'ReturnType'}) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | not($opts->{'ReturnType'} eq 'Array' or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 19 | ▶ | $opts->{'ReturnType'} eq 'Hash')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
ref $opts eq "HASH" and defined $opts->{"ReturnType"} and not $opts->{"ReturnType"} eq "Array" || $opts->{"ReturnType"} eq "Hash"
ref $opts eq "HASH" and defined $opts->{"ReturnType"}
$opts->{'ReturnType'} eq 'Array' || $opts->{'ReturnType'} eq 'Hash'
MC/DC0%0/4
ref $opts eq "HASH" and defined $opts->{"ReturnType"} and not $opts->{"ReturnType"} eq "Array" || $opts->{"ReturnType"} eq "Hash"
ref $opts eq "HASH"
defined $opts->{"ReturnType"}
$opts->{'ReturnType'} eq 'Array'
$opts->{'ReturnType'} eq 'Hash'
Truth table40%2/5
A and B and not(C || D)
A= ref $opts eq "HASH"B= defined $opts->{"ReturnType"}C= $opts->{'ReturnType'} eq 'Array'D= $opts->{'ReturnType'} eq 'Hash'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ## DBI method for retrieving data depends on options 'ReturnType' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | ## and 'FirstRow'. If 'ReturnType' is not specified, data is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | ## retrieved as an array. If 'FirstRow' option is unspecified, or is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | ## false, a single row is returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 19 | my $dat = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 19 | ▶ | if (ref($opts) eq 'HASH' and defined($opts->{'ReturnType'}) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $opts eq "HASH" and defined $opts->{"ReturnType"} and $opts->{"ReturnType"} eq "Hash"
ref $opts eq "HASH" and defined $opts->{"ReturnType"}
MC/DC33%1/3
ref $opts eq "HASH" and defined $opts->{"ReturnType"} and $opts->{"ReturnType"} eq "Hash"
ref $opts eq "HASH"
defined $opts->{"ReturnType"}
$opts->{"ReturnType"} eq "Hash"
Truth table50%2/4
A and B and C
A= ref $opts eq "HASH"B= defined $opts->{"ReturnType"}C= $opts->{"ReturnType"} eq "Hash"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | $opts->{'ReturnType'} eq 'Hash') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 10 | ▶ | if (ref($opts) eq 'HASH' and $opts->{'FirstRow'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $opts eq "HASH" and $opts->{"FirstRow"}
MC/DC50%1/2
ref $opts eq "HASH" and $opts->{"FirstRow"}
ref $opts eq "HASH"
$opts->{"FirstRow"}
Truth table66%2/3
A and B
A= ref $opts eq "HASH"B= $opts->{"FirstRow"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # Retrieve single row as a hash indexed by column names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 9 | $dat = $self->dbi->selectrow_hashref($slc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # Retrieve rows as an array of arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | my $adt = $self->dbi->selectall_arrayref($slc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | ## Map array to hash of arrays indexed by column names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | $dat = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | map { $dat->{$_} = [] } @$cln; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 1 | map { my $r = $_; map { push @{$dat->{$cln->[$_]}}, $r->[$_] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | @{[0 .. (scalar @$cln-1)]} } @$adt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 9 | ▶ | if (ref($opts) eq 'HASH' and $opts->{'FirstRow'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $opts eq "HASH" and $opts->{"FirstRow"}
MC/DC50%1/2
ref $opts eq "HASH" and $opts->{"FirstRow"}
ref $opts eq "HASH"
$opts->{"FirstRow"}
Truth table66%2/3
A and B
A= ref $opts eq "HASH"B= $opts->{"FirstRow"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # Retrieve single row as an array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 7 | $dat = $self->dbi->selectrow_arrayref($slc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # Retrieve rows as an array of arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | $dat = $self->dbi->selectall_arrayref($slc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 19 | return $dat; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Remove data from table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=4 SCAR=15.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | my $opts = shift; # Delete options | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # Throw exception if options hash includes neither a 'Where' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # constraint nor the 'RemoveAll' flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | throw File::Properties::Error("Method remove called without row selection") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | if (ref($opts) ne 'HASH' or (not defined $opts->{'Where'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | #and not defined $opts->{'Suffix'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | ▶ | and not $opts->{'RemoveAll'})); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $opts ne "HASH" or not defined $opts->{"Where"} || $opts->{"RemoveAll"}
defined $opts->{'Where'} || $opts->{'RemoveAll'}
MC/DC0%0/3
ref $opts ne "HASH" or not defined $opts->{"Where"} || $opts->{"RemoveAll"}
ref $opts ne "HASH"
defined $opts->{'Where'}
$opts->{'RemoveAll'}
Truth table25%1/4
A or not(B || C)
A= ref $opts ne "HASH"B= defined $opts->{'Where'}C= $opts->{'RemoveAll'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # Set up SQL DELETE statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | my $sqlc = "DELETE FROM $tbnm "._optionwhere($opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | ## Append optional additional clauses to delete statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | #$sqlc .= $opts->{'Suffix'} if (ref($opts) eq 'HASH' and $opts->{'Suffix'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # Execute SQL DELETE statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | return $self->sql($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # Determine names of all tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub tables { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tables: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # Set up SQL statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | my $sqlc = "SELECT * FROM sqlite_master WHERE type = 'table'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Execute SQL statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | my $rar = $self->dbi->selectall_arrayref($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | return [map { $_->[1] } @$rar]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # Determine names of columns in specified table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | sub columns { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # Set up SQL statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | my $sqlc = "PRAGMA table_info($tbnm)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # Execute SQL statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $rar = $self->dbi->selectall_arrayref($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | return [map { $_->[1] } @$rar]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # Determine number of rows in specified table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | sub numrows { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
numrows: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # Set up SQL statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my $sqlc = "SELECT Count(*) FROM $tbnm"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # Execute SQL statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my $rar = $self->dbi->selectall_arrayref($sqlc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | return $rar->[0]->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # Determine whether table exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 7 | ▶ | sub tableexists { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tableexists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 7 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 7 | return _tableexists($self->dbi, $tbnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # Create column name array for insert and update operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 15 | ▶ | sub _optioncols { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_optioncols: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 15 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 15 | my $opts = shift; # Options hash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | ## Column names are taken from keys of the 'Data' option if it is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | ## hash, otherwise from 'Columns' option if provided, otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | ## assumed to be all columns in table order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 15 | my $clnm; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 15 | ▶ | if (ref($opts) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 15 | ▶ | if (ref($opts->{'Data'}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 12 | $clnm = [sort keys %{$opts->{'Data'}}]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } elsif (ref($opts->{'Columns'}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 1 | $clnm = $opts->{'Columns'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | $clnm = $self->definedcolumns($tbnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $clnm = $self->definedcolumns($tbnm); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 15 | return $clnm; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # Create data array for insert and update operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 15 | ▶ | sub _optiondata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_optiondata: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 15 | my $tbnm = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 15 | my $clnm = shift; # Column names | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 15 | my $opts = shift; # Options hash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | ## If 'Data' option provided, process it into a form ready for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | ## insertion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 15 | my $data = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 15 | ▶ | if (ref($opts) eq 'HASH' and ref($opts->{'Data'})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $opts eq "HASH" and ref $opts->{"Data"}
MC/DC0%0/2
ref $opts eq "HASH" and ref $opts->{"Data"}
ref $opts eq "HASH"
ref $opts->{"Data"}
Truth table33%1/3
A and B
A= ref $opts eq "HASH"B= ref $opts->{"Data"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | # Data is provided as a hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 15 | ▶ | if (ref($opts->{'Data'}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # If first column in hash is an array reference, assume all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | # columns are array references, specifying multiple insertion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | # rows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 12 | ▶ | if (ref($opts->{'Data'}->{$clnm->[0]}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 1 | $data = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | my $nrow = scalar @{$opts->{'Data'}->{$clnm->[0]}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 1 | ▶ | for (my $n = 0; $n < $nrow; $n++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 4 | push @$data, [map { $opts->{'Data'}->{$_}->[$n] } @$clnm]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # Assume that only a single row is to be inserted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 11 | $data = [[map { $opts->{'Data'}->{$_} } @$clnm]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # Data is provided as an array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | elsif (ref($opts->{'Data'}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # If the first entry in the array is an array reference, assume | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # multiple insertion rows are provided | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 3 | ▶ | if (ref($opts->{'Data'}->[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 1 | $data = $opts->{'Data'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # Assume that only a single row is to be inserted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 2 | $data = [[@{$opts->{'Data'}}]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | throw File::Properties::Error("Data option must be a hash or an array"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 15 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # Create where statement for retrieve and remove operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 21 | ▶ | sub _optionwhere { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_optionwhere: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 21 | my $opts = shift; # Options hash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 21 | my $whr = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | ## If option 'Where' is a hash reference, key/value pairs are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | ## assumed to correspond to column name/value pairs in a conjunction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | ## of equality constraints in a WHERE clause. If it is not a hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | ## reference, the value is assume to be a string containing the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | ## WHERE clause. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 21 | ▶ | if (ref($opts) eq 'HASH' and defined $opts->{'Where'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $opts eq "HASH" and defined $opts->{"Where"}
MC/DC0%0/2
ref $opts eq "HASH" and defined $opts->{"Where"}
ref $opts eq "HASH"
defined $opts->{"Where"}
Truth table33%1/3
A and B
A= ref $opts eq "HASH"B= defined $opts->{"Where"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 21 | ▶ | if (ref($opts->{'Where'}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | $whr .= " WHERE " . join ' AND ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 19 | map { "$_='$opts->{'Where'}->{$_}'" } keys %{$opts->{'Where'}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 2 | $whr .= " WHERE " . $opts->{'Where'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 21 | return $whr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # Execute statement handle object over specified data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 15 | ▶ | sub _executedata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_executedata: called CC=5 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 15 | my $sth = shift; # Statement handle object | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 15 | my $data = shift; # Data array | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | ## If data provided, insert each row and then commit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | ## otherwise return the statement handle object from prepare | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 15 | ▶ | if (defined $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 15 | my $row; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 15 | foreach $row (@$data) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 20 | ▶ | $sth->execute(@$row) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | throw File::Properties::Error("DBI error ".$self->dbi->errstr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 15 | ▶ | if (not $self->dbi->commit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | my $err = $self->dbi->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | $self->dbi->rollback; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | throw File::Properties::Error("DBI error $err"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 15 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | return $sth; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Connect to SQLite database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 3 | ▶ | sub _dbopen { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dbopen: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 3 | my $dbf = shift; # DB file path | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 3 | my $rof = shift; # Read only flag | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | my $dbh = DBI->connect("dbi:SQLite:dbname=$dbf",'','', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | { ReadOnly => $rof, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | HandleError => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | ▶ | sub {throw File::Properties::Error($_[0])} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 3 | ▶ | $dbh->{sqlite_unicode} = 1 if defined $dbh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 3 | return $dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | # Disconnect from SQLite database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 3 | ▶ | sub _dbclose { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dbclose: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 3 | my $dbh = shift; # DBI handle | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 3 | $dbh->disconnect(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | # Determine whether specified table exists in database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 7 | ▶ | sub _tableexists { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tableexists: called CC=6 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 7 | my $dbs = shift; # DBI handle or DB file path | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 7 | my $tbl = shift; # Table name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 7 | my $dbh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | ## If $dbs is a DBI handle, use that handle for database access, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | ## otherwise assume it is the path to an SQLite DB file and attempt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | ## to open it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 7 | ▶ | if (ref($dbs) eq 'DBI::db') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 7 | $dbh = $dbs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | my $dbf = $dbs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | ▶ | return 0 if not -f $dbf; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | $dbh = DBI->connect("dbi:SQLite:$dbf",'','', {PrintError => 0, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | RaiseError => 0}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | ▶ | return 0 if not defined $dbh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | ## Determine whether named table exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 7 | my $slc = "SELECT NAME FROM sqlite_master WHERE TYPE='table' ". | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | "AND NAME='$tbl'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 7 | my $a = $dbh->selectrow_arrayref($slc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | # Close database connection if it was opened in this function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 7 | ▶ | $dbh->disconnect() if not ref($dbs) eq 'DBI::db'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 7 | ▶ | return (defined $a)?(@$a > 0):0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | # End of method definitions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||