root / elixir / tags / 0.7.0 / elixir / __init__.py

Revision 416, 3.8 kB (checked in by ged, 3 years ago)

add EntityBase to all

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
39
40
41__version__ = '0.7.0'
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 = EntityCollection()
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    This is called automatically if any entity of the collection is configured
90    with the `autosetup` option and it is first accessed,
91    instanciated (called) or the create_all method of a metadata containing
92    tables from any of those entities is called.
93    '''
94    setup_entities(entities)
95
96    # issue the "CREATE" SQL statements
97    if create_tables:
98        create_all(*args, **kwargs)
99
100
101def cleanup_all(drop_tables=False, *args, **kwargs):
102    '''Clear all mappers, clear the session, and clear all metadatas.
103    Optionally drops the tables.
104    '''
105    session.close()
106
107    cleanup_entities(entities)
108
109    sqlalchemy.orm.clear_mappers()
110    entities.clear()
111
112    if drop_tables:
113        drop_all(*args, **kwargs)
114
115    for md in metadatas:
116        md.clear()
117    metadatas.clear()
118
Note: See TracBrowser for help on using the browser.