| 1 | # $Id: DBI.pm 84 2020-05-31 06:29:34Z stro $ | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package CPAN::SQLite::DBI; | ||||||||||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 17 | our $VERSION = '0.219'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 17 | ▶ | use English qw/-no_match_vars/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 17 | require File::Spec; | |||||||||||||||||||
| 12 | 17 | ▶ | use DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 17 | ▶ | use parent 'Exporter'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 17 | our ($dbh, $tables, @EXPORT_OK); | |||||||||||||||||||
| 16 | 17 | @EXPORT_OK = qw($dbh $tables); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 17 | $tables = { | |||||||||||||||||||
| 19 | 'info' => { | ||||||||||||||||||||
| 20 | 'primary' => { | ||||||||||||||||||||
| 21 | 'status' => q!INTEGER NOT NULL PRIMARY KEY!, | ||||||||||||||||||||
| 22 | }, | ||||||||||||||||||||
| 23 | 'other' => {}, | ||||||||||||||||||||
| 24 | 'key' => [], | ||||||||||||||||||||
| 25 | 'name' => 'status', | ||||||||||||||||||||
| 26 | 'id' => 'status', | ||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||
| 28 | mods => { | ||||||||||||||||||||
| 29 | primary => { mod_id => q{INTEGER NOT NULL PRIMARY KEY} }, | ||||||||||||||||||||
| 30 | other => { | ||||||||||||||||||||
| 31 | mod_name => q{VARCHAR(100) NOT NULL}, | ||||||||||||||||||||
| 32 | dist_id => q{INTEGER NOT NULL}, | ||||||||||||||||||||
| 33 | mod_abs => q{TEXT}, | ||||||||||||||||||||
| 34 | mod_vers => q{VARCHAR(10)}, | ||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||
| 36 | key => [qw/dist_id mod_name/], | ||||||||||||||||||||
| 37 | name => 'mod_name', | ||||||||||||||||||||
| 38 | id => 'mod_id', | ||||||||||||||||||||
| 39 | has_a => { dists => 'dist_id' }, | ||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||
| 41 | dists => { | ||||||||||||||||||||
| 42 | primary => { dist_id => q{INTEGER NOT NULL PRIMARY KEY} }, | ||||||||||||||||||||
| 43 | other => { | ||||||||||||||||||||
| 44 | dist_name => q{VARCHAR(90) NOT NULL}, | ||||||||||||||||||||
| 45 | auth_id => q{INTEGER NOT NULL}, | ||||||||||||||||||||
| 46 | dist_file => q{VARCHAR(110) NOT NULL}, | ||||||||||||||||||||
| 47 | dist_vers => q{VARCHAR(20)}, | ||||||||||||||||||||
| 48 | dist_abs => q{TEXT}, | ||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||
| 50 | key => [qw/auth_id dist_name/], | ||||||||||||||||||||
| 51 | name => 'dist_name', | ||||||||||||||||||||
| 52 | id => 'dist_id', | ||||||||||||||||||||
| 53 | has_a => { auths => 'auth_id' }, | ||||||||||||||||||||
| 54 | has_many => { mods => 'dist_id', }, | ||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||
| 56 | auths => { | ||||||||||||||||||||
| 57 | primary => { auth_id => q{INTEGER NOT NULL PRIMARY KEY} }, | ||||||||||||||||||||
| 58 | other => { | ||||||||||||||||||||
| 59 | cpanid => q{VARCHAR(20) NOT NULL}, | ||||||||||||||||||||
| 60 | fullname => q{VARCHAR(40) NOT NULL}, | ||||||||||||||||||||
| 61 | email => q{TEXT}, | ||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||
| 63 | key => [qw/cpanid/], | ||||||||||||||||||||
| 64 | has_many => { dists => 'dist_id' }, | ||||||||||||||||||||
| 65 | name => 'cpanid', | ||||||||||||||||||||
| 66 | id => 'auth_id', | ||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | sub new { | ||||||||||||||||||||
| 71 | 371 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.1
Pod
undocumented
| |||||||||||||||||||||
| 72 | 371 | ▶ | my $db_dir = $args{db_dir} || $args{CPAN}; | ||||||||||||||||||
|
Condition33%1/3
$args{'db_dir'} || $args{'CPAN'}
MC/DC0%0/2
$args{'db_dir'} || $args{'CPAN'}
$args{'db_dir'}
$args{'CPAN'}
Truth table33%1/3
A || B
A= $args{'db_dir'}B= $args{'CPAN'}
| |||||||||||||||||||||
| 73 | 371 | my $db = File::Spec->catfile($db_dir, $args{db_name}); | |||||||||||||||||||
| 74 | 371 | ▶ | $dbh ||= DBI->connect( | ||||||||||||||||||
|
Condition66%2/3
$CPAN::SQLite::DBI::dbh ||= "DBI"->connect("DBI:SQLite:$db", "", "", {"RaiseError", 1, "AutoCommit", 0, "sqlite_use_immediate_transaction", 0})
MC/DC0%0/2
$CPAN::SQLite::DBI::dbh ||= "DBI"->connect("DBI:SQLite:$db", "", "", {"RaiseError", 1, "AutoCommit", 0, "sqlite_use_immediate_transaction", 0})
$CPAN::SQLite::DBI::dbh
"DBI"->connect("DBI:SQLite:$db", "", "", {"RaiseError", 1, "AutoCommit", 0, "sqlite_use_immediate_transaction", 0})
Truth table66%2/3
A ||= B
A= $CPAN::SQLite::DBI::dbhB= "DBI"->connect("DBI:SQLite:$db", "", "", {"RaiseError", 1, "AutoCommit", 0, "sqlite_use_immediate_transaction", 0})
| |||||||||||||||||||||
| 75 | "DBI:SQLite:$db", | ||||||||||||||||||||
| 76 | '', '', | ||||||||||||||||||||
| 77 | { | ||||||||||||||||||||
| 78 | RaiseError => 1, | ||||||||||||||||||||
| 79 | AutoCommit => 0, | ||||||||||||||||||||
| 80 | sqlite_use_immediate_transaction => 0, | ||||||||||||||||||||
| 81 | }); | ||||||||||||||||||||
| 82 | 371 | ▶ | die "Cannot connect to $db" unless $dbh; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 371 | $dbh->{AutoCommit} = 0; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 371 | my $objs; | |||||||||||||||||||
| 86 | 371 | foreach my $table (keys %$tables) { | |||||||||||||||||||
| 87 | 1484 | my $cl = $class . '::' . $table; | |||||||||||||||||||
| 88 | 1484 | $objs->{$table} = $cl->make(table => $table); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 371 | for my $table (keys %$tables) { | |||||||||||||||||||
| 92 | 1484 | foreach my $type (qw(primary other)) { | |||||||||||||||||||
| 93 | 2968 | foreach my $column (keys %{ $tables->{$table}->{$type} }) { | |||||||||||||||||||
| 94 | 5936 | push @{ $tables->{$table}->{columns} }, $column; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 371 | return bless { objs => $objs }, $class; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | sub make { | ||||||||||||||||||||
| 103 | 1484 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||
|
Subroutine
make: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 104 | 1484 | my $table = $args{table}; | |||||||||||||||||||
| 105 | 1484 | ▶ | die qq{No table exists corresponding to '$class'} unless $table; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 1484 | my $info = $tables->{$table}; | |||||||||||||||||||
| 107 | 1484 | ▶ | die qq{No information available for table '$table'} unless $info; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | my $self = { | ||||||||||||||||||||
| 109 | table => $table, | ||||||||||||||||||||
| 110 | columns => $info->{columns}, | ||||||||||||||||||||
| 111 | id => $info->{id}, | ||||||||||||||||||||
| 112 | name => $info->{name}, | ||||||||||||||||||||
| 113 | 1484 | }; | |||||||||||||||||||
| 114 | 1484 | foreach (qw(name has_a has_many)) { | |||||||||||||||||||
| 115 | 4452 | ▶ | next unless defined $info->{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 2968 | $self->{$_} = $info->{$_}; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | 1484 | return bless $self, $class; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | sub db_error { | ||||||||||||||||||||
| 122 | 0 | ▶ | my ($obj, $sth) = @_; | ||||||||||||||||||
|
Subroutine
db_error: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 123 | 0 | ▶ | return unless $dbh; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | ▶ | if ($sth) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | $sth->finish; | |||||||||||||||||||
| 126 | 0 | undef $sth; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 0 | return $obj->{error_msg} = q{Database error: } . $dbh->errstr; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 17 | 1; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 NAME | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | CPAN::SQLite::DBI - DBI information for the CPAN::SQLite database | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 VERSION | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | version 0.219 | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 DESCRIPTION | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | This module is used by L<CPAN::SQLite::Index> and | ||||||||||||||||||||
| 144 | L<CPAN::SQLite::Search> to set up some basic database | ||||||||||||||||||||
| 145 | information. It exports two variables: | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =over 3 | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =item C<$tables> | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | This is a hash reference whose keys are the table names, with | ||||||||||||||||||||
| 152 | corresponding values being hash references whose keys are the | ||||||||||||||||||||
| 153 | columns of the table and values being the associated data types. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =item C<$dbh> | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | This is a L<DBI> database handle used to connect to the | ||||||||||||||||||||
| 158 | database. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =back | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | The main method of this module is C<make>, which is used | ||||||||||||||||||||
| 163 | to make the tables of the database. | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head1 SEE ALSO | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | L<CPAN::SQLite::Index> and L<CPAN::SQLite::Search> | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||