Exemple #1
0
def include_array_schemas(columns, schema):
    schema['definitions'] = copy.deepcopy(BASE_RECURSIVE_SCHEMAS)

    decimal_array_columns = [key for key, value in columns.items() if value.sql_data_type == 'numeric[]']
    for col in decimal_array_columns:
        scale = post_db.numeric_scale(columns[col])
        precision = post_db.numeric_precision(columns[col])
        schema_name = schema_name_for_numeric_array(precision, scale)
        schema['definitions'][schema_name] = {'type': ['null', 'number', 'array'],
                                              'multipleOf': post_db.numeric_multiple_of(scale),
                                              'exclusiveMaximum': True,
                                              'maximum': post_db.numeric_max(precision, scale),
                                              'exclusiveMinimum': True,
                                              'minimum': post_db.numeric_min(precision, scale),
                                              'items': {'$ref': '#/definitions/{}'.format(schema_name)}}

    return schema
Exemple #2
0
def include_array_schemas(columns, schema):
    schema["definitions"] = copy.deepcopy(BASE_RECURSIVE_SCHEMAS)

    decimal_array_columns = [
        key for key, value in columns.items()
        if value.sql_data_type == "numeric[]"
    ]
    for c in decimal_array_columns:
        scale = post_db.numeric_scale(columns[c])
        precision = post_db.numeric_precision(columns[c])
        schema_name = schema_name_for_numeric_array(precision, scale)
        schema["definitions"][schema_name] = {
            "type": ["null", "number", "array"],
            "multipleOf": post_db.numeric_multiple_of(scale),
            "exclusiveMaximum": True,
            "maximum": post_db.numeric_max(precision, scale),
            "exclusiveMinimum": True,
            "minimum": post_db.numeric_min(precision, scale),
            "items": {
                "$ref": "#/definitions/{}".format(schema_name)
            },
        }

    return schema
Exemple #3
0
def schema_for_column_datatype(c):
    schema = {}
    #remove any array notation from type information as we use a separate field for that
    data_type = c.sql_data_type.lower().replace('[]', '')

    if data_type in INTEGER_TYPES:
        schema['type'] = nullable_column('integer', c.is_primary_key)
        schema['minimum'] = -1 * (2**(c.numeric_precision - 1))
        schema['maximum'] = 2**(c.numeric_precision - 1) - 1
        return schema
    if data_type == 'money':
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema
    if c.is_enum:
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    if data_type == 'bit' and c.character_maximum_length == 1:
        schema['type'] = nullable_column('boolean', c.is_primary_key)
        return schema

    if data_type == 'boolean':
        schema['type'] = nullable_column('boolean', c.is_primary_key)
        return schema

    if data_type == 'uuid':
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    if data_type == 'hstore':
        schema['type'] = nullable_column('object', c.is_primary_key)
        schema['properties'] = {}
        return schema

    if data_type == 'citext':
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    if data_type in JSON_TYPES:
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    if data_type == 'numeric':
        schema['type'] = nullable_column('number', c.is_primary_key)
        scale = post_db.numeric_scale(c)
        precision = post_db.numeric_precision(c)

        schema['exclusiveMaximum'] = True
        schema['maximum'] = post_db.numeric_max(precision, scale)
        schema['multipleOf'] = post_db.numeric_multiple_of(scale)
        schema['exclusiveMinimum'] = True
        schema['minimum'] = post_db.numeric_min(precision, scale)
        return schema

    if data_type in {'time without time zone', 'time with time zone'}:
        #times are treated as ordinary strings as they can not possible match RFC3339
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    if data_type in ('date', 'timestamp without time zone',
                     'timestamp with time zone'):
        schema['type'] = nullable_column('string', c.is_primary_key)

        schema['format'] = 'date-time'
        return schema

    if data_type in FLOAT_TYPES:
        schema['type'] = nullable_column('number', c.is_primary_key)
        return schema

    if data_type == 'text':
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    if data_type == 'character varying':
        schema['type'] = nullable_column('string', c.is_primary_key)
        if c.character_maximum_length:
            schema['maxLength'] = c.character_maximum_length

        return schema

    if data_type == 'character':
        schema['type'] = nullable_column('string', c.is_primary_key)
        if c.character_maximum_length:
            schema['maxLength'] = c.character_maximum_length
        return schema

    if data_type in {'cidr', 'inet', 'macaddr'}:
        schema['type'] = nullable_column('string', c.is_primary_key)
        return schema

    return schema
Exemple #4
0
def schema_for_column(c):
    #NB> from the post postgres docs: The current implementation does not enforce the declared number of dimensions either.
    #these means we can say nothing about an array column. its items may be more arrays or primitive types like integers
    #and this can vary on a row by row basis

    column_schema = {'type': ["null", "array"]}
    if not c.is_array:
        return schema_for_column_datatype(c)

    if c.sql_data_type == 'integer[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_integer_array'
        }
    elif c.sql_data_type == 'bit[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_boolean_array'
        }
    elif c.sql_data_type == 'boolean[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_boolean_array'
        }
    elif c.sql_data_type == 'character varying[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'cidr[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'citext[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'date[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_timestamp_array'
        }
    elif c.sql_data_type == 'numeric[]':
        scale = post_db.numeric_scale(c)
        precision = post_db.numeric_precision(c)
        schema_name = schema_name_for_numeric_array(precision, scale)
        column_schema['items'] = {
            '$ref': '#/definitions/{}'.format(schema_name)
        }

    elif c.sql_data_type == 'double precision[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_number_array'
        }
    elif c.sql_data_type == 'hstore[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_object_array'
        }
    elif c.sql_data_type == 'inet[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'json[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'jsonb[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'mac[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'money[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'real[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_number_array'
        }
    elif c.sql_data_type == 'smallint[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_integer_array'
        }
    elif c.sql_data_type == 'text[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'timestamp without time zone[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_timestamp_array'
        }
    elif c.sql_data_type == 'timestamp with time zone[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_timestamp_array'
        }
    elif c.sql_data_type == 'time[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    elif c.sql_data_type == 'uuid[]':
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    else:
        #custom datatypes like enums
        column_schema['items'] = {
            '$ref': '#/definitions/sdc_recursive_string_array'
        }
    return column_schema
Exemple #5
0
def schema_for_column_datatype(c):
    schema = {}
    # remove any array notation from type information as we use a separate field for that
    data_type = c.sql_data_type.lower().replace("[]", "")

    if data_type in INTEGER_TYPES:
        schema["type"] = nullable_column("integer", c.is_primary_key)
        schema["minimum"] = -1 * (2**(c.numeric_precision - 1))
        schema["maximum"] = 2**(c.numeric_precision - 1) - 1
        return schema

    if data_type == "money":
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema
    if c.is_enum:
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type == "bit" and c.character_maximum_length == 1:
        schema["type"] = nullable_column("boolean", c.is_primary_key)
        return schema

    if data_type == "boolean":
        schema["type"] = nullable_column("boolean", c.is_primary_key)
        return schema

    if data_type == "uuid":
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type == "hstore":
        schema["type"] = nullable_column("object", c.is_primary_key)
        schema["properties"] = {}
        return schema

    if data_type == "citext":
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type in JSON_TYPES:
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type == "numeric":
        schema["type"] = nullable_column("number", c.is_primary_key)
        scale = post_db.numeric_scale(c)
        precision = post_db.numeric_precision(c)

        schema["exclusiveMaximum"] = True
        schema["maximum"] = post_db.numeric_max(precision, scale)
        schema["multipleOf"] = post_db.numeric_multiple_of(scale)
        schema["exclusiveMinimum"] = True
        schema["minimum"] = post_db.numeric_min(precision, scale)
        return schema

    if data_type in {"time without time zone", "time with time zone"}:
        # times are treated as ordinary strings as they can not possible match RFC3339
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type in ("date", "timestamp without time zone",
                     "timestamp with time zone"):
        schema["type"] = nullable_column("string", c.is_primary_key)

        schema["format"] = "date-time"
        return schema

    if data_type in FLOAT_TYPES:
        schema["type"] = nullable_column("number", c.is_primary_key)
        return schema

    if data_type == "text":
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type == "character varying":
        schema["type"] = nullable_column("string", c.is_primary_key)
        if c.character_maximum_length:
            schema["maxLength"] = c.character_maximum_length

        return schema

    if data_type == "character":
        schema["type"] = nullable_column("string", c.is_primary_key)
        if c.character_maximum_length:
            schema["maxLength"] = c.character_maximum_length
        return schema

    if data_type in {"cidr", "inet", "macaddr"}:
        schema["type"] = nullable_column("string", c.is_primary_key)
        return schema

    if data_type == "geometry":
        schema["type"] = nullable_column("string", c.is_primary_key)

    return schema
Exemple #6
0
def schema_for_column(c):
    # NB> from the post postgres docs: The current implementation does not enforce the declared
    # number of dimensions either. This means we can say nothing about an array column. Its items
    # may be more arrays or primitive types like integers and this can vary on a row by row basis

    column_schema = {"type": ["null", "array"]}
    if not c.is_array:
        return schema_for_column_datatype(c)

    if c.sql_data_type == "integer[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_integer_array"
        }
    elif c.sql_data_type == "bigint[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_integer_array"
        }
    elif c.sql_data_type == "bit[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_boolean_array"
        }
    elif c.sql_data_type == "boolean[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_boolean_array"
        }
    elif c.sql_data_type == "character varying[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "cidr[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "citext[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "date[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_timestamp_array"
        }
    elif c.sql_data_type == "numeric[]":
        scale = post_db.numeric_scale(c)
        precision = post_db.numeric_precision(c)
        schema_name = schema_name_for_numeric_array(precision, scale)
        column_schema["items"] = {
            "$ref": "#/definitions/{}".format(schema_name)
        }
    elif c.sql_data_type == "double precision[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_number_array"
        }
    elif c.sql_data_type == "hstore[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_object_array"
        }
    elif c.sql_data_type == "inet[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "json[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "jsonb[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "mac[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "money[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "real[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_number_array"
        }
    elif c.sql_data_type == "smallint[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_integer_array"
        }
    elif c.sql_data_type == "text[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "timestamp without time zone[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_timestamp_array"
        }
    elif c.sql_data_type == "timestamp with time zone[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_timestamp_array"
        }
    elif c.sql_data_type == "time[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "uuid[]":
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    elif c.sql_data_type == "bytea[]":
        # bytea[] are unsupported
        del column_schema["type"]
    else:
        # custom datatypes like enums
        column_schema["items"] = {
            "$ref": "#/definitions/sdc_recursive_string_array"
        }
    return column_schema