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