File Coverage

blib/lib/Geoffrey/Changelog/Database.pm
Criterion Covered Total %
statement 20 93 21.5
branch 0 40 0.0
condition 0 13 0.0
subroutine 7 21 33.3
pod 10 10 100.0
total 37 177 20.9


line stmt bran cond sub pod time code
1             package Geoffrey::Changelog::Database;
2              
3 1     1   67117 use utf8;
  1         15  
  1         5  
4 1     1   45 use 5.024;
  1         4  
5 1     1   6 use strict;
  1         2  
  1         22  
6 1     1   5 use warnings;
  1         2  
  1         24  
7 1     1   736 use SQL::Abstract;
  1         11127  
  1         48  
8 1     1   472 use Geoffrey::Exception::Database;
  1         12803  
  1         43  
9              
10             $Geoffrey::Changelog::Database::VERSION = '0.000200';
11              
12 1     1   450 use parent 'Geoffrey::Role::Changelog';
  1         272  
  1         7  
13              
14             sub new {
15 0     0 1   my $class = shift;
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 0     $_[0]->{changlog_entries_table} //= 'geoffrey_changlog_entries';
26 0           return $_[0]->{changlog_entries_table};
27             }
28              
29              
30             sub _changlog_entries_table {
31 0     0     my ($self) = @_;
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     0     my ($self) = @_;
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 0         if ($hr_params) {
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 0         if ($self->schema) {
64 0 0         $o_statement_handle->execute($self->schema, $self->_changlog_entries_table_name) or Carp::confess $!;
65             }
66             else {
67 0 0         $o_statement_handle->execute($self->_changlog_entries_table_name) or Carp::confess $!;
68             }
69 0           $hr_params = $o_statement_handle->fetchrow_hashref;
70 0 0         return $hr_params ? undef : $o_action_able->add({
71             name => $self->_changlog_entries_table_name,
72             columns => $self->_changlog_entries_table,
73             schema => $self->schema,
74             });
75             }
76              
77       0 1   sub tpl_main { }
78       0 1   sub tpl_sub { }
79              
80 0     0 1   sub file_extension { return $_[0]->{file_extension}; }
81              
82             sub _get_changeset_entries {
83 0     0     my ($self, $hr_unhandeled_changelog) = @_;
84 0 0         my $s_table_name = ($self->schema ? $self->schema . q/./ : q//) . $self->_changlog_entries_table_name;
85 0           my $s_entries_sql = $self->{sql_abstract}->select($s_table_name, qw/*/, {geoffrey_changelog => {'=', '?'}});
86              
87 0   0       my $ar_entries = $self->dbh->selectall_arrayref($s_entries_sql, {Slice => {}}, ($hr_unhandeled_changelog->{ID}))
88             || Geoffrey::Exception::Database::throw_sql_handle($!, $s_entries_sql);
89 0           return $ar_entries;
90             }
91              
92             sub load {
93 0     0 1   my ($self, $i_changeset_id) = @_;
94 0           $self->_prepare_tables;
95 0 0         my $s_changelog_name = ($self->schema ? $self->schema . q/./ : q//) . $self->geoffrey_changelogs;
96 0 0         my $hr_changeset_sql_params = $i_changeset_id ? {id => $i_changeset_id} : {};
97 0           my $s_changeset_sql = $self->{sql_abstract}->select($s_changelog_name, qw/*/, $hr_changeset_sql_params);
98 0   0       my $ar_changesets
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};
  0            
102 0           require Geoffrey::Utils;
103 0           Geoffrey::Utils::to_lowercase($_) for @{$ar_changesets};
  0            
104 0 0 0       return ($i_changeset_id && scalar @{$ar_changesets} == 1) ? $ar_changesets->[0] : $ar_changesets;
105             }
106              
107             sub write {
108 0     0 1   my ($self, $s_file, $ur_data) = @_;
109 0           require Ref::Util;
110 0 0         return $self->{generated_sql} if Ref::Util::is_hashref($ur_data);
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}) {
  0            
116 0 0         next unless (exists $hr_changeset->{id});
117 0 0         next unless scalar @{$hr_changeset->{entries}};
  0            
118              
119             push(
120 0           @{$self->{generated_sql}},
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 0         ($hr_changeset->{comment} ? (comment => $hr_changeset->{comment}) : ()),
    0          
    0          
132             }]}));
133              
134 0           for my $hr_entry (@{$hr_changeset->{entries}}) {
  0            
135             push(
136 0           @{$self->{generated_sql}},
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 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     0 1   my ($self, $s_schema) = @_;
153 0 0         $self->{schema} = $s_schema if $s_schema;
154 0           return $self->{schema};
155             }
156              
157             sub converter {
158 0     0 1   my ($self, $o_converter) = @_;
159 0 0         $self->{converter} = $o_converter if $o_converter;
160 0           return $self->{converter};
161             }
162              
163             sub dbh {
164 0     0 1   my ($self, $o_dbh) = @_;
165 0 0         $self->{dbh} = $o_dbh if $o_dbh;
166 0           return $self->{dbh};
167             }
168              
169             sub geoffrey_changelogs {
170 0     0 1   my ($self, $s_geoffrey_changelogs) = @_;
171 0 0         $self->{geoffrey_changelogs} = $s_geoffrey_changelogs if $s_geoffrey_changelogs;
172 0   0       $self->{geoffrey_changelogs} //= 'geoffrey_changelogs';
173 0           return $self->{geoffrey_changelogs};
174             }
175              
176             1; # End of Geoffrey::Changelog
177              
178             __END__