Sqlalchemy and Elixir: Jonathan Lacour
Sqlalchemy and Elixir: Jonathan Lacour
Jonathan LaCour
http://cleverdevil.org
[email protected]
What is SQLAlchemy?
• SQLAlchemy Overview
• Concepts
• Example
• Elixir Overview
• Concepts
• Example
• Questions and Advanced Topics
SQLAlchemy Concepts
• Engine
• MetaData
• Table
• Mapper
• Session
• Query
SQLAlchemy Concepts: Engine
# create an engine
engine = create_engine('postgres://user:pass@host/database')
# perform a query
connection = engine.connect()
result = connection.execute('select * from users')
for row in result:
print row['username'], row['email']
class User(object):
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
mapper(User, users_table)
SQLAlchemy Concepts: Session
user = User()
user.username = 'cleverdevil'
user.email = '[email protected]'
user.password = 's3cr3tw0rd'
• Weblog Software
• People write Articles
• Plan: Tables, Classes, Mappers
SQLAlchemy Example: Tables
metadata = MetaData()
class Person(object):
def __init__(self, name=None, email=None, password=None):
self.name = name
self.email = email
self.password = password
class Article(object):
def __init__(self, title=None, description=None,
content=None, author=None):
self.title = title
self.description = description
self.content = content
self.author = author
SQLAlchemy Example: Mappers
# bind our metadata to an in-memory # create a session and save the post, which
# sqlite database, and create tables # will also save the dependant Person object
metadata.bind = 'sqlite:///' session = create_session()
metadata.create_all() session.save(blog_post)
session.flush()
# create a Person instance
jonathan = Person( # create another session and use it to query
name='Jonathan' # for our just created instance
email='[email protected]' session = create_session()
password='s3cr3t' query = session.query(Person)
) jonathan = query.get_by(name='Jonathan')
create an Article instance # walk through the object, printing out some
blog_post = Article( # properties, including related objects
title='Some Blog Post', print jonathan.name
description='Some description', print jonathan.articles[0].title
content=Some content, print jonathan.articles[0].author.name
author=jonathan
)
SQLAlchemy: In Summary
class User(Entity):
has_field('username', Unicode(16))
has_field('email', Unicode(60))
has_field('password', Unicode(20))
Elixir Example
class Person(Entity):
has_field('name', Unicode)
has_field('email', Unicode)
has_field('password', Unicode)
class Article(Entity):
has_field('title', Unicode)
has_field('description', Unicode)
has_field('content', Unicode)
class Article(Entity):
has_field('title', Unicode)
has_field('description', Unicode)
has_field('content', Unicode)
belongs_to('author', of_kind='Person', inverse='articles')
@after_update
def notify_author(self):
print 'Notifying', self.author.email
print ' -> article:', self.title, 'was updated'
More Elixir: Encryption
class Person(Entity):
has_field('name', Unicode)
has_field('email', Unicode)
has_field('password', Unicode)
class Article(Entity):
has_field('title', Unicode)
has_field('description', Unicode)
has_field('content', Unicode)
belongs_to('author', of_kind='Person', inverse='articles')
acts_as_versioned()
@after_revert
def notify_author(self):
print 'Notifying', self.author.email
print ' -> article:', self.title, 'was reverted'
More Elixir: Versioning
acts_as_taggable('tags')
More Elixir: Lower Level Operations
results = select(
[Person.c.email, Article.c.title],
and_(
Person.c.name == 'Jonathan',
Person.c.id==Article.c.author_id
)
).execute()
person_rows = Person.table.select().execute()
Elixir: Summary
Elixir http://elixir.ematia.de
SQLAlchemy http://sqlalchemy.org
Jonathan LaCour http://cleverdevil.org