zhyq0826 / zhyq0826.github.io

三月沙的博客
http://sanyuesha.com
6 stars 1 forks source link

sqlalchemy first glance : 初探 #54

Closed zhyq0826 closed 5 years ago

zhyq0826 commented 7 years ago

http://www.sqlalchemy.org/features.html

sqlalchemy 特性

No ORM Required

ORM 不是必须的。 sqlalchemy 包含两部分独立的组件: core 和 ORM。core 层本质就是提供各种特性的 SQL 抽象工具集,包含有

ORM 就是构建于 core 之上的一个可选 package

Mature, High Performing Architecture

DBA Approved

是对 DBA 友好的,提供生成 SQL 语句的功能,而且经过全面优化,完全的事物支持和批量写入支持,支持对象之间的实体引用

unit of work

The Unit Of Work system, 中央式的对象映射系统,实现对 insert、update、delete 操作进行队列式管理,批量写入。这样的实现,最大程度的提升了效率,并且保证事物是安全的,而且把死锁发生的几率降到最低。

Fowler's "Unit of Work" pattern

Function-based query construction

基于函数的查询构建语言,允许 SQL 语句构建在 Python 的函数和表达式之上,包含

The full range of what's possible includes boolean expressions, operators, functions, table aliases, selectable subqueries, insert/update/delete statements, correlated updates, selects, and EXISTS clauses, UNION clauses, inner and outer joins, bind parameters, and free mixing of literal text within expressions。

Modular and Extensible

模块化和可扩展的。sqlalchemy 的不同部分都可以单独使用而不互相影响,比如 connect pooling,SQL statement compilation,transactionl service 都可以独立使用而不相互依赖,而且他们还可以通过各种 plugin points 进行扩展。

内部集成的事件系统可以把自定义代码注入到调用会话多达50处,包含

core statement execution,schema generation and introspection,connection pool operation,object relation configuration,persistence operations,attribute mutation events, and transactional stages。

新的 SQL 语句和 SQL 类型也可以无缝的集成。

Separate mapping and class design

类的定义和映射是完全分开的。虽然 ORM 提供了使用 class 的方式来映射 table metadata,但是这种方式是完全可选的,ORM 认为 class 和 class mapping 的 table metata 是完全独立的。通过 mapper 函数,任意的 Python class 都可以映射 到 table 和 视图

Eager-loading and caching of related objects and collections

Pre- and post-processing of data

预处理和后处理数据

Raw SQL statement mapping

支持纯 SQL 语句的执行,并且返回的结果集可以像 ORM 一样,以对象的方式存取。

SQLA's object relational query facilities can accommodate raw SQL statements as well as plain result sets, and object instances can be generated from these results in the same manner as any other ORM operation