| 1 | # Copyright Infomation | ||||||||||||||||||||||||||||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||||||||||||||||||||||||||||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||||||||||||||||||||||||||||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||||||||||||||||||||||||||||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||||||||||||||||||||||||||||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 8 | package Nile::DBI; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.55'; | |||||||||||||||||||||||||||
| 11 | 1 | our $AUTHORITY = 'cpan:MEWSOFT'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =pod | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =encoding utf8 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Nile::DBI - SQL database manager. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # set database connection params to connect | ||||||||||||||||||||||||||||
| 24 | # $arg{driver}, $arg{host}, $arg{dsn}, $arg{port}, $arg{attr} | ||||||||||||||||||||||||||||
| 25 | # $arg{name}, $arg{user}, $arg{pass} | ||||||||||||||||||||||||||||
| 26 | # if called without params, it will try to load from the default config vars. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # get app context | ||||||||||||||||||||||||||||
| 29 | $app = $self->app; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | $dbh = $app->db->connect(%arg); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Nile::DBI - SQL database manager. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use Nile::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use DBI::Profile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use DBI::ProfileDumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use Hash::AsObject; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | #my $hash = Hash::AsObject->new(\%hash); $hash->foo(27); print $hash->foo; print $hash->baz->quux; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 47 | =head2 dbh() | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $app->db->dbh; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Get or set the current database connection handle. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | has 'dbh' => ( | |||||||||||||||||||||||||||
| 56 | is => 'rw', | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 59 | =head2 connect() | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | $dbh = $app->db->connect(%arg); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Connect to the database. If %arg empty, it will try to get arg from the config object. | ||||||||||||||||||||||||||||
| 64 | Returns the database connection handle is success. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my ($self, %arg) = @_; | |||||||||||||||||||||||||||
| 71 | 0 | my ($dbh, $dsn, $app); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | $app = $self->app; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my $default = $app->config->get("dbi"); | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | $default ||= +{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$default ||= {}
MC/DC0%0/1
$default ||= {}
$default
Truth table0%0/2
A
A= $default
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | %arg = (%{$default}, %arg); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $arg{driver} ||= "mysql"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{"driver"} ||= "mysql"
MC/DC0%0/1
$arg{"driver"} ||= "mysql"
$arg{"driver"}
Truth table0%0/2
A
A= $arg{"driver"}
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $arg{dsn} ||= ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{"dsn"} ||= ""
MC/DC0%0/1
$arg{"dsn"} ||= ""
$arg{"dsn"}
Truth table0%0/2
A
A= $arg{"dsn"}
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $arg{host} ||= "localhost"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{"host"} ||= "localhost"
MC/DC0%0/1
$arg{"host"} ||= "localhost"
$arg{"host"}
Truth table0%0/2
A
A= $arg{"host"}
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $arg{port} ||= 3306; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{"port"} ||= 3306
MC/DC0%0/1
$arg{"port"} ||= 3306
$arg{"port"}
Truth table0%0/2
A
A= $arg{"port"}
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $arg{attr} ||= +{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{"attr"} ||= {}
MC/DC0%0/1
$arg{"attr"} ||= {}
$arg{"attr"}
Truth table0%0/2
A
A= $arg{"attr"}
| |||||||||||||||||||||||||||||
| 85 | #$arg{attr} = {RaiseError => 0, PrintError => 0, mysql_enable_utf8 => 1} | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (!$arg{name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $app->abort("Database error: Empty database name."); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | #$self->dbh->disconnect if ($self->dbh); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($arg{driver} =~ m/ODBC/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | $dbh = DBI->connect("DBI:ODBC:$arg{dsn}", $arg{user}, $arg{pass}, $arg{attr}) | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | or $self->db_error("$DBI::errstr, DSN: $arg{dsn}"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $arg{dsn} ||= "DBI:$arg{driver}:database=$arg{name};host=$arg{host};port=$arg{port}"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg{"dsn"} ||= qq[DBI:$arg{"driver"}:database=$arg{"name"};host=$arg{"host"};port=$arg{"port"}]
MC/DC0%0/1
$arg{"dsn"} ||= qq[DBI:$arg{"driver"}:database=$arg{"name"};host=$arg{"host"};port=$arg{"port"}]
$arg{"dsn"}
Truth table0%0/2
A
A= $arg{"dsn"}
| |||||||||||||||||||||||||||||
| 99 | $dbh = DBI->connect($arg{dsn}, $arg{user}, $arg{pass}, $arg{attr}) | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | or $self->db_error("$DBI::errstr, DSN: $arg{dsn}"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $self->dbh($dbh); | |||||||||||||||||||||||||||
| 104 | 0 | return $dbh; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #$dbh->{'mysql_enable_utf8'} = 1; | ||||||||||||||||||||||||||||
| 107 | #$dbh->do('SET NAMES utf8'); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 110 | =head2 disconnect() | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $app->db->disconnect; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Disconnect from this connection handle. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub disconnect { | ||||||||||||||||||||||||||
|
Subroutine
disconnect: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | $self->dbh->disconnect if ($self->dbh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub table { | ||||||||||||||||||||||||||
|
Subroutine
table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | $self->app->load_once("Nile::DBI::Table"); | |||||||||||||||||||||||||||
| 126 | 0 | $self->app->object("Nile::DBI::Table", (name => $name)); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 129 | =head2 run() | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | $app->db->run($qry); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Run query using the DBI do command or abort if error. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | $self->dbh->do($qry) or $self->db_error($qry); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 142 | =head2 do() | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | $app->db->do($qry); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Run query using the DBI do command and ignore errors. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub do { | ||||||||||||||||||||||||||
|
Subroutine
do: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 152 | 0 | $self->dbh->do($qry); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 155 | =head2 exec() | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | $sth = $app->db->exec($qry); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Prepare and execute the query and return the statment handle. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub exec { | ||||||||||||||||||||||||||
|
Subroutine
exec: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | my $sth = $self->dbh->prepare($qry) or $self->db_error($qry); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | $sth->execute() or $self->db_error($qry); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return $sth; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 170 | =head2 begin() | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | $app->db->begin; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Enable transactions (by turning AutoCommit off) until the next call to commit or rollback. After the next commit or rollback, AutoCommit will automatically be turned on again. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub begin { | ||||||||||||||||||||||||||
|
Subroutine
begin: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 179 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 180 | 0 | return $self->dbh->begin_work or $self->db_error(); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 183 | =head2 commit() | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | $app->db->commit; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | $self->dbh->commit or $self->db_error(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 196 | =head2 rollback() | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | $app->db->rollback; | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Rollback (undo) the most recent series of uncommitted database changes if the database supports transactions and AutoCommit is off. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub rollback { | ||||||||||||||||||||||||||
|
Subroutine
rollback: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 206 | 0 | ▶ | $self->dbh->rollback or $self->db_error(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 209 | =head2 quote() | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | $app->db->quote($value); | ||||||||||||||||||||||||||||
| 212 | $app->db->quote($value, $data_type); | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Quote a string literal for use as a literal value in an SQL statement, by escaping any special characters (such as quotation marks) | ||||||||||||||||||||||||||||
| 215 | contained within the string and adding the required type of outer quotation marks. | ||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | sub quote { | ||||||||||||||||||||||||||
|
Subroutine
quote: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 219 | 0 | my ($self, @arg) = @_; | |||||||||||||||||||||||||||
| 220 | 0 | return $self->dbh->quote(@arg); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 223 | =head2 col() | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # select id from users. return one column array from all rows | ||||||||||||||||||||||||||||
| 226 | @cols = $app->db->col($qry); | ||||||||||||||||||||||||||||
| 227 | $cols_ref = $app->db->col($qry); | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Return one column array from all rows | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | sub col { | ||||||||||||||||||||||||||
|
Subroutine
col: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 234 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 235 | # select id from users. return one column array from all rows | ||||||||||||||||||||||||||||
| 236 | 0 | my $ret = $self->dbh->selectcol_arrayref($qry); | |||||||||||||||||||||||||||
| 237 | 0 | ▶ | if (!defined($ret) && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ret and $self->dbh->err
MC/DC0%0/2
not defined $ret and $self->dbh->err
not defined $ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not defined $retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return wantarray? @{$ret} : $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 241 | =head2 row() | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # select id, email, fname, lname from users | ||||||||||||||||||||||||||||
| 244 | @row = $app->db->row($qry); | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Returns one row as array. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =cut | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | ▶ | sub row { | ||||||||||||||||||||||||||
|
Subroutine
row: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 251 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 252 | # select id, email, fname, lname from users | ||||||||||||||||||||||||||||
| 253 | 0 | my @ret = $self->dbh->selectrow_array($qry); | |||||||||||||||||||||||||||
| 254 | 0 | ▶ | if (!@ret && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @ret and $self->dbh->err
MC/DC0%0/2
not @ret and $self->dbh->err
not @ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not @retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 255 | 0 | return @ret; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 258 | =head2 rows() | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # select id, fname, lname, email from users | ||||||||||||||||||||||||||||
| 261 | @rows = $app->db->rows($qry); | ||||||||||||||||||||||||||||
| 262 | $rows_ref = $app->db->rows($qry); | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Returns all matched rows as array or array ref. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | sub rows { | ||||||||||||||||||||||||||
|
Subroutine
rows: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 269 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 270 | # select id, fname, lname, email from users | ||||||||||||||||||||||||||||
| 271 | 0 | my $ret = $self->dbh->selectall_arrayref($qry); | |||||||||||||||||||||||||||
| 272 | 0 | ▶ | if (!defined($ret) && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ret and $self->dbh->err
MC/DC0%0/2
not defined $ret and $self->dbh->err
not defined $ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not defined $retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | return wantarray? @$ret : $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 276 | =head2 hash() | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | # select * from users where id=$id limit 1 | ||||||||||||||||||||||||||||
| 279 | %user = $app->db->hash($qry); | ||||||||||||||||||||||||||||
| 280 | $user_ref = $app->db->hash($qry); | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | Returns one row as a hash or hash ref | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =cut | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | sub hash { | ||||||||||||||||||||||||||
|
Subroutine
hash: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 287 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 288 | # select * from users where id=$id limit 1 | ||||||||||||||||||||||||||||
| 289 | 0 | my $ret = $self->dbh->selectrow_hashref($qry); | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | if (!defined($ret) && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ret and $self->dbh->err
MC/DC0%0/2
not defined $ret and $self->dbh->err
not defined $ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not defined $retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | return wantarray? %{$ret} : $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 294 | =head2 row_object() | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # select * from users where id=$id limit 1 | ||||||||||||||||||||||||||||
| 297 | $row_obj = $app->db->row_object($qry); | ||||||||||||||||||||||||||||
| 298 | print $row_obj->email; | ||||||||||||||||||||||||||||
| 299 | print $row_obj->fname; | ||||||||||||||||||||||||||||
| 300 | print $row_obj->lname; | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Returns one row as object with columns names as object properties. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub row_object { | ||||||||||||||||||||||||||
|
Subroutine
row_object: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 307 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 308 | # select * from users where id=$id limit 1 | ||||||||||||||||||||||||||||
| 309 | 0 | my $ret = $self->dbh->selectrow_hashref($qry); | |||||||||||||||||||||||||||
| 310 | 0 | ▶ | if (!defined($ret) && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ret and $self->dbh->err
MC/DC0%0/2
not defined $ret and $self->dbh->err
not defined $ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not defined $retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 311 | 0 | return Hash::AsObject->new($ret); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 314 | =head2 hashes() | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | %hashes = $app->db->hashes($qry, $col); | ||||||||||||||||||||||||||||
| 317 | $hashes_ref = $app->db->hashes($qry, $col); | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | Returns list or hashes of all rows. Each hash element is a hash of one row | ||||||||||||||||||||||||||||
| 320 | =cut | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub hashes { | ||||||||||||||||||||||||||
|
Subroutine
hashes: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 323 | 0 | my ($self, $qry, $col) = @_; | |||||||||||||||||||||||||||
| 324 | 0 | my $ret = $self->dbh->selectall_hashref($qry, $col); | |||||||||||||||||||||||||||
| 325 | 0 | ▶ | if (!defined($ret) && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ret and $self->dbh->err
MC/DC0%0/2
not defined $ret and $self->dbh->err
not defined $ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not defined $retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 326 | 0 | ▶ | return wantarray? %{$ret} : $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 329 | =head2 colhash() | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # select id, user from users | ||||||||||||||||||||||||||||
| 332 | %hash = $app->db->colhash($qry); | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Returns all rows as a hash of the first column as the keys and the second column as the values. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub colhash { | ||||||||||||||||||||||||||
|
Subroutine
colhash: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 339 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 340 | # select id, user from users | ||||||||||||||||||||||||||||
| 341 | 0 | my %list = map {$_->[0], $_->[1]} @{$self->dbh->selectall_arrayref($qry)}; | |||||||||||||||||||||||||||
| 342 | 0 | ▶ | return wantarray? %list : \%list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 345 | =head2 value() | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # select email from users where id=123. return one column value | ||||||||||||||||||||||||||||
| 348 | $value = $app->db->value($qry); | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | Returns one column value from one row. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub value { | ||||||||||||||||||||||||||
|
Subroutine
value: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | 0 | my ($self, $qry) = @_; | |||||||||||||||||||||||||||
| 356 | # select email from users where id=123. return one column value | ||||||||||||||||||||||||||||
| 357 | 0 | my $ret = $self->dbh->selectcol_arrayref($qry); | |||||||||||||||||||||||||||
| 358 | 0 | ▶ | if (!defined($ret) && $self->dbh->err()) {$self->db_error($qry);} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ret and $self->dbh->err
MC/DC0%0/2
not defined $ret and $self->dbh->err
not defined $ret
$self->dbh->err
Truth table0%0/3
not(A) and B
A= not defined $retB= $self->dbh->err
| |||||||||||||||||||||||||||||
| 359 | 0 | ($ret) = @{$ret}; | |||||||||||||||||||||||||||
| 360 | 0 | return $ret; | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 363 | =head2 insertid() | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | $id = $app->db->insertid; | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | Returns the last insert id from auto increment. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 0 | ▶ | sub insertid { | ||||||||||||||||||||||||||
|
Subroutine
insertid: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 372 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 373 | 0 | return $self->dbh->{mysql_insertid}; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 376 | =head2 profile() | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # level: | ||||||||||||||||||||||||||||
| 379 | # 0x01=DBI, 0x02=!Statement ,0x04=!MethodName, 0x06=!Statement:!Method, | ||||||||||||||||||||||||||||
| 380 | # 0x08=!MethodClass, 0x10=!Caller2, 0=disable | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | $app->db->profile($level); | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | # this will generate the reports file app/log/dbi.prof | ||||||||||||||||||||||||||||
| 385 | # then run the command dbiprof to view it: | ||||||||||||||||||||||||||||
| 386 | # dbiprof --number 15 --sort count | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Enable DBI profiling. See L<DBI::Profile> and L<DBI::ProfileDumper>. | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub profile { | ||||||||||||||||||||||||||
|
Subroutine
profile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 0 | my ($self, $level) = @_; | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 0 | ▶ | if (!$level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 0 | $self->dbh->{Profile} = 0; | |||||||||||||||||||||||||||
| 399 | 0 | return; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | # level: ,0x01=DBI, 0x02=!Statement ,0x04=!MethodName, 0x06=!Statement:!Method, | ||||||||||||||||||||||||||||
| 402 | # 0x08=!MethodClass, 0x10=!Caller2, 0=disable | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 0 | my $file = $self->app->file->catfile($self->app->var->get("log_dir"), "dbi.prof"); | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | $self->dbh->{Profile} = "$level/DBI::ProfileDumper/File:$file"; | |||||||||||||||||||||||||||
| 407 | #then run % dbiprof --number 15 --sort count | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | #shell: >set DBI_PROFILE=2/DBI::ProfileDumper then %perl program.pl | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | #$sth->{Profile} = 4; | ||||||||||||||||||||||||||||
| 412 | #$sth->execute; while (@array = $sth->fetchrow_array()) {}; | ||||||||||||||||||||||||||||
| 413 | #print $sth->{Profile}->format; | ||||||||||||||||||||||||||||
| 414 | #finally, disable the profile status, so it does nothing at DESTROY time | ||||||||||||||||||||||||||||
| 415 | #$sth->{Profile} = 0; | ||||||||||||||||||||||||||||
| 416 | #mysql: mysql>SET profiling=1;mysql>show profiles;mysql>show profile for query 1; | ||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 419 | =head2 db_error() | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | $app->db->db_error; | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Aborts the application and display the last database error message. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =cut | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 0 | ▶ | sub db_error { | ||||||||||||||||||||||||||
|
Subroutine
db_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 428 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 429 | 0 | $self->app->abort("Database Error: $DBI::errstr<br>@_"); | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =pod | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =head1 Bugs | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =head1 HOMEPAGE | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =head1 SOURCE | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | See L<Nile> for details about the complete framework. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||||||||||||||||||||||||||||
| 454 | Website: http://www.mewsoft.com | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||||||||||||||||||||||||||||
| 459 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =cut | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 1 | 1; | |||||||||||||||||||||||||||