10 |
use DBI; |
use DBI; |
11 |
|
|
12 |
my %Table; # index tables which where visited in this run |
my %Table; # index tables which where visited in this run |
13 |
|
my %sth_cache; # cache prepared statements |
14 |
|
|
15 |
sub new { |
sub new { |
16 |
my $class = shift; |
my $class = shift; |
32 |
|
|
33 |
my $field = shift; |
my $field = shift; |
34 |
|
|
35 |
|
#print "#### delete_and_create($field)\n"; |
36 |
|
|
37 |
$self->{dbh}->commit; |
$self->{dbh}->commit; |
|
$self->{dbh}->begin_work; |
|
38 |
|
|
39 |
my $sql = "select count(*) from $field"; |
my $sql = "select count(*) from $field"; |
40 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
41 |
# FIX: this is not a good way to check if table exists! |
# FIX: this is not a good way to check if table exists! |
42 |
if (1 || $sth->execute() && $sth->fetchrow_hashref) { |
if ($sth->execute() && $sth->fetchrow_hashref) { |
43 |
my $sql = "drop table $field"; |
my $sql = "drop table $field"; |
44 |
# my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
$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 ( |
$sql = "create table $field ( |
49 |
item varchar(255), |
item varchar(255), |
52 |
ord int, |
ord int, |
53 |
primary key (item,ident) |
primary key (item,ident) |
54 |
)"; |
)"; |
|
# $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
|
|
$sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr(); |
|
55 |
|
|
56 |
|
$self->{dbh}->begin_work; |
57 |
|
$sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr(); |
58 |
$self->{dbh}->commit; |
$self->{dbh}->commit; |
59 |
|
|
60 |
$self->{dbh}->begin_work; |
$self->{dbh}->begin_work; |
61 |
} |
} |
62 |
|
|
64 |
my $self = shift; |
my $self = shift; |
65 |
|
|
66 |
my $field = shift; |
my $field = shift; |
67 |
my $index_data = shift || return; |
my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)"; |
68 |
my $ident = shift || return; # e.g. library id |
my $ident = shift || ''; # e.g. library id |
69 |
|
|
70 |
if (! $index_data) { |
if (! $index_data) { |
71 |
print STDERR "\$index->insert() -- no value to insert\n"; |
print STDERR "\$index->insert() -- no value to insert\n"; |
74 |
|
|
75 |
if (! $Table{$field}) { |
if (! $Table{$field}) { |
76 |
$self->delete_and_create($field); |
$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}++; |
$Table{$field}++; |
88 |
|
|
89 |
my $sql = "select item from $field where upper(item)=upper(?)"; |
$sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr(); |
90 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
if (! $sth_cache{$field."select"}->fetchrow_hashref) { |
91 |
$sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr(); |
$index_data = substr($index_data,0,255); |
92 |
if (! $sth->fetchrow_hashref) { |
$sth_cache{$field."insert"}->execute($index_data,$ident,1) || die "cache: $field insert; ".$self->{dbh}->errstr(); |
|
my $sql = "insert into $field (item,ident,count) values (?,?,?)"; |
|
|
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
|
|
$sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr(); |
|
93 |
#print stderr "in index: $index_data\n"; |
#print stderr "in index: $index_data\n"; |
94 |
} else { |
} else { |
95 |
my $sql = "update $field set count = count + 1 where item = ? and ident = ?"; |
$sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr(); |
|
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
|
|
$sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr(); |
|
96 |
} |
} |
97 |
} |
} |
98 |
|
|
141 |
$sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr(); |
$sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr(); |
142 |
my @arr; |
my @arr; |
143 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
144 |
$row->{item} = HTML::Entities::encode($row->{item}); |
$row->{item} = HTML::Entities::encode($row->{item},'<>&"'); |
145 |
push @arr,$row; |
push @arr,$row; |
146 |
} |
} |
147 |
return @arr; |
return @arr; |
158 |
|
|
159 |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
160 |
|
|
161 |
my $sql = "select oid from $table order by item"; |
my $sql = "select oid from $table order by upper(item)"; |
162 |
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
163 |
$sql = "update $table set ord=? where oid=?"; |
$sql = "update $table set ord=? where oid=?"; |
164 |
my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |