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

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

changed the "extra" options system to work like Jonathan suggested

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 *
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', 
30           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many', 
31           'using_options', 'using_table_options', 'using_mapper_options',
32           'create_all', 'drop_all', 'metadata', 'objectstore'] + \
33          sqlalchemy.types.__all__
34
35# connect
36metadata = sqlalchemy.DynamicMetaData('elixir')
37
38try:
39    objectstore = sqlalchemy.objectstore
40except AttributeError:
41    # thread local SessionContext
42    class Objectstore(object):
43        def __init__(self, *args, **kwargs):
44            self.context = SessionContext(*args, **kwargs)
45        def __getattr__(self, name):
46            return getattr(self.context.current, name)
47        session = property(lambda s:s.context.current)
48   
49    objectstore = Objectstore(sqlalchemy.create_session)
50
51metadatas = set()
52
53def create_all():
54    """Create all necessary tables for all declared entities"""
55    for md in metadatas:
56        md.create_all()
57
58def drop_all():
59    """Drop all tables for all declared entities"""
60    for md in metadatas:
61        md.drop_all()
Note: See TracBrowser for help on using the browser.