Esempio n. 1
0
    def _create_objects(self):
        '''Create all objects

        Each object has scalar fields corresponding to all columns in the database,
        except FOREIGN KEY columns, which are used to create relationships further.

        PRIMARY KEY is also set.
        '''
        #   All table names, without schema
        table_names = self._q.get_table_names_in_schema(self._name)
        for table_name in sorted(table_names):

            #   Create object
            obj = self._dm.create_object(table_name)

            #   Create all scalar fields
            columns = self._q.get_columns_data(self._name, table_name)

            for column in columns:
                if column['fkey']:
                    #   This will be used further, in _create_relationship
                    self._fk_columns[table_name].add(column['name'])
                else:
                    #   Note: default is always 'None', because we don't want to
                    #   duplicate internal PostgreSQL defaults
                    obj.add_field(
                        Scalar(column['name'],
                               pkey=column['pkey'],
                               default=None,
                               type_=column['type']))
Esempio n. 2
0
    def add_field_closed(dm):
        jar = dm.object('jar')
        jar.add_field(Scalar('closed', default=False))

        #   Add database column if PGStorage
        if issubclass(type(engine.world().storage), engine.PGStorage):
            engine.world().storage._conn.cursor().execute('''
                ALTER TABLE jar ADD COLUMN closed boolean;
            ''')
Esempio n. 3
0
def test_scalar_type(type_, value, raises):
    field = Scalar('some_field', type_=type_)
    if raises:
        with pytest.raises(exceptions.BadFieldValue):
            field.val(value)
    elif type_:
        assert field.val(value)._val == type_(value)
    else:
        assert field.val(value)._val == value
Esempio n. 4
0
def init_cookies_with_shelf_1(init_world):
    '''Add shelf to cookies datamodel. 
    Shelf knows it's jars, jar has no idea about shelf
    '''
    dm = deepcopy(cookies.dm)
    shelf = dm.create_object('shelf')
    shelf.add_field(Scalar('id', pkey=True, type_=int))
    shelf.add_field(Rel('jars', stores=dm.object('jar'), multi=True))

    init_world(dm)

    #   If this is PG storage, we need to update database schema
    if issubclass(type(world().storage), PGStorage):
        conn = world().storage._conn
        conn.cursor().execute('CREATE TABLE shelf (id serial PRIMARY KEY, jars integer[])')
        conn.commit()
Esempio n. 5
0
def init_cookies_with_shelf_2(init_world):
    '''Add shelf to cookies datamodel. 
    Jar knows it's shelf, shelf has no idea about jars.
    '''
    dm = deepcopy(cookies.dm)
    shelf = dm.create_object('shelf')
    shelf.add_field(Scalar('id', pkey=True, type_=int))
    jar = dm.object('jar')
    jar.add_field(Rel('shelf', stores=shelf, multi=False))
    init_world(dm)

    #   If this is PG storage, we need to update database schema
    if issubclass(type(world().storage), PGStorage):
        conn = world().storage._conn
        conn.cursor().execute('CREATE TABLE shelf (id serial PRIMARY KEY)')
        conn.cursor().execute('ALTER TABLE jar ADD COLUMN shelf integer REFERENCES shelf(id)')
        conn.commit()
Esempio n. 6
0
from blargh.data_model import DataModel
from blargh.data_model.fields import Scalar, Rel

#   Initialize the data model. This name 'cookies' is just an identifier,
#   useful if we are dealing with multiple data models.
dm = DataModel('cookies')

#   Create 'cookie' object. Each cookie has
#   *   id, which is an integer primary key
#   *   type, which is a string
cookie = dm.create_object('cookie')
cookie.add_field(Scalar('id', pkey=True, type_=int))
cookie.add_field(Scalar('type', type_=str))

#   Create 'jar' object. The only field is id,
#   which is also an integer primary key
jar = dm.create_object('jar')
jar.add_field(Scalar('id', pkey=True, type_=int))

#   Add a relational field to cookie.
#   This field ('jar') can hold at most one jar (and nothing else)
cookie.add_field(Rel('jar', stores=jar, multi=False))

#   Jar gets 'cookies' field, where any number of cookies can be held.
jar.add_field(Rel('cookies', stores=cookie, multi=True))

#   Until now, fields cookie.jar and jar.cookie could mean totally different things,
#   e.g. cookie.jar could be a jar with ingredients required to bake this cookie,
#   and jar.cookies could be a list of all cookies that could fit in the jar.
#
#   Now we declare that this is the same relationship: if certain cookie has certain
Esempio n. 7
0
from blargh.data_model import DataModel
from blargh.data_model.fields import Scalar, Rel, Calc

dm = DataModel('family')

#   OBJECTS & SCALAR FIELDS
male = dm.create_object('male')
male.add_field(Scalar('id', pkey=True, type_=int))
male.add_field(Scalar('name', type_=str))

female = dm.create_object('female')
female.add_field(Scalar('id', pkey=True, type_=int))
female.add_field(Scalar('name', type_=str))

child = dm.create_object('child')
child.add_field(Scalar('id', pkey=True, type_=int))
child.add_field(Scalar('name', type_=str))

#   REL FIELDS
male.add_field(Rel('wife', stores=female, multi=False))  # noqa: E241
male.add_field(Rel('children', stores=child, multi=True))  # noqa: E241
female.add_field(Rel('husband', stores=male, multi=False))  # noqa: E241
female.add_field(Rel('children', stores=child, multi=True))  # noqa: E241
child.add_field(Rel('father', stores=male, multi=False))  # noqa: E241
child.add_field(Rel('mother', stores=female, multi=False))  # noqa: E241

#   CONNECTIONS
dm.connect(male, 'wife', female, 'husband')  # noqa: E241
dm.connect(child, 'father', male, 'children')  # noqa: E241
dm.connect(child, 'mother', female, 'children')  # noqa: E241