| 1 | ''' |
|---|
| 2 | Elixir package |
|---|
| 3 | |
|---|
| 4 | A declarative layer on top of SQLAlchemy, which is intended to replace the |
|---|
| 5 | ActiveMapper SQLAlchemy extension, and the TurboEntity project. Elixir is a |
|---|
| 6 | fairly thin wrapper around SQLAlchemy, which provides the ability to define |
|---|
| 7 | model objects following the Active Record design pattern, and using a DSL |
|---|
| 8 | syntax similar to that of the Ruby on Rails ActiveRecord system. |
|---|
| 9 | |
|---|
| 10 | Elixir does not intend to replace SQLAlchemy's core features, but instead |
|---|
| 11 | focuses on providing a simpler syntax for defining model objects when you do |
|---|
| 12 | not need the full expressiveness of SQLAlchemy's manual mapper definitions. |
|---|
| 13 | |
|---|
| 14 | For an example of how to use Elixir, please refer to the examples directory |
|---|
| 15 | and the unit tests. The examples directory includes a TurboGears application |
|---|
| 16 | with full identity support called 'videostore'. |
|---|
| 17 | ''' |
|---|
| 18 | |
|---|
| 19 | import sqlalchemy |
|---|
| 20 | |
|---|
| 21 | from sqlalchemy.ext.sessioncontext import SessionContext |
|---|
| 22 | from sqlalchemy.types import * |
|---|
| 23 | |
|---|
| 24 | from elixir.options import using_options, using_table_options, \ |
|---|
| 25 | using_mapper_options, options_defaults |
|---|
| 26 | from elixir.entity import Entity, EntityMeta, EntityDescriptor |
|---|
| 27 | from elixir.fields import has_field, with_fields, Field |
|---|
| 28 | from elixir.relationships import belongs_to, has_one, has_many, \ |
|---|
| 29 | has_and_belongs_to_many |
|---|
| 30 | from elixir.properties import has_property |
|---|
| 31 | |
|---|
| 32 | try: |
|---|
| 33 | set |
|---|
| 34 | except 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 |
|---|
| 52 | metadata = sqlalchemy.DynamicMetaData(threadlocal=False) |
|---|
| 53 | |
|---|
| 54 | try: |
|---|
| 55 | # this only happens when the threadlocal extension is used |
|---|
| 56 | objectstore = sqlalchemy.objectstore |
|---|
| 57 | except 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 | |
|---|
| 70 | metadatas = set() |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | def create_all(): |
|---|
| 74 | 'Create all necessary tables for all declared entities' |
|---|
| 75 | for md in metadatas: |
|---|
| 76 | md.create_all() |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | def drop_all(): |
|---|
| 80 | 'Drop all tables for all declared entities' |
|---|
| 81 | for md in metadatas: |
|---|
| 82 | md.drop_all() |
|---|
| 83 | |
|---|
| 84 | delayed_entities = set() |
|---|
| 85 | delay_setup = False |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | def 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 | |
|---|
| 110 | def 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() |
|---|