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