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'] elif 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.exclusiveMaximum = True result.maximum = 10**(c.numeric_precision - c.numeric_scale) result.multipleOf = 10**(0 - c.numeric_scale) if 'unsigned' in column_type: result.minimum = 0 else: result.exclusiveMinimum = True result.minimum = -10**(c.numeric_precision - 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 {}'.format(column_type)) return result
def schema_for_column(c): data_type = c.sql_data_type.lower() result = Schema() if data_type in INTEGER_TYPES: result.type = nullable_column('integer', c.is_primary_key) result.minimum = -1 * (2**(c.numeric_precision - 1)) result.maximum = 2**(c.numeric_precision - 1) - 1 return result elif data_type == 'bit' and c.character_maximum_length == 1: result.type = nullable_column('boolean', c.is_primary_key) return result elif data_type == 'boolean': result.type = nullable_column('boolean', c.is_primary_key) return result elif data_type == 'uuid': result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'hstore': result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'citext': result.type = nullable_column('string', c.is_primary_key) return result elif data_type in JSON_TYPES: result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'numeric': result.type = nullable_column('number', c.is_primary_key) if c.numeric_scale is None or c.numeric_scale > MAX_SCALE: LOGGER.warning( 'capping decimal scale to 38. THIS MAY CAUSE TRUNCATION') scale = MAX_SCALE else: scale = c.numeric_scale if c.numeric_precision is None or c.numeric_precision > MAX_PRECISION: LOGGER.warning( 'capping decimal precision to 100. THIS MAY CAUSE TRUNCATION') precision = MAX_PRECISION else: precision = c.numeric_precision result.exclusiveMaximum = True result.maximum = 10**(precision - scale) result.multipleOf = 10**(0 - scale) result.exclusiveMinimum = True result.minimum = -10**(precision - scale) return result elif data_type in {'time without time zone', 'time with time zone'}: #times are treated as ordinary strings as they can not possible match RFC3339 result.type = nullable_column('string', c.is_primary_key) return result elif data_type in ('date', 'timestamp without time zone', 'timestamp with time zone'): result.type = nullable_column('string', c.is_primary_key) result.format = 'date-time' return result elif data_type in FLOAT_TYPES: result.type = nullable_column('number', c.is_primary_key) return result elif data_type == 'text': result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'character varying': result.type = nullable_column('string', c.is_primary_key) result.maxLength = c.character_maximum_length return result elif data_type == 'character': result.type = nullable_column('string', c.is_primary_key) result.maxLength = c.character_maximum_length return result return Schema(None)
def schema_for_column(c, pks_for_table): data_type = c.data_type.lower() result = Schema() numeric_scale = c.numeric_scale or DEFAULT_NUMERIC_SCALE numeric_precision = c.numeric_precision or DEFAULT_NUMERIC_PRECISION if data_type == 'number' and numeric_scale <= 0: result.type = nullable_column(c.column_name, 'integer', pks_for_table) result.minimum = -1 * (10**numeric_precision - 1) result.maximum = (10**numeric_precision - 1) if numeric_scale < 0: result.multipleOf = -10 * numeric_scale return result elif data_type == 'number': result.type = nullable_column(c.column_name, 'number', pks_for_table) result.exclusiveMaximum = True result.maximum = 10**(numeric_precision - numeric_scale) result.multipleOf = 10**(0 - numeric_scale) result.exclusiveMinimum = True result.minimum = -10**(numeric_precision - numeric_scale) return result elif data_type == 'date' or data_type.startswith("timestamp"): result.type = nullable_column(c.column_name, 'string', pks_for_table) result.format = 'date-time' return result elif data_type in FLOAT_TYPES: result.type = nullable_column(c.column_name, 'number', pks_for_table) return result elif data_type in STRING_TYPES: character_used = c.character_used result.type = nullable_column(c.column_name, 'string', pks_for_table) if character_used == 'C': result.maxLength = c.char_length return result #these column types are insane. they are NOT actually ieee754 floats #instead they are represented as decimals, but despite this #it appears we can say nothing about their max or min #"real" elif data_type == 'float' and c.numeric_precision == 63: result.type = nullable_column(c.column_name, 'number', pks_for_table) result.multipleOf = 10**-18 return result #"float", "double_precision", elif data_type in ['float', 'double_precision']: result.type = nullable_column(c.column_name, 'number', pks_for_table) result.multipleOf = 10**-38 return result return Schema(None)