def reflect_table_data(table, mapping=None, engine_name='default'):
    """
    Write table to Model dict
    """
    table = reflect_table(table, engine_name)
    mapping = mapping or {}

    from uliweb.utils.sorteddict import SortedDict

    field_type_map = {'VARCHAR':'str', 'VARCHAR2':'str', 'INTEGER':'int', 'FLOAT':'float'}

    meta = {}
    columns = SortedDict()
    #write columns
    _primary_key = None
    for k, v in table.columns.items():
        column_type = v.type
        type_name = column_type.__class__.__name__.lower()
        kwargs = SortedDict()
        field_type = type_name.upper()

        if type_name in ('char', 'varchar'):
            kwargs['max_length'] = column_type.length
        elif type_name in ('text', 'blob', 'integer', 'float', 'bigint'):
            pass
        elif type_name == 'long':
            field_type = 'bigint'
        elif type_name in ('clob',):
            field_type = 'TEXT'
        elif type_name in ('decimal', 'float'):
            kwargs['precision'] = v.type.precision
            kwargs['scale'] = v.type.scale
        elif type_name == 'raw': #oracle
            field_type = 'binary'
            kwargs['max_length'] = column_type.length
        elif type_name == 'number':
            if v.type.scale:
                kwargs['precision'] = v.type.precision
                kwargs['scale'] = v.type.scale
                field_type = 'DECIMAL'
            else:
                field_type = 'int'
        elif type_name == 'numeric':
            field_type = 'DECIMAL'
            kwargs['precision'] = v.type.precision
            kwargs['scale'] = v.type.scale
        elif type_name in ('timestamp',):
            field_type = 'TIMESTAMP'
        elif type_name in ('datetime', 'date', 'time'):
            pass
        #for tinyint will be treated as bool
        elif type_name in ('tinyint', 'boolean'):
            field_type = 'bool'
        else:
            raise ValueError("Don't support column [{0}] for type [{1}] when parsing {2}".format(k, type_name, table.name))

        if v.primary_key:
            kwargs['primary_key'] = True
            _primary_key = k
            if v.autoincrement:
                kwargs['autoincrement'] = True
        if not v.nullable:
            kwargs['nullable'] = False
        if v.server_default:
            server_default = v.server_default.arg
            kwargs['server_default'] = server_default
        if v.index:
            kwargs['index'] = True
        if v.unique:
            kwargs['unique'] = True
        if sa_version >= '1.2' and v.comment:
            kwargs['verbose_name'] = v.comment

        #convert field_type to common python data type
        field_type = field_type_map.get(field_type, field_type)
        columns[k] = field_type, kwargs

    meta['columns'] = columns

    indexes = []
    indexes_names = []
    for index in table.indexes:
        cols = list(index.columns)
        _len = len(cols)
        #if only one column it'll be set to Property
        if _len == 1:
            column_name = cols[0].name
            d = {'index':True}
            if index.unique:
                d['unique'] = index.unique
            columns[column_name][1].update(d)
        else:
            if not index.name in indexes_names:
                indexes.append({'name':index.name, 'columns':[x.name for x in index.columns],
                            'unique':index.unique})
                indexes_names.append(index.name)

    meta['indexes'] = indexes
    return meta
def reflect_table_data ( table , mapping = None , engine_name = 'default' ) : table = reflect_table ( table , engine_name ) mapping = mapping or { } from uliweb . utils . sorteddict import SortedDict field_type_map = { 'VARCHAR' : 'str' , 'VARCHAR2' : 'str' , 'INTEGER' : 'int' , 'FLOAT' : 'float' } meta = { } columns = SortedDict ( ) _primary_key = None for k , v in table . columns . items ( ) : column_type = v . type type_name = column_type . __class__ . __name__ . lower ( ) kwargs = SortedDict ( ) field_type = type_name . upper ( ) if type_name in ( 'char' , 'varchar' ) : kwargs [ 'max_length' ] = column_type . length elif type_name in ( 'text' , 'blob' , 'integer' , 'float' , 'bigint' ) : pass elif type_name == 'long' : field_type = 'bigint' elif type_name in ( 'clob' , ) : field_type = 'TEXT' elif type_name in ( 'decimal' , 'float' ) : kwargs [ 'precision' ] = v . type . precision kwargs [ 'scale' ] = v . type . scale elif type_name == 'raw' : field_type = 'binary' kwargs [ 'max_length' ] = column_type . length elif type_name == 'number' : if v . type . scale : kwargs [ 'precision' ] = v . type . precision kwargs [ 'scale' ] = v . type . scale field_type = 'DECIMAL' else : field_type = 'int' elif type_name == 'numeric' : field_type = 'DECIMAL' kwargs [ 'precision' ] = v . type . precision kwargs [ 'scale' ] = v . type . scale elif type_name in ( 'timestamp' , ) : field_type = 'TIMESTAMP' elif type_name in ( 'datetime' , 'date' , 'time' ) : pass elif type_name in ( 'tinyint' , 'boolean' ) : field_type = 'bool' else : raise ValueError ( "Don't support column [{0}] for type [{1}] when parsing {2}" . format ( k , type_name , table . name ) ) if v . primary_key : kwargs [ 'primary_key' ] = True _primary_key = k if v . autoincrement : kwargs [ 'autoincrement' ] = True if not v . nullable : kwargs [ 'nullable' ] = False if v . server_default : server_default = v . server_default . arg kwargs [ 'server_default' ] = server_default if v . index : kwargs [ 'index' ] = True if v . unique : kwargs [ 'unique' ] = True if sa_version >= '1.2' and v . comment : kwargs [ 'verbose_name' ] = v . comment field_type = field_type_map . get ( field_type , field_type ) columns [ k ] = field_type , kwargs meta [ 'columns' ] = columns indexes = [ ] indexes_names = [ ] for index in table . indexes : cols = list ( index . columns ) _len = len ( cols ) if _len == 1 : column_name = cols [ 0 ] . name d = { 'index' : True } if index . unique : d [ 'unique' ] = index . unique columns [ column_name ] [ 1 ] . update ( d ) else : if not index . name in indexes_names : indexes . append ( { 'name' : index . name , 'columns' : [ x . name for x in index . columns ] , 'unique' : index . unique } ) indexes_names . append ( index . name ) meta [ 'indexes' ] = indexes return meta
Write table to Model dict