root / elixir / trunk / elixir / __init__.py @ 22

Revision 22, 2.3 kB (checked in by ged, 6 years ago)

* implemented order_by translation on relations (has_many and
has_and_belongs_to_many)
* added unit test to demonstrate it, and moved the order_by test there too.
* test_options is now empty.
* some minor cleanups (mainly docstrings adjustment to 79 chars max

Line 
1'''
2Elixir
3   
4A declarative layer on top of SQLAlchemy, which is intended to replace the
5ActiveMapper SQLAlchemy extension, and the TurboEntity project.  Elixir is a
6fairly thin wrapper around SQLAlchemy, which provides the ability to define
7model objects following the Active Record design pattern, and using a DSL
8syntax similar to that of the Ruby on Rails ActiveRecord system.
9
10Elixir does not intend to replace SQLAlchemy's core features, but instead
11focuses on providing a simpler syntax for defining model objects when you do
12not need the full expressiveness of SQLAlchemy's manual mapper definitions.
13
14For an example of how to use Elixir, please refer to the examples directory and
15the unit tests.  The examples directory includes a TurboGears application with
16full identity support called 'videostore'.
17'''
18
19import sqlalchemy
20
21from sqlalchemy.ext.sessioncontext  import SessionContext
22from sqlalchemy.types               import *
23from elixir.options                 import using_options
24from elixir.entity                  import Entity, EntityDescriptor
25from elixir.fields                  import Field, has_field, with_fields
26from elixir.relationships           import belongs_to, has_one, has_many, \
27                                           has_and_belongs_to_many
28
29__all__ = ['Entity', 'Field', 'has_field', 'with_fields', 'belongs_to', 
30           'has_one', 'has_many', 'has_and_belongs_to_many', 'using_options', 
31           'create_all', 'drop_all', 'metadata', 'objectstore'] + \
32          sqlalchemy.types.__all__
33
34# connect
35metadata = sqlalchemy.DynamicMetaData('elixir')
36
37try:
38    objectstore = sqlalchemy.objectstore
39except AttributeError:
40    # thread local SessionContext
41    class Objectstore(object):
42        def __init__(self, *args, **kwargs):
43            self.context = SessionContext(*args, **kwargs)
44        def __getattr__(self, name):
45            return getattr(self.context.current, name)
46        session = property(lambda s:s.context.current)
47   
48    objectstore = Objectstore(sqlalchemy.create_session)
49
50metadatas = set()
51
52def create_all():
53    """Create all necessary tables for all declared entities"""
54    for md in metadatas:
55        md.create_all()
56
57def drop_all():
58    """Drop all tables for all declared entities"""
59    for md in metadatas:
60        md.drop_all()
Note: See TracBrowser for help on using the browser.