예제 #1
0
    def _get_column_info(self, name, type_, nullable, autoincrement, default,
                         precision, scale, length):

        coltype = self.ischema_names.get(type_, None)

        kwargs = {}

        if coltype in (NUMERIC, DECIMAL):
            args = (precision, scale)
        elif coltype == FLOAT:
            args = (precision,)
        elif coltype in (CHAR, VARCHAR, UNICHAR, UNIVARCHAR, NCHAR, NVARCHAR):
            args = (length,)
        else:
            args = ()

        if coltype:
            coltype = coltype(*args, **kwargs)
            # is this necessary
            # if is_array:
            #     coltype = ARRAY(coltype)
        else:
            util.warn("Did not recognize type '%s' of column '%s'" %
                      (type_, name))
            coltype = sqltypes.NULLTYPE

        if default:
            default = default.replace("DEFAULT", "").strip()
            default = re.sub("^'(.*)'$", lambda m: m.group(1), default)
        else:
            default = None

        column_info = dict(name=name, type=coltype, nullable=nullable,
                           default=default, autoincrement=autoincrement)
        return column_info
예제 #2
0
    def _get_column_info(self, name, type_, nullable, autoincrement, default,
                         precision, scale, length):

        coltype = self.ischema_names.get(type_, None)

        kwargs = {}

        if coltype in (NUMERIC, DECIMAL):
            args = (precision, scale)
        elif coltype == FLOAT:
            args = (precision,)
        elif coltype in (CHAR, VARCHAR, UNICHAR, UNIVARCHAR, NCHAR, NVARCHAR):
            args = (length,)
        else:
            args = ()

        if coltype:
            coltype = coltype(*args, **kwargs)
            # is this necessary
            # if is_array:
            #     coltype = ARRAY(coltype)
        else:
            util.warn("Did not recognize type '%s' of column '%s'" %
                      (type_, name))
            coltype = sqltypes.NULLTYPE

        if default:
            default = default.replace("DEFAULT", "").strip()
            default = re.sub("^'(.*)'$", lambda m: m.group(1), default)
        else:
            default = None

        column_info = dict(name=name, type=coltype, nullable=nullable,
                           default=default, autoincrement=autoincrement)
        return column_info
    def get_columns(self, connection, table_name, schema=None, **kw):
        stmt = text(
                    "SELECT column_name, data_type, is_nullable, "
                    "character_maximum_length, "
                    "numeric_precision, numeric_scale, "
                    "column_default, "
                    "is_identity, identity_start, identity_increment "
                    "FROM information_schema.columns "
                    "WHERE table_schema=:schema AND table_name=:table "
                    "ORDER BY ordinal_position").bindparams(
                        schema=schema or self.default_schema_name,
                        table=table_name
                    )

        columns = []
        for cname, type_, nullable, length, precision, \
            scale, default, is_ident, ident_start, ident_increment in connection.execute(stmt):

            try:
                coltype = ischema_names[type_]
            except KeyError:
                util.warn("Did not recognize type '%s' of column '%s'" %
                      (type_, cname))
                coltype = sqltypes.NULLTYPE
            else:
                if issubclass(coltype, sqltypes.Float):
                    coltype = coltype()
                elif issubclass(coltype, sqltypes.Numeric):
                    coltype = coltype(precision, scale)
                elif issubclass(coltype, sqltypes.String):
                    coltype = coltype(length)
                    if default:
                        default = "'%s'" % default.replace("'", "''")
                else:
                    coltype = coltype()
            autoincrement = is_ident == 'YES'
            nullable = nullable == 'YES'

            column_info = dict(name=cname, type=coltype, nullable=nullable,
                           default=default, autoincrement=autoincrement)

            columns.append(column_info)
        return columns