Search in sources :

Example 1 with Address

use of com.github.drinkjava2.functionstest.entitynet.entities.Address in project jDialects by drinkjava2.

the class TableModelTest method cloneTest.

@Test
public void cloneTest() {
    TableModel t1 = new TableModel("customers");
    t1.column("name").STRING(20).pkey();
    t1.column("email").STRING(20).pkey().entityField("email").updatable(true).insertable(false);
    t1.column("address").VARCHAR(50).defaultValue("'Beijing'").comment("address comment");
    t1.column("phoneNumber").VARCHAR(50).singleIndex("IDX2");
    t1.column("age").INTEGER().notNull().check("'>0'");
    t1.index("idx3").columns("address", "phoneNumber").unique();
    Assert.assertNotNull(t1.getColumn("name").getTableModel());
    TableModel t2 = t1.newCopy();
    System.out.println(t1);
    System.out.println(t2);
    Assert.assertNotEquals(t1, t2);
    Assert.assertNotNull(t2.getColumn("name").getTableModel());
    System.out.println("================");
    for (ColumnModel item : t1.getColumns()) {
        System.out.println(item);
    }
    System.out.println("================");
    for (ColumnModel item : t2.getColumns()) {
        System.out.println(item);
    }
}
Also used : ColumnModel(com.github.drinkjava2.jdialects.model.ColumnModel) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 2 with Address

use of com.github.drinkjava2.functionstest.entitynet.entities.Address in project jDialects by drinkjava2.

the class DDLTest method sampleTest.

@Test
public void sampleTest() {
    // An example used to put on README.md
    TableModel t1 = new TableModel("customers");
    t1.column("name").STRING(20).pkey();
    t1.column("email").STRING(20).pkey().entityField("email").updatable(true).insertable(false);
    t1.column("address").VARCHAR(50).defaultValue("'Beijing'").comment("address comment");
    t1.column("phoneNumber").VARCHAR(50).singleIndex("IDX2");
    t1.column("age").INTEGER().notNull().check("'>0'");
    t1.index("idx3").columns("address", "phoneNumber").unique();
    TableModel t2 = new TableModel("orders").comment("order comment");
    t2.column("id").LONG().autoId().pkey();
    t2.column("name").STRING(20);
    t2.column("email").STRING(20);
    t2.column("name2").STRING(20).pkey().tail(" default 'Sam'");
    t2.column("email2").STRING(20);
    t2.fkey().columns("name2", "email2").refs("customers", "name", "email");
    t2.fkey("fk1").columns("name", "email").refs("customers", "name", "email");
    t2.unique("uk1").columns("name2", "email2");
    TableModel t3 = new TableModel("sampletable");
    t3.column("id").LONG().identityId().pkey();
    t3.tableGenerator("table_gen1", "tb1", "pkcol2", "valcol", "pkval", 1, 10);
    t3.column("id1").INTEGER().idGenerator("table_gen1");
    t3.sequenceGenerator("seq1", "seq_1", 1, 1);
    t3.column("id2").INTEGER().idGenerator("seq1");
    t3.engineTail(" DEFAULT CHARSET=utf8");
    String[] dropAndCreateDDL = Dialect.H2Dialect.toDropAndCreateDDL(t1, t2, t3);
    for (String ddl : dropAndCreateDDL) System.out.println(ddl);
    testOnCurrentRealDatabase(t1, t2, t3);
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 3 with Address

use of com.github.drinkjava2.functionstest.entitynet.entities.Address in project jDialects by drinkjava2.

the class DDLTest method testFKEY.

@Test
public void testFKEY() {
    // FKEY
    TableModel t1 = new TableModel("master1");
    t1.column("id").INTEGER().pkey();
    TableModel t2 = new TableModel("master2");
    t2.column("name").VARCHAR(20).pkey();
    t2.column("address").VARCHAR(20).pkey();
    t2.column("fid").INTEGER().singleFKey("master1");
    TableModel t3 = new TableModel("child");
    t3.column("id").INTEGER().pkey();
    t3.column("masterid1").INTEGER().singleFKey("master1", "id").fkeyTail("ON DELETE CASCADE ON UPDATE CASCADE").fkeyName("fknm");
    t3.column("myname").VARCHAR(20).singleFKey("master2", "name").fkeyTail("ON DELETE CASCADE ON UPDATE CASCADE");
    t3.column("myaddress").VARCHAR(20).singleFKey("master2", "address");
    t3.fkey().columns("masterid1").refs("master1", "id").fkeyTail("ON DELETE CASCADE ON UPDATE CASCADE");
    ;
    t3.fkey("FKNAME1").columns("myname", "myaddress").refs("master2", "name", "address");
    t3.fkey("FKNAME2").columns("myname", "myaddress").refs("master2");
    TableModel t4 = new TableModel("child2");
    t4.column("id").INTEGER().pkey();
    t4.column("masterid2").INTEGER();
    t4.column("myname2").VARCHAR(20);
    t4.column("myaddress2").VARCHAR(20);
    t4.fkey().columns("masterid2").refs("master1", "id");
    t4.fkey().columns("myname2", "myaddress2").refs("master2", "name", "address");
    printAllDialectsDDLs(t1, t2, t3);
    printOneDialectsDDLs(Dialect.MySQL5InnoDBDialect, t1, t2, t3, t4);
    testOnCurrentRealDatabase(t1, t2, t3, t4);
}
Also used : TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 4 with Address

use of com.github.drinkjava2.functionstest.entitynet.entities.Address in project jSqlBox by drinkjava2.

the class EntityNetDemoTest method testEntityCrudWithTransientAnnotation.

/**
 * Test User class with Transient Annotated fields Address and Role
 */
@Test
public void testEntityCrudWithTransientAnnotation() {
    System.out.println(DebugUtils.getTableModelDebugInfo(TableModelUtils.entity2Model(User.class)));
    new User().put("id", "u1").put("userName", "user1").insert();
    Assert.assertEquals(1, ctx.nQueryForLongValue("select count(*) from usertb"));
    User u = new User().load("u1");
    Assert.assertEquals("user1", u.getUserName());
    u.setUserName("user2");
    u.update();
    Assert.assertEquals("user2", ((User) (new User().load("u1"))).getUserName());
    u.delete();
    Assert.assertEquals(0, ctx.nQueryForLongValue("select count(*) from usertb"));
}
Also used : User(com.github.drinkjava2.functionstest.entitynet.entities.User) Test(org.junit.Test)

Example 5 with Address

use of com.github.drinkjava2.functionstest.entitynet.entities.Address in project jSqlBox by drinkjava2.

the class UsuageAndSpeedTest method xXxxStyle_BasicTemplate.

@Test
public void xXxxStyle_BasicTemplate() {
    SqlBoxContextConfig config = new SqlBoxContextConfig();
    config.setTemplateEngine(BasicSqlTemplate.instance());
    SqlBoxContext ctx = new SqlBoxContext(dataSource, config);
    for (int i = 0; i < REPEAT_TIMES; i++) {
        UserAR user = new UserAR("Sam", "Canada");
        UserAR tom = new UserAR("Tom", "China");
        put0("user", user);
        ctx.xExecute("insert into users (name, address) values(#{user.name},#{user.address})");
        put0("user", tom);
        ctx.xExecute("update users set name=#{user.name}, address=#{user.address}");
        Assert.assertEquals(1L, ctx.xQueryForObject("select count(*) from users where ${col}=#{name} and address=#{addr}", put0("name", "Tom"), put("addr", "China"), replace("col", "name")));
        ctx.xExecute("delete from users where name=#{u.name} or address=#{u.address}", put0("u", tom));
    }
}
Also used : SqlBoxContextConfig(com.github.drinkjava2.jsqlbox.SqlBoxContextConfig) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 SqlBoxContext (com.github.drinkjava2.jsqlbox.SqlBoxContext)8 TableModel (com.github.drinkjava2.jdialects.model.TableModel)4 User (com.github.drinkjava2.functionstest.entitynet.entities.User)2 SQLException (java.sql.SQLException)2 Address (com.github.drinkjava2.functionstest.entitynet.entities.Address)1 Email (com.github.drinkjava2.functionstest.entitynet.entities.Email)1 Privilege (com.github.drinkjava2.functionstest.entitynet.entities.Privilege)1 Role (com.github.drinkjava2.functionstest.entitynet.entities.Role)1 RolePrivilege (com.github.drinkjava2.functionstest.entitynet.entities.RolePrivilege)1 UserRole (com.github.drinkjava2.functionstest.entitynet.entities.UserRole)1 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)1 Table (com.github.drinkjava2.jdialects.model.Table)1 SqlBoxContextConfig (com.github.drinkjava2.jsqlbox.SqlBoxContextConfig)1 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1