11 |
use vars qw($Count); |
use vars qw($Count); |
12 |
use HTML::Entities; |
use HTML::Entities; |
13 |
use URI::Escape; |
use URI::Escape; |
|
use locale; |
|
14 |
use Carp; |
use Carp; |
|
|
|
15 |
use DBI; |
use DBI; |
16 |
|
use locale; |
17 |
|
|
18 |
# bench time |
# bench time |
19 |
my $bench_time = time(); |
my $bench_time = time(); |
59 |
sub delete_and_create { |
sub delete_and_create { |
60 |
my $self = shift; |
my $self = shift; |
61 |
|
|
62 |
my $index = shift || croak "need index name!"; |
my $table = shift || croak "need table name!"; |
63 |
my $sql = shift || croak "need sql to create table!"; |
my $sql = shift || croak "need sql to create table!"; |
64 |
|
|
65 |
print STDERR "## delete_and_create($index)\n" if ($debug); |
print STDERR "## delete_and_create($table)\n" if ($debug); |
66 |
|
|
67 |
my $sql_delete = "delete from $index"; |
my $sql_delete = "delete from $table"; |
68 |
my $sth = $self->{dbh}->prepare($sql_delete) || confess "can't prepare: $sql_delete"; |
my $sth = $self->{dbh}->prepare($sql_delete); |
69 |
|
|
70 |
if ($sth->execute()) { |
if ($sth && $sth->execute()) { |
71 |
print STDERR "## deleted rows from table $index\n" if ($debug); |
print STDERR "## deleted rows from table $table\n" if ($debug); |
72 |
} else { |
} else { |
73 |
# can't delete from table, assume it doesn't exists! |
# can't delete from table, assume it doesn't exists! |
74 |
$self->{dbh}->rollback; |
$self->{dbh}->rollback; |
75 |
$self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr(); |
$self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr(); |
76 |
print STDERR "## creating table $index\n" if ($debug); |
print STDERR "## creating table $table\n" if ($debug); |
77 |
$self->{dbh}->begin_work; |
$self->{dbh}->begin_work; |
78 |
} |
} |
79 |
} |
} |
102 |
|
|
103 |
if (! $self->{c}->{$uc}->{$field}) { |
if (! $self->{c}->{$uc}->{$field}) { |
104 |
#print stderr "in index: $index_data\n"; |
#print stderr "in index: $index_data\n"; |
105 |
$self->{c}->{$uc}->{$field}->{item} = $index_data; |
$self->{c}->{$uc}->{$field}->{item} = lc($index_data); |
106 |
$self->{c}->{$uc}->{$field}->{display} = $display; |
$self->{c}->{$uc}->{$field}->{display} = $display; |
107 |
} |
} |
108 |
|
|
116 |
my $field = shift; |
my $field = shift; |
117 |
my $where = shift; |
my $where = shift; |
118 |
|
|
119 |
my $sql = "select count(*) from index where name = ? and upper(item) like upper(?)||'%'"; |
my $filter = shift; |
120 |
|
|
121 |
|
my $tables_sql = 'data'; |
122 |
|
my $where_sql = ''; |
123 |
|
my @sql_args = ( $field, lc($where) ); |
124 |
|
|
125 |
|
if ($filter) { |
126 |
|
$tables_sql .= ",filters"; |
127 |
|
$where_sql .= " |
128 |
|
and data.ord = filters.ord |
129 |
|
and filter = ? |
130 |
|
"; |
131 |
|
push @sql_args, $filter; |
132 |
|
} |
133 |
|
|
134 |
|
my $sql = qq{ |
135 |
|
select count(*) |
136 |
|
from $tables_sql |
137 |
|
where name = ? and item like ?||'%' |
138 |
|
$where_sql |
139 |
|
}; |
140 |
|
|
141 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr(); |
142 |
$sth->execute($field,$where) || die "sql: $sql; ".$self->{dbh}->errstr(); |
$sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr(); |
143 |
|
|
144 |
my ($total) = $sth->fetchrow_array(); |
my ($total) = $sth->fetchrow_array(); |
145 |
|
|
146 |
# no results, count all |
# no results, count all |
147 |
if (! $total) { |
if (! $total) { |
148 |
my $sql = "select count(*) from index wheere name = ?"; |
my $sql = qq{ |
149 |
|
select count(*) |
150 |
|
from $tables_sql |
151 |
|
where data.name = ? |
152 |
|
$where_sql |
153 |
|
}; |
154 |
|
|
155 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
@sql_args = ( $field ); |
156 |
$sth->execute($field) || die "sql: $sql; ".$self->{dbh}->errstr(); |
push @sql_args, $filter if ($filter); |
157 |
|
|
158 |
|
my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr(); |
159 |
|
$sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr(); |
160 |
$total = $sth->fetchrow_array(); |
$total = $sth->fetchrow_array(); |
161 |
|
|
162 |
} |
} |
163 |
|
|
164 |
return $total || 1; |
return $total || '0'; |
165 |
} |
} |
166 |
|
|
167 |
|
|
173 |
|
|
174 |
my $offset = shift || 0; |
my $offset = shift || 0; |
175 |
my $rows = shift || 10; |
my $rows = shift || 10; |
176 |
|
my $filter = shift; |
177 |
|
|
178 |
my $from_ord = 0; |
my $from_ord = 0; |
179 |
|
|
180 |
my @sql_args; |
my $tables_sql = 'data'; |
181 |
|
my $where_sql = ''; |
182 |
|
|
183 |
my $sql = qq{ |
my @sql_args = ( $field, lc($where) ); |
184 |
select item,display,count |
|
185 |
from index |
if ($filter) { |
186 |
where name = ? |
$tables_sql .= ",filters"; |
187 |
and ord > ? |
$where_sql .= " |
188 |
order by ord |
and data.ord = filters.ord |
189 |
limit ? offset ? |
and filter = ? |
190 |
}; |
"; |
191 |
|
push @sql_args, $filter; |
192 |
|
} |
193 |
|
|
194 |
if ($where) { |
if ($where) { |
195 |
my $sql2 = "select ord from index where name = ? and upper(item) like upper(?)||'%'"; |
my $sql2 = qq{ |
196 |
my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
select data.ord as ord |
197 |
|
from $tables_sql |
198 |
|
where name = ? and item like ?||'%' |
199 |
|
$where_sql |
200 |
|
order by data.ord |
201 |
|
}; |
202 |
|
my $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr(); |
203 |
|
|
204 |
$sth->execute($field, $where) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
$sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr(); |
205 |
if (my $row = $sth->fetchrow_hashref) { |
if (my $row = $sth->fetchrow_hashref) { |
206 |
$from_ord += $row->{ord} - 1; |
$from_ord = $row->{ord} - 1; |
207 |
} else { |
} else { |
208 |
# if no match is found when searching from beginning |
# if no match is found when searching from beginning |
209 |
# of word in index, try substring match anywhere |
# of word in index, try substring match anywhere |
210 |
$sql2 = "select ord from index where name = ? and upper(item) like '% '||upper(?)||'%'"; |
$sql2 = qq{ |
211 |
$sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
select data.ord as ord |
212 |
$sth->execute($field, $where) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
from $tables_sql |
213 |
|
where name = ? and item like '%'||?||'%' |
214 |
|
$where_sql |
215 |
|
order by data.ord |
216 |
|
}; |
217 |
|
|
218 |
|
$sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr(); |
219 |
|
$sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr(); |
220 |
|
|
221 |
if (my $row = $sth->fetchrow_hashref) { |
if (my $row = $sth->fetchrow_hashref) { |
222 |
$from_ord += $row->{ord} - 1; |
$from_ord = $row->{ord} - 1; |
223 |
} |
} |
224 |
} |
} |
225 |
} |
} |
226 |
|
|
227 |
my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr(); |
@sql_args = ( $field, $from_ord ); |
228 |
$sth->execute($field,$from_ord,$rows,$offset) || die "execute: $sql; ".$self->{dbh}->errstr(); |
push @sql_args, $filter if ($filter); |
229 |
|
|
230 |
|
my $sql = qq{ |
231 |
|
select item,display,data.count as count |
232 |
|
from $tables_sql |
233 |
|
where name = ? |
234 |
|
and data.ord > ? |
235 |
|
$where_sql |
236 |
|
order by data.ord |
237 |
|
}; |
238 |
|
|
239 |
|
# fix SQLite problem which doesn't allow placeholders in limit and offset |
240 |
|
# http://thread.gmane.org/gmane.comp.db.sqlite.general/9707 |
241 |
|
$sql .= "limit $rows offset $offset"; |
242 |
|
|
243 |
|
my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr(); |
244 |
|
$sth->execute(@sql_args) || confess "execute: $sql; ".join("|",@sql_args)." ".$self->{dbh}->errstr(); |
245 |
my @arr; |
my @arr; |
246 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
247 |
$row->{item} = HTML::Entities::encode($row->{item},' <>&"'); |
$row->{item} = HTML::Entities::encode($row->{item},' <>&"'); |
258 |
|
|
259 |
return if (! $self->{dbh}); |
return if (! $self->{dbh}); |
260 |
|
|
261 |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
$self->{dbh}->begin_work || confess $self->{dbh}->errstr(); |
262 |
|
|
263 |
$self->delete_and_create('index', qq{ |
$self->delete_and_create('data', qq{ |
264 |
create table index ( |
create table data ( |
265 |
name varchar(255), |
name varchar(255), |
266 |
ord int, |
ord int, |
267 |
item text, |
item text, |
285 |
$self->bench("got ".($#items+1)." items, now sorting"); |
$self->bench("got ".($#items+1)." items, now sorting"); |
286 |
@items = sort @items; |
@items = sort @items; |
287 |
|
|
288 |
my $sql = "insert into index (name,ord,item,display,count) values (?,?,?,?,?)"; |
my $sql = "insert into data (name,ord,item,display,count) values (?,?,?,?,?)"; |
289 |
my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr(); |
my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr(); |
290 |
|
|
291 |
$sql = "insert into filters (filter, ord, count) values (?,?,?)"; |
$sql = "insert into filters (filter, ord, count) values (?,?,?)"; |
315 |
|
|
316 |
} |
} |
317 |
|
|
318 |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
$self->{dbh}->commit || confess $self->{dbh}->errstr(); |
319 |
|
|
320 |
$self->bench("vacuuming"); |
$self->bench("vacuuming"); |
321 |
|
|