zhyq0826 / zhyq0826.github.io

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

sqlalchemy architechture : 架构 #55

Closed zhyq0826 closed 5 years ago

zhyq0826 commented 7 years ago

http://aosabook.org/en/sqlalchemy.html

The core/orm dichotomy

core 和 orm 分离,各自负担各自的功能

turbo

taming DBAPI

The Dialect System

Dialect system

The result returned by the execute method of Engine or Connection is called a ResultProxy, which offers an interface similar to the DBAPI cursor but with richer behavior. The Engine, Connection, and ResultProxy correspond to the DBAPI module, an instance of a specific DBAPI connection, and an instance of a specific DBAPI cursor, respectively.

Engine 和 Connection 的执行 将会返回 ResultProxy,类似于 DBAPI 的cursor,但是提供了更丰富的功能。 Engine, Connection, ResultProxy 分别对应 DBAPI 模块,特定 DBAPI 连接实例,特定 DBAPI cursor。

Behind the scenes, the Engine references an object called a Dialect. The Dialect is an abstract class for which many implementations exist, each one targeted at a specific DBAPI/database combination. A Connection created on behalf of the Engine will refer to this Dialect for all decisions, which may have varied behaviors depending on the target DBAPI and database in use.

Engine 引用着 Dialect,Dialect 是一个抽象 Class,针对特定的 DBAPI 和 相对应数据库的都有一个对应的 DIalect。Engine 创建时 会创建 一个 Connection,而 Connection 会根据选择的 DBAPI 和 数据库 而引用相应的 Dialect。

During a statement execution, an additional object called an ExecutionContext is created by the Connection. The object lasts from the point of execution throughout the lifespan of the ResultProxy. It may also be available as a specific subclass for some DBAPI/database combinations

在 statement 执行期间,一个叫 ExecutionContext 对象会被创建。这个对象将伴随 ResultProxy 的整个生命周期。

Dealing with DBAPI Variability

The Dialect and ExecutionContext objects provide a means to define every interaction with the database and DBAPI, including how connection arguments are formatted and how special quirks during statement execution are handled. The Dialect is also a factory for SQL compilation constructs that render SQL correctly for the target database, and type objects which define how Python data should be marshaled to and from the target DBAPI and database

Dialect 和 ExecutionContext 提供了一种可以自定义和 DBAPI 以及 db 交互的方式。比如连接参数是如何格式化。Dialect 同时也是一个 SQL 的工厂函数,负责着 SQL 的翻译,Python 对象如何和数据库交互。

Schema Definition

This is where SQLAlchemy offers the first strong division of Core and ORM, by offering the Table and Column constructs that describe the structure of the database independently of a user's model class definition. The rationale behind the division of schema definition from object relational mapping is that the relational schema can be designed unambiguously in terms of the relational database, including platform-specific details if necessary, without being muddled by object-relational concepts—these remain a separate concern

sqlalchemy 把 table 和 column 的构建与 user model 的定义完全分开了。其背后的意义在于,relational schema 定义根据 db 的不同将完全不一样,为了不把两个概念混淆,sqlalchemy 将其完全分开。

The Table and Column model falls under the scope of what's referred to as metadata, offering a collection object called MetaData to represent a collection of Table objects.

Table represents the name and other attributes of an actual table present in a target schema. Its collection of Column objects represents naming and typing information about individual table columns. A full array of objects describing constraints, indexes, and sequences is provided to fill in many more details, some of which impact the behavior of the engine and SQL construction system. In particular, ForeignKeyConstraint is central to determining how two tables should be joined.

sqlalchemy 提供了称为 metadata 的 table 和 column 用以表示数据库对应的 table 和 column,除此之外还提供了一系列的其他数据库对象的表示,比如 index,sequence 等。

SQL expression

Expression Trees

sqlalchemy using Python objects and expressions to generatively construct expression tree structures, even re-purposing Python operators so that operators could be given SQL statement behavior。

sqlalchemy 使用 Python object 和 表达式来生成对应的 SQL expression statement。这个不同于一般的文本化转变。

在 sqlalchemy 中,sql 语句各个部分都有对应的 Python objects,sql 中的 operator 对应着 Python 中的 operator。典型如

SELECT id FROM user WHERE name = ? >>

from sqlalchemy.sql import table, column, select
user = table('user', column('id'), column('name'))
stmt = select([user.c.id]).where(user.c.name=='ed')

compilation

Compile Class 负责对 Expression trees 进行翻译和转换。它会把 sqlalchemy 中对应的 express trees 翻译成特定数据库的 sql 语言。根据 sql 语句作用的不同又分为 DQLCompile (结构化查询),DDLCompile(数据定义),DMLCompile(数据操作).