コード例 #1
0
ファイル: utils.py プロジェクト: woebbi/ReadableWebProxy
def version_obj(session, parent_obj):
    manager = get_versioning_manager(parent_obj)
    uow = manager.unit_of_work(session)
    for version_obj in uow.version_session:
        if (parent_class(version_obj.__class__) == parent_obj.__class__
                and identity(version_obj)[:-1] == identity(parent_obj)):
            return version_obj
コード例 #2
0
async def deepcopy_recursive(
    model: Model,
    object_id: str,
    new_fk_link_id: Optional[str] = None,
    fk_column: Optional[Column] = None,
    new_id: Optional[str] = None,
):
    logger.debug(
        f"Making a deepcopy of {model} with id {object_id} linking to foreign key"
        f" {fk_column} with id {new_fk_link_id}"
    )
    new_obj_key = await create_object_copy(
        model.__tablename__, object_id, fk_column, new_fk_link_id, new_id=new_id
    )
    primary_key_col = identity(model)[0]
    dependent_models = {}
    # TODO(ehborisov): check how it works in the case of composite key
    for m_id, data in cfg.models.items():
        for column in cfg.app.db.tables[m_id].columns:
            if column.references(primary_key_col):
                dependent_models[data["model"]] = column
    for dep_model in dependent_models:
        fk_column = dependent_models[dep_model]
        all_referencing_instance_ids = (
            await dep_model.select(identity(dep_model)[0].name)
            .where(fk_column == object_id)
            .gino.all()
        )
        # TODO(ehborisov): can gather be used there? Only if we have a connection pool?
        for inst_id in all_referencing_instance_ids:
            await deepcopy_recursive(dep_model, inst_id[0], new_obj_key, fk_column)
    logger.debug(f"Finished copying, returning newly created object's id {new_obj_key}")
    return new_obj_key
コード例 #3
0
def version_obj(session, parent_obj):
    manager = get_versioning_manager(parent_obj)
    uow = manager.unit_of_work(session)
    for version_obj in uow.version_session:
        if (
            parent_class(version_obj.__class__) == parent_obj.__class__ and
            identity(version_obj)[:-1] == identity(parent_obj)
        ):
            return version_obj
コード例 #4
0
async def deepcopy_recursive(
    model: Model,
    object_id: str,
    model_data: Dict,
    *,
    new_fk_link_id: Optional[str] = None,
    fk_column: Optional[Column] = None,
    new_id: Optional[str] = None,
):
    logger.debug(
        f"Making a deepcopy of {model} with id {object_id} linking to foreign key"
        f" {fk_column} with id {new_fk_link_id}"
    )
    new_obj_key = await create_object_copy(
        model.__tablename__,
        object_id,
        model_data,
        fk_column=fk_column,
        new_fk_link_id=new_fk_link_id,
        new_id=new_id,
    )
    if len(identity(model)) == 0:
        primary_key_col = object_id
        return (
            "Deepcopy does not available for tables without primary keys right now",
            "error",
        )
    else:
        primary_key_col = identity(model)[0]

    dependent_models = {}
    # TODO(ehborisov): check how it works in the case of composite key
    for m_id, data in cfg.models.items():
        table_name = get_table_name(m_id)
        for column in cfg.app.db.tables[table_name].columns:
            if column.references(primary_key_col):
                dependent_models[data["model"]] = column
    for dep_model in dependent_models:
        fk_column = dependent_models[dep_model]
        all_referencing_instance_ids = (
            await dep_model.select(identity(dep_model)[0].name)
            .where(fk_column == object_id[primary_key_col.name])
            .gino.all()
        )
        # TODO(ehborisov): can gather be used there? Only if we have a connection pool?
        for inst_id in all_referencing_instance_ids:
            result = await deepcopy_recursive(
                dep_model,
                {identity(dep_model)[0].name: inst_id[0]},
                model_data,
                new_fk_link_id=new_obj_key[identity(model)[0].name],
                fk_column=fk_column,
            )
            if isinstance(result, tuple):
                return result
    logger.debug(f"Finished copying, returning newly created object's id {new_obj_key}")
    return new_obj_key
コード例 #5
0
ファイル: generic.py プロジェクト: myneuronews/flbrh
 def __eq__(self, other):
     discriminator = six.text_type(type(other).__name__)
     q = self.property._discriminator_col == discriminator
     other_id = identity(other)
     for index, id in enumerate(self.property._id_cols):
         q &= id == other_id[index]
     return q
コード例 #6
0
ファイル: generic.py プロジェクト: cheungpat/sqlalchemy-utils
 def __eq__(self, other):
     discriminator = six.text_type(type(other).__name__)
     q = self.property._discriminator_col == discriminator
     other_id = identity(other)
     for index, id in enumerate(self.property._id_cols):
         q &= id == other_id[index]
     return q
コード例 #7
0
    def wrapper(self, mapper, connection, target):
        if not self.manager.options['versioning']:
            return
        if not is_versioned(target):
            return
        # we cannot use target._sa_instance_state.identity here since object's
        # is not yet updated at this phase
        key = (target.__class__, identity(target))

        return func(self, key, target)
コード例 #8
0
 def test_identity_for_class(self):
     assert identity(self.Building) == (self.Building.id, )
コード例 #9
0
    def test_for_transient_class_with_id(self):
        building = self.Building(name=u'Some building')
        self.session.add(building)
        self.session.flush()

        assert identity(building) == (building.id, )
コード例 #10
0
 def test_for_transient_class_without_id(self):
     assert identity(self.Building()) == (None, )
コード例 #11
0
    def test_for_transient_class_with_id(self):
        building = self.Building(name=u'Some building')
        self.session.add(building)
        self.session.flush()

        assert identity(building) == (building.id, )
コード例 #12
0
 def test_for_transient_class_without_id(self):
     assert identity(self.Building()) is None
コード例 #13
0
 def test_identity_for_class(self):
     assert identity(self.Building) == (self.Building.id, )
コード例 #14
0
 def test_identity_for_class(self, Building):
     assert identity(Building) == (Building.id, )
コード例 #15
0
 def test_identity_for_class(self, Building):
     assert identity(Building) == (Building.id, )
コード例 #16
0
 def test_for_transient_class_without_id(self, Building):
     assert identity(Building()) == (None, )