当前位置: 首页>>代码示例>>Python>>正文


Python Lexer.matchId方法代码示例

本文整理汇总了Python中lexer.Lexer.matchId方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.matchId方法的具体用法?Python Lexer.matchId怎么用?Python Lexer.matchId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lexer.Lexer的用法示例。


在下文中一共展示了Lexer.matchId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import matchId [as 别名]
class Parser :
    '''
        Called by database.py
        process create and insert SQL command
        raise an exception on syntax error
    '''
    maxVarcharLen = 40
    
    def __init__(self, query):
        self.lex = Lexer(query)
    
    def parse(self):
        if self.lex.matchKeyword('create'):
            return self.create()
        elif self.lex.matchKeyword('insert'):
            return self.insert()
        else:
            return self.select()

    def select(self):
        self.lex.eatKeyword('select')
        projs = self.projectList()
        self.lex.eatKeyword('from')
        tables = self.tableList()
        pred = {}
        if self.lex.matchKeyword('where'):
            self.lex.eatKeyword('where')
            pred = self.predicate()
#print {'select':projs, 'from':tables, 'where':pred}
        return {'select':projs, 'from':tables, 'where':pred}

    def projectList(self):
        fieldNames = []
        aggFn = []
        if self.lex.matchId() or self.lex.matchDelim('*'):
            fieldNames.append(self.projectField())
        else:
            aggFn.append(self.aggregationFn())
        while self.lex.matchDelim(','):
            self.lex.eatDelim(',')
            if self.lex.matchId():
                fieldNames.append(self.projectField())
            else:
                aggFn.append(self.aggregationFn())
        return {'fieldNames':fieldNames, 'aggFn':aggFn}

    def projectField(self):
        name = ''
        if self.lex.matchDelim('*'):
            name = self.lex.eatDelim('*')
        else:
            name = self.lex.eatId()
            if self.lex.matchDelim('.'):
                name += self.lex.eatDelim('.')
                if self.lex.matchDelim('*'):
                    name += self.lex.eatDelim('*')
                else:
                    name += self.lex.eatId()
        return name

    def aggregationFn(self):
        fnType = ''
        field = ''
        if self.lex.matchKeyword('count'):
            fnType = self.lex.eatKeyword('count')
        else:
            fnType = self.lex.eatKeyword('sum')
        self.lex.eatDelim('(')
        field = self.projectField()
        self.lex.eatDelim(')')
        return {'type':fnType, 'field':field}

    def tableList(self):
        tList = []
        tList.append(self.tableName())
        while self.lex.matchDelim(','):
            self.lex.eatDelim(',')
            tList.append(self.tableName())
        return tList

    def tableName(self):
        alias = ''
        name = self.lex.eatId()
        if self.lex.matchKeyword('as'):
            self.lex.eatKeyword('as')
            alias = self.lex.eatId()
        return {'tableName':name, 'alias':alias}

    def predicate(self):
        logic = ''
        term2 = {}
        term1 = self.term()
        if self.lex.matchKeyword('and'):
            logic = self.lex.eatKeyword('and')
            term2 = self.term()
        elif self.lex.matchKeyword('or'):
            logic = self.lex.eatKeyword('or')
            term2 = self.term()
        return {'term1':term1, 'term2':term2, 'logic':logic}

#.........这里部分代码省略.........
开发者ID:ju5td0m7m1nd,项目名称:DataBason,代码行数:103,代码来源:sqlparser.py


注:本文中的lexer.Lexer.matchId方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。