| 1 | ''' |
|---|
| 2 | Elixir package |
|---|
| 3 | |
|---|
| 4 | A declarative layer on top of the `SQLAlchemy library |
|---|
| 5 | <http://www.sqlalchemy.org/>`_. It is a fairly thin wrapper, which provides |
|---|
| 6 | the ability to create simple Python classes that map directly to relational |
|---|
| 7 | database tables (this pattern is often referred to as the Active Record design |
|---|
| 8 | pattern), providing many of the benefits of traditional databases |
|---|
| 9 | without losing the convenience of Python objects. |
|---|
| 10 | |
|---|
| 11 | Elixir is intended to replace the ActiveMapper SQLAlchemy extension, and the |
|---|
| 12 | TurboEntity project but does not intend to replace SQLAlchemy's core features, |
|---|
| 13 | and instead focuses on providing a simpler syntax for defining model objects |
|---|
| 14 | when you do not need the full expressiveness of SQLAlchemy's manual mapper |
|---|
| 15 | definitions. |
|---|
| 16 | ''' |
|---|
| 17 | |
|---|
| 18 | try: |
|---|
| 19 | set |
|---|
| 20 | except NameError: |
|---|
| 21 | from sets import Set as set |
|---|
| 22 | |
|---|
| 23 | import sqlalchemy |
|---|
| 24 | from sqlalchemy.types import * |
|---|
| 25 | |
|---|
| 26 | from elixir.options import using_options, using_table_options, \ |
|---|
| 27 | using_mapper_options, options_defaults |
|---|
| 28 | from elixir.entity import Entity, EntityMeta, EntityDescriptor, \ |
|---|
| 29 | setup_entities, cleanup_entities |
|---|
| 30 | from elixir.fields import has_field, with_fields, Field |
|---|
| 31 | from elixir.relationships import belongs_to, has_one, has_many, \ |
|---|
| 32 | has_and_belongs_to_many, \ |
|---|
| 33 | ManyToOne, OneToOne, OneToMany, ManyToMany |
|---|
| 34 | from elixir.properties import has_property, GenericProperty, ColumnProperty |
|---|
| 35 | from elixir.statements import Statement |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | __version__ = '0.5.0' |
|---|
| 39 | |
|---|
| 40 | __all__ = ['Entity', 'EntityMeta', |
|---|
| 41 | 'Field', 'has_field', 'with_fields', |
|---|
| 42 | 'has_property', 'GenericProperty', 'ColumnProperty', |
|---|
| 43 | 'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many', |
|---|
| 44 | 'ManyToOne', 'OneToOne', 'OneToMany', 'ManyToMany', |
|---|
| 45 | 'using_options', 'using_table_options', 'using_mapper_options', |
|---|
| 46 | 'options_defaults', 'metadata', 'objectstore', 'session', |
|---|
| 47 | 'create_all', 'drop_all', |
|---|
| 48 | 'setup_all', 'cleanup_all', |
|---|
| 49 | 'setup_entities', 'cleanup_entities'] + \ |
|---|
| 50 | sqlalchemy.types.__all__ |
|---|
| 51 | |
|---|
| 52 | __doc_all__ = ['create_all', 'drop_all', |
|---|
| 53 | 'setup_all', 'cleanup_all', |
|---|
| 54 | 'metadata', 'session'] |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | class Objectstore(object): |
|---|
| 58 | """a wrapper for a SQLAlchemy session-making object, such as |
|---|
| 59 | SessionContext or ScopedSession. |
|---|
| 60 | |
|---|
| 61 | Uses the ``registry`` attribute present on both objects |
|---|
| 62 | (versions 0.3 and 0.4) in order to return the current |
|---|
| 63 | contextual session. |
|---|
| 64 | """ |
|---|
| 65 | |
|---|
| 66 | def __init__(self, ctx): |
|---|
| 67 | self.context = ctx |
|---|
| 68 | |
|---|
| 69 | def __getattr__(self, name): |
|---|
| 70 | return getattr(self.context.registry(), name) |
|---|
| 71 | |
|---|
| 72 | session = property(lambda s:s.context.registry()) |
|---|
| 73 | |
|---|
| 74 | # default session |
|---|
| 75 | try: |
|---|
| 76 | from sqlalchemy.orm import scoped_session |
|---|
| 77 | session = scoped_session(sqlalchemy.orm.create_session) |
|---|
| 78 | except ImportError: |
|---|
| 79 | # Not on version 0.4 of sqlalchemy |
|---|
| 80 | from sqlalchemy.ext.sessioncontext import SessionContext |
|---|
| 81 | session = Objectstore(SessionContext(sqlalchemy.orm.create_session)) |
|---|
| 82 | |
|---|
| 83 | # backward-compatible name |
|---|
| 84 | objectstore = session |
|---|
| 85 | |
|---|
| 86 | # default metadata |
|---|
| 87 | metadata = sqlalchemy.MetaData() |
|---|
| 88 | |
|---|
| 89 | metadatas = set() |
|---|
| 90 | |
|---|
| 91 | # default entity collection |
|---|
| 92 | entities = list() |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | def create_all(*args, **kwargs): |
|---|
| 96 | '''Create the necessary tables for all declared entities''' |
|---|
| 97 | for md in metadatas: |
|---|
| 98 | md.create_all(*args, **kwargs) |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | def drop_all(*args, **kwargs): |
|---|
| 102 | '''Drop tables for all declared entities''' |
|---|
| 103 | for md in metadatas: |
|---|
| 104 | md.drop_all(*args, **kwargs) |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | def setup_all(create_tables=False, *args, **kwargs): |
|---|
| 108 | '''Setup the table and mapper of all entities in the default entity |
|---|
| 109 | collection. |
|---|
| 110 | |
|---|
| 111 | This is called automatically if any entity of the collection is configured |
|---|
| 112 | with the `autosetup` option and it is first accessed, |
|---|
| 113 | instanciated (called) or the create_all method of a metadata containing |
|---|
| 114 | tables from any of those entities is called. |
|---|
| 115 | ''' |
|---|
| 116 | setup_entities(entities) |
|---|
| 117 | |
|---|
| 118 | # issue the "CREATE" SQL statements |
|---|
| 119 | if create_tables: |
|---|
| 120 | create_all(*args, **kwargs) |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | def cleanup_all(drop_tables=False, *args, **kwargs): |
|---|
| 124 | '''Clear all mappers, clear the session, and clear all metadatas. |
|---|
| 125 | Optionally drops the tables. |
|---|
| 126 | ''' |
|---|
| 127 | if drop_tables: |
|---|
| 128 | drop_all(*args, **kwargs) |
|---|
| 129 | |
|---|
| 130 | cleanup_entities(entities) |
|---|
| 131 | |
|---|
| 132 | for md in metadatas: |
|---|
| 133 | md.clear() |
|---|
| 134 | metadatas.clear() |
|---|
| 135 | |
|---|
| 136 | session.clear() |
|---|
| 137 | |
|---|
| 138 | sqlalchemy.orm.clear_mappers() |
|---|
| 139 | del entities[:] |
|---|