2 # (optional) ARRL Dx Database handling
6 # Copyright (c) 2005 Dirk Koopman G1TLH
13 use vars qw($VERSION $BRANCH $dbh $dbname %tabledefs $error);
15 #main::mkver($VERSION = q$Revision$);
23 $dbname = "$main::root/data/arrldx.db";
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)',
35 my $class = ref $pkg || $pkg;
41 $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "");
44 Log('err', $DBI::errstr);
45 $error = $DBI::errstr;
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;
53 while (my @row = $sth->fetchrow_array) {
54 $f{$row[0]} = $row[1];
56 foreach my $t (sort keys %tabledefs) {
57 $dbh->do($tabledefs{$t}) unless exists $f{$t};
65 if (ref $args{file}) {
66 $self->{f} = $args{file};
68 $self->{f} = IO::File->new($args{file}) or $error = $!, return;
72 return bless $self, $class;