SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
SQLAlchemy 简介

全功能数据访问框架 SQLAlchemy
ORM

项目开发常见的体系结构:
  UI
  Middle Layer
     BR
     Database Access Layer
        ORM
        DAO
        Database Access Driver
  DATABASE
ORM与数据访问工具

ORM 通常位于业务逻辑与数据驱动之间
简化数据库访问编码量
提高项目移植性
提高项目质量
Python 领域中的 ORM

基于动态语言的便利性,Python 领域出现了多个ORM框架。
  SQLAlchemy
  SQLObject
  Storm
  Django ORM
  Web2py DAL
  webpy ORM
  ...
ORM 评估与选型

 能简化编码量
 与应用层逻辑良好结合
 对数据库友好
   将关系数据模型友好的映射为对象结构
   支持业务所需的数据类型
   支持定制的局部查询
   支持原始SQL
 可移植
 可扩展nt
SQLAlchemy 评价

 能有效简化复杂查询
 对应用层友好
   允许数据模型与业务逻辑对象分离
   支持局部映射和多映射
 对数据库友好
   允许对象化的定制查询
   允许执行可参数化的原生SQL语句
   支持大量的数据类型
   允许扩展数据类型
 可移植性良好
   支持常用的多种数据库/源
   提供丰富的通用方法
 提供强大的扩展能力
SQLAlchemy 体系
SQLAlchemy 体系—— Engine

 Engine 提供数据驱动接口
   连接池
   URI format link (postgres://user:***@server/database)
   允许直接执行SQL(ddl/dml)
       允许参数化操作
   bind to all others
   可以直接建立 SQLite 数据库
   对象化结果集
SQLAlchemy 体系—— Session

 Session 是 DML 操作的核心组件
    支持对象化的 SQL Expression
    支持 Query 对象的迭代式生成和惰性执行
    支持 Select 表达式执行
    支持原始SQL
SQLAlchemy 体系—— MetaData

 MetaData 是 DDL 操作的核心组件
   支持数据结构定义生成为数据库结构
   支持数据库结构反射
   为查询和数据库定义组件提供依据
   允许
SQLAlchemy 体系—— TABLE

 Table 类型定义数据库映射结构
    支持数据结构定义生成为数据库结构
    支持数据库结构反射
    为查询组件提供表结构
    支持独立的约束定义(或定义于每个 Column 对象)
       不依赖ID列
    允许定义各种复杂的关系
SQLAlchemy 体系—— Mapper

 Mapper 为业务类型和数据库定义提供中间连接
   允许在Mapper中详细定义数据关系和字段
   允许定义各种组合类型字段
   支持常见关系模式
      http://www.sqlalchemy.org/docs/05/ormtutorial.html
      基于 relation
      允许定制惰性级别
SQLAlchemy 应用——访问数据库

 最简单的使用可以直接调用 engine
   from sqlalchemy import create_engine
   engine = create_engine(uri)
   res = engine.execute
 可以增加 session 支持
   from sqlalchemy.orm import sessionmaker
   Session = sessionmaker(bind=engine)
   session = Session()
   session.execute
SQLAlchemy 应用——访问数据库

 定义表
    from sqlalchemy.orm import Table, Column, String,...
    table = Table(name, metadata, ...)
    table = Table(name, metadata, ..., autoload=True)
 session 支持Query操作
    session.query(table).filter_by(...).filter(...).scalar()
    session.query(table).filter_by(...).filter(...).count()
    session.query(table).filter_by(...).filter(...).all()
    ...
SQLAlchemy应用——业务对象分离

 定义业务对象
   class BR(object):pass
   可以在此编写数据无关的测试代码
 定义数据模型
   table = Table(...)
 定义映射
   mapper
       properties
       include/exclude_properties
       relation
SQLAlchemy Tips

 可以简化模型定义
    Declaratively
 可以反射数据库结构
    metadata.reflect(bind=engine)
 SqlSoup
 table.tometadata
    Adapting Tables to Alternate Metadata
SQLAlchemy 实践

 Umass 统计项目
    快速查询
    复杂查询的映射和比对
    业务逻辑分离
 KUSE 搜索引擎项目
    快速构造查询
    灵活的即用查询集
    特化数据类型的运用
    特化运算符的使用
 Socrates 语义模型存储工具
    展望
    特化的动态数据类型支持
    高可移植性

Más contenido relacionado

Similar a Sql Alchemy Story

Entity Framework實戰
Entity Framework實戰Entity Framework實戰
Entity Framework實戰國昭 張
 
Essential oracle security internal for dba
Essential oracle security internal for dbaEssential oracle security internal for dba
Essential oracle security internal for dbamaclean liu
 
Azure Data Lake 簡介
Azure Data Lake 簡介Azure Data Lake 簡介
Azure Data Lake 簡介Herman Wu
 
性能测试实践1
性能测试实践1性能测试实践1
性能测试实践1yiditushe
 
信息系统开发平台OpenExpressApp
信息系统开发平台OpenExpressApp信息系统开发平台OpenExpressApp
信息系统开发平台OpenExpressAppzhoujg
 
低成本和高性能MySQL云架构探索
低成本和高性能MySQL云架构探索低成本和高性能MySQL云架构探索
低成本和高性能MySQL云架构探索Feng Yu
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發koji lin
 
+++º+ ¦¦ ¦ ¦¦ ¦+ =
+++º+ ¦¦  ¦ ¦¦ ¦+ =+++º+ ¦¦  ¦ ¦¦ ¦+ =
+++º+ ¦¦ ¦ ¦¦ ¦+ =guesta6295f3
 
数据库性能诊断的七种武器
数据库性能诊断的七种武器数据库性能诊断的七种武器
数据库性能诊断的七种武器Leyi (Kamus) Zhang
 
Terracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering ArchitectureTerracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering ArchitectureTarget Source
 
如何架构和开发高性能,高伸缩性Web 应用系统
如何架构和开发高性能,高伸缩性Web 应用系统如何架构和开发高性能,高伸缩性Web 应用系统
如何架构和开发高性能,高伸缩性Web 应用系统melity78
 
網站設計100步
網站設計100步網站設計100步
網站設計100步evercislide
 
Open Api&Sip
Open Api&SipOpen Api&Sip
Open Api&Sipcenwenchu
 
运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践Li JianYe
 
20120324 sql server 2012新特性by_rico
20120324 sql server 2012新特性by_rico20120324 sql server 2012新特性by_rico
20120324 sql server 2012新特性by_ricoRico Chen
 
3 hibernate映射元素和类型
3 hibernate映射元素和类型3 hibernate映射元素和类型
3 hibernate映射元素和类型Zelin Wang
 

Similar a Sql Alchemy Story (20)

Entity Framework實戰
Entity Framework實戰Entity Framework實戰
Entity Framework實戰
 
Hibernate教程
Hibernate教程Hibernate教程
Hibernate教程
 
Essential oracle security internal for dba
Essential oracle security internal for dbaEssential oracle security internal for dba
Essential oracle security internal for dba
 
Azure Data Lake 簡介
Azure Data Lake 簡介Azure Data Lake 簡介
Azure Data Lake 簡介
 
性能测试实践1
性能测试实践1性能测试实践1
性能测试实践1
 
Hibernate
HibernateHibernate
Hibernate
 
信息系统开发平台OpenExpressApp
信息系统开发平台OpenExpressApp信息系统开发平台OpenExpressApp
信息系统开发平台OpenExpressApp
 
低成本和高性能MySQL云架构探索
低成本和高性能MySQL云架构探索低成本和高性能MySQL云架构探索
低成本和高性能MySQL云架构探索
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發
 
Spark tutorial
Spark tutorialSpark tutorial
Spark tutorial
 
+++º+ ¦¦ ¦ ¦¦ ¦+ =
+++º+ ¦¦  ¦ ¦¦ ¦+ =+++º+ ¦¦  ¦ ¦¦ ¦+ =
+++º+ ¦¦ ¦ ¦¦ ¦+ =
 
数据库性能诊断的七种武器
数据库性能诊断的七种武器数据库性能诊断的七种武器
数据库性能诊断的七种武器
 
Terracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering ArchitectureTerracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering Architecture
 
如何架构和开发高性能,高伸缩性Web 应用系统
如何架构和开发高性能,高伸缩性Web 应用系统如何架构和开发高性能,高伸缩性Web 应用系统
如何架构和开发高性能,高伸缩性Web 应用系统
 
網站設計100步
網站設計100步網站設計100步
網站設計100步
 
Open Api&Sip
Open Api&SipOpen Api&Sip
Open Api&Sip
 
运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践
 
Esb
EsbEsb
Esb
 
20120324 sql server 2012新特性by_rico
20120324 sql server 2012新特性by_rico20120324 sql server 2012新特性by_rico
20120324 sql server 2012新特性by_rico
 
3 hibernate映射元素和类型
3 hibernate映射元素和类型3 hibernate映射元素和类型
3 hibernate映射元素和类型
 

Más de Zoom Quiet

产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变Zoom Quiet
 
01s0401 go,互联网时代的c语言 许式伟
01s0401 go,互联网时代的c语言   许式伟01s0401 go,互联网时代的c语言   许式伟
01s0401 go,互联网时代的c语言 许式伟Zoom Quiet
 
Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoom Quiet
 
金山云查询系统改进之路1
金山云查询系统改进之路1金山云查询系统改进之路1
金山云查询系统改进之路1Zoom Quiet
 
Zh120226techparty zd-story
Zh120226techparty zd-storyZh120226techparty zd-story
Zh120226techparty zd-storyZoom Quiet
 
Zh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZoom Quiet
 
Zh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZoom Quiet
 
Velocity2011分享
Velocity2011分享Velocity2011分享
Velocity2011分享Zoom Quiet
 
陈正 Introduction to-sae_python
陈正   Introduction to-sae_python陈正   Introduction to-sae_python
陈正 Introduction to-sae_pythonZoom Quiet
 
111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿Zoom Quiet
 
111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析Zoom Quiet
 
111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发Zoom Quiet
 
ImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yZoom Quiet
 
Import this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersImport this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersZoom Quiet
 
金山卫士界面框架
金山卫士界面框架金山卫士界面框架
金山卫士界面框架Zoom Quiet
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysqlZoom Quiet
 

Más de Zoom Quiet (20)

42qu thrift1
42qu thrift142qu thrift1
42qu thrift1
 
产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变产品信息收集系统Infoc的演变
产品信息收集系统Infoc的演变
 
Go courseday3
Go courseday3Go courseday3
Go courseday3
 
Go courseday2
Go courseday2Go courseday2
Go courseday2
 
Go courseday1
Go courseday1Go courseday1
Go courseday1
 
01s0401 go,互联网时代的c语言 许式伟
01s0401 go,互联网时代的c语言   许式伟01s0401 go,互联网时代的c语言   许式伟
01s0401 go,互联网时代的c语言 许式伟
 
Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿Zoz pwned-by-the-owner-表惹程序猿
Zoz pwned-by-the-owner-表惹程序猿
 
金山云查询系统改进之路1
金山云查询系统改进之路1金山云查询系统改进之路1
金山云查询系统改进之路1
 
Zh120226techparty zd-story
Zh120226techparty zd-storyZh120226techparty zd-story
Zh120226techparty zd-story
 
Zh120226techparty velocity2011-review
Zh120226techparty velocity2011-reviewZh120226techparty velocity2011-review
Zh120226techparty velocity2011-review
 
Zh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolboxZh120226techparty jeff kit-ios-toolbox
Zh120226techparty jeff kit-ios-toolbox
 
Velocity2011分享
Velocity2011分享Velocity2011分享
Velocity2011分享
 
陈正 Introduction to-sae_python
陈正   Introduction to-sae_python陈正   Introduction to-sae_python
陈正 Introduction to-sae_python
 
111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿111218 zhtechparty-panda讲稿
111218 zhtechparty-panda讲稿
 
111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析111218 zhtechparty-移动互联网产品需求分析
111218 zhtechparty-移动互联网产品需求分析
 
111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发111218 zhtechparty-zd-浅谈symbian开发
111218 zhtechparty-zd-浅谈symbian开发
 
ImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_yImpoImport this, that, and the other thing: custom importersrt not for_y
ImpoImport this, that, and the other thing: custom importersrt not for_y
 
Import this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importersImport this, that, and the other thing: custom importers
Import this, that, and the other thing: custom importers
 
金山卫士界面框架
金山卫士界面框架金山卫士界面框架
金山卫士界面框架
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql
 

Sql Alchemy Story