示例#1
0
def getcolumns(stream):
    """Function that return the colums of a SELECT query"""
    pipe = Pipeline()

    pipe.append(ColumnsSelect())

    return pipe(stream)
示例#2
0
def compact(stream):
    """Function that return a compacted version of the stream"""
    pipe = Pipeline()

    pipe.append(StripComments())
    pipe.append(StripWhitespace)

    return pipe(stream)
示例#3
0
def getlimit(stream):
    """Function that return the LIMIT of a input sql """
    pipe = Pipeline()

    pipe.append(Limit())

    result = pipe(stream)
    try:
        return int(result)
    except ValueError:
        return result
示例#4
0
    def parse_string(self, sql, method_name, dir_path='sql',
                       bypass_types=False, lazy=False):
        """
        Build a function from a string containing a SQL query

        Also add the function as a method to the AntiORM class

        :param sql: the SQL code of the method to be parsed
        :type sql: string
        :param method_name: the name of the method
        :type method_name: string
        :param dir_path: path to the dir with the SQL files (for INCLUDE)
        :type dir_path: string
        :param bypass_types: set if parsing should bypass types
        :type bypass_types: boolean
        :param lazy: set if parsing should be postpone until required
        :type lazy: boolean

        :return: the parsed function or None if `lazy` is True
        :rtype: function or None
        """

        # Lazy processing, store data & only do the parse if later is required
        if lazy:
            self._lazy[method_name] = (self.parse_string, sql, dir_path,
                                       bypass_types)
            return

        # Disable by-pass of types if not using CPython compatible bytecode
        if bypass_types and not _getframe:
            warn(RuntimeWarning("Can't acces to stack. "
                                "Disabling by-pass of types."))
            bypass_types = False

        # Set the dirpaths where to look for the INCLUDE statements
        dirpaths = self._dirpaths
        if dir_path not in dirpaths:
            dirpaths.append(dir_path)

        pipe = Pipeline()
        pipe.append(tokenize)
        pipe.append(IncludeStatement(dirpaths))

        stream = compact(pipe(sql.strip()))

        # One statement query
        if len(split2(stream)) == 1:
            return self._one_statement(method_name, stream, bypass_types)

        # Multiple statement query
        return self._multiple_statement(method_name, stream, bypass_types)
示例#5
0
 def setUp(self):
     self.pipe = Pipeline()
     self.pipe.append(tokenize)
     self.pipe.append(ColumnsSelect())