add the beginnings of an ARRL log query and updater
[spider.git] / perl / ARRL / DX.pm
1 #
2 # (optional) ARRL Dx Database handling
3 #
4 # $Id$
5 #
6 # Copyright (c) 2005 Dirk Koopman G1TLH
7 #
8
9 use strict;
10
11 package ARRL::DX;
12
13 use vars qw($VERSION $BRANCH $dbh $dbname %tabledefs $error);
14
15 #main::mkver($VERSION = q$Revision$);
16
17 use DXLog;
18 use DXDebug;
19 use DXUtil;
20 use DBI;
21 use IO::File;
22
23 $dbname = "$main::root/data/arrldx.db";
24 %tabledefs = (
25                           paragraph => 'CREATE TABLE paragraph(p text, t int)',
26                           paragraph_t_idx => 'CREATE INDEX paragraph_t_idx ON paragraph(t DESC)',
27                           refer => 'CREATE TABLE refer(r text, id int, t int, pos int)',
28                           refer_id_idx => 'CREATE INDEX refer_id_idx ON refer(id)',
29                           refer_t_idx => 'CREATE INDEX refer_t_idx ON refer(t DESC)',
30                          );
31
32 sub new
33 {
34         my $pkg = shift;
35         my $class = ref $pkg || $pkg;
36         my %args = $@;
37         
38         $error = undef;
39         
40         unless ($dbh) {
41                 $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "");
42                 unless ($dbh) {
43                         dbg($DBI::errstr);
44                         Log('err', $DBI::errstr);
45                         $error = $DBI::errstr;
46                         return;
47                 }
48                 
49                 # check that all the tables are present and correct
50                 my $sth = $dbh->prepare("select name,type from sqlite_master") or $error = $DBI::errstr, return;
51                 $sth->execute or $error = $DBI::errstr, return;
52                 my %f;
53                 while (my @row = $sth->fetchrow_array) {
54                         $f{$row[0]} = $row[1];
55                 }
56                 foreach my $t (sort keys %tabledefs) {
57                         $dbh->do($tabledefs{$t}) unless exists $f{$t};
58                 }
59                 $sth->finish;
60         }
61
62         my $self = {};
63         
64         if ($args{file}) {
65                 if (ref $args{file}) {
66                         $self->{f} = $args{file};
67                 } else {
68                         $self->{f} = IO::File->new($args{file}) or $error = $!, return;
69                 }
70         } 
71         
72         return bless $self, $class; 
73 }
74
75 sub process
76 {
77         my $self = shift;
78         
79 }
80
81 sub insert
82 {
83         my $self = shift;
84         
85 }
86 1;