def schema_for_column(column):  # pylint: disable=too-many-branches
    """Returns the Schema object for the given Column."""

    data_type = column.data_type.lower()
    column_type = column.column_type.lower()

    inclusion = 'available'
    # We want to automatically include all primary key columns
    if column.column_key.lower() == 'pri':
        inclusion = 'automatic'

    result = Schema(inclusion=inclusion)

    if data_type in BOOL_TYPES or column_type.startswith('tinyint(1)'):
        result.type = ['null', 'boolean']

    elif data_type in BYTES_FOR_INTEGER_TYPE:
        result.type = ['null', 'integer']
        bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8
        if 'unsigned' in column_type:
            result.minimum = 0
            result.maximum = 2**bits - 1
        else:
            result.minimum = 0 - 2**(bits - 1)
            result.maximum = 2**(bits - 1) - 1

    elif data_type in FLOAT_TYPES:
        result.type = ['null', 'number']

        if data_type == 'decimal':
            result.multipleOf = 10**(0 - column.numeric_scale)

    elif data_type in JSON_TYPES:
        result.type = ['null', 'object']

    elif data_type in STRING_TYPES:
        result.type = ['null', 'string']
        result.maxLength = column.character_maximum_length

    elif data_type in DATETIME_TYPES:
        result.type = ['null', 'string']

        if data_type == 'time':
            result.format = 'time'
        else:
            result.format = 'date-time'

    elif data_type in BINARY_TYPES:
        result.type = ['null', 'string']
        result.format = 'binary'

    elif data_type in SPATIAL_TYPES:
        result.type = ['null', 'object']
        result.format = 'spatial'

    else:
        result = Schema(None,
                        inclusion='unsupported',
                        description=f'Unsupported column type {column_type}')
    return result
Exemplo n.º 2
0
def schema_for_column(c):
    '''Returns the Schema object for the given Column.'''
    data_type = c.data_type.lower()
    # column_type = c.column_type.lower()

    inclusion = 'available'
    # We want to automatically include all primary key columns
    # if c.column_key.lower() == 'pri':
    #     inclusion = 'automatic'

    result = Schema(inclusion=inclusion)

    # if data_type == 'bit' or column_type.startswith('tinyint(1)'):
    #     result.type = ['null', 'boolean']

    if data_type in BYTES_FOR_INTEGER_TYPE:
        result.type = ['null', 'integer']
        bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8
        # if 'unsigned' in c.column_type:
        #     result.minimum = 0
        #     result.maximum = 2 ** bits - 1
        # else:
        #     result.minimum = 0 - 2 ** (bits - 1)
        #     result.maximum = 2 ** (bits - 1) - 1

    elif data_type in FLOAT_TYPES:
        result.type = ['null', 'number']

    elif data_type == 'decimal':
        result.type = ['null', 'number']
        result.multipleOf = 10**(0 - c.numeric_scale)
        return result

    elif data_type in STRING_TYPES:
        result.type = ['null', 'string']
        result.maxLength = c.character_maximum_length

    elif data_type in DATETIME_TYPES:
        result.type = ['null', 'string']
        result.format = 'date-time'

    else:
        result = Schema(None,
                        inclusion='unsupported',
                        description='Unsupported column type')
    return result
Exemplo n.º 3
0
def schema_for_column(col_info: Column, table_pks: List[str]):
    data_type = col_info.sql_data_type
    result = Schema()

    if data_type in INTEGER_TYPES:
        result.type = nullable_column(col_info.column_name, 'integer',
                                      table_pks)
        result.minimum = -1 * (2**(col_info.numeric_precision - 1))
        result.maximum = 2**(col_info.numeric_precision - 1)
        return result
    elif data_type in DATE_TIME_TYPES:
        result.type = nullable_column(col_info.column_name, 'string',
                                      table_pks)
        if data_type == 'time':
            result.format = 'time'
        else:
            result.format = 'date-time'
        return result
    elif data_type in FLOAT_TYPES:
        result.type = nullable_column(col_info.column_name, 'number',
                                      table_pks)
        result.exclusiveMaximum = True
        result.exclusiveMinimum = True
        result.maximum = 10**(col_info.numeric_precision -
                              col_info.numeric_scale)
        result.minimum = -10**(col_info.numeric_precision -
                               col_info.numeric_scale)
        result.multipleOf = 10**(0 - col_info.numeric_scale)
        return result
    elif data_type == 'bit':
        result.type = nullable_column(col_info.column_name, 'boolean',
                                      table_pks)
        return result
    elif data_type in STRING_TYPES:
        result.type = nullable_column(col_info.column_name, 'string',
                                      table_pks)
        result.maxLength = col_info.character_maximum_length
        return result
    elif data_type in INFERRED_STRING_TYPES:
        result.type = nullable_column(col_info.column_name, 'string',
                                      table_pks)
        return result
    return Schema(None)
def schema_for_column(column):  # pylint: disable=too-many-branches
    """Returns the Schema object for the given Column."""

    data_type = column.data_type.lower()
    column_type = column.column_type.lower()

    inclusion = "available"
    # We want to automatically include all primary key columns
    if column.column_key.lower() == "pri":
        inclusion = "automatic"

    result = Schema(inclusion=inclusion)

    if data_type == "bit" or column_type.startswith("tinyint(1)"):
        result.type = ["null", "boolean"]

    elif data_type in BYTES_FOR_INTEGER_TYPE:
        result.type = ["null", "integer"]
        bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8
        if "unsigned" in column.column_type:
            result.minimum = 0
            result.maximum = 2 ** bits - 1
        else:
            result.minimum = 0 - 2 ** (bits - 1)
            result.maximum = 2 ** (bits - 1) - 1

    elif data_type in FLOAT_TYPES:
        result.type = ["null", "number"]

    elif data_type == "json":
        result.type = ["null", "object"]

    elif data_type == "decimal":
        result.type = ["null", "number"]
        result.multipleOf = 10 ** (0 - column.numeric_scale)
        return result

    elif data_type in STRING_TYPES:
        result.type = ["null", "string"]
        if data_type in ("longtext", "mediumtext"):
            result.maxLength = 65535
        else:
            result.maxLength = column.character_maximum_length

    elif data_type in DATETIME_TYPES:
        result.type = ["null", "string"]
        result.format = "date-time"

    elif data_type == "time":
        result.type = ["null", "string"]
        result.format = "time"

    elif data_type in BINARY_TYPES:
        result.type = ["null", "string"]
        result.format = "binary"
        if data_type in ("longblob", "blob"):
            result.maxLength = 65535

    else:
        result = Schema(
            None,
            inclusion="unsupported",
            description=f"Unsupported column type {column_type}",
        )
    return result