/[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 37 - (hide annotations)
Sat Mar 15 19:37:07 2003 UTC (21 years ago) by dpavlin
File size: 4905 byte(s)
escape only entities which are dangerous to html

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

Properties

Name Value
cvs2svn:cvs-rev 1.8

  ViewVC Help
Powered by ViewVC 1.1.26