root / elixir / trunk / elixir / fields.py @ 18

Revision 18, 3.1 kB (checked in by cleverdevil, 6 years ago)

A bunch of docstring updates, representing a good chunk of documentation.
The docstring on relationships is only partially done, as I ran out of
gas after describing the 'belongs_to' relationship type.

Line 
1from sqlalchemy             import Column
2from elixir.statements      import Statement
3
4__all__ = [
5    'has_field',
6    'with_fields',
7    'Field'
8]
9
10
11class Field(object):
12    '''
13    Represents the definition of a 'field' on an entity.  In other words, this
14    class represents a column on the table where the entity is stored.  This
15    object is only used with the 'with_fields' syntax for defining all fields
16    for an entity at the same time.  The 'has_field' syntax does not require
17    the manual creation of this object.
18    '''
19   
20    def __init__(self, type, *args, **kwargs):
21        self.colname = kwargs.pop('colname', None)
22        self.type = type
23        self.primary_key = kwargs.get('primary_key', False)
24       
25        self.args = args
26        self.kwargs = kwargs
27   
28    @property
29    def column(self):
30        '''
31        Returns the corresponding sqlalchemy-column
32        '''
33   
34        if hasattr(self, '_column'):
35            return self._column
36       
37        self._column = Column(self.colname, self.type,
38                              *self.args, **self.kwargs)
39        return self._column
40
41
42class HasField(object):
43    '''
44    Statement object for specifying a single field on an entity.  The first
45    argument is the name of the field, the second is its type, and following
46    this any number of keyword arguments can be specified for additional
47    behavior.  The keyword arguments are passed on to the SQLAlchemy 'Column'
48    object.  Please refer to the SQLAlchemy 'Column' object's documentation for
49    further detail about which keyword arguments are supported.
50   
51    Here is a quick example of how to use 'has_field'.
52   
53        class Person(Entity):
54            has_field('id', Integer, primary_key=True)
55            has_field('name', String(50))
56    '''
57   
58    def __init__(self, entity, name, *args, **kwargs):
59        field = Field(*args, **kwargs)
60        field.colname = name
61        entity._descriptor.add_field(field)
62
63
64class WithFields(object):
65    '''
66    Statement object for specifying all fields on an entity at once.  Each
67    keyword argument to this statement represents one field, which should be
68    a Field object.  The first argument to a Field object is its type, and
69    following this any number of keyword arguments can be specified for
70    additional behavior.  The keyword arguments are passed on to the SQLAlchemy
71    'Column' object.  Please refer to the SQLAlchemy 'Column' object's
72    documentation for further detail about which keyword arguments are
73    supported.
74   
75    Here is a quick example of how to use 'with_fields'.
76   
77        class Person(Entity):
78            with_fields(
79                id = Field(Integer, primary_key=True),
80                name = Field(String(50))
81            )
82    '''
83   
84    def __init__(self, entity, *args, **fields):
85        columns = list()
86        desc = entity._descriptor
87       
88        for colname, field in fields.iteritems():
89            if not field.colname:
90                field.colname = colname
91            desc.add_field(field)
92
93
94has_field   = Statement(HasField)
95with_fields = Statement(WithFields)
Note: See TracBrowser for help on using the browser.