| 1 | package NG::DB; | ||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use base 'NG::Object'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use NG::Hashtable; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use SQL::Abstract; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 10 | 0 | my $pkg = shift; | |||||||||||||||||||||||||||||||
| 11 | 0 | my $options = {@_}; | |||||||||||||||||||||||||||||||
| 12 | return bless { | ||||||||||||||||||||||||||||||||
| 13 | dsn => $options->{dsn} || 'DBI:mysql:database=test;host=dbhost', | ||||||||||||||||||||||||||||||||
| 14 | user => $options->{username} || 'root', | ||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | pass => $options->{password} || '' | ||||||||||||||||||||||||||||||
|
Condition0%0/6
$options->{'dsn'} || 'DBI:mysql:database=test;host=dbhost'
$options->{'username'} || 'root'
$options->{'password'} || ''
MC/DC0%0/1
$options->{'dsn'} || 'DBI:mysql:database=test;host=dbhost'
$options->{'dsn'}
MC/DC0%0/1
$options->{'username'} || 'root'
$options->{'username'}
MC/DC0%0/1
$options->{'password'} || ''
$options->{'password'}
Truth table0%0/2
A
A= $options->{'dsn'}
Truth table0%0/2
A
A= $options->{'username'}
Truth table0%0/2
A
A= $options->{'password'}
| |||||||||||||||||||||||||||||||||
| 16 | }, $pkg; | ||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub dbh { | ||||||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 20 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | return ref($self->dbh) eq 'DBI' ? $self->dbh : DBI->connect($self->{dsn}, $self->{user}, $self->{pass}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub disconnect { | ||||||||||||||||||||||||||||||
|
Subroutine
disconnect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 25 | 0 | shift->dbh->disconnect; | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | =head2 select | ||||||||||||||||||||||||||||||||
| 29 | Just use SQL::Abstract to generate SQL, so watch SQL::Abstract for options. | ||||||||||||||||||||||||||||||||
| 30 | For example: | ||||||||||||||||||||||||||||||||
| 31 | my fields => ['col1', 'col2'], | ||||||||||||||||||||||||||||||||
| 32 | my $where => { | ||||||||||||||||||||||||||||||||
| 33 | user => 'nwiger', | ||||||||||||||||||||||||||||||||
| 34 | priority => [ | ||||||||||||||||||||||||||||||||
| 35 | { '=', 2 }, | ||||||||||||||||||||||||||||||||
| 36 | { '>', 5 }, | ||||||||||||||||||||||||||||||||
| 37 | ], | ||||||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||||||
| 39 | my $order => ['col1', {-desc => 'col2'}] | ||||||||||||||||||||||||||||||||
| 40 | select $table, $fields, $where, $order, sub { | ||||||||||||||||||||||||||||||||
| 41 | my ($row) = @_; | ||||||||||||||||||||||||||||||||
| 42 | $row->each(sub{ | ||||||||||||||||||||||||||||||||
| 43 | my ($colname, $colvalue) = @_; | ||||||||||||||||||||||||||||||||
| 44 | }) | ||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub select { | ||||||||||||||||||||||||||||||
|
Subroutine
select: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 49 | 0 | $self->query('select', @_); | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub insert { | ||||||||||||||||||||||||||||||
|
Subroutine
insert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 54 | 0 | $self->query('insert', @_); | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 59 | 0 | $self->query('update', @_); | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 64 | 0 | $self->query('delete', @_); | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub query { | ||||||||||||||||||||||||||||||
|
Subroutine
query: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 68 | 0 | my ($self, $operator, $table, @options) = @_; | |||||||||||||||||||||||||||||||
| 69 | 0 | my $cb; | |||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (lc($operator) eq 'select') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 0 | $cb = pop @options; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||
| 75 | 0 | my $sql = SQL::Abstract->new; | |||||||||||||||||||||||||||||||
| 76 | 0 | my ( $stmt, @bind ) = eval $sql->$operator( $table, @options ); | |||||||||||||||||||||||||||||||
| 77 | 0 | my $sth = $dbh->prepare($stmt); | |||||||||||||||||||||||||||||||
| 78 | 0 | my $rv = $sth->execute(@bind); | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (lc($operator) eq 'select') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | while (my $ref = $sth->fetchrow_hashref()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 0 | $cb->( Hashtable->new(%$ref) ); | |||||||||||||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 0 | $sth->finish(); | |||||||||||||||||||||||||||||||
| 87 | 0 | return $rv; | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 1 | 1; | |||||||||||||||||||||||||||||||