Package org.apache.cayenne.map

Examples of org.apache.cayenne.map.DbAttribute


        dropTableIfPresent(node, "NEW_TABLE");
        assertTokensAndExecute(node, map, 0, 0);

        DbEntity dbEntity = new DbEntity("NEW_TABLE");

        DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
        column1.setMandatory(true);
        column1.setPrimaryKey(true);
        dbEntity.addAttribute(column1);

        DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
        column2.setMaxLength(10);
        column2.setMandatory(false);
        dbEntity.addAttribute(column2);

        // for the new entity to the db
        execute(mergerFactory().createCreateTableToDb(dbEntity));
View Full Code Here


    public void test() throws Exception {
        DbEntity dbEntity = map.getDbEntity("PAINTING");
        assertNotNull(dbEntity);

        // create and add new column to model and db
        DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);

        try {

            column.setMandatory(true);
            column.setMaxLength(10);
            dbEntity.addAttribute(column);
            assertTokensAndExecute(node, map, 2, 0);

            // check that is was merged
            assertTokensAndExecute(node, map, 0, 0);

            // set null
            column.setMandatory(false);

            // merge to db
            assertTokensAndExecute(node, map, 1, 0);

            // check that is was merged
            assertTokensAndExecute(node, map, 0, 0);

            // clean up
        }
        finally {
            dbEntity.removeAttribute(column.getName());
            assertTokensAndExecute(node, map, 1, 0);
            assertTokensAndExecute(node, map, 0, 0);
        }
    }
View Full Code Here

    static class DbAttributeFactory extends ObjAttributeFactory {

        @Override
        protected Object create(String name, Object namingContext) {
            return new DbAttribute(
                    name,
                    TypesMapping.NOT_DEFINED,
                    (DbEntity) namingContext);
        }
View Full Code Here

        dropTableIfPresent(node, "NEW_TABLE");
        assertTokensAndExecute(node, map, 0, 0);

        DbEntity dbEntity = new DbEntity("NEW_TABLE");

        DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
        column1.setMandatory(true);
        column1.setPrimaryKey(true);
        dbEntity.addAttribute(column1);

        DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
        column2.setMaxLength(10);
        column2.setMandatory(false);
        dbEntity.addAttribute(column2);

        map.addDbEntity(dbEntity);

        assertTokensAndExecute(node, map, 1, 0);
        assertTokensAndExecute(node, map, 0, 0);

        ObjEntity objEntity = new ObjEntity("NewTable");
        objEntity.setDbEntity(dbEntity);
        ObjAttribute oatr1 = new ObjAttribute("name");
        oatr1.setDbAttributePath(column2.getName());
        oatr1.setType("java.lang.String");
        objEntity.addAttribute(oatr1);
        map.addObjEntity(objEntity);

        // force drop table in db
View Full Code Here

        assertTokensAndExecute(node, map, 0, 0);

        DbEntity dbEntity1 = new DbEntity("NEW_TABLE");

        DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
        e1col1.setMandatory(true);
        e1col1.setPrimaryKey(true);
        dbEntity1.addAttribute(e1col1);

        DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
        e1col2.setMaxLength(10);
        e1col2.setMandatory(false);
        dbEntity1.addAttribute(e1col2);

        map.addDbEntity(dbEntity1);

        DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
        DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
        e2col1.setMandatory(true);
        e2col1.setPrimaryKey(true);
        dbEntity2.addAttribute(e2col1);
        DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
        dbEntity2.addAttribute(e2col2);
        DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
        e2col3.setMaxLength(10);
        dbEntity2.addAttribute(e2col3);

        map.addDbEntity(dbEntity2);

        // create db relationships
        DbRelationship rel1To2 = new DbRelationship("rel1To2");
        rel1To2.setSourceEntity(dbEntity1);
        rel1To2.setTargetEntity(dbEntity2);
        rel1To2.setToMany(true);
        rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
        dbEntity1.addRelationship(rel1To2);
        DbRelationship rel2To1 = new DbRelationship("rel2To1");
        rel2To1.setSourceEntity(dbEntity2);
        rel2To1.setTargetEntity(dbEntity1);
        rel2To1.setToMany(false);
        rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
        dbEntity2.addRelationship(rel2To1);
        assertSame(rel1To2, rel2To1.getReverseRelationship());
        assertSame(rel2To1, rel1To2.getReverseRelationship());

        assertTokensAndExecute(node, map, 4, 0);
        assertTokensAndExecute(node, map, 0, 0);

        // create ObjEntities
        ObjEntity objEntity1 = new ObjEntity("NewTable");
        objEntity1.setDbEntity(dbEntity1);
        ObjAttribute oatr1 = new ObjAttribute("name");
        oatr1.setDbAttributePath(e1col2.getName());
        oatr1.setType("java.lang.String");
        objEntity1.addAttribute(oatr1);
        map.addObjEntity(objEntity1);
        ObjEntity objEntity2 = new ObjEntity("NewTable2");
        objEntity2.setDbEntity(dbEntity2);
        ObjAttribute o2a1 = new ObjAttribute("name");
        o2a1.setDbAttributePath(e2col3.getName());
        o2a1.setType("java.lang.String");
        objEntity2.addAttribute(o2a1);
        map.addObjEntity(objEntity2);
       
        // create ObjRelationships
View Full Code Here

public class PostgresAdapterTest extends TestCase {
   
    public void testCreateTableWithFloatAttributeWithScale () {
        PostgresAdapter adapter = new PostgresAdapter();
        DbEntity e = new DbEntity("Test");
        DbAttribute dblPrec = new DbAttribute("dbl1");
        dblPrec.setType(Types.FLOAT);
        dblPrec.setMaxLength(22);
        dblPrec.setScale(12);
        e.addAttribute(dblPrec);
       
        String sql = adapter.createTable(e);

        // CAY-1363.
View Full Code Here

            o.writeProperty("paintingTitle", "ptitle" + i);
        }
        ctxt.commitChanges();

        // create and add new column to model and db
        DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);

        column.setMandatory(false);
        column.setMaxLength(10);
        dbEntity.addAttribute(column);
        assertTrue(dbEntity.getAttributes().contains(column));
        assertEquals(column, dbEntity.getAttribute(column.getName()));
        assertTokensAndExecute(node, map, 1, 0);

        // need obj attr to be able to query
        ObjAttribute objAttr = new ObjAttribute("newcol2");
        objAttr.setDbAttributePath(column.getName());
        objEntity.addAttribute(objAttr);

        // check that is was merged
        assertTokensAndExecute(node, map, 0, 0);

        // set not null
        column.setMandatory(true);

        // merge to db
        assertTokensAndExecute(node, map, 2, 0);

        // check that is was merged
        assertTokensAndExecute(node, map, 0, 0);

        // check values for null
        Expression qual = ExpressionFactory.matchExp(
                objAttr.getName(),
                DEFAULT_VALUE_STRING);
        SelectQuery query = new SelectQuery("Painting", qual);
        List<Persistent> rows = ctxt.performQuery(query);
        assertEquals(nrows, rows.size());

        // clean up
        dbEntity.removeAttribute(column.getName());
        assertTokensAndExecute(node, map, 1, 0);
        assertTokensAndExecute(node, map, 0, 0);
    }
View Full Code Here

        assertTokensAndExecute(node, map, 0, 0);

        DbEntity dbEntity = new DbEntity("NEW_TABLE");

        DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
        column1.setMandatory(true);
        column1.setPrimaryKey(true);
        dbEntity.addAttribute(column1);

        DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
        column2.setMaxLength(10);
        column2.setMandatory(false);
        dbEntity.addAttribute(column2);

        map.addDbEntity(dbEntity);

        assertTokensAndExecute(node, map, 1, 0);
        assertTokensAndExecute(node, map, 0, 0);

        ObjEntity objEntity = new ObjEntity("NewTable");
        objEntity.setDbEntity(dbEntity);
        ObjAttribute oatr1 = new ObjAttribute("name");
        oatr1.setDbAttributePath(column2.getName());
        oatr1.setType("java.lang.String");       
        objEntity.addAttribute(oatr1);
        map.addObjEntity(objEntity);

        // force drop name column in db
        MergerToken token = mergerFactory().createDropColumnToDb(dbEntity, column2);
        execute(token);

        List<MergerToken> tokens = createMergeTokens();
        assertEquals(1, tokens.size());
        token = tokens.get(0);
        if (token.getDirection().isToDb()) {
            token = token.createReverse(mergerFactory());
        }
        assertTrue(token instanceof DropColumnToModel);
        execute(token);
        assertNull(dbEntity.getAttribute(column2.getName()));
        assertNull(objEntity.getAttribute(oatr1.getName()));

        DataContext ctxt = createDataContext();

        // clear up
View Full Code Here

        assertTokensAndExecute(node, map, 0, 0);

        DbEntity dbEntity1 = new DbEntity("NEW_TABLE");

        DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
        e1col1.setMandatory(true);
        e1col1.setPrimaryKey(true);
        dbEntity1.addAttribute(e1col1);

        DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
        e1col2.setMaxLength(10);
        e1col2.setMandatory(false);
        dbEntity1.addAttribute(e1col2);

        map.addDbEntity(dbEntity1);

        DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
        DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
        e2col1.setMandatory(true);
        e2col1.setPrimaryKey(true);
        dbEntity2.addAttribute(e2col1);
        DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
        dbEntity2.addAttribute(e2col2);
        DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
        e2col3.setMaxLength(10);
        dbEntity2.addAttribute(e2col3);

        map.addDbEntity(dbEntity2);

        assertTokensAndExecute(node, map, 2, 0);
        assertTokensAndExecute(node, map, 0, 0);
       
        // force drop fk column in db
        execute(mergerFactory().createDropColumnToDb(dbEntity2, e2col2));

        // create db relationships, but do not sync them to db
        DbRelationship rel1To2 = new DbRelationship("rel1To2");
        rel1To2.setSourceEntity(dbEntity1);
        rel1To2.setTargetEntity(dbEntity2);
        rel1To2.setToMany(true);
        rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
        dbEntity1.addRelationship(rel1To2);
        DbRelationship rel2To1 = new DbRelationship("rel2To1");
        rel2To1.setSourceEntity(dbEntity2);
        rel2To1.setTargetEntity(dbEntity1);
        rel2To1.setToMany(false);
        rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
        dbEntity2.addRelationship(rel2To1);
        assertSame(rel1To2, rel2To1.getReverseRelationship());
        assertSame(rel2To1, rel1To2.getReverseRelationship());

        // create ObjEntities
        ObjEntity objEntity1 = new ObjEntity("NewTable");
        objEntity1.setDbEntity(dbEntity1);
        ObjAttribute oatr1 = new ObjAttribute("name");
        oatr1.setDbAttributePath(e1col2.getName());
        oatr1.setType("java.lang.String");
        objEntity1.addAttribute(oatr1);
        map.addObjEntity(objEntity1);
        ObjEntity objEntity2 = new ObjEntity("NewTable2");
        objEntity2.setDbEntity(dbEntity2);
        ObjAttribute o2a1 = new ObjAttribute("name");
        o2a1.setDbAttributePath(e2col3.getName());
        o2a1.setType("java.lang.String");
        objEntity2.addAttribute(o2a1);
        map.addObjEntity(objEntity2);
       
        // create ObjRelationships
View Full Code Here

    }
   
    public void testCreateTableQuoteSqlIdentifiers() {
        
        DbEntity entity = new DbEntity();
        DbAttribute attr = new DbAttribute();
        attr.setName("name column");
        attr.setType(1);
        entity.addAttribute(attr);
       
        DbKeyGenerator id = new DbKeyGenerator();
        entity.setPrimaryKeyGenerator(id);
       
View Full Code Here

TOP

Related Classes of org.apache.cayenne.map.DbAttribute

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.