예제 #1
0
    def export(self, **options):
        '''
        Exports the data into a SQLite 3 file-like stream.

        Arguments:
            options (dict): The exporting options

        Returns:
            io.BytesIO: A SQLite 3 file-like stream

        Raises:
            ExportError: When data fails to export
        '''
        sql_options = dict(options, dialect='sqlite')
        sql_data = SqlExporter(self._data).export(**sql_options)
        sqlite_data = io.BytesIO()

        with tempfile.NamedTemporaryFile() as sqlite_file:
            with sqlite3.connect(sqlite_file.name) as sqlite_con:
                sqlite_cursor = sqlite_con.cursor()
                sqlite_cursor.execute('PRAGMA page_size = 1024')
                sqlite_cursor.execute('PRAGMA foreign_keys = ON')
                sqlite_cursor.executescript('BEGIN; {} COMMIT' \
                                                .format(sql_data.read()))

            sqlite_data.write(sqlite_file.read())
            sqlite_data.seek(0)

        return sqlite_data
예제 #2
0
    def export(self, **options):
        '''
        Exports the data into a Firebird Embedded file-like stream.

        Arguments:
            options (dict): The exporting options

        Returns:
            io.BytesIO: A Firebird Embedded file-like stream

        Raises:
            ExportError: When data fails to export
        '''
        sql_options = dict(options, dialect='firebird')
        sql_data = SqlExporter(self._data).export(**sql_options)
        fdb_data = io.BytesIO()
        fdb_file = tempfile.mktemp()
        fdb_con = fdb.create_database(
            "CREATE DATABASE '{}' USER '{}' PASSWORD '{}'" \
                .format(fdb_file, 'sysdba', 'masterkey'),
            sql_dialect=3
        )
        fdb_cursor = fdb_con.cursor()
        in_trans = False

        for stmt in sql_data.read().rstrip(';').split(';'):
            if stmt.startswith('INSERT') and not in_trans:
                fdb_con.begin()
                in_trans = True
            else:
                fdb_con.commit()
                in_trans = False

            fdb_cursor.execute(stmt)

        fdb_data.write(File(fdb_file).readBytes())
        fdb_data.seek(0)

        return fdb_data