| 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 | using_options_defaults |
|---|
| 29 | from elixir.entity import Entity, EntityBase, EntityMeta, EntityDescriptor, \ |
|---|
| 30 | setup_entities, cleanup_entities |
|---|
| 31 | from elixir.fields import has_field, Field |
|---|
| 32 | from elixir.relationships import belongs_to, has_one, has_many, \ |
|---|
| 33 | has_and_belongs_to_many, \ |
|---|
| 34 | ManyToOne, OneToOne, OneToMany, ManyToMany |
|---|
| 35 | from elixir.properties import has_property, GenericProperty, ColumnProperty, \ |
|---|
| 36 | Synonym |
|---|
| 37 | from elixir.statements import Statement |
|---|
| 38 | from 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 |
|---|
| 62 | session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker()) |
|---|
| 63 | |
|---|
| 64 | # default metadata |
|---|
| 65 | metadata = sqlalchemy.MetaData() |
|---|
| 66 | |
|---|
| 67 | metadatas = set() |
|---|
| 68 | |
|---|
| 69 | # default entity collection |
|---|
| 70 | entities = EntityCollection() |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | def 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 | |
|---|
| 79 | def drop_all(*args, **kwargs): |
|---|
| 80 | '''Drop tables for all declared entities''' |
|---|
| 81 | for md in metadatas: |
|---|
| 82 | md.drop_all(*args, **kwargs) |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | def 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 | |
|---|
| 101 | def 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 | |
|---|