예제 #1
0
    def __init__(self, tree: Node):

        #do query
        for element in tree.get_children():
            if element.get_type() == TK.TOK_FROM:
                (self._index,
                 self._type) = parse_tok_table_name(element.get_child(0))
                if element.get_children_count() == 2:
                    self.route = parse_value(element.get_child(1))

            if element.get_type() == TK.TOK_SELECT:
                self.selexpr = parse_tok_selexpr(element)

            if element.get_type() == TK.TOK_WHERE:
                self.query_body = QueryBody(element.get_child(0))

            if element.get_type() == TK.TOK_LIMIT:
                (self._from, self._size) = parse_tok_limit(element)

            if element.get_type() == TK.TOK_ORDERBY:
                self.sorts = parse_tok_sorts(element)

        #do aggregations
        for element in tree.get_children():
            if element.get_type() == TK.TOK_GROUPBY:
                agg_size = -1
                if hasattr(self, '_size'):
                    agg_size = self._size
                self.groupby = AggBuckets(element, agg_size)
예제 #2
0
 def __init__(self, tree: Node):
     self.compare = tree.get_value().lower()
     if tree.get_child(0).get_type() == TK.TOK_EXPRESSION_LEFT:
         self.left_values = parse_left_values(
             tree.get_child(0).get_children())
     if tree.get_child(1).get_type() == TK.TOK_EXPRESSION_RIGHT:
         self.right_value = parse_value(tree.get_child(1).get_child(0))
예제 #3
0
 def __init__(self, tree: Node):
     if tree.get_child(0).get_type() == TK.TOK_FUNCTION:
         self.selexpr = FunctionXpr(tree.get_child(0))
     else:
         self.selexpr = parse_value(tree.get_child(0))
     if tree.get_children_count() == 2:
         self.alias = parse_value(tree.get_child(1))
예제 #4
0
def parse_table_name(tree: Node):
    if tree.get_type() == TK.TOK_DOT:
        _index = parse_value(tree.get_child(0))
        _type = parse_value(tree.get_child(1))
    elif tree.get_type() == TK.TOK_VALUE:
        _index = tree.get_value()
        _type = 'base'
    return (_index,_type)
예제 #5
0
def parse_value(tree: Node) -> str:
    if tree.get_type() == TK.TOK_DOT:
        retval = parse_value(tree.get_child(0))
        retval += '.'
        retval += parse_value(tree.get_child(1))
        return retval
    elif tree.get_type() == TK.TOK_VALUE:
        return tree.get_value()
    else:
        pass
예제 #6
0
def parse_tok_limit(tree: Node):
    _from = 0
    _size = 0

    if tree.get_children_count() == 2:
        _from = tree.get_child(0).get_value()
        _size = tree.get_child(1).get_value()
    else:
        _from = 0
        _size = tree.get_child(0).get_value()
    return (_from, _size)
예제 #7
0
def parse_kv(tree: Node):
    right=None
    left=None
    if tree.get_type() == TK.TOK_KEY_VALUE:
        left = parse_value(tree.get_child(0).get_child(0))
        if tree.get_child(1).get_child(0).get_type() in (TK.TOK_DICT,TK.TOK_LIST):
            right = parse_object(tree.get_child(1).get_child(0))
        else:
            right = parse_value(tree.get_child(1).get_child(0))
    else:
        pass
    return {left:right}
예제 #8
0
    def __init__(self, tree: Node, root=True):

        if tree.get_type() == TK.TOK_REVERSED:
            tree = tree.get_child(0)
            self.reversed = True

        self.root = root
        self.combine = 'must'
        if tree.get_type() == TK.TOK_COMPOUND:
            self.combine = tree.get_value()
            self.lchild = QueryBody(tree.get_child(0), False)
            self.rchild = QueryBody(tree.get_child(1), False)
        else:
            self.lchild = query_expression(tree)
예제 #9
0
def parse_object(tree: Node):
    retval = None
    if tree.get_type() == TK.TOK_DICT:
        retval = {}
        for element in tree.get_children():
            retval.update(parse_kv(element))
    if tree.get_type() == TK.TOK_LIST:
        retval = []
        for element in tree.get_children():
            if element.get_type() in (TK.TOK_DICT,TK.TOK_LIST):
                retval.append(parse_object(element))
            else:
                retval.append(parse_value(element))
    return retval
예제 #10
0
def parse_insert_row(tree: Node):
    retval = []
    for e in tree.get_children():
        if e.get_type() == TK.TOK_VALUE:
            retval.append(parse_value(e))
        elif e.get_type() in (TK.TOK_DICT, TK.TOK_LIST):
            retval.append(parse_object(e))
    return retval
예제 #11
0
 def __init__(self, tree: Node):
     for element in tree.get_children():
         if element.get_type() == TK.TOK_TABLE_NAME:
             (self._index, self._type) = parse_tok_table_name(element)
         if element.get_type() == TK.TOK_SET_COLUMNS_CLAUSE:
             self.update_sets = parse_update_sets(element)
         if element.get_type() == TK.TOK_WHERE:
             self.conditions = parse_conditions(element)
예제 #12
0
def parse_tok_sorts(tree: Node):
    sorts = {}
    for sort in tree.get_children():
        if sort.get_type() == TK.TOK_SORT:
            order = 'asc'
            if sort.get_children_count() == 2:
                order = sort.get_child(1).get_value()
            sorts[sort.get_child(0).get_value()] = {'order': order}
    return sorts
예제 #13
0
 def __init__(self, tree: Node):
     self.insert_columns = []
     self.bulk_rows = []
     for element in tree.get_children():
         if element.get_type() == TK.TOK_TABLE_NAME:
             (self._index, self._type) = parse_tok_table_name(element)
         elif element.get_type() == TK.TOK_INSERT_COLUMNS:
             self.insert_columns = parse_insert_columns(element)
         elif element.get_type() == TK.TOK_INSERT_ROWS:
             self.bulk_rows = parse_bulk_rows(element)
예제 #14
0
def bucket_function(tree: Node, _size):
    bucket = {}
    bucket[tree.get_value()] = {}
    for i in range(0, tree.get_children_count()):
        if tree.get_child(i).get_type() == TK.TOK_DICT:
            bucket[tree.get_value()].update(parse_object(tree.get_child(i)))
    if _size != -1:
        bucket[tree.get_value()]['size'] = _size
    aggs = {"aggs": {}}
    field = bucket[tree.get_value()]['field']
    aggs['aggs'][field] = bucket
    return (field, aggs)
예제 #15
0
    def __init__(self, tree: Node):

        exec_node = tree.get_child(0)

        stmt = None
        self.dsl_body = {}
        self.curl_str = ''

        es_url = 'http://localhost:9200/'

        if exec_node.get_type() == TK.TOK_QUERY:
            stmt = Query(exec_node)
            self.curl_str = 'curl -XPOST ' + es_url + stmt._index + '/' + stmt._type + '/_search'
            self.dsl_body = stmt.dsl()
예제 #16
0
 def __init__(self, tree: Node):
     self.insert_columns = []
     self.insert_row = []
     self.metas = {}
     for element in tree.get_children():
         if element.get_type() == TK.TOK_TABLE_NAME:
             (self._index, self._type) = parse_tok_table_name(element)
         elif element.get_type() == TK.TOK_INSERT_COLUMNS:
             self.insert_columns = parse_insert_columns(element)
         elif element.get_type() == TK.TOK_INSERT_ROW:
             self.insert_row = parse_insert_row(element)
     for i in range(0, len(self.insert_columns)):
         if self.insert_columns[i] in [
                 '_id', '_parent', '_routing', '_type'
         ]:
             self.metas[self.insert_columns[i][1:]] = self.insert_row[i]
예제 #17
0
 def __init__(self, tree: Node):
     for element in tree.get_children():
         if element.get_type() == TK.TOK_TABLE_NAME:
             (self._index, self._type) = parse_tok_table_name(element)
         if element.get_type() == TK.TOK_WHERE:
             self.conditions = QueryBody(element.get_child(0))
예제 #18
0
def parse_tok_selexpr(tree: Node):
    retval = []
    for e in tree.get_children():
        retval.append(Selexpr(e))
    return retval
예제 #19
0
def parse_tok_table_name(tree : Node):
    if tree.get_type() == TK.TOK_TABLE_NAME:
        return  parse_table_name(tree.get_child(0))
    else:
        pass
예제 #20
0
def parse_tok_value(tree: Node):
    if tree.children != None:
        if tree.get_child(0).get_type() == TK.TOK_DQ_VALUE:
            return '"' + tree.get_value() + '"'
    return tree.get_value()
예제 #21
0
 def __init__(self, tree: Node):
     self.function_name = tree.get_value()
     self.function_parms = tree.get_children()
예제 #22
0
def query_expression(tree: Node):
    if tree.get_type() == TK.TOK_COMPARE:
        return CompareExpression(tree)
    if tree.get_type() == TK.TOK_FUNCTION:
        return FunctionExpression(tree)
예제 #23
0
 def __init__(self, tree: Node, _size, root=True):
     self.buckets = []
     for element in tree.get_children():
         self.buckets.append(bucket(element, _size))
예제 #24
0
def bucket(tree: Node, _size):
    if tree.get_type() == TK.TOK_FUNCTION:
        return bucket_function(tree, _size)
    else:
        return bucket_field(tree, _size)
예제 #25
0
파일: Drop.py 프로젝트: zhanglei/esql5
 def __init__(self, tree: Node):
     for element in tree.get_children():
         if element.get_type() == TK.TOK_TABLE_NAME:
             (self._index, self._type) = parse_tok_table_name(element)
예제 #26
0
def parse_insert_columns(tree: Node):
    retval = []
    for e in tree.get_children():
        if e.get_type() == TK.TOK_VALUE:
            retval.append(parse_value(e))
    return retval
예제 #27
0
def parse_conditions(tree: Node):
    query_body = QueryBody(tree.get_child(0))
    condition = {}
    query_body_travel(query_body, condition)
    return condition
예제 #28
0
def parse_update_sets(tree: Node):

    retval = {}
    for e in tree.get_children():
        retval.update(parse_kv(e))
    return retval
예제 #29
0
def parse_bulk_rows(tree: Node):
    retval = []
    for e in tree.get_children():
        retval.append(parse_insert_row(e))
    return retval