/[webpac]/branches/hidra/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 /branches/hidra/index_DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (hide annotations)
Sat Mar 22 23:40:14 2003 UTC (21 years ago) by dpavlin
Original Path: trunk/index_DBI.pm
File size: 5132 byte(s)
speedups using profiling

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 dpavlin 12 use HTML::Entities;
9 dpavlin 10
10     use DBI;
11    
12     my %Table; # index tables which where visited in this run
13 dpavlin 45 my %sth_cache; # cache prepared statements
14 dpavlin 10
15     sub new {
16     my $class = shift;
17     my $self = {};
18     bless($self, $class);
19    
20     # FIX: config params
21 dpavlin 11 $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr;
22 dpavlin 10 # begin transaction
23     $self->{dbh}->begin_work || die $self->{dbh}->errstr();
24    
25     $Count++;
26    
27     return $self;
28     }
29    
30     sub delete_and_create {
31     my $self = shift;
32    
33     my $field = shift;
34    
35 dpavlin 19 #print "#### delete_and_create($field)\n";
36    
37 dpavlin 10 $self->{dbh}->commit;
38    
39     my $sql = "select count(*) from $field";
40     my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
41     # FIX: this is not a good way to check if table exists!
42 dpavlin 18 if ($sth->execute() && $sth->fetchrow_hashref) {
43 dpavlin 10 my $sql = "drop table $field";
44 dpavlin 19 $self->{dbh}->begin_work;
45 dpavlin 18 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
46 dpavlin 19 $self->{dbh}->commit;
47 dpavlin 10 }
48     $sql = "create table $field (
49     item varchar(255),
50     ident varchar(255),
51     count int,
52 dpavlin 12 ord int,
53 dpavlin 10 primary key (item,ident)
54     )";
55 dpavlin 19
56     $self->{dbh}->begin_work;
57 dpavlin 18 $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
58 dpavlin 19 $self->{dbh}->commit;
59 dpavlin 10
60     $self->{dbh}->begin_work;
61     }
62    
63     sub insert {
64     my $self = shift;
65    
66     my $field = shift;
67 dpavlin 18 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
68     my $ident = shift || ''; # e.g. library id
69 dpavlin 10
70     if (! $index_data) {
71     print STDERR "\$index->insert() -- no value to insert\n";
72     return;
73     }
74    
75     if (! $Table{$field}) {
76     $self->delete_and_create($field);
77 dpavlin 45
78     my $sql = "select item from $field where upper(item)=upper(?)";
79     $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
80    
81     $sql = "insert into $field (item,ident,count) values (?,?,?)";
82     $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
83    
84     $sql = "update $field set count = count + 1 where item = ? and ident = ?";
85     $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
86 dpavlin 10 }
87     $Table{$field}++;
88    
89 dpavlin 45 $sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
90     if (! $sth_cache{$field."select"}->fetchrow_hashref) {
91 dpavlin 28 $index_data = substr($index_data,0,255);
92 dpavlin 45 $sth_cache{$field."insert"}->execute($index_data,$ident,1) || die "cache: $field insert; ".$self->{dbh}->errstr();
93 dpavlin 11 #print stderr "in index: $index_data\n";
94 dpavlin 10 } else {
95 dpavlin 45 $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();
96 dpavlin 10 }
97     }
98    
99 dpavlin 11 sub check {
100     my $self = shift;
101    
102     my $field = shift;
103    
104     my $sql = "select count(*) from $field";
105 dpavlin 12
106     my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
107     $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
108    
109     my ($total) = $sth->fetchrow_array();
110    
111     return $total;
112 dpavlin 11 }
113    
114    
115     sub fetch {
116     my $self = shift;
117    
118     my $field = shift;
119     my $what = shift || 'item'; # 'item,ident'
120     my $where = shift;
121    
122 dpavlin 12 my $from_ord = shift || 0;
123     my $rows = shift || 10;
124    
125 dpavlin 11 my @sql_args;
126    
127 dpavlin 13 my $sql = "select $what,ord from $field";
128 dpavlin 12
129 dpavlin 11 if ($where) {
130 dpavlin 12 my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
131     my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
132    
133     $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
134     if (my $row = $sth->fetchrow_hashref) {
135 dpavlin 13 $from_ord += $row->{ord} - 1;
136 dpavlin 12 }
137 dpavlin 11 }
138 dpavlin 12 $sql .= " order by ord limit $rows offset $from_ord";
139 dpavlin 11
140 dpavlin 12 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
141     $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
142 dpavlin 11 my @arr;
143     while (my $row = $sth->fetchrow_hashref) {
144 dpavlin 37 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
145 dpavlin 11 push @arr,$row;
146     }
147     return @arr;
148     }
149    
150 dpavlin 10 sub close {
151     my $self = shift;
152    
153 dpavlin 12
154     # re-create ord column (sorted order) in table
155     sub create_ord {
156    
157     my $table = shift;
158    
159     $self->{dbh}->begin_work || die $self->{dbh}->errstr();
160    
161 dpavlin 28 my $sql = "select oid from $table order by upper(item)";
162 dpavlin 12 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
163     $sql = "update $table set ord=? where oid=?";
164     my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
165     $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
166     my $ord = 1;
167     while (my $row = $sth->fetchrow_hashref) {
168     $sth_update->execute($ord++,$row->{oid});
169     }
170    
171     $self->{dbh}->commit || die $self->{dbh}->errstr();
172     }
173     #--- end of sub
174    
175 dpavlin 10 if ($self->{dbh}) {
176 dpavlin 12
177     # commit
178 dpavlin 10 $self->{dbh}->commit || die $self->{dbh}->errstr();
179 dpavlin 12
180     foreach my $table (keys %Table) {
181     # FIX
182     print STDERR "creating ord for $table...\n";
183     create_ord($table);
184     }
185    
186 dpavlin 10 $self->{dbh}->disconnect;
187     undef $self->{dbh};
188     }
189     }
190    
191     END {
192     $Count--;
193 dpavlin 11 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
194 dpavlin 10 # FIX: debug output
195 dpavlin 11 # print STDERR "usage\ttable\n";
196     # foreach (keys %Table) {
197     # print STDERR $Table{$_},"\t$_\n";
198     # }
199 dpavlin 10 }
200    
201     1;

Properties

Name Value
cvs2svn:cvs-rev 1.9

  ViewVC Help
Powered by ViewVC 1.1.26