]> gitweb @ CieloNegro.org - pci-nopaste.git/blob - lib/POE/Component/IRC/Plugin/NoPaste/DB.pm
migrate from GNU arch to Git
[pci-nopaste.git] / lib / POE / Component / IRC / Plugin / NoPaste / DB.pm
1 # -*- cperl -*-
2 package POE::Component::IRC::Plugin::NoPaste::DB;
3 use strict;
4 use warnings;
5 use DBI;
6 use Hash::Util qw(lock_keys);
7
8 sub new {
9     my ($class, $config) = @_;
10     my $this = bless {} => $class;
11
12     $this->{config} = $config;
13     $this->{dbh} = DBI->connect(
14         "dbi:SQLite:dbname=$config->{db}{file}", "", "", {
15             RaiseError => 1,
16             AutoCommit => 1,
17         });
18
19     lock_keys %$this;
20
21     $this->setup_db;
22     $this;
23 }
24
25 sub dbh {
26     my $this = shift;
27
28     $this->{dbh};
29 }
30
31 sub setup_db {
32     my $this = shift;
33
34     eval {
35         $this->{dbh}->do(q{
36             SELECT *
37               FROM post
38              LIMIT 0
39         });
40     };
41     if ($@) {
42         $this->{dbh}->do(q{
43 CREATE TABLE post (
44     post_id     INTEGER PRIMARY KEY,   -- unique id of the post
45     channel_id  VARCHAR(512) NOT NULL, -- id of the channel in ascii
46
47     posted_time INTEGER NOT NULL,      -- timestamp in epoch
48     nick        VARCHAR(512) NOT NULL, -- nick of the person who posted this
49     title       BLOB NOT NULL,         -- title of the post
50     body        BLOB NOT NULL          -- body of the post
51 );
52 });
53     }
54 }
55
56 1;