| 1 | from elixir.statements import Statement |
|---|
| 2 | |
|---|
| 3 | __all__ = [ |
|---|
| 4 | 'using_options', |
|---|
| 5 | 'using_table_options', |
|---|
| 6 | 'using_mapper_options', |
|---|
| 7 | ] |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | class UsingOptions(object): |
|---|
| 11 | ''' |
|---|
| 12 | The 'using_options' DSL statement allows you to set up some additional |
|---|
| 13 | behaviors on your model objects, including table names, ordering, and |
|---|
| 14 | more. To specify an option, simply supply the option as a keyword |
|---|
| 15 | argument onto the statement, as follows: |
|---|
| 16 | |
|---|
| 17 | :: |
|---|
| 18 | |
|---|
| 19 | class Person(Entity): |
|---|
| 20 | has_field('name', Unicode(64)) |
|---|
| 21 | using_options(shortnames=True, order_by='name') |
|---|
| 22 | |
|---|
| 23 | The list of supported arguments are as follows: |
|---|
| 24 | |
|---|
| 25 | * metadata: Specify a custom MetaData. |
|---|
| 26 | |
|---|
| 27 | * autoload: Automatically load column definitions from the |
|---|
| 28 | existing database table. |
|---|
| 29 | |
|---|
| 30 | * tablename: Specify a custom tablename. |
|---|
| 31 | |
|---|
| 32 | * shortnames: Usually tablenames include the full module-path to |
|---|
| 33 | the entity, but lower-cased and separated by |
|---|
| 34 | underscores ("_"), eg.: "project1_model_myentity", |
|---|
| 35 | for an entity named "MyEntity" in the module |
|---|
| 36 | "project1.model". If shortnames is True, the |
|---|
| 37 | tablename will just be the entity's classname |
|---|
| 38 | lower-cased, ie. "myentity". |
|---|
| 39 | |
|---|
| 40 | * auto_primarykey: If given as string, it will represent the |
|---|
| 41 | auto-primary-key's column name. If this option is |
|---|
| 42 | |
|---|
| 43 | True: Allow auto-creation of a primary key, |
|---|
| 44 | if there's no primary key defined for |
|---|
| 45 | the corresponding entity. |
|---|
| 46 | |
|---|
| 47 | False: Disallow auto-creation of a primary key. |
|---|
| 48 | |
|---|
| 49 | * order_by: How to order select results. Either a string or a |
|---|
| 50 | list of strings, composed of the field name, |
|---|
| 51 | optionally lead by a minus (descending order). |
|---|
| 52 | |
|---|
| 53 | * extension: Use one or more MapperExtensions. |
|---|
| 54 | ''' |
|---|
| 55 | |
|---|
| 56 | valid_options = ( |
|---|
| 57 | 'metadata', |
|---|
| 58 | 'autoload', |
|---|
| 59 | 'tablename', |
|---|
| 60 | 'shortnames', |
|---|
| 61 | 'auto_primarykey', |
|---|
| 62 | 'order_by', |
|---|
| 63 | 'extension', |
|---|
| 64 | ) |
|---|
| 65 | |
|---|
| 66 | def __init__(self, entity, *args, **kwargs): |
|---|
| 67 | desc = entity._descriptor |
|---|
| 68 | |
|---|
| 69 | for kwarg in kwargs: |
|---|
| 70 | if kwarg in UsingOptions.valid_options: |
|---|
| 71 | setattr(desc, kwarg, kwargs[kwarg]) |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | class UsingTableOptions(object): |
|---|
| 75 | ''' |
|---|
| 76 | The 'using_table_options' DSL statement allows you to set up some |
|---|
| 77 | additional options on your entity table. It is meant only to handle the |
|---|
| 78 | options which are not supported directly by the 'using_options' statement. |
|---|
| 79 | By opposition to the 'using_options' statement, these options are passed |
|---|
| 80 | directly to the underlying SQLAlchemy Table object (as keyword arguments) |
|---|
| 81 | without any processing. |
|---|
| 82 | |
|---|
| 83 | For further information, please refer to the SQLAlchemy documentation. |
|---|
| 84 | ''' |
|---|
| 85 | |
|---|
| 86 | def __init__(self, entity, *args, **kwargs): |
|---|
| 87 | entity._descriptor.table_options = kwargs |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | class UsingMapperOptions(object): |
|---|
| 91 | ''' |
|---|
| 92 | The 'using_mapper_options' DSL statement allows you to set up some |
|---|
| 93 | additional options on your entity mapper. It is meant only to handle the |
|---|
| 94 | options which are not supported directly by the 'using_options' statement. |
|---|
| 95 | By opposition to the 'using_options' statement, these options are passed |
|---|
| 96 | directly to the underlying SQLAlchemy mapper (as keyword arguments) |
|---|
| 97 | without any processing. |
|---|
| 98 | |
|---|
| 99 | For further information, please refer to the SQLAlchemy documentation. |
|---|
| 100 | ''' |
|---|
| 101 | |
|---|
| 102 | def __init__(self, entity, *args, **kwargs): |
|---|
| 103 | entity._descriptor.mapper_options = kwargs |
|---|
| 104 | |
|---|
| 105 | using_options = Statement(UsingOptions) |
|---|
| 106 | using_table_options = Statement(UsingTableOptions) |
|---|
| 107 | using_mapper_options = Statement(UsingMapperOptions) |
|---|