| 1 | package CPAN::MetaCurator::Database; | ||
| 2 | |||
| 3 | use boolean; | ||
| 4 | use feature 'say'; | ||
| 5 | use parent 'CPAN::MetaCurator::Config'; | ||
| 6 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 7 | |||
| 8 | use Data::Dumper::Concise; # For Dumper(). | ||
| 9 | use DateTime::Tiny; | ||
| 10 | |||
| 11 | use DBI; | ||
| 12 | use DBIx::Admin::CreateTable; | ||
| 13 | use DBIx::Simple; | ||
| 14 | |||
| 15 | use Data::Dumper::Concise; # For Dumper(). | ||
| 16 | |||
| 17 | use File::Spec; | ||
| 18 | |||
| 19 | use Mew; | ||
| 20 | |||
| 21 | use Text::CSV::Encoded; | ||
| 22 | |||
| 23 | has -column_names => (ArrayRef, default => sub{return []}, chained => 1); | ||
| 24 | |||
| 25 | has -creator => (Object, default => sub{return Ref}, chained => 1); | ||
| 26 | |||
| 27 | has -db => (Any, default => sub{return ''}, chained => 1); | ||
| 28 | |||
| 29 | has -dbh => (Any, default => sub{return ''}, chained => 1); | ||
| 30 | |||
| 31 | has -engine => (Str, default => sub{return ''}, chained => 1); | ||
| 32 | |||
| 33 | has -include_packages => (Bool, default => sub{return 0}, chained => 1); | ||
| 34 | |||
| 35 | has input_path => (Str, default => sub{return ''}, chained => 1); | ||
| 36 | |||
| 37 | has -metapackager_db => (Any, default => sub{return ''}, chained => 1); | ||
| 38 | |||
| 39 | has -metapackager_dbh => (Any, default => sub{return ''}, chained => 1); | ||
| 40 | |||
| 41 | has output_path => (Str, default => sub{return ''}, chained => 1); | ||
| 42 | |||
| 43 | has -packages_path => (Str, default => sub{return '/tmp/02packages.details.txt'}, chained => 1); | ||
| 44 | |||
| 45 | has -pad => (HashRef, default => sub{return {} }, chained => 1); | ||
| 46 | |||
| 47 | has -time_option => (Str, default => sub{return ''}, chained => 1); | ||
| 48 | |||
| 49 | our $VERSION = '1.29'; | ||
| 50 | |||
| 51 | # ----------------------------------------------- | ||
| 52 | |||
| 53 | sub build_pad | ||
| 54 | { | ||
| 55 | my($self) = @_; | ||
| 56 | |||
| 57 | $self -> logger -> debug("Entered Database.build_pad()"); | ||
| 58 | |||
| 59 | my($pad) = {}; | ||
| 60 | $$pad{count} = {}; | ||
| 61 | $$pad{count}{$_} = 0 for (@{$self -> node_types}); | ||
| 62 | |||
| 63 | # Special case. See HTML.pm's build_html(). | ||
| 64 | |||
| 65 | my($packages_path) = $self -> packages_path; | ||
| 66 | $$pad{packages_id} = `head -8 $packages_path | tail -1`; | ||
| 67 | |||
| 68 | # Read all tables into the pad. | ||
| 69 | |||
| 70 | for (@{$self -> table_names}) {$$pad{$_} = $self -> read_table($_) }; | ||
| 71 | |||
| 72 | # Constants. Eg: encoding. | ||
| 73 | # The constants table used to contain page_name=html/cpan.metacurator.tree.html. | ||
| 74 | # Now we get the value from export.tree.pl & its parameter jstree_html_path. | ||
| 75 | |||
| 76 | $$pad{$$_{name} } = $$_{value} for (@{$$pad{constants} }); | ||
| 77 | |||
| 78 | # MetaPackager. | ||
| 79 | |||
| 80 | if ($self -> include_packages) | ||
| 81 | { | ||
| 82 | my($table_name) = $self -> read_metapackager_table($pad); | ||
| 83 | |||
| 84 | $self -> logger -> info("Including metapackager table 'packages'"); | ||
| 85 | $self -> logger -> info("Size of cpan.metapackager.sqlite table '$table_name': $$pad{count}{$table_name}"); | ||
| 86 | } | ||
| 87 | else | ||
| 88 | { | ||
| 89 | $self -> logger -> info("Excluding metapackager table 'packages'"); | ||
| 90 | } | ||
| 91 | |||
| 92 | # Topics. | ||
| 93 | # There is a db table called topics so we need another name for the hash | ||
| 94 | # where the keys are the names of the topics and the values are db ids. | ||
| 95 | # For useage, see $leaf_id in Export.pm. | ||
| 96 | |||
| 97 | $$pad{topic_ids} = {}; | ||
| 98 | $$pad{topic_names} = {}; | ||
| 99 | |||
| 100 | for (@{$$pad{topics} }) | ||
| 101 | { | ||
| 102 | $$pad{count}{topic}++; | ||
| 103 | |||
| 104 | $$pad{topic_ids}{$$_{id} } = $$_{title}; | ||
| 105 | $$pad{topic_names}{$$_{title} } = $$_{id}; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Dates. | ||
| 109 | # DateTime::Tiny does not handle time_zone. | ||
| 110 | |||
| 111 | $_ = DateTime::Tiny -> now; | ||
| 112 | $$pad{now} = $_ -> as_string; | ||
| 113 | $$pad{current_year} = substr($$pad{now}, 0, 4); | ||
| 114 | |||
| 115 | $self -> pad($pad); | ||
| 116 | $self -> logger -> debug("Leaving Database.build_pad()"); | ||
| 117 | |||
| 118 | return $pad; | ||
| 119 | |||
| 120 | } # End of build_pad. | ||
| 121 | |||
| 122 | # ----------------------------------------------- | ||
| 123 | |||
| 124 | sub get_table_column_names | ||
| 125 | { | ||
| 126 | my($self, $discard_id, $table_name) = @_; | ||
| 127 | my($sth) = $self -> dbh -> prepare("PRAGMA table_info($table_name)"); | ||
| 128 | |||
| 129 | $sth -> execute; | ||
| 130 | |||
| 131 | my($row); | ||
| 132 | my(@column_names); | ||
| 133 | |||
| 134 | while ($row = $sth -> fetchrow_hashref) | ||
| 135 | { | ||
| 136 | push @column_names, $$row{name} if (! $discard_id); | ||
| 137 | } | ||
| 138 | |||
| 139 | $sth -> finish; | ||
| 140 | $self -> column_names(\@column_names); | ||
| 141 | |||
| 142 | } # End of get_table_column_names; | ||
| 143 | |||
| 144 | # ----------------------------------------------- | ||
| 145 | |||
| 146 | sub init_db | ||
| 147 | { | ||
| 148 | my($self) = @_; | ||
| 149 | my($config) = $self -> config; | ||
| 150 | |||
| 151 | my(%attributes) = | ||
| 152 | ( | ||
| 153 | AutoCommit => $$config{AutoCommit}, | ||
| 154 | mysql_enable_utf8 => $$config{mysql_enable_utf8}, # Ignored if not using MySQL. | ||
| 155 | mysql_enable_utf8mb4 => $$config{mysql_enable_utf8mb4}, # Ignored if not using MySQL. | ||
| 156 | pg_enable_utf8 => $$config{pg_enable_utf8}, # Ignored if not using Pg. | ||
| 157 | RaiseError => $$config{RaiseError}, | ||
| 158 | sqlite_unicode => $$config{sqlite_unicode}, # Ignored if not using SQLite. | ||
| 159 | ); | ||
| 160 | |||
| 161 | my(@dsn) = split('=', $$config{dsn}); | ||
| 162 | $dsn[1] = File::Spec -> catfile($self -> home_path, $dsn[1]); | ||
| 163 | $dsn[0] = "$dsn[0]=$dsn[1]"; | ||
| 164 | |||
| 165 | $self -> dbh(DBI -> connect($dsn[0], $$config{username}, $$config{password}, \%attributes) ); | ||
| 166 | $self -> dbh -> do('PRAGMA foreign_keys = ON') if ($$config{dsn} =~ /SQLite/i); | ||
| 167 | $self -> db(DBIx::Simple -> new($self -> dbh) ); | ||
| 168 | $self -> creator(DBIx::Admin::CreateTable -> new(dbh => $self -> dbh, verbose => 0) ); | ||
| 169 | $self -> engine($self -> creator -> db_vendor =~ /(?:Mysql)/i ? 'engine=innodb' : ''); | ||
| 170 | $self -> time_option($self -> creator -> db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : ''); | ||
| 171 | $self -> logger -> info("Database.init_db(). Connected to $dsn[0]"); | ||
| 172 | |||
| 173 | } # End of init_db. | ||
| 174 | |||
| 175 | # ----------------------------------------------- | ||
| 176 | |||
| 177 | sub insert_hashref | ||
| 178 | { | ||
| 179 | my($self, $table_name, $hashref) = @_; | ||
| 180 | |||
| 181 | $self -> db -> insert($table_name, {map{($_ => $$hashref{$_})} keys %$hashref}) | ||
| 182 | || die $self -> db -> error; | ||
| 183 | |||
| 184 | return $self -> db -> last_insert_id(undef, undef, $table_name, undef); | ||
| 185 | |||
| 186 | } # End of insert_hashref. | ||
| 187 | |||
| 188 | # ----------------------------------------------- | ||
| 189 | |||
| 190 | sub init_metapackager_db | ||
| 191 | { | ||
| 192 | my($self) = @_; | ||
| 193 | my($config) = $self -> metapackager_config; | ||
| 194 | |||
| 195 | my(%attributes) = | ||
| 196 | ( | ||
| 197 | AutoCommit => $$config{AutoCommit}, | ||
| 198 | mysql_enable_utf8 => $$config{mysql_enable_utf8}, # Ignored if not using MySQL. | ||
| 199 | mysql_enable_utf8mb4 => $$config{mysql_enable_utf8mb4}, # Ignored if not using MySQL. | ||
| 200 | pg_enable_utf8 => $$config{pg_enable_utf8}, # Ignored if not using Pg. | ||
| 201 | RaiseError => $$config{RaiseError}, | ||
| 202 | sqlite_unicode => $$config{sqlite_unicode}, # Ignored if not using SQLite. | ||
| 203 | ); | ||
| 204 | |||
| 205 | my(@dsn) = split('=', $$config{dsn}); | ||
| 206 | $dsn[1] = File::Spec -> catfile($dsn[1]); | ||
| 207 | $dsn[0] = "$dsn[0]=$dsn[1]"; | ||
| 208 | |||
| 209 | $self -> metapackager_dbh(DBI -> connect($dsn[0], $$config{username}, $$config{password}, \%attributes) ); | ||
| 210 | $self -> metapackager_dbh -> do('PRAGMA foreign_keys = ON') if ($$config{dsn} =~ /SQLite/i); | ||
| 211 | $self -> metapackager_db(DBIx::Simple -> new($self -> metapackager_dbh) ); | ||
| 212 | $self -> logger -> info("Database.init_metapackager_db(). Connected to $dsn[0]"); | ||
| 213 | |||
| 214 | } # End of init_metapackager_db. | ||
| 215 | |||
| 216 | # ----------------------------------------------- | ||
| 217 | |||
| 218 | sub read_csv_file | ||
| 219 | { | ||
| 220 | my($self, $path) = @_; | ||
| 221 | |||
| 222 | my($csv) = Text::CSV::Encoded -> new | ||
| 223 | ({ | ||
| 224 | allow_whitespace => 1, | ||
| 225 | encoding_in => 'utf-8', | ||
| 226 | strict => 1, | ||
| 227 | }); | ||
| 228 | |||
| 229 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 230 | |||
| 231 | my(@column_names) = $csv -> column_names($csv -> getline($io) ); | ||
| 232 | my($count) = 0; | ||
| 233 | |||
| 234 | my(@records); | ||
| 235 | |||
| 236 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 237 | { | ||
| 238 | $count++; | ||
| 239 | |||
| 240 | push @records, $$item{$column_names[0]}; | ||
| 241 | } | ||
| 242 | |||
| 243 | close $io; | ||
| 244 | |||
| 245 | $self -> logger -> info("Read 1 heading + $count records from '$path'"); | ||
| 246 | |||
| 247 | return [@records]; | ||
| 248 | |||
| 249 | } # End of read_csv_file. | ||
| 250 | |||
| 251 | # -------------------------------------------------- | ||
| 252 | |||
| 253 | sub read_table | ||
| 254 | { | ||
| 255 | my($self, $table_name) = @_; | ||
| 256 | my($sql) = "select * from $table_name"; | ||
| 257 | my($set) = $self -> db -> query($sql) || die $self -> db -> error; | ||
| 258 | |||
| 259 | # Return an arrayref of hashrefs. | ||
| 260 | |||
| 261 | return [$set -> hashes]; | ||
| 262 | |||
| 263 | } # End of read_table. | ||
| 264 | |||
| 265 | # -------------------------------------------------- | ||
| 266 | |||
| 267 | sub read_metapackager_table | ||
| 268 | { | ||
| 269 | my($self, $pad) = @_; | ||
| 270 | |||
| 271 | $self -> init_metapackager_config; | ||
| 272 | $self -> init_metapackager_db; | ||
| 273 | |||
| 274 | # Return an arrayref of hashrefs. | ||
| 275 | |||
| 276 | my($table_name) = 'packages'; | ||
| 277 | my($sql) = "select * from $table_name"; | ||
| 278 | my($set) = $self -> metapackager_db -> query($sql) || die $self -> metapackager_db -> error; | ||
| 279 | $set = [$set -> hashes]; | ||
| 280 | $$pad{count}{$table_name} = $#$set + 1; | ||
| 281 | $$pad{$table_name} = {}; | ||
| 282 | $$pad{$table_name}{$$_{name} } = $$_{id} for (@$set); | ||
| 283 | |||
| 284 | $self -> metapackager_dbh -> disconnect; | ||
| 285 | |||
| 286 | return $table_name; | ||
| 287 | |||
| 288 | } # End of read_metapackager_table. | ||
| 289 | |||
| 290 | # -------------------------------------------------- | ||
| 291 | |||
| 292 | sub read_1_record | ||
| 293 | { | ||
| 294 | my($self, $table_name, $id) = @_; | ||
| 295 | my($sql) = "select * from $table_name where id = $id"; | ||
| 296 | my($set) = $self -> db -> query($sql) || die $self -> db -> error; | ||
| 297 | |||
| 298 | # Return a hashref. | ||
| 299 | |||
| 300 | return ${$set -> hashes}[0]; | ||
| 301 | |||
| 302 | } # End of read_1_record. | ||
| 303 | |||
| 304 | # -------------------------------------------------- | ||
| 305 | |||
| 306 | sub update_table | ||
| 307 | { | ||
| 308 | my($self, $table_name, $id, $columns) = @_; | ||
| 309 | my($sql) = "update $table_name set $columns where id = $id"; | ||
| 310 | my($set) = $self -> db -> query($sql) || die $self -> db -> error; | ||
| 311 | |||
| 312 | return $set; | ||
| 313 | |||
| 314 | } # End of update_table. | ||
| 315 | |||
| 316 | # -------------------------------------------------- | ||
| 317 | |||
| 318 | 1; | ||
| 319 | |||
| 320 | =pod | ||
| 321 | |||
| 322 | =head1 Machine-Readable Change Log | ||
| 323 | |||
| 324 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 325 | |||
| 326 | =head1 Version Numbers | ||
| 327 | |||
| 328 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 329 | |||
| 330 | =head1 Support | ||
| 331 | |||
| 332 | Email the author. | ||
| 333 | |||
| 334 | =head1 Author | ||
| 335 | |||
| 336 | L<CPAN::MetaCurator> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2025. | ||
| 337 | |||
| 338 | My homepage: L<https://savage.net.au/>. | ||
| 339 | |||
| 340 | =head1 Copyright | ||
| 341 | |||
| 342 | Australian copyright (c) 2025, Ron Savage. | ||
| 343 | |||
| 344 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 345 | you can redistribute them and/or modify them under the terms of | ||
| 346 | The Perl License, a copy of which is available at: | ||
| 347 | http://dev.perl.org/licenses/ | ||
| 348 | |||
| 349 | =cut |