root / elixir / trunk / elixir / __init__.py

Revision 515, 3.6 kB (checked in by ged, 2 years ago)

- Added a new collection which can resolve entities relative to the current

entity, for example "..other_module.Class" (based on patches from Johannes
Janssen, closes #93).

- Added a new entity option "resolve_root", which allows one to specify the

root module where your entities are defined. The string will be prepended
to all "absolute" entity paths. It can also be used on a per-entity basis.
This feature is based on a patch from Johannes Janssen, see #93.

Line 
1'''
2Elixir package
3
4A declarative layer on top of the `SQLAlchemy library
5<http://www.sqlalchemy.org/>`_. It is a fairly thin wrapper, which provides
6the ability to create simple Python classes that map directly to relational
7database tables (this pattern is often referred to as the Active Record design
8pattern), providing many of the benefits of traditional databases
9without losing the convenience of Python objects.
10
11Elixir is intended to replace the ActiveMapper SQLAlchemy extension, and the
12TurboEntity project but does not intend to replace SQLAlchemy's core features,
13and instead focuses on providing a simpler syntax for defining model objects
14when you do not need the full expressiveness of SQLAlchemy's manual mapper
15definitions.
16'''
17
18try:
19    set
20except NameError:
21    from sets import Set as set
22
23import sqlalchemy
24from sqlalchemy.types import *
25
26from elixir.options import using_options, using_table_options, \
27                           using_mapper_options, options_defaults, \
28                           using_options_defaults
29from elixir.entity import Entity, EntityBase, EntityMeta, EntityDescriptor, \
30                          setup_entities, cleanup_entities
31from elixir.fields import has_field, Field
32from elixir.relationships import belongs_to, has_one, has_many, \
33                                 has_and_belongs_to_many, \
34                                 ManyToOne, OneToOne, OneToMany, ManyToMany
35from elixir.properties import has_property, GenericProperty, ColumnProperty, \
36                              Synonym
37from elixir.statements import Statement
38from elixir.collection import EntityCollection, GlobalEntityCollection
39
40
41__version__ = '0.8.0dev'
42
43__all__ = ['Entity', 'EntityBase', 'EntityMeta', 'EntityCollection',
44           'entities',
45           'Field', 'has_field',
46           'has_property', 'GenericProperty', 'ColumnProperty', 'Synonym',
47           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many',
48           'ManyToOne', 'OneToOne', 'OneToMany', 'ManyToMany',
49           'using_options', 'using_table_options', 'using_mapper_options',
50           'options_defaults', 'using_options_defaults',
51           'metadata', 'session',
52           'create_all', 'drop_all',
53           'setup_all', 'cleanup_all',
54           'setup_entities', 'cleanup_entities'] + \
55           sqlalchemy.types.__all__
56
57__doc_all__ = ['create_all', 'drop_all',
58               'setup_all', 'cleanup_all',
59               'metadata', 'session']
60
61# default session
62session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker())
63
64# default metadata
65metadata = sqlalchemy.MetaData()
66
67metadatas = set()
68
69# default entity collection
70entities = GlobalEntityCollection()
71
72
73def create_all(*args, **kwargs):
74    '''Create the necessary tables for all declared entities'''
75    for md in metadatas:
76        md.create_all(*args, **kwargs)
77
78
79def drop_all(*args, **kwargs):
80    '''Drop tables for all declared entities'''
81    for md in metadatas:
82        md.drop_all(*args, **kwargs)
83
84
85def setup_all(create_tables=False, *args, **kwargs):
86    '''Setup the table and mapper of all entities in the default entity
87    collection.
88    '''
89    setup_entities(entities)
90
91    # issue the "CREATE" SQL statements
92    if create_tables:
93        create_all(*args, **kwargs)
94
95
96def cleanup_all(drop_tables=False, *args, **kwargs):
97    '''Clear all mappers, clear the session, and clear all metadatas.
98    Optionally drops the tables.
99    '''
100    session.close()
101
102    cleanup_entities(entities)
103
104    sqlalchemy.orm.clear_mappers()
105    entities.clear()
106
107    if drop_tables:
108        drop_all(*args, **kwargs)
109
110    for md in metadatas:
111        md.clear()
112    metadatas.clear()
113
Note: See TracBrowser for help on using the browser.