Example #1
0
    def string_to_metadata(string):
        temp_metadata = MetaData()
        tb_datas = string.split(";")
        tb_datas.remove("")

        for data in tb_datas:
            tb_name = data.split(",")[0].lower()
            columns_data = data.split(",")[1:]

            table = Table(tb_name, temp_metadata)
            for column_data in columns_data:
                args = re.findall(r"[A-Z][^A-Z]+", column_data)
                c = Column(args[0].lower(),
                           ColumnTool.get_type(args[1].upper()))

                if "Nullable" in args:
                    c.nullable = True
                else:
                    c.nullable = False
                if "Unique" in args:
                    c.unique = True
                else:
                    c.unique = False

                table.append_column(c)

        return temp_metadata
Example #2
0
    def strings_to_metadata(table_str):
        temp_metadata = MetaData()
        
        for table_name, data in table_str.items():
            table = Table(table_name, temp_metadata)

            columns_datas = data.split(",")
            for column_data in columns_datas:
                args = re.findall(r"[A-Z][^A-Z]+", column_data)
                c = Column(args[0].lower(),
                           ColumnTool.get_type(args[1].upper()))
                
                if "Nullable" in args:
                    c.nullable = True
                else:
                    c.nullable = False
                if "Unique" in args:
                    c.unique = True
                else:
                    c.unique = False
                
                table.append_column(c)
        
        return temp_metadata
Example #3
0
    def render_column(self, column: Column, show_name: bool) -> str:
        args = []
        kwargs = OrderedDict()
        kwarg = []
        is_sole_pk = column.primary_key and len(column.table.primary_key) == 1
        dedicated_fks = [c for c in column.foreign_keys
                         if c.constraint and len(c.constraint.columns) == 1]
        is_unique = any(isinstance(c, UniqueConstraint) and set(c.columns) == {column}
                        for c in column.table.constraints)
        is_unique = is_unique or any(i.unique and set(i.columns) == {column}
                                     for i in column.table.indexes)
        has_index = any(set(i.columns) == {column} for i in column.table.indexes)

        if show_name:
            args.append(repr(column.name))

        # Render the column type if there are no foreign keys on it or any of them points back to
        # itself
        if not dedicated_fks or any(fk.column is column for fk in dedicated_fks):
            args.append(self.render_column_type(column.type))

        for constraint in dedicated_fks:
            args.append(self.render_constraint(constraint))

        for constraint in column.constraints:
            args.append(repr(constraint))

        if column.key != column.name:
            kwargs['key'] = column.key
        if column.primary_key:
            kwargs['primary_key'] = True
        if not column.nullable and not is_sole_pk:
            kwargs['nullable'] = False

        if is_unique:
            column.unique = True
            kwargs['unique'] = True
        elif has_index:
            column.index = True
            kwarg.append('index')
            kwargs['index'] = True

        if isinstance(column.server_default, DefaultClause):
            kwargs['server_default'] = f'text({column.server_default.arg.text!r})'
        elif Computed and isinstance(column.server_default, Computed):
            expression = column.server_default.sqltext.text

            persist_arg = ''
            if column.server_default.persisted is not None:
                persist_arg = f', persisted={column.server_default.persisted}'

            args.append(f'Computed({expression!r}{persist_arg})')
        elif Identity and isinstance(column.server_default, Identity):
            args.append(repr(column.server_default))
        elif column.server_default:
            kwargs['server_default'] = repr(column.server_default)

        comment = getattr(column, 'comment', None)
        if comment:
            kwargs['comment'] = repr(comment)

        for key, value in kwargs.items():
            args.append(f'{key}={value}')

        return f'Column({", ".join(args)})'