def test_compact1(self):
        stream = compact(tokenize(self.sql))

        result = Tokens2Unicode(stream)

        self.assertEqual(result,
                         'INSERT INTO directories(inode)VALUES(:inode)LIMIT 1')
    def test_compact2(self):
        stream = tokenize(self.sql2)

        result = compact(stream)

        self.assertEqual(Tokens2Unicode(result),
            'SELECT child_entry,asdf AS inode,creation FROM links WHERE '
            'parent_dir==:parent_dir AND name==:name LIMIT 1')
    def test_compact2(self):
        stream = tokenize(self.sql2)

        result = compact(stream)

        self.assertEqual(
            Tokens2Unicode(result),
            'SELECT child_entry,asdf AS inode,creation FROM links WHERE '
            'parent_dir==:parent_dir AND name==:name LIMIT 1')
    def test_includeStatement(self):
        stream = tokenize(self.sql)
        includeStatement = IncludeStatement('tests/files', raiseexceptions=True)
        stream = includeStatement.process(None, stream)
        stream = compact(stream)

        result = Tokens2Unicode(stream)

        self.assertEqual(result,
            'INSERT INTO dir_entries(type)VALUES(:type);INSERT INTO '
            'directories(inode)VALUES(:inode)LIMIT 1')
    def test_includeStatement(self):
        stream = tokenize(self.sql)
        includeStatement = IncludeStatement(FILES_DIR,
                                            raiseexceptions=True)
        stream = includeStatement.process(None, stream)
        stream = compact(stream)

        result = Tokens2Unicode(stream)

        self.assertEqual(
            result, (
                'INSERT INTO dir_entries(type)VALUES(:type);INSERT INTO '
                'directories(inode)VALUES(:inode)LIMIT 1'))
Beispiel #6
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)
    def test_compact3(self):
        stream = tokenize(self.sql3)

        result = compact(stream)

        self.assertEqual(Tokens2Unicode(result),
            'SELECT 0 AS st_dev,0 AS st_uid,0 AS st_gid,dir_entries.type AS '
            'st_mode,dir_entries.inode AS st_ino,COUNT(links.child_entry)AS '
            'st_nlink,:creation AS st_ctime,dir_entries.access AS st_atime,'
            'dir_entries.modification AS st_mtime,COALESCE(files.size,0)AS '
            'st_size,COALESCE(files.size,0)AS size FROM dir_entries LEFT JOIN'
            ' files ON dir_entries.inode==files.inode LEFT JOIN links ON '
            'dir_entries.inode==links.child_entry WHERE dir_entries.inode=='
            ':inode GROUP BY dir_entries.inode LIMIT 1')
    def test_compact3(self):
        stream = tokenize(self.sql3)

        result = compact(stream)

        self.assertEqual(
            Tokens2Unicode(result),
            'SELECT 0 AS st_dev,0 AS st_uid,0 AS st_gid,dir_entries.type AS '
            'st_mode,dir_entries.inode AS st_ino,COUNT(links.child_entry)AS '
            'st_nlink,:creation AS st_ctime,dir_entries.access AS st_atime,'
            'dir_entries.modification AS st_mtime,COALESCE(files.size,0)AS '
            'st_size,COALESCE(files.size,0)AS size FROM dir_entries LEFT JOIN'
            ' files ON dir_entries.inode==files.inode LEFT JOIN links ON '
            'dir_entries.inode==links.child_entry WHERE dir_entries.inode=='
            ':inode GROUP BY dir_entries.inode LIMIT 1')