1.
SQLAlchemy SQLの錬金術
Plone Symposym Tokyo 2015
aodag
2.
「Who Are You?」
Atsushi Odagiri
Use Python from 2001
Work at Be Proud, Inc.
3.
What is SQLAlchemy?
Python Library for Data Accessing to RDBMS.
That has SQL Expression and OR Mapper.
4.
Why I use SQLAlchemy
I Don’t Know Other OR Mapper with Python.
I Love SQL And Python!
SQLAlchemy has SQL Expression That makes
SQL Python Object.
SQLAlchemy doesn’t keep me away from SQL.
5.
SQLAlchemy features
SQL Expression
Object-Relational Mapper
Unit of Work
10.
OR Mapper
mapper(User, users)
user = Session.query(User).filter(
User.first_name == u’Atsushi’).first()
user.first_name = u’aodag’
Session.flush()
11.
Unit Of Work
There are no save method.
Session object manages Object states.
That calls DML depending on the object state.
12.
Unit Of Work
user = User(u’Atsushi’, u’Odagiri’)
Session.add(user)
user.company = Company(u’BeProud’)
Session.flush()
user.first_name = u’aodag’
Session.flush()
13.
Getting Started
Install
Declare Models
Connect
Create Table
Query Model
Use Session
25.
Session And Transaction
Session.remove()
Session.add(user)
Session.flush()
Session.commit()
26.
With Web Application
Transaction Manager With zope.sqlalchemy
● repoze.tm2
● pyramid_tm
● Zope2 transaction
27.
With Web Application
Transaction WSGI Middleware
@wsgify.middleware
def transaction_middleware(app, req, session):
try:
return req.get_response(app)
except:
session.rollback()
else:
session.commit()
28.
With asyncio/aiopg
@async.coroutine
def get():
with (yeild from engine) as con:
res = yeild from con.execute(
select([users]).where(
users.c.first_name == u’Atsushi’)
29.
Conclusion
SQLAlchemy has many features.
SQLAlchemy supports many kind of SQL
Statements.
That’s Great!
30.
Bib.
● http://www.sqlalchemy.org/
● http://aiopg.readthedocs.org/
● http://repozetm2.readthedocs.org/
● https://pypi.python.org/pypi/zope.
sqlalchemy
● PEP 0249 -- Python Database API
Specification v2.0
Be the first to comment