public PropertyBuilder addProperty(PropertyType propertyType, String propertyName) { if (!propertyNames.add(propertyName)) { throw new RuntimeException("Property already defined: " + propertyName); } PropertyBuilder builder = new PropertyBuilder(schema, this, propertyType, propertyName); properties.add(builder.getProperty()); return builder; }
@Test public void testMinimalSchema() throws Exception { Schema schema = new Schema(1, "org.greenrobot.testdao"); Entity addressEntity = schema.addEntity("Addresse"); Property idProperty = addressEntity.addIdProperty().getProperty(); addressEntity.addIntProperty("count").index(); addressEntity.addIntProperty("dummy").notNull(); assertEquals(1, schema.getEntities().size()); assertEquals(3, addressEntity.getProperties().size()); File outputDir = new File("build/test-out"); outputDir.mkdirs(); File daoFile = new File(outputDir, "org/greenrobot/testdao/" + addressEntity.getClassName() + "Dao.java"); daoFile.delete(); assertFalse(daoFile.exists()); new DaoGenerator().generateAll(schema, outputDir.getPath()); assertEquals("PRIMARY KEY", idProperty.getConstraints()); assertTrue(daoFile.toString(), daoFile.exists()); }
public ToOne addToOneWithoutProperty(String name, Entity target, String fkColumnName, boolean notNull, boolean unique) { PropertyBuilder propertyBuilder = new PropertyBuilder(schema, this, null, name); if (notNull) { propertyBuilder.notNull(); } if (unique) { propertyBuilder.unique(); } propertyBuilder.dbName(fkColumnName); Property column = propertyBuilder.getProperty(); Property[] fkColumns = {column}; ToOne toOne = new ToOne(schema, this, target, fkColumns, false); toOne.setName(name); toOneRelations.add(toOne); return toOne; }
public static void main(String[] args) throws Exception{ Schema schema = new Schema(1, "com.codingbingo.fastreader.dao"); Entity book = schema.addEntity("Book"); book.addIdProperty().primaryKey().autoincrement(); book.addStringProperty("bookName").notNull(); book.addStringProperty("bookImagePath"); book.addStringProperty("description"); book.addStringProperty("tags"); book.addStringProperty("writer"); book.addStringProperty("charSet"); book.addStringProperty("bookPath"); book.addIntProperty("currentChapter"); book.addIntProperty("currentPosition"); book.addIntProperty("processStatus").notNull(); Entity chapter = schema.addEntity("Chapter"); chapter.addIdProperty().primaryKey().autoincrement(); chapter.addStringProperty("title"); chapter.addIntProperty("position").notNull(); chapter.addIntProperty("pageCount"); chapter.addBooleanProperty("isRead"); Property bookIdProperty = chapter.addLongProperty("bookId").notNull().getProperty(); chapter.addToOne(book, bookIdProperty); new DaoGenerator().generateAll(schema, "/Users/bingo/Git/FastReader/app/src/main/java"); }
protected void createToMany() { Entity toManyTargetEntity = schema.addEntity("ToManyTargetEntity"); Property toManyIdProperty = toManyTargetEntity.addLongProperty("toManyId").getProperty(); Property toManyIdDescProperty = toManyTargetEntity.addLongProperty("toManyIdDesc").getProperty(); Property targetIdProperty = toManyTargetEntity.addIdProperty().getProperty(); Property targetJoinProperty = toManyTargetEntity.addStringProperty("targetJoinProperty").getProperty(); Entity toManyEntity = schema.addEntity("ToManyEntity"); Property sourceIdProperty = toManyEntity.addIdProperty().getProperty(); Property sourceJoinProperty = toManyEntity.addStringProperty("sourceJoinProperty").getProperty(); ToMany toMany = toManyEntity.addToMany(toManyTargetEntity, toManyIdProperty); toMany.orderAsc(targetIdProperty); ToMany toManyDesc = toManyEntity.addToMany(toManyTargetEntity, toManyIdDescProperty); toManyDesc.setName("toManyDescList"); toManyDesc.orderDesc(targetIdProperty); ToMany toManyByJoinProperty = toManyEntity .addToMany(sourceJoinProperty, toManyTargetEntity, targetJoinProperty); toManyByJoinProperty.setName("toManyByJoinProperty"); toManyByJoinProperty.orderAsc(targetIdProperty); Property[] sourceProperties = {sourceIdProperty, sourceJoinProperty}; Property[] targetProperties = {toManyIdProperty, targetJoinProperty}; ToMany toManyJoinTwo = toManyEntity.addToMany(sourceProperties, toManyTargetEntity, targetProperties); toManyJoinTwo.setName("toManyJoinTwo"); toManyJoinTwo.orderDesc(targetJoinProperty); toManyJoinTwo.orderDesc(targetIdProperty); Entity toManyJoinEntity = schema.addEntity("JoinManyToDateEntity"); toManyJoinEntity.addIdProperty(); Property id1 = toManyJoinEntity.addLongProperty("idToMany").getProperty(); Property id2 = toManyJoinEntity.addLongProperty("idDate").getProperty(); toManyEntity.addToMany(dateEntity, toManyJoinEntity, id1, id2); }
private Schema createSchema2() { Schema schema2 = new Schema(1, "org.greenrobot.greendao.daotest2"); schema2.setDefaultJavaPackageTest("org.greenrobot.greendao.daotest2.entity"); schema2.setDefaultJavaPackageDao("org.greenrobot.greendao.daotest2.dao"); schema2.enableKeepSectionsByDefault(); Entity keepEntity = schema2.addEntity("KeepEntity"); keepEntity.addIdProperty(); Entity toManyTarget2 = schema2.addEntity("ToManyTarget2"); toManyTarget2.addIdProperty(); Property toManyTarget2FkId = toManyTarget2.addLongProperty("fkId").getProperty(); toManyTarget2.setSkipGenerationTest(true); Entity toOneTarget2 = schema2.addEntity("ToOneTarget2"); toOneTarget2.addIdProperty(); toOneTarget2.setJavaPackage("org.greenrobot.greendao.daotest2.to1_specialentity"); toOneTarget2.setJavaPackageDao("org.greenrobot.greendao.daotest2.to1_specialdao"); toOneTarget2.setJavaPackageTest("org.greenrobot.greendao.daotest2.to1_specialtest"); toOneTarget2.setSkipGenerationTest(true); Entity relationSource2 = schema2.addEntity("RelationSource2"); relationSource2.addIdProperty(); relationSource2.addToMany(toManyTarget2, toManyTarget2FkId); Property toOneId = relationSource2.addLongProperty("toOneId").getProperty(); relationSource2.addToOne(toOneTarget2, toOneId); relationSource2.setJavaPackage("org.greenrobot.greendao.daotest2.specialentity"); relationSource2.setJavaPackageDao("org.greenrobot.greendao.daotest2.specialdao"); relationSource2.setJavaPackageTest("org.greenrobot.greendao.daotest2.specialtest"); relationSource2.setSkipGenerationTest(true); return schema2; }
protected void createRelation() { Entity relationEntity = schema.addEntity("RelationEntity"); relationEntity.addIdProperty(); Property parentIdProperty = relationEntity.addLongProperty("parentId").getProperty(); relationEntity.addToOne(relationEntity, parentIdProperty).setName("parent"); Property testIdProperty = relationEntity.addLongProperty("testId").getProperty(); relationEntity.addToOne(testEntity, testIdProperty); Property testIdNotNullProperty = relationEntity.addLongProperty("testIdNotNull").notNull().getProperty(); relationEntity.addToOne(testEntity, testIdNotNullProperty).setName("testNotNull"); relationEntity.addStringProperty("simpleString"); relationEntity.addToOneWithoutProperty("testWithoutProperty", testEntity, "WITHOUT_PROPERTY_TEST_ID"); }
protected void createTreeEntity() { Entity treeEntity = schema.addEntity("TreeEntity"); treeEntity.addIdProperty(); Property parentIdProperty = treeEntity.addLongProperty("parentId").getProperty(); treeEntity.addToOne(treeEntity, parentIdProperty).setName("parent"); treeEntity.addToMany(treeEntity, parentIdProperty).setName("children"); }