GulagDB::get_where_clause_from_filters() implements jqgrid-style search filtering

This commit is contained in:
Dominik Chilla 2018-12-25 19:48:21 +01:00
parent 937b805779
commit 87fadeeff5
2 changed files with 60 additions and 24 deletions

View File

@ -59,7 +59,7 @@ class Gulag:
for arg in args:
if(arg == 'query_offset' or arg == 'query_limit'
or arg == 'sort_index' or arg == 'sort_order'
or arg == 'rfc822_message'):
or arg == 'rfc822_message' or arg == 'filters'):
continue
if arg not in self.fields[fields_target]:
raise GulagException(
@ -229,7 +229,7 @@ class Gulag:
) from e
if 'rfc822_message' not in args:
return qms_db
# recognise all IMAP mailboxes to read from
# recognize all IMAP mailboxes to read from
mailboxes = {}
for qm in qms_db:
if qm['mailbox_id'] not in mailboxes:

View File

@ -5,6 +5,7 @@ from Entities import(
AttachmentException,URI,URIException
)
from GulagUtils import whoami
import json
class GulagDBException(Exception):
message = None
@ -14,6 +15,7 @@ class GulagDBException(Exception):
class GulagDB:
conn = None
uri_prefixes = None
vcols = None
def __init__(self, args, uri_prefixes):
try:
@ -34,6 +36,9 @@ class GulagDB:
autocommit=True
)
self.uri_prefixes = uri_prefixes
# virtual columns cannot not be stated in where-clause
self.vcols['attach_count'] = {}
self.vcols['uri_count'] = {}
except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e
@ -94,16 +99,43 @@ class GulagDB:
or arg == 'rfc822_message'):
continue
if(cnt == 0):
if arg in self.vcols:
where_clause += "having " + arg + "='" + args[arg] + "' "
else:
where_clause += "where " + arg + "='" + args[arg] + "' "
else:
where_clause += "and " + arg + "='" + args[arg] + "' "
cnt += 1
return where_clause
def parse_filters(self,filters):
# TODO
# {"groupOp":"AND","rules":[{"field":"Customer","op":"eq","data":"eosp"}]}
return True
def get_where_clause_from_filters(self,filters_json):
# {"groupOp":"AND","rules":[{"field":"available","op":"eq","data":"true"}]}
filters = None
where_clause = ""
try:
filters = json.loads(filters_json)
except json.JSONDecodeError as e:
raise GulagDBException(whoami(self) + "JSON parse error: " + e.msg) from e
for rule in filters['rules']:
field_op_data = None
if(rule['op'] == 'eq'):
field_op_data = rule['field'] + "='" + rule['data'] + "'"
elif(rule['op'] == 'bw'):
field_op_data = rule['field'] + " like '" + rule['data'] + "%'"
elif(rule['op'] == 'ew'):
field_op_data = rule['field'] + " like '%" + rule['data'] + "'"
elif(rule['op'] == 'cn'):
field_op_data = rule['field'] + " like '%" + rule['data'] + "%'"
if(field_op_data == None):
raise GulagDBException(whoami(self) + "invalid rule-op: " + rule['op'])
if(len(filters['rules']) == 1 or len(where_clause) == 0):
if rule['field'] in self.vcols:
where_clause = "having " + field_op_data
else:
where_clause = "where " + field_op_data
else:
where_clause += " " + filters['groupOp'] + " " + field_op_data
return where_clause
def get_mailboxes(self):
try:
@ -183,13 +215,18 @@ class GulagDB:
raise GulagDBException(whoami(self) + str(e)) from e
def get_quarmails(self,args):
where_clause = ""
if 'filters' in args:
where_clause = self.get_where_clause_from_filters(args['filters'])
else:
where_clause = self.get_where_clause(args)
try:
cursor = self.conn.cursor()
query = "select *,(select count(*) from QuarMail2Attachment"
query += " where QuarMails.id=QuarMail2Attachment.quarmail_id) as attach_count,"
query += " (select count(*) from QuarMail2URI"
query += " where QuarMails.id=QuarMail2URI.quarmail_id) as uri_count"
query += " from QuarMails " + self.get_where_clause(args)
query += " from QuarMails " + where_clause
query += " " + self.get_limit_clause(args) + " ;"
cursor.execute(query)
results = []
@ -463,4 +500,3 @@ class GulagDB:
raise GulagDBException(whoami(self) + str(e)) from e
cursor.close()
return True