Ejemplo n.º 1
0
    def open(self, configuration, create=False, reflect=True):

        self._rdb = self._rdb_from_configuration(configuration)

        if create and self._rdb_metadata:
            self.create(self._rdb)

        if self._orm_classes is None:
            self.OrmBase = _dm.orm_automap_base(name='OrmBase',
                                                base_iri=self.base_iri,
                                                bind=self._rdb,
                                                metadata=self._rdb_metadata)
            self.OrmBase.prepare(reflect=reflect)
            self._rdb_metadata = self.OrmBase.metadata
            self._orm_classes = \
                _frozendict((self._table_iri(class_.__table__.name), class_)
                            for class_ in self.OrmBase.classes)

        if self._orm_mappers is None:
            mappers_items = []
            colprops_items = []
            cols_datatypes_items = []
            rels_items = []
            bnode_tables = set()
            for table_iri, class_ in self._orm_classes.items():
                class_mapper = _sqla.inspect(class_)
                props = _orm_column_property_by_name(mapper=class_mapper)

                mappers_items.append((table_iri, class_mapper))
                colprops_items.append((table_iri, props))
                cols_datatypes_items\
                 .append((table_iri,
                          {colname: _common.canon_rdf_datatype_from_sql
                                     (prop.columns[0].type)
                           for colname, prop in props.items()}))
                rels_items\
                 .append((table_iri,
                          _orm_relationship_by_local_column_names
                           (mapper=class_mapper)))
                if class_mapper.has_pseudo_primary_key:
                    bnode_tables.add(table_iri)
            self._orm_mappers = _frozendict(mappers_items)
            self._orm_columns_properties = _frozendict(colprops_items)
            self._orm_columns_rdf_datatypes = _frozendict(cols_datatypes_items)
            self._orm_relationships = _frozendict(rels_items)
            self._orm_bnode_tables = frozenset(bnode_tables)

        if self._orm is None:
            self._orm = _sqla_orm.sessionmaker(bind=self._rdb)()
        self._rdb_transaction = self._rdb.begin().transaction
Ejemplo n.º 2
0
    def open(self, configuration, create=False, reflect=True):

        self._rdb = self._rdb_from_configuration(configuration)

        if create and self._rdb_metadata:
            self.create(self._rdb)

        if self._orm_classes is None:
            self.OrmBase = _dm.orm_automap_base(name='OrmBase',
                                                base_iri=self.base_iri,
                                                bind=self._rdb,
                                                metadata=self._rdb_metadata)
            self.OrmBase.prepare(reflect=reflect)
            self._rdb_metadata = self.OrmBase.metadata
            self._orm_classes = \
                _frozendict((self._table_iri(class_.__table__.name), class_)
                            for class_ in self.OrmBase.classes)

        if self._orm_mappers is None:
            mappers_items = []
            colprops_items = []
            cols_datatypes_items = []
            rels_items = []
            bnode_tables = set()
            for table_iri, class_ in self._orm_classes.items():
                class_mapper = _sqla.inspect(class_)
                props = _orm_column_property_by_name(mapper=class_mapper)

                mappers_items.append((table_iri, class_mapper))
                colprops_items.append((table_iri, props))
                cols_datatypes_items\
                 .append((table_iri,
                          {colname: _common.canon_rdf_datatype_from_sql
                                     (prop.columns[0].type)
                           for colname, prop in props.items()}))
                rels_items\
                 .append((table_iri,
                          _orm_relationship_by_local_column_names
                           (mapper=class_mapper)))
                if class_mapper.has_pseudo_primary_key:
                    bnode_tables.add(table_iri)
            self._orm_mappers = _frozendict(mappers_items)
            self._orm_columns_properties = _frozendict(colprops_items)
            self._orm_columns_rdf_datatypes = _frozendict(cols_datatypes_items)
            self._orm_relationships = _frozendict(rels_items)
            self._orm_bnode_tables = frozenset(bnode_tables)

        if self._orm is None:
            self._orm = _sqla_orm.sessionmaker(bind=self._rdb)()
        self._rdb_transaction = self._rdb.begin().transaction
Ejemplo n.º 3
0
def _orm_relationship_remote_column_name_by_local_name(mapper):
    return _frozendict((rel, _frozendict((local_col.name, remote_col.name)
                                         for local_col, remote_col
                                         in rel.local_remote_pairs))
                        for rel in mapper.relationships
                        if not rel.collection_class)
Ejemplo n.º 4
0
def _orm_relationship_by_local_column_names(mapper):
    return _frozendict((frozenset(col.name for col in rel.local_columns),
                        rel)
                       for rel in mapper.relationships
                       if not rel.collection_class)
Ejemplo n.º 5
0
def _orm_column_property_by_name(mapper):
    return _frozendict((prop.key, prop) for prop in mapper.column_attrs)
Ejemplo n.º 6
0
def _orm_relationship_remote_column_name_by_local_name(mapper):
    return _frozendict(
        (rel,
         _frozendict((local_col.name, remote_col.name)
                     for local_col, remote_col in rel.local_remote_pairs))
        for rel in mapper.relationships if not rel.collection_class)
Ejemplo n.º 7
0
def _orm_relationship_by_local_column_names(mapper):
    return _frozendict((frozenset(col.name for col in rel.local_columns), rel)
                       for rel in mapper.relationships
                       if not rel.collection_class)
Ejemplo n.º 8
0
def _orm_column_property_by_name(mapper):
    return _frozendict((prop.key, prop) for prop in mapper.column_attrs)