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

Revision 139, 3.7 kB (checked in by ged, 7 years ago)

- Applied patch from Ants Aasma to make Elixir compatible with the 0.4 branch

of SQLAlchemy.

Line 
1'''
2Elixir package
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
15and the unit tests. The examples directory includes a TurboGears application
16with full identity support called 'videostore'.
17'''
18
19import sqlalchemy
20
21from sqlalchemy.ext.sessioncontext import SessionContext
22from sqlalchemy.types import *
23
24from elixir.options import using_options, using_table_options, \
25                           using_mapper_options, options_defaults
26from elixir.entity import Entity, EntityMeta, EntityDescriptor
27from elixir.fields import has_field, with_fields, Field
28from elixir.relationships import belongs_to, has_one, has_many, \
29                                 has_and_belongs_to_many
30from elixir.properties import has_property
31
32try:
33    set
34except NameError:
35    from sets import Set as set
36
37__version__ = '0.4.0'
38
39__all__ = ['Entity', 'EntityMeta', 'Field', 'has_field', 'with_fields',
40           'has_property', 
41           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many',
42           'using_options', 'using_table_options', 'using_mapper_options',
43           'options_defaults', 'metadata', 'objectstore',
44           'create_all', 'drop_all', 'setup_all', 'cleanup_all',
45           'delay_setup'] + \
46          sqlalchemy.types.__all__
47
48__pudge_all__ = ['create_all', 'drop_all', 'setup_all', 'cleanup_all',
49                 'metadata', 'objectstore', 'delay_setup']
50
51# connect
52metadata = sqlalchemy.DynamicMetaData(threadlocal=False)
53
54try:
55    # this only happens when the threadlocal extension is used
56    objectstore = sqlalchemy.objectstore
57except AttributeError:
58    # thread local SessionContext
59    class Objectstore(object):
60
61        def __init__(self, *args, **kwargs):
62            self.context = SessionContext(*args, **kwargs)
63
64        def __getattr__(self, name):
65            return getattr(self.context.current, name)
66        session = property(lambda s:s.context.current)
67
68    objectstore = Objectstore(sqlalchemy.orm.create_session)
69
70metadatas = set()
71
72
73def create_all():
74    'Create all necessary tables for all declared entities'
75    for md in metadatas:
76        md.create_all()
77
78
79def drop_all():
80    'Drop all tables for all declared entities'
81    for md in metadatas:
82        md.drop_all()
83
84delayed_entities = set()
85delay_setup = False
86
87
88def setup_all():
89    '''Setup the table and mapper for all entities which have been delayed.
90
91    This should be used in conjunction with setting ``delay_setup`` to ``True``
92    before defining your entities.
93    '''
94    for entity in delayed_entities:
95        entity.setup_table()
96    for entity in delayed_entities:
97        entity.setup_mapper()
98
99    # setup all relationships
100    for entity in delayed_entities:
101        for rel in entity.relationships.itervalues():
102            rel.setup()
103
104    delayed_entities.clear()
105
106    # issue the "CREATE" SQL statements
107    create_all()
108
109
110def cleanup_all():
111    '''Drop table and clear mapper for all entities, and clear all metadatas.
112    '''
113    drop_all()
114    for md in metadatas:
115        md.clear()
116    metadatas.clear()
117    EntityDescriptor.uninitialized_rels.clear()
118
119    objectstore.clear()
120    sqlalchemy.clear_mappers()
Note: See TracBrowser for help on using the browser.