5 |
package index_DBI; |
package index_DBI; |
6 |
use strict qw(vars); |
use strict qw(vars); |
7 |
use vars qw($Count); |
use vars qw($Count); |
8 |
|
use HTML::Entities; |
9 |
|
|
10 |
use DBI; |
use DBI; |
11 |
|
|
31 |
|
|
32 |
my $field = shift; |
my $field = shift; |
33 |
|
|
34 |
|
#print "#### delete_and_create($field)\n"; |
35 |
|
|
36 |
$self->{dbh}->commit; |
$self->{dbh}->commit; |
|
$self->{dbh}->begin_work; |
|
37 |
|
|
38 |
my $sql = "select count(*) from $field"; |
my $sql = "select count(*) from $field"; |
39 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
40 |
# FIX: this is not a good way to check if table exists! |
# FIX: this is not a good way to check if table exists! |
41 |
if (1 || $sth->execute() && $sth->fetchrow_hashref) { |
if ($sth->execute() && $sth->fetchrow_hashref) { |
42 |
my $sql = "drop table $field"; |
my $sql = "drop table $field"; |
43 |
# my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
$self->{dbh}->begin_work; |
44 |
|
my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
45 |
|
$self->{dbh}->commit; |
46 |
} |
} |
47 |
$sql = "create table $field ( |
$sql = "create table $field ( |
48 |
item varchar(255), |
item varchar(255), |
49 |
ident varchar(255), |
ident varchar(255), |
50 |
count int, |
count int, |
51 |
|
ord int, |
52 |
primary key (item,ident) |
primary key (item,ident) |
53 |
)"; |
)"; |
|
# $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
|
|
$sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr(); |
|
54 |
|
|
55 |
|
$self->{dbh}->begin_work; |
56 |
|
$sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr(); |
57 |
$self->{dbh}->commit; |
$self->{dbh}->commit; |
58 |
|
|
59 |
$self->{dbh}->begin_work; |
$self->{dbh}->begin_work; |
60 |
} |
} |
61 |
|
|
63 |
my $self = shift; |
my $self = shift; |
64 |
|
|
65 |
my $field = shift; |
my $field = shift; |
66 |
my $index_data = shift; |
my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)"; |
67 |
my $ident = shift; # e.g. library id |
my $ident = shift || ''; # e.g. library id |
68 |
|
|
69 |
if (! $index_data) { |
if (! $index_data) { |
70 |
print STDERR "\$index->insert() -- no value to insert\n"; |
print STDERR "\$index->insert() -- no value to insert\n"; |
82 |
if (! $sth->fetchrow_hashref) { |
if (! $sth->fetchrow_hashref) { |
83 |
my $sql = "insert into $field (item,ident,count) values (?,?,?)"; |
my $sql = "insert into $field (item,ident,count) values (?,?,?)"; |
84 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
85 |
|
$index_data = substr($index_data,0,255); |
86 |
$sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr(); |
$sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr(); |
87 |
#print stderr "in index: $index_data\n"; |
#print stderr "in index: $index_data\n"; |
88 |
} else { |
} else { |
98 |
my $field = shift; |
my $field = shift; |
99 |
|
|
100 |
my $sql = "select count(*) from $field"; |
my $sql = "select count(*) from $field"; |
101 |
return $self->{dbh}->do($sql); |
|
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 |
} |
} |
109 |
|
|
110 |
|
|
115 |
my $what = shift || 'item'; # 'item,ident' |
my $what = shift || 'item'; # 'item,ident' |
116 |
my $where = shift; |
my $where = shift; |
117 |
|
|
118 |
|
my $from_ord = shift || 0; |
119 |
|
my $rows = shift || 10; |
120 |
|
|
121 |
my @sql_args; |
my @sql_args; |
122 |
|
|
123 |
my $sql = "select $what from $field"; |
my $sql = "select $what,ord from $field"; |
124 |
|
|
125 |
if ($where) { |
if ($where) { |
126 |
$sql .= " where upper(item) like upper(?)||'%'"; |
my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'"; |
127 |
push @sql_args,$where; |
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 |
|
$from_ord += $row->{ord} - 1; |
132 |
|
} |
133 |
} |
} |
134 |
$sql .= " order by item"; |
$sql .= " order by ord limit $rows offset $from_ord"; |
135 |
|
|
136 |
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr(); |
137 |
$sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr(); |
$sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr(); |
138 |
my @arr; |
my @arr; |
139 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
140 |
|
$row->{item} = HTML::Entities::encode($row->{item}); |
141 |
push @arr,$row; |
push @arr,$row; |
142 |
} |
} |
143 |
return @arr; |
return @arr; |
146 |
sub close { |
sub close { |
147 |
my $self = shift; |
my $self = shift; |
148 |
|
|
149 |
|
|
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 |
|
my $sql = "select oid from $table order by upper(item)"; |
158 |
|
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 |
if ($self->{dbh}) { |
if ($self->{dbh}) { |
172 |
|
|
173 |
|
# commit |
174 |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
175 |
|
|
176 |
|
foreach my $table (keys %Table) { |
177 |
|
# FIX |
178 |
|
print STDERR "creating ord for $table...\n"; |
179 |
|
create_ord($table); |
180 |
|
} |
181 |
|
|
182 |
$self->{dbh}->disconnect; |
$self->{dbh}->disconnect; |
183 |
undef $self->{dbh}; |
undef $self->{dbh}; |
184 |
} |
} |