def queryone(self, query, mapper=None):
        timer = LogTimer(logger, "Query one", "Querying one:\n%s", query)
        result = query.one()
        timer.stop()

        if mapper:
            mapper.map(result)

        return result
    def queryall(self, query, mapper=None):
        timer = LogTimer(logger, "Query all", "Querying all:\n%s", query)
        result = query.all()
        timer.stop()

        if mapper:
            for row in result:
                mapper.map(row)

        return result
    def execute(self, query):
        cur = self.cursor()
        if not cur:
            raise Exception("Database is not connected")

        timer = LogTimer(logger, "Execution", "Executing:\n%s", query)
        try:
            return cur.execute(escape_statement(query))
        finally:
            timer.stop()
示例#4
0
def read_sql(file_):
    timer = LogTimer(logger, 'Reading input statements')

    try:
        sql = file_.read()
    except KeyboardInterrupt:  # pragma: no cover
        sql = None
    finally:
        file_.close()

    timer.stop()

    return sql
示例#5
0
def read_statements(opts):
    if opts.statements is not None:
        sql = opts.statements
    else:
        sql = read_sql(opts.infile)

    if not sql:
        return None

    timer = LogTimer(logger, 'Splitting SQL statements')

    # Removes the shebang, if any
    sql = re.sub(r'^#!.*\n', '', sql)

    stmts = filter(lambda s: len(s.strip()) > 0, sqlparse.split(sql))

    timer.stop()

    logger.info('Number of SQL statements: %d', len(stmts))

    return stmts
示例#6
0
    def execute(self):
        """The main method that splits the arguments and starts the magic"""
        options = self.options

        # search exact match of connection
        connection, opts = find_connection(
            Source.connections(),
            options,
            lambda con, opts: (
                con.matches(opts)
                and opts.show in ['databases', 'tables', 'columns', 'values']))

        if connection is None:
            raise UnknownConnectionException(options.uri)

        # Reads the statements
        stmts = read_statements(opts)

        # Exit gracefully when no statements have been found (or the
        # input got cancelled)
        if not stmts:
            return []

        # Collects results
        results = []
        # Counts the changes (inserts, updates)
        changes = 0
        # Counts the errors (useful with option --ignore-errors)
        errors = 0
        # Counts the statements
        counter = 0

        timer = None
        executer = None
        try:
            # Connects to the database and starts a transaction
            connection.connect(opts.database)

            timer = LogTimer(logger, 'Executing SQL statements')

            if opts.isolate_statements:
                executer = IsolationExecuter(connection, opts)
            else:
                executer = DefaultExecuter(connection, opts)

            executer.begin()

            for stmt in stmts:
                start = datetime.now()
                sys.stdout.write(
                    EXECUTION_START.get(opts.verbose, '').format(
                        time=start,
                        statement=stmt))

                res, changed, failed = executer.execute(stmt)
                results.extend(res)
                changes += changed
                errors += failed
                counter += 1

                if (opts.progress > 0 and counter % opts.progress == 0):
                    sys.stderr.write('.')
                    sys.stderr.flush()

                end = datetime.now()
                sys.stdout.write(
                    EXECUTION_END.get(opts.verbose, '').format(
                        time=end,
                        statement=trim_space(stmt),
                        duration=(end - start).total_seconds()))

            if opts.dry_run:
                executer.rollback()
            else:
                executer.commit()

            if opts.progress > 0 and counter >= opts.progress:
                # Write a new line after progress indicator dots have
                # been written
                sys.stderr.write('\n')
        except BaseException:
            errors += 1
            if executer:
                executer.rollback()
            if not opts.mute_errors:
                raise
        finally:
            connection.close()
            if timer:
                timer.stop()

        if not results:
            dry_run = ''
            if opts.dry_run:
                dry_run = ' (dry run)'
            sys.stdout.write(
                'Changed rows: {0}{1}\n'.format(changes, dry_run))
        if errors:
            sys.stdout.write('Errors: {0}'.format(errors))

        return to_dto(results)