Exemplo n.º 1
0
def _csv_reference_values(value, spec):
    """
    Returns the CSV values for the given reference and type specification
    Note that the result is an array, as a reference value results in multiple CSV values

    :param value:
    :param spec:
    :return:
    """
    values = []
    if spec['type'] == "GOB.Reference":
        dst = value or {}
        add_unique_reference(dst)
        for field in get_reference_fields(spec):
            sub_value = dst.get(field, None)
            values.append(_csv_value(sub_value))
    else:  # GOB.ManyReference
        dsts = value or []
        for dst in dsts:
            add_unique_reference(dst)
        for field in get_reference_fields(spec):
            sub_values = []
            for dst in dsts:
                sub_value = dst.get(field, None)
                sub_values.append(_csv_value(sub_value))
            values.append("[" + ",".join(sub_values) + "]")
    return values
Exemplo n.º 2
0
def _create_table(schema,
                  catalog_name,
                  collection_name,
                  model,
                  tablename=None):
    """
    Returns a SQL statement to create a table in a schema
    The table fields are constructed from the specs

    :param schema:
    :param collection_name:
    :param specs:
    :param tablename: if None, collection_name is used
    :return:
    """
    specs = get_field_specifications(model)
    order = _autorized_order(get_field_order(model), catalog_name,
                             collection_name)
    catalog = GOBModel().get_catalog(catalog_name)
    catalog_description = quote_sql_string(catalog['description'])
    fields = []
    for field_name in order:
        field_spec = specs[field_name]
        field_description = quote_sql_string(field_spec['description'])
        if field_spec['type'] in REFERENCE_TYPES:
            for reference_field in get_reference_fields(field_spec):
                name = joined_names(field_name, reference_field)
                fields.append(
                    _create_field(name, fully_qualified_type_name(String),
                                  f"{field_description} ({reference_field})"))
        elif field_spec['type'] in JSON_TYPES:
            for field, spec in field_spec['attributes'].items():
                name = joined_names(field_name, field)
                # Make all JSON attribute columns of type String (in case the resulting values are merged into a list)
                fields.append(
                    _create_field(name, fully_qualified_type_name(String),
                                  f"{field_description} ({field})"))
        else:
            fields.append(
                _create_field(field_name, field_spec['type'],
                              field_description))

    field_lengths = [len(field['name']) for field in fields]
    max_length = max(field_lengths) if field_lengths else 1

    table_name = (f"{_quote(schema)}.{_quote(tablename or collection_name)}")
    table_fields = ",\n  ".join(
        [f"{field['name']:{max_length}} {field['type']}" for field in fields])

    comments = ";\n".join([
        f"COMMENT ON COLUMN {table_name}.{field['name']:{max_length}} "
        f"IS {SQL_QUOTATION_MARK}{field['description']}{SQL_QUOTATION_MARK}"
        for field in fields
    ])

    primary_key = f",PRIMARY KEY ({UNIQUE_ID})" if UNIQUE_ID in order else ""

    return f"""
Exemplo n.º 3
0
def _create_indexes(model):
    indexes = []
    for field, spec in get_field_specifications(model).items():
        if field == model['entity_id'] or re.compile(r"(^|.+_)ref$").match(
                field) or field == FIELD.LAST_EVENT:
            indexes.append(
                {'field': field}
            )  # Plain entity id, full entity id (ref) or rel. foreign key (eg dst_ref)
        elif "GOB.Geo" in spec['type']:
            indexes.append({'field': field, 'method': "gist"})  # Spatial index
        elif spec['type'] == "GOB.Reference" and "ref" in get_reference_fields(
                spec):
            indexes.append({'field': f"{field}_ref"})  # Foreign key index

    return indexes
Exemplo n.º 4
0
def _csv_header(field_specs, field_order):
    """
    Returns the CSV header fields for the given type specifications

    :param field_specs:
    :return:
    """
    fields = []
    for field_name in field_order:
        field_spec = field_specs[field_name]
        if field_spec['type'] in REFERENCE_TYPES:
            for reference_field in get_reference_fields(field_spec):
                fields.append(
                    _csv_value(joined_names(field_name, reference_field)))
        elif field_spec['type'] == 'GOB.JSON':
            for field in field_spec['attributes'].keys():
                fields.append(_csv_value(joined_names(field_name, field)))
        else:
            fields.append(_csv_value(field_name))
    return fields
Exemplo n.º 5
0
    def _get_field_names(self) -> list:
        """
        Returns the fieldnames for field specifications, based on the field order

        :return:
        """
        fields = []

        for field_name in self.field_order:
            field_spec = self.field_specs[field_name]

            if field_spec['type'] in REFERENCE_TYPES:
                for reference_field in get_reference_fields(field_spec):
                    fields.append(joined_names(field_name, reference_field))

            elif field_spec['type'] in JSON_TYPES:
                for field in field_spec['attributes'].keys():
                    fields.append(joined_names(field_name, field))

            else:
                fields.append(field_name)

        return fields
Exemplo n.º 6
0
 def test_reference_fields(self):
     self.assertEqual([FIELD.SOURCE_VALUE], get_reference_fields({'ref': 'a:b'}))