| 1 | package Dezi::Stats::DBI; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use base 'Dezi::Stats'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use DBIx::Connector; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use DBIx::InsertHash; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | our $VERSION = '0.001006'; | |||||||||||
| 11 | |||||||||||||
| 12 | =head1 NAME | ||||||||||||
| 13 | |||||||||||||
| 14 | Dezi::Stats::DBI - store Dezi statistics in a database | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||
| 17 | |||||||||||||
| 18 | # see Dezi::Stats | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | Dezi::Stats::DBI logs statistics to any backend supported by DBI. | ||||||||||||
| 23 | This class uses DBIx::Connector to manage a persistent DBI connection. | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 METHODS | ||||||||||||
| 26 | |||||||||||||
| 27 | =head2 init_store() | ||||||||||||
| 28 | |||||||||||||
| 29 | Sets up the internal database handle (accessible via conn() attribute). | ||||||||||||
| 30 | |||||||||||||
| 31 | =cut | ||||||||||||
| 32 | |||||||||||||
| 33 | 1 | ▶ | sub init_store { | ||||||||||
|
Subroutine
init_store: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||
| 34 | 1 | my $self = shift; | |||||||||||
| 35 | 1 | ▶ | my $dsn = delete $self->{dsn} or croak "dsn required"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 1 | ▶ | my $username = delete $self->{username} or croak "username required"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 1 | ▶ | my $password = delete $self->{password} or croak "password required"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 1 | ▶ | $self->{table_name} ||= 'dezi_stats'; | ||||||||||
|
Condition50%1/2
$self->{"table_name"} ||= "dezi_stats"
MC/DC0%0/1
$self->{"table_name"} ||= "dezi_stats"
$self->{"table_name"}
Truth table50%1/2
A
A= $self->{"table_name"}
| |||||||||||||
| 39 | 1 | $self->{conn} = DBIx::Connector->new( | |||||||||||
| 40 | $dsn, | ||||||||||||
| 41 | $username, | ||||||||||||
| 42 | $password, | ||||||||||||
| 43 | { RaiseError => 1, | ||||||||||||
| 44 | AutoCommit => 1, | ||||||||||||
| 45 | } | ||||||||||||
| 46 | ); | ||||||||||||
| 47 | 1 | $self->{conn}->mode('fixup'); # ping only on failure | |||||||||||
| 48 | $self->{ih} = DBIx::InsertHash->new( | ||||||||||||
| 49 | table => $self->{table_name}, | ||||||||||||
| 50 | quote => $self->{quote}, | ||||||||||||
| 51 | quote_char => $self->{quote_char}, | ||||||||||||
| 52 | 1 | ); | |||||||||||
| 53 | 1 | return $self; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | =head2 conn | ||||||||||||
| 57 | |||||||||||||
| 58 | Returns the internal DBIx::Connector object. | ||||||||||||
| 59 | |||||||||||||
| 60 | =cut | ||||||||||||
| 61 | |||||||||||||
| 62 | 1 | ▶ | sub conn { | ||||||||||
|
Subroutine
conn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 63 | 1 | return shift->{conn}; | |||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | =head2 table_name | ||||||||||||
| 67 | |||||||||||||
| 68 | Returns the table_name. Default is C<dezi_stats>. | ||||||||||||
| 69 | |||||||||||||
| 70 | =cut | ||||||||||||
| 71 | |||||||||||||
| 72 | 0 | ▶ | sub table_name { | ||||||||||
|
Subroutine
table_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 73 | 0 | return shift->{table_name}; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | =head2 insert( I<hashref> ) | ||||||||||||
| 77 | |||||||||||||
| 78 | Writes I<hashref> to the database. | ||||||||||||
| 79 | |||||||||||||
| 80 | =cut | ||||||||||||
| 81 | |||||||||||||
| 82 | 1 | ▶ | sub insert { | ||||||||||
|
Subroutine
insert: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 83 | 1 | my $self = shift; | |||||||||||
| 84 | 1 | ▶ | my $row = shift or croak "hashref required"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | $self->{conn}->run( | ||||||||||||
| 86 | 1 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 87 | 1 | my $dbh = $_; # just for clarity | |||||||||||
| 88 | 1 | $self->{ih}->insert( $row, $self->{table_name}, $dbh ); | |||||||||||
| 89 | } | ||||||||||||
| 90 | 1 | ); | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | =head2 schema | ||||||||||||
| 94 | |||||||||||||
| 95 | Callable as a function or class method. Returns string suitable | ||||||||||||
| 96 | for initializing a B<dezi_stats> SQL table. | ||||||||||||
| 97 | |||||||||||||
| 98 | Example: | ||||||||||||
| 99 | |||||||||||||
| 100 | perl -e 'use Dezi::Stats::DBI; print Dezi::Stats::DBI::schema' | sqlite3 dezi.index/stats.db | ||||||||||||
| 101 | |||||||||||||
| 102 | You can use SQL::Translator to initialize a non-SQLite database: | ||||||||||||
| 103 | |||||||||||||
| 104 | my $dbh = DBI->connect($dsn, $user, $pass); | ||||||||||||
| 105 | my $sql = Dezi::Stats::DBI::schema(); | ||||||||||||
| 106 | my $translator = SQL::Translator->new( | ||||||||||||
| 107 | show_warnings => 1, | ||||||||||||
| 108 | validate => 1, | ||||||||||||
| 109 | quote_identifiers => 1, | ||||||||||||
| 110 | no_comments => 1, | ||||||||||||
| 111 | ); | ||||||||||||
| 112 | my $mysql = $translator->translate( | ||||||||||||
| 113 | from => 'SQLite', | ||||||||||||
| 114 | to => 'MySQL', | ||||||||||||
| 115 | datasource => \$sql | ||||||||||||
| 116 | ) or die $translator->error; | ||||||||||||
| 117 | |||||||||||||
| 118 | # Translator adds extra statements that do() can't handle. | ||||||||||||
| 119 | $mysql =~ s/^.*(CREATE TABLE .+?\));.*$/$1/s; | ||||||||||||
| 120 | |||||||||||||
| 121 | $dbh->do($mysql); | ||||||||||||
| 122 | |||||||||||||
| 123 | =cut | ||||||||||||
| 124 | |||||||||||||
| 125 | 1 | ▶ | sub schema { | ||||||||||
|
Subroutine
schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 126 | return <<EOF | ||||||||||||
| 127 | create table if not exists dezi_stats ( | ||||||||||||
| 128 | id integer primary key autoincrement, | ||||||||||||
| 129 | tstamp integer, | ||||||||||||
| 130 | q text, | ||||||||||||
| 131 | build_time float, | ||||||||||||
| 132 | search_time float, | ||||||||||||
| 133 | remote_user text, | ||||||||||||
| 134 | path varchar(255), | ||||||||||||
| 135 | total integer, | ||||||||||||
| 136 | s text, | ||||||||||||
| 137 | o integer, | ||||||||||||
| 138 | p integer, | ||||||||||||
| 139 | h integer, | ||||||||||||
| 140 | c integer, | ||||||||||||
| 141 | L text, | ||||||||||||
| 142 | f integer, | ||||||||||||
| 143 | r integer, | ||||||||||||
| 144 | t varchar(128), | ||||||||||||
| 145 | b varchar(32) | ||||||||||||
| 146 | ); | ||||||||||||
| 147 | EOF | ||||||||||||
| 148 | 1 | } | |||||||||||
| 149 | |||||||||||||
| 150 | 1 | 1; | |||||||||||
| 151 | |||||||||||||
| 152 | __END__ | ||||||||||||