| 1 | package CPAN::Digger::DB; | ||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 5 | our $VERSION = '1.03'; | |||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 5 | ▶ | use File::HomeDir (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 5 | ▶ | use FindBin (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Path::Tiny qw(path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 5 | our @EXPORT_OK = qw(get_fields); | |||||||||
| 14 | |||||||||||
| 15 | 5 | my @fields = qw(distribution version author date vcs_url vcs_name travis github_actions appveyor circleci has_ci license issues azure_pipelines); | |||||||||
| 16 | sub get_fields { | ||||||||||
| 17 | 5 | ▶ | return @fields; | ||||||||
|
Subroutine
get_fields: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | sub new { | ||||||||||
| 22 | 5 | ▶ | my ($class, %args) = @_; | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 23 | |||||||||||
| 24 | 5 | my $self = bless {}, $class; | |||||||||
| 25 | 5 | for my $key (keys %args) { | |||||||||
| 26 | 5 | $self->{$key} = $args{$key}; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 5 | my $dbh = $self->{dbh} = $self->get_db(); | |||||||||
| 30 | 5 | $self->{sth_get_distro} = $dbh->prepare('SELECT * FROM dists WHERE distribution=?'); | |||||||||
| 31 | 5 | $self->{sth_get_every_distro} = $dbh->prepare('SELECT * FROM dists ORDER by date DESC'); | |||||||||
| 32 | 5 | my $fields = join ', ', @fields; | |||||||||
| 33 | 5 | my $places = join ', ', ('?') x scalar @fields; | |||||||||
| 34 | 5 | $self->{sth_insert} = $dbh->prepare("INSERT INTO dists ($fields) VALUES ($places)"); | |||||||||
| 35 | 5 | $self->{sth_delete} = $dbh->prepare("DELETE FROM dists WHERE distribution=?"); | |||||||||
| 36 | 5 | return $self; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | sub get_db { | ||||||||||
| 41 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
get_db: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 42 | |||||||||||
| 43 | # Default to in-memory database | ||||||||||
| 44 | 5 | my $db_file = ':memory:'; | |||||||||
| 45 | 5 | my $exists = undef; | |||||||||
| 46 | |||||||||||
| 47 | 5 | ▶ | if ($self->{db}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 2 | $db_file = $self->{db}; | |||||||||
| 49 | 2 | $exists = -e $db_file; | |||||||||
| 50 | } | ||||||||||
| 51 | 5 | my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "", { | |||||||||
| 52 | PrintError => 0, | ||||||||||
| 53 | RaiseError => 1, | ||||||||||
| 54 | AutoCommit => 1, | ||||||||||
| 55 | FetchHashKeyName => 'NAME_lc', | ||||||||||
| 56 | }); | ||||||||||
| 57 | 5 | ▶ | if (not $exists) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 4 | local $/ = undef; | |||||||||
| 59 | 4 | my $schema = <DATA>; | |||||||||
| 60 | 4 | $dbh->do($schema); | |||||||||
| 61 | } | ||||||||||
| 62 | 5 | return $dbh | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | sub db_insert_into { | ||||||||||
| 66 | 76 | ▶ | my ($self, @params) = @_; | ||||||||
|
Subroutine
db_insert_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 67 | 76 | $self->{sth_insert}->execute(@params); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # TODO have an update here? | ||||||||||
| 71 | sub db_update { | ||||||||||
| 72 | 0 | ▶ | my ($self, $distribution, @params) = @_; | ||||||||
|
Subroutine
db_update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 73 | 0 | $self->{sth_delete}->execute($distribution); | |||||||||
| 74 | 0 | $self->{sth_insert}->execute(@params); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | sub db_get_distro { | ||||||||||
| 79 | 112 | ▶ | my ($self, $distribution) = @_; | ||||||||
|
Subroutine
db_get_distro: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 80 | |||||||||||
| 81 | 112 | $self->{sth_get_distro}->execute($distribution); | |||||||||
| 82 | 112 | my $row = $self->{sth_get_distro}->fetchrow_hashref; | |||||||||
| 83 | 112 | return $row; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | sub db_get_every_distro { | ||||||||||
| 87 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
db_get_every_distro: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 88 | |||||||||||
| 89 | 1 | $self->{sth_get_every_distro}->execute; | |||||||||
| 90 | 1 | my @distros; | |||||||||
| 91 | 1 | ▶ | while (my $row = $self->{sth_get_every_distro}->fetchrow_hashref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 2 | push @distros, $row; | |||||||||
| 93 | } | ||||||||||
| 94 | 1 | return \@distros; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 5 | 42; | |||||||||
| 98 | |||||||||||
| 99 | __DATA__ | ||||||||||
| 100 | CREATE TABLE dists ( | ||||||||||
| 101 | distribution VARCHAR(255) NOT NULL UNIQUE, | ||||||||||
| 102 | date VARCHAR(255), | ||||||||||
| 103 | version VARCHAR(255), | ||||||||||
| 104 | author VARCHAR(255), | ||||||||||
| 105 | license VARCHAR(255), | ||||||||||
| 106 | issues VARCHAR(255), | ||||||||||
| 107 | vcs_url VARCHAR(255), | ||||||||||
| 108 | vcs_name VARCHAR(255), | ||||||||||
| 109 | appveyor BOOLEAN, | ||||||||||
| 110 | circleci BOOLEAN, | ||||||||||
| 111 | travis BOOLEAN, | ||||||||||
| 112 | github_actions BOOLEAN, | ||||||||||
| 113 | azure_pipelines BOOLEAN, | ||||||||||
| 114 | has_ci BOOLEAN | ||||||||||
| 115 | ); | ||||||||||