| 1 | # $Id: /mirror/gungho/lib/Gungho/Plugin/Statistics/Storage/SQLite.pm 4238 2007-10-29T15:08:17.605700Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Plugin::Statistics::Storage::SQLite; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Base); | ||
| 10 | use DBI; | ||
| 11 | use File::Temp; | ||
| 12 | use Path::Class::Dir; | ||
| 13 | use Sys::Hostname (); | ||
| 14 | |||
| 15 | __PACKAGE__->mk_accessors($_) for qw(file _dbh); | ||
| 16 | |||
| 17 | sub setup | ||
| 18 | { | ||
| 19 | my $self = shift; | ||
| 20 | my $dir = Path::Class::Dir->new( ($self->config && $self->config->{storage}) || File::Spec->tmpdir); | ||
| 21 | my $file = $dir->file(sprintf("gungho-stats-%s-%s-%06d", Sys::Hostname::hostname(), $$, rand(100000))); | ||
| 22 | |||
| 23 | $self->file( $file ); | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | sub dbh | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | |||
| 31 | my $dbh = $self->_dbh; | ||
| 32 | if (! $dbh || ! $dbh->ping) { | ||
| 33 | my $file = $self->file; | ||
| 34 | |||
| 35 | $dbh = DBI->connect( | ||
| 36 | "dbi:SQLite:dbname=$file", | ||
| 37 | undef, | ||
| 38 | undef, | ||
| 39 | { RaiseError => 1, AutoCommit => 1 } | ||
| 40 | ); | ||
| 41 | |||
| 42 | $dbh->do(<<EOSQL); | ||
| 43 | CREATE TABLE counters ( | ||
| 44 | name TEXT PRIMARY KEY, | ||
| 45 | value INTEGER NOT NULL DEFAULT 0 | ||
| 46 | ); | ||
| 47 | EOSQL | ||
| 48 | foreach my $name qw(active_requests finished_requests) { | ||
| 49 | $dbh->do("INSERT INTO counters (name, value) VALUES (?, ?)", undef, $name, 0); | ||
| 50 | } | ||
| 51 | |||
| 52 | $self->_dbh( $dbh ); | ||
| 53 | } | ||
| 54 | return $dbh; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub incr | ||
| 58 | { | ||
| 59 | my ($self, $action) = @_; | ||
| 60 | |||
| 61 | my $dbh = $self->dbh(); | ||
| 62 | my $sth = $dbh->prepare_cached("UPDATE counters SET value = value + 1 WHERE name = ?"); | ||
| 63 | $sth->execute($action); | ||
| 64 | $sth->finish(); | ||
| 65 | } | ||
| 66 | |||
| 67 | #sub _dump | ||
| 68 | #{ | ||
| 69 | # my ($self, $action) = @_; | ||
| 70 | # | ||
| 71 | # my $dbh = $self->dbh(); | ||
| 72 | # my $sth = $dbh->prepare("SELECT value FROM counters"); | ||
| 73 | # $sth->execute(); | ||
| 74 | # use Data::Dump; | ||
| 75 | # while (my $h = $sth->fetchrow_hashref) { | ||
| 76 | # print STDERR Data::Dump::dump($h); | ||
| 77 | # } | ||
| 78 | #} | ||
| 79 | |||
| 80 | sub decr | ||
| 81 | { | ||
| 82 | my ($self, $action) = @_; | ||
| 83 | |||
| 84 | my $dbh = $self->dbh(); | ||
| 85 | my $sth = $dbh->prepare_cached("UPDATE counters SET value = value - 1 WHERE name = ?"); | ||
| 86 | $sth->execute($action); | ||
| 87 | $sth->finish(); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub get | ||
| 91 | { | ||
| 92 | my ($self, $action) = @_; | ||
| 93 | |||
| 94 | my $dbh = $self->dbh(); | ||
| 95 | my $sth = $dbh->prepare_cached("SELECT value FROM counters WHERE name = ?"); | ||
| 96 | $sth->execute($action); | ||
| 97 | my ($value) = $sth->fetchrow_array; | ||
| 98 | $sth->finish; | ||
| 99 | return $value; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub DESTROY | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | $self->file->remove if $self->file; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | __END__ |