def test_query_parser(mapper, in_put): """input query ==parse==> output dictionary ==mapper==> select sentences""" presult = qparse.parse(in_put) if presult == None: raise Error("parse result is empty") # print "parse result: ", presult # construct a query keywords = presult['keywords'] for keyword in keywords: if mapper.has_key(keyword[0]): key = mapper.get_column(keyword[0]) # print "%s ==map==> %s"% (keyword[0], key) keyword.remove(keyword[0]) keyword.insert(0, key) else: _LOGGER.error("""keyword not in mapper %s""", str(keyword[0])) raise Error(\ "ERROR: keyword not in mapper %s" % str(keyword[0])) constraint = None if presult.has_key('constraints'): constraint = presult['constraints'] stack = [] while (constraint): # push all constraint on [] into stack if type(constraint) is type([]): for con in constraint: stack.append(con) cons = stack.pop() # we find a constraint do mapper if type(cons) is type({}): keyword = cons['keyword'] if mapper.has_key(keyword[0]): key = mapper.get_column(keyword[0]) cons['keyword'] = [key] # print "%s ==map==> %s"% (keyword[0], key) else: _LOGGER.error("""keyword not in mapper %s""", str(keyword[0])) raise Error("ERROR: keyword not in mapper %s"\ % str(keyword[0])) # we finished the last element in stack if len(stack) == 0: break constraint = stack.pop() # we find a [] put it in constraint elif type(cons) is type([]): constraint = cons _LOGGER.debug("""user input is: %s""" % str(in_put)) _LOGGER.debug("""parse result is: %s""" % str(presult)) # print presult return presult
def query_parser(mapper, in_put): """ 1.perform parser on input query 2.perform keyword to table[.column] mapping 3.return mapped query dictionary {'keywords':{}, 'constraints':{}} keylist also return to be used in appending attribute link titles? """ keylist = {'keywords':[], 'constraints':[], 'keyset':[], 'mkeywords':[]} # keyset is needed to log down the table concerned. keyset = keylist['keyset'] presult = qparse.parse(in_put) if presult == None: _LOGGER.error("query is not accepted due to syntax reason") return None, None _LOGGER.debug("""query dictionary before mapping %s""" % \ str(presult)) # do mapping keywords = presult['keywords'] for keyword in keywords: if mapper.has_key(keyword[0]): keylist['keywords'].append(keyword[0]) key = mapper.get_column(keyword[0]) keyset.append(key) keyword.remove(keyword[0]) keyword.insert(0, key) keylist['mkeywords'].append(keyword) else: _LOGGER.error("""keyword %s is not known""", str(keyword[0])) return None, None constraint = None if presult.has_key('constraints'): constraint = presult['constraints'] stack = [] while (constraint): # push all constraint on [] into stack if type(constraint) is type([]): for con in constraint: stack.append(con) cons = stack.pop() # we find a constraint do mapper if type(cons) is type({}): keyword = cons['keyword'] if mapper.has_key(keyword[0]): keylist['constraints'].append(keyword[0]) key = mapper.get_column(keyword[0]) keyset.append(key) cons['keyword'] = [key, keyword[0]] else: _LOGGER.error("""keyword %s not in mapper """, str(keyword[0])) return None, None # replace wildcard with % if cons['value'].count('*') > 0: cons['value'] = cons['value'].replace('*', '%') cons['sign'] = 'like' # we finished the last element in stack if len(stack) == 0: break constraint = stack.pop() # we find a [] put it in constraint elif type(cons) is type([]): constraint = cons _LOGGER.debug("""user input is: %s""" % str(in_put)) _LOGGER.debug("""parse result is: %s""" % str(presult)) _LOGGER.debug("""keylist is: %s""" % str(keylist)) return presult, keylist