| 1 | package Geoffrey::Changelog::Database; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use 5.024; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use SQL::Abstract; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Geoffrey::Exception::Database; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | $Geoffrey::Changelog::Database::VERSION = '0.000200'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use parent 'Geoffrey::Role::Changelog'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 17 | 0 | $self->{needs_converter} = 1; | |||||||||||||||||||||||||||
| 18 | 0 | $self->{needs_dbh} = 1; | |||||||||||||||||||||||||||
| 19 | 0 | $self->{sql_abstract} = SQL::Abstract->new; | |||||||||||||||||||||||||||
| 20 | 0 | $self->{generated_sql} = []; | |||||||||||||||||||||||||||
| 21 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub _changlog_entries_table_name { | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | $_[0]->{changlog_entries_table} //= 'geoffrey_changlog_entries'; | ||||||||||||||||||||||||||
|
Subroutine
_changlog_entries_table_name: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]{"changlog_entries_table"} //= "geoffrey_changlog_entries"
MC/DC0%0/1
$_[0]{"changlog_entries_table"} //= "geoffrey_changlog_entries"
$_[0]{"changlog_entries_table"}
Truth table0%0/2
A
A= $_[0]{"changlog_entries_table"}
| |||||||||||||||||||||||||||||
| 26 | 0 | return $_[0]->{changlog_entries_table}; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub _changlog_entries_table { | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_changlog_entries_table: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | return [ | ||||||||||||||||||||||||||||
| 33 | 0 | {name => 'id', type => 'integer', primarykey => 1, notnull => 1, default => 'autoincrement',}, | |||||||||||||||||||||||||||
| 34 | {name => 'action', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 35 | {name => 'name', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 36 | {name => 'template', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 37 | {name => 'type', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 38 | {name => 'plain_sql', type => 'text'}, | ||||||||||||||||||||||||||||
| 39 | {name => 'refcolumn', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 40 | {name => 'reftable', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 41 | {name => 'columns', type => 'varchar', lenght => 64,}, | ||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | name => 'geoffrey_changelog', | ||||||||||||||||||||||||||||
| 44 | type => 'varchar', | ||||||||||||||||||||||||||||
| 45 | lenght => 64, | ||||||||||||||||||||||||||||
| 46 | notnull => 1, | ||||||||||||||||||||||||||||
| 47 | foreignkey => {reftable => $self->geoffrey_changelogs, refcolumn => 'id'}, | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | ]; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub _prepare_tables { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_prepare_tables: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 54 | 0 | require Geoffrey::Action::Table; | |||||||||||||||||||||||||||
| 55 | 0 | my $o_action_able = Geoffrey::Action::Table->new(dbh => $self->dbh, converter => $self->converter); | |||||||||||||||||||||||||||
| 56 | 0 | my $hr_params = $self->converter->get_changelog_table_hashref($self->dbh, $self->schema); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($hr_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $hr_params->{schema} = $self->schema; | |||||||||||||||||||||||||||
| 59 | 0 | Geoffrey::Action::Table->new(dbh => $self->dbh, converter => $self->converter)->add($hr_params); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $o_statement_handle = $self->dbh->prepare($self->converter->select_get_table); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($self->schema) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $o_statement_handle->execute($self->schema, $self->_changlog_entries_table_name) or Carp::confess $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $o_statement_handle->execute($self->_changlog_entries_table_name) or Carp::confess $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | $hr_params = $o_statement_handle->fetchrow_hashref; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | return $hr_params ? undef : $o_action_able->add({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | name => $self->_changlog_entries_table_name, | ||||||||||||||||||||||||||||
| 72 | columns => $self->_changlog_entries_table, | ||||||||||||||||||||||||||||
| 73 | schema => $self->schema, | ||||||||||||||||||||||||||||
| 74 | }); | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | ▶ | sub tpl_main { } | |||||||||||||||||||||||||||
|
Subroutine
tpl_main: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | ▶ | sub tpl_sub { } | |||||||||||||||||||||||||||
|
Subroutine
tpl_sub: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub file_extension { return $_[0]->{file_extension}; } | ||||||||||||||||||||||||||
|
Subroutine
file_extension: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub _get_changeset_entries { | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my ($self, $hr_unhandeled_changelog) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_changeset_entries: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $s_table_name = ($self->schema ? $self->schema . q/./ : q//) . $self->_changlog_entries_table_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | my $s_entries_sql = $self->{sql_abstract}->select($s_table_name, qw/*/, {geoffrey_changelog => {'=', '?'}}); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $ar_entries = $self->dbh->selectall_arrayref($s_entries_sql, {Slice => {}}, ($hr_unhandeled_changelog->{ID})) | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->dbh->selectall_arrayref($s_entries_sql, {'Slice', {}}, $hr_unhandeled_changelog->{'ID'}) || Geoffrey::Exception::Database::throw_sql_handle($!, $s_entries_sql)
MC/DC0%0/2
$self->dbh->selectall_arrayref($s_entries_sql, {'Slice', {}}, $hr_unhandeled_changelog->{'ID'}) || Geoffrey::Exception::Database::throw_sql_handle($!, $s_entries_sql)
$self->dbh->selectall_arrayref($s_entries_sql, {'Slice', {}}, $hr_unhandeled_changelog->{'ID'})
Geoffrey::Exception::Database::throw_sql_handle($!, $s_entries_sql)
Truth table0%0/3
A || B
A= $self->dbh->selectall_arrayref($s_entries_sql, {'Slice', {}}, $hr_unhandeled_changelog->{'ID'})B= Geoffrey::Exception::Database::throw_sql_handle($!, $s_entries_sql)
| |||||||||||||||||||||||||||||
| 88 | || Geoffrey::Exception::Database::throw_sql_handle($!, $s_entries_sql); | ||||||||||||||||||||||||||||
| 89 | 0 | return $ar_entries; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub load { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my ($self, $i_changeset_id) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 0 | $self->_prepare_tables; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $s_changelog_name = ($self->schema ? $self->schema . q/./ : q//) . $self->geoffrey_changelogs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $hr_changeset_sql_params = $i_changeset_id ? {id => $i_changeset_id} : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | my $s_changeset_sql = $self->{sql_abstract}->select($s_changelog_name, qw/*/, $hr_changeset_sql_params); | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $ar_changesets | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->dbh->selectall_arrayref($s_changeset_sql, {'Slice', {}}, $i_changeset_id ? $i_changeset_id : ()) || Geoffrey::Exception::Database::throw_sql_handle($!, $s_changeset_sql)
MC/DC0%0/2
$self->dbh->selectall_arrayref($s_changeset_sql, {'Slice', {}}, $i_changeset_id ? $i_changeset_id : ()) || Geoffrey::Exception::Database::throw_sql_handle($!, $s_changeset_sql)
$self->dbh->selectall_arrayref($s_changeset_sql, {'Slice', {}}, $i_changeset_id ? $i_changeset_id : ())
Geoffrey::Exception::Database::throw_sql_handle($!, $s_changeset_sql)
Truth table0%0/3
A || B
A= $self->dbh->selectall_arrayref($s_changeset_sql, {'Slice', {}}, $i_changeset_id ? $i_changeset_id : ())B= Geoffrey::Exception::Database::throw_sql_handle($!, $s_changeset_sql)
| |||||||||||||||||||||||||||||
| 99 | = $self->dbh->selectall_arrayref($s_changeset_sql, {Slice => {}}, ($i_changeset_id ? ($i_changeset_id) : ())) | ||||||||||||||||||||||||||||
| 100 | || Geoffrey::Exception::Database::throw_sql_handle($!, $s_changeset_sql); | ||||||||||||||||||||||||||||
| 101 | 0 | $_->{entries} = $self->_get_changeset_entries($_) for @{$ar_changesets}; | |||||||||||||||||||||||||||
| 102 | 0 | require Geoffrey::Utils; | |||||||||||||||||||||||||||
| 103 | 0 | Geoffrey::Utils::to_lowercase($_) for @{$ar_changesets}; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | return ($i_changeset_id && scalar @{$ar_changesets} == 1) ? $ar_changesets->[0] : $ar_changesets; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i_changeset_id and scalar @{$ar_changesets;} == 1
MC/DC0%0/2
$i_changeset_id and scalar @{$ar_changesets;} == 1
$i_changeset_id
scalar @{$ar_changesets;} == 1
Truth table0%0/3
A and B
A= $i_changeset_idB= scalar @{$ar_changesets;} == 1
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub write { | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my ($self, $s_file, $ur_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 0 | require Ref::Util; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | return $self->{generated_sql} if Ref::Util::is_hashref($ur_data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $self->_prepare_tables; | |||||||||||||||||||||||||||
| 112 | 0 | require Geoffrey::Action::Entry; | |||||||||||||||||||||||||||
| 113 | 0 | my $o_action_entry = Geoffrey::Action::Entry->new(dbh => $self->dbh, converter => $self->converter); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | for my $hr_changeset (@{$ur_data}) { | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | next unless (exists $hr_changeset->{id}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | next unless scalar @{$hr_changeset->{entries}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | push( | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | @{$self->{generated_sql}}, | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 121 | $o_action_entry->add({ | ||||||||||||||||||||||||||||
| 122 | schema => $self->schema, | ||||||||||||||||||||||||||||
| 123 | table => $self->geoffrey_changelogs, | ||||||||||||||||||||||||||||
| 124 | values => [{ | ||||||||||||||||||||||||||||
| 125 | id => $hr_changeset->{id}, | ||||||||||||||||||||||||||||
| 126 | filename => __PACKAGE__ . '::' . __LINE__, | ||||||||||||||||||||||||||||
| 127 | created_by => $hr_changeset->{created_by} | ||||||||||||||||||||||||||||
| 128 | ? $hr_changeset->{created_by} : $hr_changeset->{author} | ||||||||||||||||||||||||||||
| 129 | ? $hr_changeset->{author} : undef, | ||||||||||||||||||||||||||||
| 130 | geoffrey_version => $Geoffrey::Changelog::Database::VERSION, | ||||||||||||||||||||||||||||
| 131 | 0 | ($hr_changeset->{comment} ? (comment => $hr_changeset->{comment}) : ()), | |||||||||||||||||||||||||||
| 132 | }]})); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | for my $hr_entry (@{$hr_changeset->{entries}}) { | |||||||||||||||||||||||||||
| 135 | push( | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | @{$self->{generated_sql}}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | $o_action_entry->add({ | ||||||||||||||||||||||||||||
| 138 | schema => $self->schema, | ||||||||||||||||||||||||||||
| 139 | table => $self->_changlog_entries_table_name, | ||||||||||||||||||||||||||||
| 140 | values => [{ | ||||||||||||||||||||||||||||
| 141 | geoffrey_changelog => $hr_changeset->{id}, | ||||||||||||||||||||||||||||
| 142 | action => $hr_entry->{action}, | ||||||||||||||||||||||||||||
| 143 | name => $hr_entry->{entry_name}, | ||||||||||||||||||||||||||||
| 144 | 0 | (exists $hr_entry->{as} ? (plain_sql => $hr_entry->{as}) : ()), | |||||||||||||||||||||||||||
| 145 | }]})); | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | return $self->{generated_sql}; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub schema { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ($self, $s_schema) = @_; | ||||||||||||||||||||||||||
|
Subroutine
schema: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | $self->{schema} = $s_schema if $s_schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | return $self->{schema}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub converter { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my ($self, $o_converter) = @_; | ||||||||||||||||||||||||||
|
Subroutine
converter: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | $self->{converter} = $o_converter if $o_converter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | return $self->{converter}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub dbh { | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my ($self, $o_dbh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | $self->{dbh} = $o_dbh if $o_dbh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | return $self->{dbh}; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub geoffrey_changelogs { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my ($self, $s_geoffrey_changelogs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
geoffrey_changelogs: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | $self->{geoffrey_changelogs} = $s_geoffrey_changelogs if $s_geoffrey_changelogs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $self->{geoffrey_changelogs} //= 'geoffrey_changelogs'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"geoffrey_changelogs"} //= "geoffrey_changelogs"
MC/DC0%0/1
$self->{"geoffrey_changelogs"} //= "geoffrey_changelogs"
$self->{"geoffrey_changelogs"}
Truth table0%0/2
A
A= $self->{"geoffrey_changelogs"}
| |||||||||||||||||||||||||||||
| 173 | 0 | return $self->{geoffrey_changelogs}; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2 | 1; # End of Geoffrey::Changelog | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||