예제 #1
0
    def test_yaml_value(self):
        """Tests the exporter.writer.yaml_value function"""

        DbTestCase.connection.close()
        DbTestCase.connection = MockSource().list()[0]
        DbTestCase.connection.connect()
        con = DbTestCase.connection
        user = con.table('user2')
        user_dto = to_dto(user)

        self.assertEqual(
            u'!!null null',
            exporter.writer.yaml_value(
                to_dto(user.column('id')), user_dto, None))
        self.assertEqual(
            u'!!float 3.141500',
            exporter.writer.yaml_value(
                to_dto(user.column('score')), user_dto, 3.141500))
        self.assertEqual(
            u'Yes',
            exporter.writer.yaml_value(
                to_dto(user.column('deleted')), user_dto, True))
        self.assertEqual(
            u'!!str Yes',
            exporter.writer.yaml_value(
                to_dto(user.column('url')), user_dto, 'Yes'))
        self.assertEqual(
            u'!!int 3',
            exporter.writer.yaml_value(
                to_dto(user.column('score')), user_dto, Decimal(3.0)))
        self.assertEqual(
            u'!!float 3.100000',
            exporter.writer.yaml_value(
                to_dto(user.column('score')), user_dto, Decimal(3.1)))
예제 #2
0
    def test_yaml_value(self):
        """Tests the exporter.writer.yaml_value function"""

        DbTestCase.connection.close()
        DbTestCase.connection = MockSource().list()[0]
        DbTestCase.connection.connect()
        con = DbTestCase.connection
        user = con.table('user2')
        user_dto = to_dto(user)

        self.assertEqual(
            u'!!null null',
            exporter.writer.yaml_value(to_dto(user.column('id')), user_dto,
                                       None))
        self.assertEqual(
            u'!!float 3.141500',
            exporter.writer.yaml_value(to_dto(user.column('score')), user_dto,
                                       3.141500))
        self.assertEqual(
            u'Yes',
            exporter.writer.yaml_value(to_dto(user.column('deleted')),
                                       user_dto, True))
        self.assertEqual(
            u'!!str Yes',
            exporter.writer.yaml_value(to_dto(user.column('url')), user_dto,
                                       'Yes'))
        self.assertEqual(
            u'!!int 3',
            exporter.writer.yaml_value(to_dto(user.column('score')), user_dto,
                                       Decimal(3.0)))
        self.assertEqual(
            u'!!float 3.100000',
            exporter.writer.yaml_value(to_dto(user.column('score')), user_dto,
                                       Decimal(3.1)))
예제 #3
0
    def execute(self):
        """The main method that splits the arguments and starts the magic"""
        left, right = self.left, self.right

        lcon = Source.connection(left)
        if not lcon:
            raise UnknownConnectionException(
                left.uri,
                map(lambda c: c.autocomplete(), Source.connections()))
        lopts = left.get(lcon.dbms)
        rcon = Source.connection(right)
        if not rcon:
            raise UnknownConnectionException(
                right.uri,
                map(lambda c: c.autocomplete(), Source.connections()))
        ropts = right.get(rcon.dbms)

        try:
            lcon.connect(lopts.database)
            rcon.connect(ropts.database)
            ltables = lcon.tables()
            if lopts.table not in ltables:
                raise UnknownTableException(lopts.table, ltables.keys())
            ltable = ltables[lopts.table]
            rtables = rcon.tables()
            if ropts.table not in rtables:
                raise UnknownTableException(ropts.table, rtables.keys())
            rtable = rtables[ropts.table]

            lcols = map(
                column_ddl if left.compare_ddl else column_name,
                ltable.columns())
            rcols = map(
                column_ddl if right.compare_ddl else column_name,
                rtable.columns())

            lplus = dict(map(
                lambda c: (c.split()[0], ltable.column(c.split()[0])),
                list(set(lcols) - set(rcols))))
            rplus = dict(map(
                lambda c: (c.split()[0], rtable.column(c.split()[0])),
                list(set(rcols) - set(lcols))))

            r = {}
            for k, v in lplus.iteritems():
                if k in rplus:
                    r[k] = (v, rplus[k])
                else:
                    r[k] = (v, None)
            for k, v in rplus.iteritems():
                if k not in lplus:
                    r[k] = (None, v)

            return to_dto(map(lambda (k, v): v, r.iteritems()))
        finally:
            lcon.close()
            rcon.close()
예제 #4
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))

        if connection is None:
            raise UnknownConnectionException(
                options.uri,
                map(lambda c: c.autocomplete(), Source.connections()))

        if opts.show not in ['tables', 'columns', 'values']:
            raise Exception('Specify the complete URI to a table')

        return to_dto(self.build(connection, opts))
예제 #5
0
def create_items(connection, items, include, exclude, substitutes):
    results_pre = []
    results_post = []
    includes = {}

    check_excludes(items, exclude)

    item = None
    for item in items:
        for i in include:
            process_item(item, i, includes)

    if item is not None:
        for fk in includes.keys():
            if fk.a.table.name == item.table.name:
                # forward references, must be in pre
                results_pre += create_items(
                    connection,
                    connection.rows(
                        fk.b.table,
                        QueryFilter(fk.b.name, 'in', includes[fk]),
                        limit=-1,
                        simplify=False),
                    remove_prefix(fk.a.name, include),
                    remove_prefix(fk.a.name, exclude),
                    remove_prefix(fk.a.name, substitutes))
            else:
                # backward reference, must be in post
                results_post += create_items(
                    connection,
                    connection.rows(
                        fk.a.table,
                        QueryFilter(fk.a.name, 'in', includes[fk]),
                        limit=-1,
                        simplify=False),
                    remove_prefix(fk.a.table.name, include),
                    remove_prefix(fk.a.table.name, exclude),
                    remove_prefix(fk.a.table.name, substitutes))

    return results_pre + map(
        lambda i: RowItem(
            to_dto(i),
            prefixes(include),
            prefixes(exclude),
            prefixes(substitutes)),
        items) + results_post
예제 #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))

        if connection is None:
            raise UnknownConnectionException(
                options.uri,
                map(lambda c: c.autocomplete(), Source.connections()))

        if opts.show not in ['tables', 'columns', 'values']:
            raise Exception('Specify the complete URI to a table')

        return to_dto(self.build(connection, opts))
예제 #7
0
    def execute(self):
        options = self.options

        options.pattern = sanitise(options.pattern)

        cons = Source.connections()

        # search exact match of connection
        for connection in cons:
            opts = options.get(connection.dbms)
            if opts.show_code > 1 and connection.matches(opts):
                try:
                    connection.connect(opts.database)
                    return to_dto(map(
                        RowItem, opts.statement_activity(connection)))
                finally:
                    connection.close()

        raise Exception('Specify the complete URI of the connection')
예제 #8
0
def create_items(connection, items, include, exclude, substitutes):
    results_pre = []
    results_post = []
    includes = {}

    check_excludes(items, exclude)

    item = None
    for item in items:
        for i in include:
            process_item(item, i, includes)

    if item is not None:
        for fk in includes.keys():
            if fk.a.table.name == item.table.name:
                # forward references, must be in pre
                results_pre += create_items(
                    connection,
                    connection.rows(fk.b.table,
                                    QueryFilter(fk.b.name, 'in', includes[fk]),
                                    limit=-1,
                                    simplify=False),
                    remove_prefix(fk.a.name, include),
                    remove_prefix(fk.a.name, exclude),
                    remove_prefix(fk.a.name, substitutes))
            else:
                # backward reference, must be in post
                results_post += create_items(
                    connection,
                    connection.rows(fk.a.table,
                                    QueryFilter(fk.a.name, 'in', includes[fk]),
                                    limit=-1,
                                    simplify=False),
                    remove_prefix(fk.a.table.name, include),
                    remove_prefix(fk.a.table.name, exclude),
                    remove_prefix(fk.a.table.name, substitutes))

    return results_pre + map(
        lambda i: RowItem(to_dto(i), prefixes(include), prefixes(exclude),
                          prefixes(substitutes)), items) + results_post
예제 #9
0
 def to_dto(self):
     return to_dto(self)
예제 #10
0
    def process(self, connection, opts):
        # 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)
예제 #11
0
    def process(self, connection, opts):
        # 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)