extended_declarative_base

Extend the power of declarative base.

class sqlalchemy_mate.orm.extended_declarative_base.ExtendedBase[source]

Provide additional method.

Example:

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base, ExtendedBase):
    ... do what you do with sqlalchemy ORM
classmethod pk_names()[source]

Primary key column name list.

Return type:tuple
pk_values()[source]

Primary key values

classmethod id_field_name()[source]

If only one primary_key, then return it. Otherwise, raise ValueError.

Return type:str
classmethod keys()[source]

return list of all declared columns.

Return type:List[str]
values()[source]

return list of value of all declared columns.

items()[source]

return list of pair of name and value of all declared columns.

to_dict(include_null=True)[source]

Convert to dict.

Return type:dict
to_OrderedDict(include_null=True)[source]

Convert to OrderedDict.

Return type:OrderedDict
glance()[source]

Print itself, only display attributes defined in ExtendedBase._settings_major_attrs

absorb(other, ignore_none=True)[source]

For attributes of others that value is not None, assign it to self.

中文文档

将另一个文档中的数据更新到本条文档。当且仅当数据值不为None时。

revise(data, ignore_none=True)[source]

Revise attributes value with dictionary data.

中文文档

将一个字典中的数据更新到本条文档。当且仅当数据值不为None时。

classmethod by_id(_id, engine_or_session)[source]

Get one object by primary_key value.

classmethod by_sql(sql, engine_or_session)[source]

Query with sql statement or texture sql.

classmethod smart_insert(engine_or_session, data, minimal_size=5, op_counter=0)[source]

An optimized Insert strategy.

Returns:number of insertion operation been executed. Usually it is greatly smaller than len(data).

Warning

This operation is not atomic, if you force stop the program, then it could be only partially completed

中文文档

在Insert中, 如果已经预知不会出现IntegrityError, 那么使用Bulk Insert的速度要 远远快于逐条Insert。而如果无法预知, 那么我们采用如下策略:

  1. 尝试Bulk Insert, Bulk Insert由于在结束前不Commit, 所以速度很快。
  2. 如果失败了, 那么对数据的条数开平方根, 进行分包, 然后对每个包重复该逻辑。
  3. 若还是尝试失败, 则继续分包, 当分包的大小小于一定数量时, 则使用逐条插入。
直到成功为止。

该Insert策略在内存上需要额外的 sqrt(nbytes) 的开销, 跟原数据相比体积很小。 但时间上是各种情况下平均最优的。

classmethod update_all(engine, obj_or_data, upsert=False)[source]

The sqlalchemy.crud.updating.update_all() function in ORM syntax.

Parameters:
  • engine (Engine) – an engine created by``sqlalchemy.create_engine``.
  • obj_or_data (Union[ExtendedBase, List[ExtendedBase]]) – single object or list of object
  • upsert (bool) – if True, then do insert also.
classmethod upsert_all(engine, obj_or_data)[source]

The sqlalchemy.crud.updating.upsert_all() function in ORM syntax.

Parameters:
  • engine (Engine) – an engine created by``sqlalchemy.create_engine``.
  • obj_or_data (Union[ExtendedBase, List[ExtendedBase]]) – single object or list of object
classmethod random(engine_or_session, limit=5)[source]

Return random ORM instance.

Return type:List[ExtendedBase]