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 |
|
|
45 |
item varchar(255), |
item varchar(255), |
46 |
ident varchar(255), |
ident varchar(255), |
47 |
count int, |
count int, |
48 |
|
ord int, |
49 |
primary key (item,ident) |
primary key (item,ident) |
50 |
)"; |
)"; |
51 |
# $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
# $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr(); |
59 |
my $self = shift; |
my $self = shift; |
60 |
|
|
61 |
my $field = shift; |
my $field = shift; |
62 |
my $index_data = shift; |
my $index_data = shift || return; |
63 |
my $ident = shift; # e.g. library id |
my $ident = shift || return; # e.g. library id |
64 |
|
|
65 |
if (! $index_data) { |
if (! $index_data) { |
66 |
print STDERR "\$index->insert() -- no value to insert\n"; |
print STDERR "\$index->insert() -- no value to insert\n"; |
93 |
my $field = shift; |
my $field = shift; |
94 |
|
|
95 |
my $sql = "select count(*) from $field"; |
my $sql = "select count(*) from $field"; |
96 |
return $self->{dbh}->do($sql); |
|
97 |
|
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
98 |
|
$sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr(); |
99 |
|
|
100 |
|
my ($total) = $sth->fetchrow_array(); |
101 |
|
|
102 |
|
return $total; |
103 |
} |
} |
104 |
|
|
105 |
|
|
110 |
my $what = shift || 'item'; # 'item,ident' |
my $what = shift || 'item'; # 'item,ident' |
111 |
my $where = shift; |
my $where = shift; |
112 |
|
|
113 |
|
my $from_ord = shift || 0; |
114 |
|
my $rows = shift || 10; |
115 |
|
|
116 |
my @sql_args; |
my @sql_args; |
117 |
|
|
118 |
my $sql = "select $what from $field"; |
my $sql = "select $what,ord from $field"; |
119 |
|
|
120 |
if ($where) { |
if ($where) { |
121 |
$sql .= " where upper(item) like upper(?)||'%'"; |
my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'"; |
122 |
push @sql_args,$where; |
my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
123 |
|
|
124 |
|
$sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
125 |
|
if (my $row = $sth->fetchrow_hashref) { |
126 |
|
$from_ord += $row->{ord} - 1; |
127 |
|
} |
128 |
} |
} |
129 |
$sql .= " order by item"; |
$sql .= " order by ord limit $rows offset $from_ord"; |
130 |
|
|
131 |
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr(); |
132 |
$sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr(); |
$sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr(); |
133 |
my @arr; |
my @arr; |
134 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
135 |
|
$row->{item} = HTML::Entities::encode($row->{item}); |
136 |
push @arr,$row; |
push @arr,$row; |
137 |
} |
} |
138 |
return @arr; |
return @arr; |
141 |
sub close { |
sub close { |
142 |
my $self = shift; |
my $self = shift; |
143 |
|
|
144 |
|
|
145 |
|
# re-create ord column (sorted order) in table |
146 |
|
sub create_ord { |
147 |
|
|
148 |
|
my $table = shift; |
149 |
|
|
150 |
|
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
151 |
|
|
152 |
|
my $sql = "select oid from $table order by item"; |
153 |
|
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
154 |
|
$sql = "update $table set ord=? where oid=?"; |
155 |
|
my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
156 |
|
$sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr(); |
157 |
|
my $ord = 1; |
158 |
|
while (my $row = $sth->fetchrow_hashref) { |
159 |
|
$sth_update->execute($ord++,$row->{oid}); |
160 |
|
} |
161 |
|
|
162 |
|
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
163 |
|
} |
164 |
|
#--- end of sub |
165 |
|
|
166 |
if ($self->{dbh}) { |
if ($self->{dbh}) { |
167 |
|
|
168 |
|
# commit |
169 |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
170 |
|
|
171 |
|
foreach my $table (keys %Table) { |
172 |
|
# FIX |
173 |
|
print STDERR "creating ord for $table...\n"; |
174 |
|
create_ord($table); |
175 |
|
} |
176 |
|
|
177 |
$self->{dbh}->disconnect; |
$self->{dbh}->disconnect; |
178 |
undef $self->{dbh}; |
undef $self->{dbh}; |
179 |
} |
} |