| 1 | package DBIx::DataFactory; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.0.5'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use base qw(Class::Data::Inheritable Class::Accessor::Fast); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | __PACKAGE__->mk_classdata('defined_types' => {}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | __PACKAGE__->mk_accessors(qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | username password dsn dbh connect_attr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | __PACKAGE__->add_type('DBIx::DataFactory::Type::Int'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | __PACKAGE__->add_type('DBIx::DataFactory::Type::Num'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | __PACKAGE__->add_type('DBIx::DataFactory::Type::Str'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | __PACKAGE__->add_type('DBIx::DataFactory::Type::Set'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Smart::Args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use DBIx::Inspector; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use SQL::Maker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Sub::Install; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Class::Load qw/load_class/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use DBIx::DataFactory::Type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub create_factory_method { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_factory_method: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | args my $self, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | my $method => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | my $table => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | my $dsn => {isa => 'Str', optional => 1}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my $username => {isa => 'Str', optional => 1}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $password => {isa => 'Str', optional => 1}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | my $dbh => {isa => 'Object', optional => 1}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | my $connect_attr => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | isa => 'HashRef', optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my $auto_inserted_columns => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'HashRef', optional => 1, default => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | my $install_package => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | isa => 'Str', optional => 1, default => 'DBIx::DataFactory', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | my $creator => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | isa => 'CodeRef', optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $username = $self->username unless $username; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $password = $self->password unless $password; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $dsn = $self->dsn unless $dsn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $dbh = $self->dbh unless $dbh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | unless ((defined $username && defined $password && defined $dsn) || $dbh) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
defined $username and defined $password and defined $dsn or $dbh
defined $username and defined $password and defined $dsn
defined $username and defined $password
MC/DC0%0/4
defined $username and defined $password and defined $dsn or $dbh
defined $username
defined $password
defined $dsn
$dbh
Truth table0%0/7
A and B and C or D
A= defined $usernameB= defined $passwordC= defined $dsnD= $dbh
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | croak('either username, password and dsn for database, or dbh are required'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $connect_attr = $self->connect_attr || {} unless $connect_attr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->connect_attr || {}
MC/DC0%0/1
$self->connect_attr || {}
$self->connect_attr
Truth table0%0/2
A
A= $self->connect_attr
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $dbh ||= DBI->connect($dsn, $username, $password, $connect_attr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$dbh ||= "DBI"->connect($dsn, $username, $password, $connect_attr)
MC/DC0%0/2
$dbh ||= "DBI"->connect($dsn, $username, $password, $connect_attr)
$dbh
"DBI"->connect($dsn, $username, $password, $connect_attr)
Truth table0%0/3
A ||= B
A= $dbhB= "DBI"->connect($dsn, $username, $password, $connect_attr)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $inspector = DBIx::Inspector->new(dbh => $dbh) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | or croak('cannot connect database'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my ($inspector_table) = grep {$_->name eq $table} $inspector->tables; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | croak("cannot find table named $table") unless $inspector_table; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $table_columns = [map {$_->name} $inspector_table->columns]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $primary_keys = [map {$_->name} $inspector_table->primary_key]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $builder = SQL::Maker->new(driver => $dbh->{Driver}->{Name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | Sub::Install::install_sub({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | code => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | return $self->_factory_method( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | dbh => $dbh, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | table => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | column_names => $table_columns, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | builder => $builder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | primary_keys => $primary_keys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | auto_inserted_columns => $auto_inserted_columns, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | creator => $creator, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | params => \%args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | into => $install_package, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | as => $method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub add_type { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my ($class, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | load_class($type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | $class->defined_types->{$type->type_name} = $type; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _make_value_from_type_info { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_value_from_type_info: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($class, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $copy_arg = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | %$copy_arg = %$args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $type_name = delete $copy_arg->{type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $type_class = $class->defined_types->{$type_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | or croak("$type_name is not defined as type"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return $type_class->make_value(%$copy_arg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub _factory_method { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_factory_method: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $dbh = $args{dbh}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $table = $args{table}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $columns = $args{column_names}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $params = $args{params}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $builder = $args{builder}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $pk = $args{primary_keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $creator = $args{creator}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $auto_inserted_columns = $args{auto_inserted_columns}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my $values = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | for my $column (@$columns) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # insert specified value if specified | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $specified = $params->{$column}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (defined $specified) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $values->{$column} = $specified; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # insert setting columns value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $default = $auto_inserted_columns->{$column}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if (ref $default eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $values->{$column} = $default->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | elsif (ref $default eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $value = DBIx::DataFactory->_make_value_from_type_info($default); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $values->{$column} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ($creator) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | return $creator->($values); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $self->_insert( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | builder => $builder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | dbh => $dbh, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | table => $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | primary_keys => $pk, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | values => $values, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub _insert { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $builder = $args{builder}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my $dbh = $args{dbh}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $table = $args{table}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $pk = $args{primary_keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | my $values = $args{values}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # make sql for insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my ($sql, @binds) = $builder->insert($table, $values); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | $sth->execute(@binds); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # set auto increment value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (scalar(@$pk) == 1 && not defined $values->{$pk->[0]}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @$pk == 1 and not defined $$values{$pk->[0]}
MC/DC0%0/2
scalar @$pk == 1 and not defined $$values{$pk->[0]}
scalar @$pk == 1
not defined $$values{$pk->[0]}
Truth table0%0/3
A and not(B)
A= scalar @$pk == 1B= not defined $$values{$pk->[0]}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | $values->{$pk->[0]} = $self->_last_insert_id( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | $dbh, $builder->{driver}, $table, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # refetch data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if (scalar(@$pk) == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | my ($sql, @binds) = $builder->select( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | $table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | ['*'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | { $pk->[0] => $values->{$pk->[0]} }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my $row_hash = $dbh->selectrow_hashref($sql, {}, @binds); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | return $row_hash if $row_hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return $values; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub _last_insert_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_last_insert_id: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my ($self, $dbh, $driver, $table_name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ( $driver eq 'mysql' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | return $dbh->{mysql_insertid}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } elsif ( $driver eq 'Pg' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | return $dbh->last_insert_id( undef, undef, undef, undef,{ sequence => join( '_', $table_name, 'id', 'seq' ) } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } elsif ( $driver eq 'SQLite' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | return $dbh->func('last_insert_rowid'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } elsif ( $driver eq 'Oracle' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | Carp::croak "Don't know how to get last insert id for $driver"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||