MetaPackager/Database.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 124 CC 5 SCAR 18.3
CC
5
Cov
0%
init_db
20.0
get_table_column_names
6.0
build_pad
2.0
CRAP 6.2
1 package CPAN::MetaPackager::Database;
2  
3 use parent 'CPAN::MetaPackager::Config';
4 use warnings qw(FATAL utf8); # Fatalize encoding glitches.
5  
6 use Data::Dumper::Concise; # For Dumper().
7 use DateTime::Tiny;
8  
9 use DBI;
10 use DBIx::Admin::CreateTable;
11 use DBIx::Simple;
12  
13 use File::Spec;
14  
15 use Moo;
16  
17 use Types::Standard qw/Any ArrayRef HashRef Object Str/;
18  
19 has column_names =>
20 (
21 default => sub{return []},
22 is => 'rw',
23 isa => ArrayRef,
24 required => 0,
25 );
26  
27 has creator =>
28 (
29 is => 'rw',
30 isa => Object, # 'DBIx::Admin::CreateTable'.
31 required => 0,
32 );
33  
34 has db =>
35 (
36 default => sub{return ''},
37 is => 'rw',
38 isa => Any,
39 required => 0,
40 );
41  
42 has dbh =>
43 (
44 default => sub{return ''},
45 is => 'rw',
46 isa => Any,
47 required => 0,
48 );
49  
50 has engine =>
51 (
52 default => sub{return ''},
53 is => 'rw',
54 isa => Str,
55 required => 0,
56 );
57  
58 has input_path =>
59 (
60 default => sub{return ''},
61 is => 'rw',
62 isa => Str,
63 required => 1,
64 );
65  
66 has output_path =>
67 (
68 default => sub{return ''},
69 is => 'rw',
70 isa => Str,
71 required => 1,
72 );
73  
74 has pad =>
75 (
76 default => sub{return {} },
77 is => 'rw',
78 isa => HashRef,
79 required => 0,
80 );
81  
82 has time_option =>
83 (
84 default => sub{return ''},
85 is => 'rw',
86 isa => Str,
87 required => 0,
88 );
89  
90 our $VERSION = '1.05';
91  
92 # -----------------------------------------------
93  
94 sub build_pad
95 {
96 my($self) = @_;
97 my($pad) = {};
98  
99 for (@{$self -> table_names}) {$$pad{$_} = $self -> read_table($_) };
100  
101 # Dates.
102 # DateTime::Tiny does not handle time_zone.
103  
104 $_ = DateTime::Tiny -> now;
105 $$pad{now} = $_ -> as_string;
106 $$pad{current_year} = substr($$pad{now}, 0, 4);
107  
108 return $self -> pad($pad);
109  
110 } # End of build_pad.
111  
112 # -----------------------------------------------
113  
114 sub get_table_column_names
115 {
116 my($self, $discard_id, $table_name) = @_;
117 my($sth) = $self -> dbh -> prepare("PRAGMA table_info($table_name)");
118  
119 $sth -> execute;
120  
121 my($row);
122 my(@column_names);
123  
124 while ($row = $sth -> fetchrow_hashref)
125 {
126 push @column_names, $$row{name} if (! $discard_id);
127 }
128  
129 $sth -> finish;
130 $self -> column_names(\@column_names);
131  
132 } # End of get_table_column_names;
133  
134 # -----------------------------------------------
135  
136 sub init_db
137 {
138 my($self) = @_;
139 my($config) = $self -> config;
140  
141 my(%attributes) =
142 (
143 AutoCommit => $$config{AutoCommit},
144 mysql_enable_utf8 => $$config{mysql_enable_utf8}, #Ignored if not using MySQL.
145 RaiseError => $$config{RaiseError},
146 sqlite_unicode => $$config{sqlite_unicode}, #Ignored if not using SQLite.
147 );
148  
149 my(@dsn) = split('=', $$config{dsn});
150 $dsn[1] = File::Spec -> catfile($self -> home_path, $dsn[1]);
151 $dsn[0] = "$dsn[0]=$dsn[1]";
152  
153 $self -> dbh(DBI -> connect($dsn[0], $$config{username}, $$config{password}, \%attributes) );
154 $self -> dbh -> do('PRAGMA foreign_keys = ON') if ($$config{dsn} =~ /SQLite/i);
155 $self -> db(DBIx::Simple -> new($self -> dbh) );
156 $self -> creator(DBIx::Admin::CreateTable -> new(dbh => $self -> dbh, verbose => 0) );
157 $self -> engine($self -> creator -> db_vendor =~ /(?:Mysql)/i ? 'engine=innodb' : '');
158 $self -> time_option($self -> creator -> db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : '');
159 $self -> logger -> info("Connected to $dsn[0]");
160 $self -> logger -> info($self -> separator);
161  
162 } # End of init_db.
163  
164 # -----------------------------------------------
165  
166 sub insert_hashref
167 {
168 my($self, $table_name, $hashref) = @_;
169  
170 $self -> db -> insert($table_name, {map{($_ => $$hashref{$_})} keys %$hashref})
171 || die $self -> db -> error;
172  
173 return $self -> db -> last_insert_id(undef, undef, $table_name, undef);
174  
175 } # End of insert_hashref.
176  
177 # --------------------------------------------------
178  
179 sub read_table
180 {
181 my($self, $table_name) = @_;
182 my($sql) = "select * from $table_name";
183 my($set) = $self -> db -> query($sql) || die $self -> db -> error;
184  
185 # Return an arrayref of hashrefs.
186  
187 return [$set -> hashes];
188  
189 } # End of read_table.
190  
191 # --------------------------------------------------
192  
193 1;
194  
195 =pod
196  
197 =head1 NAME
198  
199 CPAN::MetaPackager::Database - Manage the cpan.metapackager.sqlite database
200  
201 =head1 Instructions
202  
203 See POD in MetaPackager.pm.
204  
205 =head1 Author
206  
207 Ron Savage I<E<lt>ron@savage.net.auE<gt>>.
208  
209 My homepage: L<https://savage.net.au/>.
210  
211 =head1 License
212  
213 Perl 5.
214  
215 =head1 Copyright
216  
217 Copyright (c) 2026, Ron Savage.
218  
219 All Programs of mine are 'OSI Certified Open Source Software';
220 you can redistribute them and/or modify them under the terms of
221 The Perl License, a copy of which is available at:
222 http://dev.perl.org/licenses/
223  
224 =cut