Liste des Groupes | Revenir à co vms |
Is it bad to offer users dynamic query capabilities?
Here’s some code I wrote back in Python-2 days (MySQL-specific):
# collect list of items matching specified search criteria
condition = \
(
list
( # free-text fields
"%(name)s like %(value)s"
%
{
"name" : field[0],
"value" :
SQLString("%" + EscapeSQLWild(Params.getvalue(field[1])) + "%"),
}
for field in
(
("make", "search_make"),
("model", "search_model"),
("details", "search_details"),
("serial_nr", "search_serial"),
("inventory_nr", "search_invent"),
)
if Params.getvalue(field[1]) != ""
)
+
list
( # exact-match fields
"%(name)s = %(value)s"
%
{
"name" : field[0],
"value" : SQLString(Params.getvalue(field[1])),
}
for field in
(
("class_name", "search_class"),
("allocation", "search_allocated"),
("location_name", "search_location"),
)
if Params.getvalue(field[1]) != ""
)
+
list
( # date fields
"("
+
" or ".join
(
"%(name)s %(op)s %(value)s"
%
{
"name" : field[0],
"op" : op[0],
"value" : SQLString(Params.getvalue(field[1])),
}
for op in
(
("<", "lt"),
("=", "eq"),
(">", "gt"),
)
if GetCheckbox("%(name)s[%(op)s]" % {"name" : field[1], "op" : op[1]})
)
+
")"
for field in
(
("when_purchased", "search_when_purchased"),
("warranty_expiry", "search_warranty_expiry"),
)
if reduce
(
operator.__or__,
(
GetCheckbox("%(name)s[%(op)s]" % {"name" : field[1], "op" : op})
for op in ("lt", "eq", "gt")
)
)
)
)
condition = " and ".join(condition)
used as
select «fields» from «table» where «condition»
Let’s see your EXEC SQL cope with that ...
Les messages affichés proviennent d'usenet.