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

View File

@ -5,6 +5,7 @@ from Entities import(
AttachmentException,URI,URIException AttachmentException,URI,URIException
) )
from GulagUtils import whoami from GulagUtils import whoami
import json
class GulagDBException(Exception): class GulagDBException(Exception):
message = None message = None
@ -14,6 +15,7 @@ class GulagDBException(Exception):
class GulagDB: class GulagDB:
conn = None conn = None
uri_prefixes = None uri_prefixes = None
vcols = None
def __init__(self, args, uri_prefixes): def __init__(self, args, uri_prefixes):
try: try:
@ -34,6 +36,9 @@ class GulagDB:
autocommit=True autocommit=True
) )
self.uri_prefixes = uri_prefixes 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: except mariadb.Error as e:
raise GulagDBException(whoami(self) + str(e)) from e raise GulagDBException(whoami(self) + str(e)) from e
@ -94,16 +99,43 @@ class GulagDB:
or arg == 'rfc822_message'): or arg == 'rfc822_message'):
continue continue
if(cnt == 0): if(cnt == 0):
where_clause += "where " + arg + "='" + args[arg] + "' " if arg in self.vcols:
where_clause += "having " + arg + "='" + args[arg] + "' "
else:
where_clause += "where " + arg + "='" + args[arg] + "' "
else: else:
where_clause += "and " + arg + "='" + args[arg] + "' " where_clause += "and " + arg + "='" + args[arg] + "' "
cnt += 1 cnt += 1
return where_clause return where_clause
def parse_filters(self,filters): def get_where_clause_from_filters(self,filters_json):
# TODO # {"groupOp":"AND","rules":[{"field":"available","op":"eq","data":"true"}]}
# {"groupOp":"AND","rules":[{"field":"Customer","op":"eq","data":"eosp"}]} filters = None
return True 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): def get_mailboxes(self):
try: try:
@ -183,13 +215,18 @@ class GulagDB:
raise GulagDBException(whoami(self) + str(e)) from e raise GulagDBException(whoami(self) + str(e)) from e
def get_quarmails(self,args): 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: try:
cursor = self.conn.cursor() cursor = self.conn.cursor()
query = "select *,(select count(*) from QuarMail2Attachment" query = "select *,(select count(*) from QuarMail2Attachment"
query += " where QuarMails.id=QuarMail2Attachment.quarmail_id) as attach_count," query += " where QuarMails.id=QuarMail2Attachment.quarmail_id) as attach_count,"
query += " (select count(*) from QuarMail2URI" query += " (select count(*) from QuarMail2URI"
query += " where QuarMails.id=QuarMail2URI.quarmail_id) as uri_count" 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) + " ;" query += " " + self.get_limit_clause(args) + " ;"
cursor.execute(query) cursor.execute(query)
results = [] results = []
@ -463,4 +500,3 @@ class GulagDB:
raise GulagDBException(whoami(self) + str(e)) from e raise GulagDBException(whoami(self) + str(e)) from e
cursor.close() cursor.close()
return True return True