1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
# $Id: Index.pm 85 2022-10-29 05:44:36Z stro $
package CPAN::SQLite::DBI::Index;
use strict;
use warnings;
BEGIN {
our $VERSION = '0.220';
$CPAN::SQLite::DBI::Index::info::VERSION = $VERSION;
$CPAN::SQLite::DBI::Index::mods::VERSION = $VERSION;
$CPAN::SQLite::DBI::Index::dists::VERSION = $VERSION;
$CPAN::SQLite::DBI::Index::auths::VERSION = $VERSION;
}
use CPAN::SQLite::DBI qw($dbh);
use parent 'CPAN::SQLite::DBI';
package CPAN::SQLite::DBI::Index::info;
use parent 'CPAN::SQLite::DBI::Index';
use CPAN::SQLite::DBI qw($dbh);
package CPAN::SQLite::DBI::Index::mods;
use parent 'CPAN::SQLite::DBI::Index';
use CPAN::SQLite::DBI qw($dbh);
package CPAN::SQLite::DBI::Index::dists;
use parent 'CPAN::SQLite::DBI::Index';
use CPAN::SQLite::DBI qw($dbh);
sub fetch_ids {
my $self = shift;
my $sql = sprintf(qq{SELECT %s,%s,%s FROM %s}, $self->{id}, $self->{name}, 'dist_vers', $self->{table});
my $sth = $dbh->prepare($sql) or do {
$self->db_error();
return;
};
$sth->execute() or do {
$self->db_error($sth);
return;
};
my ($ids, $versions);
while (my ($id, $key, $vers) = $sth->fetchrow_array()) {
$ids->{$key} = $id;
$versions->{$key} = $vers;
}
$sth->finish;
undef $sth;
return ($ids, $versions);
}
package CPAN::SQLite::DBI::Index::auths;
use parent 'CPAN::SQLite::DBI::Index';
use CPAN::SQLite::DBI qw($dbh);
package CPAN::SQLite::DBI::Index;
use CPAN::SQLite::DBI qw($tables);
use CPAN::SQLite::DBI qw($dbh);
sub fetch_ids {
my $self = shift;
my $sql = sprintf(qq{SELECT %s,%s from %s}, $self->{id}, $self->{name}, $self->{table});
my $sth = $dbh->prepare($sql) or do {
$self->db_error();
return;
};
$sth->execute() or do {
$self->db_error($sth);
return;
};
my $ids;
while (my ($id, $key) = $sth->fetchrow_array()) {
$ids->{$key} = $id;
}
$sth->finish;
undef $sth;
return $ids;
}
sub schema {
my ($self, $data) = @_;
my $schema = '';
foreach my $type (qw(primary other)) {
foreach my $column (keys %{ $data->{$type} }) {
$schema .= $column . ' ' . $data->{$type}->{$column} . ", ";
}
}
$schema =~ s{, $}{};
return $schema;
}
sub create_index {
my ($self, $data) = @_;
my $key = $data->{key};
my $table = $self->{table};
return 1 unless (defined $key and ref($key) eq 'ARRAY');
foreach my $index (@$key) {
my $id_name = 'ix_' . $table . '_' . $index;
$id_name =~ s/\(\s*\d+\s*\)//;
my $sql = 'CREATE INDEX ' . $id_name . ' ON ' . $table . '( ' . $index . ' )';
my $sth = $dbh->prepare($sql);
$sth->execute() or do {
$self->db_error($sth);
return;
};
$sth->finish;
undef $sth;
}
return 1;
}
sub drop_table {
my $self = shift;
my $table = $self->{table};
my $sql = qq{SELECT name FROM sqlite_master } . qq{ WHERE type='table' AND name=?};
my $sth = $dbh->prepare($sql);
$sth->execute($table);
if (defined $sth->fetchrow_array) {
$dbh->do(qq{drop table $table}) or do {
$self->db_error($sth);
return;
};
}
$sth->finish;
undef $sth;
return 1;
}
sub create_table {
my ($self, $schema) = @_;
return unless $schema;
my $sql = sprintf(qq{CREATE TABLE %s (%s)}, $self->{table}, $schema);
my $sth = $dbh->prepare($sql);
$sth->execute() or do {
$self->db_error($sth);
return;
};
$sth->finish;
undef $sth;
return 1;
}
sub create_tables {
my ($self, %args) = @_;
return unless $args{setup};
my $objs = $self->{objs};
foreach my $table (keys %$objs) {
next unless my $schema = $self->schema($tables->{$table});
my $obj = $objs->{$table};
$obj->drop_table or return;
$obj->create_table($schema) or return;
$obj->create_index($tables->{$table}) or return;
}
return 1;
}
sub sth_insert {
my ($self, $fields) = @_;
my $flds = join ',', @{$fields};
my $vals = join ',', map { '?' } @{$fields};
my $sql = sprintf(qq{INSERT INTO %s (%s) VALUES (%s)}, $self->{table}, $flds, $vals);
my $sth = $dbh->prepare($sql) or do {
$self->db_error();
return;
};
return $sth;
}
sub sth_update {
my ($self, $fields, $id, $rep_id) = @_;
my $set = join ',', map { "$_=?" } @{$fields};
my $sql = sprintf(qq{UPDATE %s SET %s WHERE %s = %s}, $self->{table}, $set, $self->{id}, $id);
$sql .= qq { AND rep_id = $rep_id } if ($rep_id);
my $sth = $dbh->prepare($sql) or do {
$self->db_error();
return;
};
return $sth;
}
sub sth_delete {
my ($self, $table_id, $rep_id) = @_;
my $sql = sprintf(qq{DELETE FROM %s where %s = ?}, $self->{table}, $table_id);
$sql .= qq { AND rep_id = $rep_id } if ($rep_id);
my $sth = $dbh->prepare($sql) or do {
$self->db_error();
return;
};
return $sth;
}
1;
=head1 NAME
CPAN::SQLite::DBI::Index - DBI information for indexing the CPAN::SQLite database
=head1 VERSION
version 0.220
=head1 DESCRIPTION
This module provides various methods for L<CPAN::SQLite::Index> in
indexing and populating the database from the index files.
=over
=item C<create_tables>
This creates the database tables.
=item C<drop_table>
This drops a table.
=item C<sth_insert>
This returns an C<$sth> statement handle for inserting
values into a table.
=item C<sth_update>
This returns an C<$sth> statement handle for updating
values into a table.
=item C<sth_delete>
This returns an C<$sth> statement handle for deleting
values from a table.
=back
=head1 SEE ALSO
L<CPAN::SQLite::Index>
=cut
|