/[webpac]/trunk/index_DBI.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/index_DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations)
Thu Jan 16 17:35:54 2003 UTC (16 years, 11 months ago) by dpavlin
File size: 2717 byte(s)
bunch of changes: make design more modular, implement index (partial
implementation) and other small and big changes

1 dpavlin 10 #
2     # this file implements index functions using DBI
3     #
4    
5     package index_DBI;
6     use strict qw(vars);
7     use vars qw($Count);
8    
9     use DBI;
10    
11     my %Table; # index tables which where visited in this run
12    
13     sub new {
14     my $class = shift;
15     my $self = {};
16     bless($self, $class);
17    
18     # FIX: config params
19     $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","","") || die $DBI::errstr;
20     # begin transaction
21     $self->{dbh}->begin_work || die $self->{dbh}->errstr();
22    
23     $Count++;
24    
25     return $self;
26     }
27    
28     sub delete_and_create {
29     my $self = shift;
30    
31     my $field = shift;
32    
33     $self->{dbh}->commit;
34     $self->{dbh}->begin_work;
35    
36     my $sql = "select count(*) from $field";
37     my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
38     # FIX: this is not a good way to check if table exists!
39     if (1 || $sth->execute() && $sth->fetchrow_hashref) {
40     my $sql = "drop table $field";
41     # my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
42     }
43     $sql = "create table $field (
44     item varchar(255),
45     ident varchar(255),
46     count int,
47     primary key (item,ident)
48     )";
49     # $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
50     $sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr();
51    
52     $self->{dbh}->commit;
53     $self->{dbh}->begin_work;
54     }
55    
56     sub insert {
57     my $self = shift;
58    
59     my $field = shift;
60     my $index_data = shift;
61     my $ident = shift; # e.g. Library ID
62    
63     if (! $index_data) {
64     print STDERR "\$index->insert() -- no value to insert\n";
65     return;
66     }
67    
68     if (! $Table{$field}) {
69     $self->delete_and_create($field);
70     }
71     $Table{$field}++;
72    
73     my $sql = "select item from $field where upper(item)=upper(?)";
74     my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
75     $sth->execute($index_data) || die "SQL: $sql; ".$self->{dbh}->errstr();
76     if (! $sth->fetchrow_hashref) {
77     my $sql = "insert into $field (item,ident,count) values (?,?,?)";
78     my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
79     $sth->execute($index_data,$ident,1) || die "SQL: $sql; ".$self->{dbh}->errstr();
80     #print STDERR "in index: $index_data\n";
81     } else {
82     my $sql = "update $field set count = count + 1 where item = ? and ident = ?";
83     my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
84     $sth->execute($index_data,$ident) || die "SQL: $sql; ".$self->{dbh}->errstr();
85     }
86     }
87    
88     sub close {
89     my $self = shift;
90    
91     if ($self->{dbh}) {
92     $self->{dbh}->commit || die $self->{dbh}->errstr();
93     $self->{dbh}->disconnect;
94     undef $self->{dbh};
95     }
96     }
97    
98     END {
99     $Count--;
100     print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count != 0);
101     # FIX: debug output
102     print STDERR "usage\ttable\n";
103     foreach (keys %Table) {
104     print STDERR $Table{$_},"\t$_\n";
105     }
106     }
107    
108     1;

Properties

Name Value
cvs2svn:cvs-rev 1.1

  ViewVC Help
Powered by ViewVC 1.1.26