60 |
sub delete_and_create { |
sub delete_and_create { |
61 |
my $self = shift; |
my $self = shift; |
62 |
|
|
63 |
my $index = shift || croak "need index name!"; |
my $table = shift || croak "need table name!"; |
64 |
my $sql = shift || croak "need sql to create table!"; |
my $sql = shift || croak "need sql to create table!"; |
65 |
|
|
66 |
print STDERR "## delete_and_create($index)\n" if ($debug); |
print STDERR "## delete_and_create($table)\n" if ($debug); |
67 |
|
|
68 |
my $sql_delete = "delete from $index"; |
my $sql_delete = "delete from $table"; |
69 |
my $sth = $self->{dbh}->prepare($sql_delete) || confess "can't prepare: $sql_delete"; |
my $sth = $self->{dbh}->prepare($sql_delete); |
70 |
|
|
71 |
unless ($sth->execute()) { |
if ($sth && $sth->execute()) { |
72 |
|
print STDERR "## deleted rows from table $table\n" if ($debug); |
73 |
|
} else { |
74 |
# can't delete from table, assume it doesn't exists! |
# can't delete from table, assume it doesn't exists! |
75 |
$self->{dbh}->rollback; |
$self->{dbh}->rollback; |
76 |
$self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr(); |
$self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr(); |
77 |
print STDERR "## creating table $index\n" if ($debug); |
print STDERR "## creating table $table\n" if ($debug); |
78 |
$self->{dbh}->begin_work; |
$self->{dbh}->begin_work; |
79 |
} |
} |
80 |
} |
} |
85 |
my $field = shift; |
my $field = shift; |
86 |
my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)"; |
my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)"; |
87 |
my $display = shift || $index_data; |
my $display = shift || $index_data; |
88 |
my $tag = shift; |
my $filter = shift; |
89 |
|
|
90 |
if (! $index_data) { |
if (! $index_data) { |
91 |
print STDERR "\$index->insert() -- no value to insert\n"; |
print STDERR "\$index->insert() -- no value to insert\n"; |
108 |
} |
} |
109 |
|
|
110 |
$self->{c}->{$uc}->{$field}->{count}++; |
$self->{c}->{$uc}->{$field}->{count}++; |
111 |
$self->{c}->{$uc}->{$field}->{tag}->{$tag}++ if ($tag); |
$self->{c}->{$uc}->{$field}->{filter}->{$filter}++ if ($filter); |
112 |
} |
} |
113 |
|
|
114 |
sub count { |
sub count { |
117 |
my $field = shift; |
my $field = shift; |
118 |
my $where = shift; |
my $where = shift; |
119 |
|
|
120 |
my $sql = "select count(*) from index where name = ? and upper(item) like upper(?)||'%'"; |
my $filter = shift; |
121 |
|
|
122 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
my $tables_sql = 'data'; |
123 |
$sth->execute($field,$where) || die "sql: $sql; ".$self->{dbh}->errstr(); |
my $where_sql = ''; |
124 |
|
my @sql_args = ( $field, $where ); |
125 |
|
|
126 |
|
if ($filter) { |
127 |
|
$tables_sql .= ",filters"; |
128 |
|
$where_sql .= " |
129 |
|
and data.ord = filters.ord |
130 |
|
and filter = ? |
131 |
|
"; |
132 |
|
push @sql_args, $filter; |
133 |
|
} |
134 |
|
|
135 |
|
my $sql = qq{ |
136 |
|
select count(*) |
137 |
|
from $tables_sql |
138 |
|
where name = ? and upper(item) like upper(?)||'%' |
139 |
|
$where_sql |
140 |
|
}; |
141 |
|
|
142 |
|
my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr(); |
143 |
|
$sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr(); |
144 |
|
|
145 |
my ($total) = $sth->fetchrow_array(); |
my ($total) = $sth->fetchrow_array(); |
146 |
|
|
147 |
# no results, count all |
# no results, count all |
148 |
if (! $total) { |
if (! $total) { |
149 |
my $sql = "select count(*) from index wheere name = ?"; |
my $sql = qq{ |
150 |
|
select count(*) |
151 |
|
from $tables_sql |
152 |
|
where data.name = ? |
153 |
|
$where_sql |
154 |
|
}; |
155 |
|
|
156 |
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
@sql_args = ( $field ); |
157 |
$sth->execute($field) || die "sql: $sql; ".$self->{dbh}->errstr(); |
push @sql_args, $filter if ($filter); |
158 |
|
|
159 |
|
my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr(); |
160 |
|
$sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr(); |
161 |
$total = $sth->fetchrow_array(); |
$total = $sth->fetchrow_array(); |
162 |
|
|
163 |
} |
} |
164 |
|
|
165 |
return $total || 1; |
return $total || '0'; |
166 |
} |
} |
167 |
|
|
168 |
|
|
174 |
|
|
175 |
my $offset = shift || 0; |
my $offset = shift || 0; |
176 |
my $rows = shift || 10; |
my $rows = shift || 10; |
177 |
|
my $filter = shift; |
178 |
|
|
179 |
my $from_ord = 0; |
my $from_ord = 0; |
180 |
|
|
181 |
my @sql_args; |
my $tables_sql = 'data'; |
182 |
|
my $where_sql = ''; |
183 |
|
|
184 |
my $sql = qq{ |
my @sql_args = ( $field, $where ); |
185 |
select item,display,count |
|
186 |
from index |
if ($filter) { |
187 |
where name = ? |
$tables_sql .= ",filters"; |
188 |
and ord > ? |
$where_sql .= " |
189 |
order by ord |
and data.ord = filters.ord |
190 |
limit ? offset ? |
and filter = ? |
191 |
}; |
"; |
192 |
|
push @sql_args, $filter; |
193 |
|
} |
194 |
|
|
195 |
if ($where) { |
if ($where) { |
196 |
my $sql2 = "select ord from index where name = ? and upper(item) like upper(?)||'%'"; |
my $sql2 = qq{ |
197 |
my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr(); |
select data.ord as ord |
198 |
|
from $tables_sql |
199 |
|
where name = ? and upper(item) like upper(?)||'%' |
200 |
|
$where_sql |
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 upper(item) like '% '||upper(?)||'%' |
214 |
|
$where_sql |
215 |
|
}; |
216 |
|
|
217 |
|
$sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr(); |
218 |
|
$sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr(); |
219 |
|
|
220 |
if (my $row = $sth->fetchrow_hashref) { |
if (my $row = $sth->fetchrow_hashref) { |
221 |
$from_ord += $row->{ord} - 1; |
$from_ord += $row->{ord} - 1; |
222 |
} |
} |
223 |
} |
} |
224 |
} |
} |
225 |
|
|
226 |
my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr(); |
@sql_args = ( $field, $from_ord ); |
227 |
$sth->execute($field,$from_ord,$rows,$offset) || die "execute: $sql; ".$self->{dbh}->errstr(); |
push @sql_args, $filter if ($filter); |
228 |
|
|
229 |
|
my $sql = qq{ |
230 |
|
select item,display,data.count as count |
231 |
|
from $tables_sql |
232 |
|
where name = ? |
233 |
|
and data.ord > ? |
234 |
|
$where_sql |
235 |
|
order by data.ord |
236 |
|
}; |
237 |
|
|
238 |
|
# fix SQLite problem which doesn't allow placeholders in limit and offset |
239 |
|
# http://thread.gmane.org/gmane.comp.db.sqlite.general/9707 |
240 |
|
$sql .= "limit $rows offset $offset"; |
241 |
|
|
242 |
|
my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr(); |
243 |
|
$sth->execute(@sql_args) || confess "execute: $sql; ".join("|",@sql_args)." ".$self->{dbh}->errstr(); |
244 |
my @arr; |
my @arr; |
245 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
246 |
$row->{item} = HTML::Entities::encode($row->{item},' <>&"'); |
$row->{item} = HTML::Entities::encode($row->{item},' <>&"'); |
257 |
|
|
258 |
return if (! $self->{dbh}); |
return if (! $self->{dbh}); |
259 |
|
|
260 |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
$self->{dbh}->begin_work || confess $self->{dbh}->errstr(); |
261 |
|
|
262 |
$self->delete_and_create('index', qq{ |
$self->delete_and_create('data', qq{ |
263 |
create table index ( |
create table data ( |
264 |
name varchar(255), |
name varchar(255), |
265 |
ord int, |
ord int, |
266 |
item text, |
item text, |
270 |
); |
); |
271 |
}); |
}); |
272 |
|
|
273 |
$self->delete_and_create('tags', qq{ |
$self->delete_and_create('filters', qq{ |
274 |
create table tags ( |
create table filters ( |
275 |
tag varchar(255), |
filter varchar(255), |
276 |
ord int, |
ord int, |
277 |
count int, |
count int, |
278 |
primary key (tag,ord) |
primary key (filter,ord) |
279 |
); |
); |
280 |
}); |
}); |
281 |
|
|
284 |
$self->bench("got ".($#items+1)." items, now sorting"); |
$self->bench("got ".($#items+1)." items, now sorting"); |
285 |
@items = sort @items; |
@items = sort @items; |
286 |
|
|
287 |
my $sql = "insert into index (name,ord,item,display,count) values (?,?,?,?,?)"; |
my $sql = "insert into data (name,ord,item,display,count) values (?,?,?,?,?)"; |
288 |
my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr(); |
my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr(); |
289 |
|
|
290 |
$sql = "insert into tags (tag, ord, count) values (?,?,?)"; |
$sql = "insert into filters (filter, ord, count) values (?,?,?)"; |
291 |
my $sth_tag = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr(); |
my $sth_filter = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr(); |
292 |
|
|
293 |
my $ord = 0; |
my $ord = 0; |
294 |
foreach my $key (@items) { |
foreach my $key (@items) { |
303 |
$self->{c}->{$key}->{$field}->{count}, |
$self->{c}->{$key}->{$field}->{count}, |
304 |
); |
); |
305 |
|
|
306 |
# store tags |
# store filters |
307 |
next unless ($self->{c}->{$key}->{$field}->{tag}); |
next unless ($self->{c}->{$key}->{$field}->{filter}); |
308 |
|
|
309 |
foreach my $tag (keys %{$self->{c}->{$key}->{$field}->{tag}}) { |
foreach my $filter (keys %{$self->{c}->{$key}->{$field}->{filter}}) { |
310 |
$sth_tag->execute( $tag, $ord, $self->{c}->{$key}->{$field}->{tag}->{$tag} ); |
$sth_filter->execute( $filter, $ord, $self->{c}->{$key}->{$field}->{filter}->{$filter} ); |
311 |
} |
} |
312 |
} |
} |
313 |
|
|
314 |
|
|
315 |
} |
} |
316 |
|
|
317 |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
$self->{dbh}->commit || confess $self->{dbh}->errstr(); |
318 |
|
|
319 |
$self->bench("vacuuming"); |
$self->bench("vacuuming"); |
320 |
|
|