表达式序列化器扩展¶
Expression Serializer Extension
用于 SQLAlchemy 查询结构的序列化器/反序列化器对象,允许实现“上下文感知”的反序列化。
Legacy Feature
序列化器扩展是 遗留功能,不建议在新的开发中使用。
任何 SQLAlchemy 的查询结构,无论基于 sqlalchemy.sql.*
还是 sqlalchemy.orm.*
,都可以使用。
结构中引用的映射器(Mapper)、表(Table)、列(Column)、会话(Session)等对象不会以序列化形式持久化,
而是在反序列化时重新关联到该查询结构。
警告
序列化器扩展使用 Python 的 pickle 模块进行对象的序列化和反序列化,因此需要遵循 Python 官方文档 中关于安全性的说明。
用法几乎与标准的 Python pickle 模块相同:
from sqlalchemy.ext.serializer import loads, dumps
metadata = MetaData(bind=some_engine)
Session = scoped_session(sessionmaker())
# ... 定义映射器
query = (
Session.query(MyClass)
.filter(MyClass.somedata == "foo")
.order_by(MyClass.sortkey)
)
# 序列化查询对象
serialized = dumps(query)
# 反序列化。传入 metadata 和 scoped_session
query2 = loads(serialized, metadata, Session)
print(query2.all())
使用限制与原生 pickle 相似;被映射的类必须本身是可 pickle 的,即它们必须能从模块级命名空间中被导入。
该序列化器模块仅适用于查询结构。不适用于以下情况:
用户自定义类的实例。通常情况下这些实例不包含对引擎、会话或表达式结构的引用,因此可以直接序列化。
需要完全从序列化结构中加载的表元数据(即在应用中尚未声明的)。对于这种情况,可以直接使用标准的
pickle.loads()/dumps()
来完整序列化任何MetaData
对象,通常用于将某个时间点反射得到的 数据库结构保存下来。序列化器模块则是为另一种情况而设计 —— 当表元数据已存在于内存中时使用。
Serializer/Deserializer objects for usage with SQLAlchemy query structures, allowing “contextual” deserialization.
Legacy Feature
The serializer extension is legacy and should not be used for new development.
Any SQLAlchemy query structure, either based on sqlalchemy.sql.* or sqlalchemy.orm.* can be used. The mappers, Tables, Columns, Session etc. which are referenced by the structure are not persisted in serialized form, but are instead re-associated with the query structure when it is deserialized.
警告
The serializer extension uses pickle to serialize and deserialize objects, so the same security consideration mentioned in the python documentation apply.
Usage is nearly the same as that of the standard Python pickle module:
from sqlalchemy.ext.serializer import loads, dumps
metadata = MetaData(bind=some_engine)
Session = scoped_session(sessionmaker())
# ... define mappers
query = (
Session.query(MyClass)
.filter(MyClass.somedata == "foo")
.order_by(MyClass.sortkey)
)
# pickle the query
serialized = dumps(query)
# unpickle. Pass in metadata + scoped_session
query2 = loads(serialized, metadata, Session)
print(query2.all())
Similar restrictions as when using raw pickle apply; mapped classes must be themselves be pickleable, meaning they are importable from a module-level namespace.
The serializer module is only appropriate for query structures. It is not needed for:
instances of user-defined classes. These contain no references to engines, sessions or expression constructs in the typical case and can be serialized directly.
Table metadata that is to be loaded entirely from the serialized structure (i.e. is not already declared in the application). Regular pickle.loads()/dumps() can be used to fully dump any
MetaData
object, typically one which was reflected from an existing database at some previous point in time. The serializer module is specifically for the opposite case, where the Table metadata is already present in memory.
- class sqlalchemy.ext.serializer.Deserializer¶
Members
Class signature
class
sqlalchemy.ext.serializer.Deserializer
(_pickle.Unpickler
)-
method
sqlalchemy.ext.serializer.Deserializer.
get_engine()¶
-
method
sqlalchemy.ext.serializer.Deserializer.
persistent_load(id_)¶
-
method
- class sqlalchemy.ext.serializer.Serializer¶
Members
Class signature
class
sqlalchemy.ext.serializer.Serializer
(_pickle.Pickler
)-
method
sqlalchemy.ext.serializer.Serializer.
persistent_id(obj)¶
-
method
- function sqlalchemy.ext.serializer.dumps(obj, protocol=5)¶
- function sqlalchemy.ext.serializer.loads(data, metadata=None, scoped_session=None, engine=None)¶