Esempio n. 1
0
def def_table(schema, name, kfields, vfields, fkeys):
    assert not '.' in name, name
    fullname = '.'.join([schema._name, name]) if schema._name else name
    table = sql._table(name, kfields, vfields)
    table._name = fullname
    table._schema = schema
    for k in kfields:
        schema._columns['%s.%s' % (name, k)] = Column(name, k, True)
    for v in vfields:
        schema._columns['%s.%s' % (name, v)] = Column(name, v, False)
    table._columns = schema._columns.get(name)

    # normalize the fkeys syntax
    for leftcol, rightcol in fkeys.iteritems():
        if not '.' in rightcol:
            raise ValueError('You did not specify the table name in %s'
                             % rightcol)
        elif '.' in leftcol:
            # the full syntax '<tablename>.<columname>'
            tname = leftcol.rsplit('.', 1)[0]
            assert tname == name, 'Got %s instead of %s in fkeys' % (
                tname, name)
        else:
            # replace the short colname with the fullname
            fkeys['%s.%s' % (name, leftcol)] = fkeys[leftcol]
            del fkeys[leftcol]

    table._fkeys = fkeys
    setattr(schema, name, table)
    schema._tables.append(name)
    return table
Esempio n. 2
0
def test_convert():
    point = sql._table('point', ['id'], ['x', 'y'])
    driver = generic.driver.mock.instance

    @generic.convert.when_type(generic.driver.mock, point)
    def convert_point(driver, row):
        return row._replace(x=float(row.x), y=float(row.y))
    try:
        assert_equal(generic.convert(driver, point(1, '0', 0)),
                     point(1, 0.0, 0.0))
    finally:
        del generic.convert.typemap[generic.driver.mock, point]
Esempio n. 3
0
def test_convert():
    point = sql._table('point', ['id'], ['x', 'y'])
    driver = UriDict.mock()

    @generic.convert.register(UriDict.mock, point)
    def convert_point(driver, row):
        return row._replace(x=float(row.x), y=float(row.y))
    try:
        assert_equal(generic.convert(driver, point(1, '0', 0)),
                     point(1, 0.0, 0.0))
    finally:
        del generic.convert.typemap[UriDict.mock, point]