/[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

Contents of /trunk/index_DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations)
Sat Mar 22 23:40:14 2003 UTC (16 years, 6 months ago) by dpavlin
File size: 5132 byte(s)
speedups using profiling

1 #
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 use HTML::Entities;
9
10 use DBI;
11
12 my %Table; # index tables which where visited in this run
13 my %sth_cache; # cache prepared statements
14
15 sub new {
16 my $class = shift;
17 my $self = {};
18 bless($self, $class);
19
20 # FIX: config params
21 $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr;
22 # 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 #print "#### delete_and_create($field)\n";
36
37 $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 if ($sth->execute() && $sth->fetchrow_hashref) {
43 my $sql = "drop table $field";
44 $self->{dbh}->begin_work;
45 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
46 $self->{dbh}->commit;
47 }
48 $sql = "create table $field (
49 item varchar(255),
50 ident varchar(255),
51 count int,
52 ord int,
53 primary key (item,ident)
54 )";
55
56 $self->{dbh}->begin_work;
57 $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
58 $self->{dbh}->commit;
59
60 $self->{dbh}->begin_work;
61 }
62
63 sub insert {
64 my $self = shift;
65
66 my $field = shift;
67 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
68 my $ident = shift || ''; # e.g. library id
69
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
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 }
87 $Table{$field}++;
88
89 $sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
90 if (! $sth_cache{$field."select"}->fetchrow_hashref) {
91 $index_data = substr($index_data,0,255);
92 $sth_cache{$field."insert"}->execute($index_data,$ident,1) || die "cache: $field insert; ".$self->{dbh}->errstr();
93 #print stderr "in index: $index_data\n";
94 } else {
95 $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();
96 }
97 }
98
99 sub check {
100 my $self = shift;
101
102 my $field = shift;
103
104 my $sql = "select count(*) from $field";
105
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 }
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 my $from_ord = shift || 0;
123 my $rows = shift || 10;
124
125 my @sql_args;
126
127 my $sql = "select $what,ord from $field";
128
129 if ($where) {
130 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 $from_ord += $row->{ord} - 1;
136 }
137 }
138 $sql .= " order by ord limit $rows offset $from_ord";
139
140 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
141 $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
142 my @arr;
143 while (my $row = $sth->fetchrow_hashref) {
144 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
145 push @arr,$row;
146 }
147 return @arr;
148 }
149
150 sub close {
151 my $self = shift;
152
153
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 my $sql = "select oid from $table order by upper(item)";
162 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 if ($self->{dbh}) {
176
177 # commit
178 $self->{dbh}->commit || die $self->{dbh}->errstr();
179
180 foreach my $table (keys %Table) {
181 # FIX
182 print STDERR "creating ord for $table...\n";
183 create_ord($table);
184 }
185
186 $self->{dbh}->disconnect;
187 undef $self->{dbh};
188 }
189 }
190
191 END {
192 $Count--;
193 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
194 # FIX: debug output
195 # print STDERR "usage\ttable\n";
196 # foreach (keys %Table) {
197 # print STDERR $Table{$_},"\t$_\n";
198 # }
199 }
200
201 1;

Properties

Name Value
cvs2svn:cvs-rev 1.9

  ViewVC Help
Powered by ViewVC 1.1.26