Exemplo n.º 1
0
    def test_field_order(self):
        model = {
            'catalog': 'any catalog',
            'entity_id': 'any entity id',
            'fields': {},
            'all_fields': {
                'any entity id': {'type': 'any id type'},
                'ref': {'type': 'any ref type'}
            }
        }
        order = get_field_order(model)
        self.assertEqual(order, ['any entity id', 'ref'])

        model = {
            'catalog': 'any catalog',
            'entity_id': 'any',
            'fields': {
                'geo': {'type': 'GOB.Geo.Geometry'},
                'ref': {'type': 'GOB.Reference'},
                'any': {'type': 'Any type'},
                'a': {'type': 'Any type'},
                'volgnummer': {'type': 'Any type'},
                'b': {'type': 'Any type'},
            }
        }
        model['all_fields'] = {
            **model['fields'],
            'meta': {'type': 'any meta type'}
        }
        order = get_field_order(model)
        self.assertEqual(order, ['any', 'volgnummer', 'a', 'b', 'ref', 'geo', 'ref', 'meta'])
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 __init__(self, entities, model=None, ignore_fields=None, header=True):
        if model:
            self.field_specs = get_field_specifications(model)
            self.field_order = [
                f for f in get_field_order(model)
                if f not in (ignore_fields or [])
            ]
        else:
            self.field_specs, self.field_order = {}, []

        self.field_names = self._get_field_names()
        self.include_header = header
        self.entities = entities
Exemplo n.º 4
0
def csv_entities(entities, model, ignore_fields=None):
    """
    Yield the given entities as a list, starting with a header.

    :param entities:
    :param model:
    :return:
    """
    ignore_fields = ignore_fields or []
    field_specifications = get_field_specifications(model)
    field_order = [f for f in get_field_order(model) if f not in ignore_fields]

    header = _csv_header(field_specifications, field_order)
    for entity in entities:
        if header:
            yield _csv_line(header)
            header = None
        fields = _csv_record(entity, field_specifications, field_order)
        yield _csv_line(fields)
Exemplo n.º 5
0
 def test_field_order_relations(self):
     result = get_field_order('any model')
     self.assertEqual(result, REL_FIELDS)