- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
def sql_select_filter(sql, flt, order=None, limit=None, offset=None, group_by=None, lock=None, withSelectWrapper=True):
if withSelectWrapper and group_by is None:
if re.search(r'^\s*select\s+', sql, flags=re.IGNORECASE | re.MULTILINE):
sql = "SELECT * FROM ( " + sql + " ) as z99 "
else:
sql = "SELECT * FROM " + sql + " "
sql = sql.replace("%", '%%')
where, vals = _make_where_conditions(flt)
if where is not None:
sql += " WHERE " + where
if group_by is not None and len(group_by):
sql += " GROUP BY " + ",".join(group_by)
if order is not None and len(order):
order_fields = []
for field, direction in order.items():
order_fields.append(field + " " + direction)
sql += " ORDER BY " + ",".join(order_fields)
if limit is not None:
sql += " LIMIT %s"
vals.append(limit)
if offset is not None:
sql += " OFFSET %s"
vals.append(offset)
if lock is not None:
sql += " FOR " + lock
return sql, vals