Search in sources :

Example 1 with Girl

use of jodd.db.oom.tst.Girl in project jodd by oblac.

the class DbEntitySqlTest method testUpdate.

@Test
public void testUpdate() {
    Girl g = new Girl(1, "sanja", "c++");
    DbSqlBuilder b = DbEntitySql.update(g);
    assertEquals("update GIRL Girl_ set ID=:girl.id, NAME=:girl.name, SPECIALITY=:girl.speciality  where (1=1)", b.generateQuery());
    checkGirl(b);
    BadGirl bg = new BadGirl(Integer.valueOf(2), null, ".net");
    b = DbEntitySql.update(bg);
    assertEquals("update GIRL BadGirl_ set ID=:badGirl.fooid, SPECIALITY=:badGirl.foospeciality  where (BadGirl_.ID=:badGirl.fooid)", b.generateQuery());
    checkBadGirl1(b);
    b = DbEntitySql.updateAll(bg);
    assertEquals("update GIRL BadGirl_ set ID=:badGirl.fooid, NAME=:badGirl.fooname, SPECIALITY=:badGirl.foospeciality  where (BadGirl_.ID=:badGirl.fooid)", b.generateQuery());
    checkBadGirl2(b);
}
Also used : BadGirl(jodd.db.oom.tst.BadGirl) BadGirl(jodd.db.oom.tst.BadGirl) Girl(jodd.db.oom.tst.Girl) Test(org.junit.Test)

Example 2 with Girl

use of jodd.db.oom.tst.Girl in project jodd by oblac.

the class DbNoTableTest method testMappingNoTable.

@Test
public void testMappingNoTable() {
    DbSession session = new DbThreadSession(cp);
    assertEquals(1, DbEntitySql.insert(new Girl(1, "Anna", "swim")).query().autoClose().executeUpdate());
    assertEquals(0, session.getTotalQueries());
    // one
    DbOomQuery q = new DbOomQuery(DbSqlBuilder.sql("select $C{g.id} + 10, UCASE($C{g.name}) from $T{Girl g}"));
    Object[] row = q.find(Integer.class, String.class);
    assertEquals(Integer.valueOf(11), row[0]);
    assertEquals("ANNA", row[1]);
    // two
    DbSqlBuilder dbSqlBuilder = DbSqlBuilder.sql("select $g.id + 10 as $C{Bean1.sum}, UCASE($g.name) as $C{Bean1.bigName}, $C{g.*} from $T{Girl g} where $g.id=1").aliasColumnsAs(ColumnAliasType.COLUMN_CODE);
    assertEquals("select g.ID + 10 as col_0_, UCASE(g.NAME) as col_1_, g.ID as col_2_, g.NAME as col_3_, g.SPECIALITY as col_4_ from GIRL g where g.ID=1", dbSqlBuilder.generateQuery());
    dbSqlBuilder.resetAll();
    assertEquals("select g.ID + 10 as Bean1$SUM, UCASE(g.NAME) as Bean1$BIG_NAME, g.ID, g.NAME, g.SPECIALITY from GIRL g where g.ID=1", dbSqlBuilder.generateQuery());
    dbSqlBuilder.reset();
    q = new DbOomQuery(dbSqlBuilder);
    row = q.find(Bean1.class, Girl.class);
    Bean1 bean1 = (Bean1) row[0];
    Girl girl = (Girl) row[1];
    assertNotNull(bean1);
    assertEquals(11, bean1.getSum().intValue());
    assertEquals("ANNA", bean1.getBigName());
    assertNotNull(girl);
    assertEquals("Anna", girl.name);
    assertEquals("swim", girl.speciality);
    assertEquals(1, girl.id);
    // three
    dbSqlBuilder = DbSqlBuilder.sql("select $g.id + 10 as $C{Bean1.sum}, UCASE($g.name) as $C{Bean1.bigName}, $C{g.*} from $T{Girl g} where $g.id=1");
    assertEquals("select g.ID + 10 as Bean1$SUM, UCASE(g.NAME) as Bean1$BIG_NAME, g.ID, g.NAME, g.SPECIALITY from GIRL g where g.ID=1", dbSqlBuilder.generateQuery());
    dbSqlBuilder.reset();
    q = new DbOomQuery(dbSqlBuilder);
    row = q.find(Bean1.class, Girl.class);
    bean1 = (Bean1) row[0];
    girl = (Girl) row[1];
    assertNotNull(bean1);
    assertEquals(11, bean1.getSum().intValue());
    assertEquals("ANNA", bean1.getBigName());
    assertNotNull(girl);
    assertEquals("Anna", girl.name);
    assertEquals("swim", girl.speciality);
    assertEquals(1, girl.id);
    session.closeSession();
}
Also used : DbSession(jodd.db.DbSession) DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) DbThreadSession(jodd.db.DbThreadSession) Girl(jodd.db.oom.tst.Girl) Test(org.junit.Test)

Example 3 with Girl

use of jodd.db.oom.tst.Girl in project jodd by oblac.

the class DbOom2Test method testOrm2.

@Test
public void testOrm2() {
    DbOomManager.resetAll();
    DbSession session = new DbThreadSession(cp);
    executeUpdate(session, "drop table BOY if exists");
    executeUpdate(session, "drop table GIRL if exists");
    String sql = "create table GIRL (" + "ID			IDENTITY," + "NAME		varchar(20)	not null," + "SPECIALITY	varchar(20)	null," + "TIME timestamp not null default CURRENT_TIMESTAMP, " + "primary key (ID)" + ')';
    DbQuery query = new DbQuery(sql);
    query.autoClose().executeUpdate();
    assertTrue(query.isClosed());
    executeUpdate(session, "insert into GIRL(ID, NAME, SPECIALITY) values(1, 'Anna', 'swim')");
    executeUpdate(session, "insert into GIRL(ID, NAME, SPECIALITY) values(2, 'Sandra', 'piano')");
    executeUpdate(session, "insert into GIRL(NAME) values('Monica')");
    session.closeSession();
    assertTrue(session.isSessionClosed());
    // prepare
    session = new DbThreadSession(cp);
    // ---------------------------------------------------------------- girls
    DbOomQuery q = new DbOomQuery("select * from GIRL where ID=1");
    Girl girl = q.find(Girl.class);
    checkGirl1(girl);
    assertTrue(q.isActive());
    IdName idName = q.find(IdName.class);
    assertNotNull(idName);
    assertEquals(1, idName.id);
    assertEquals("Anna", idName.name);
    try {
        // this will fail since no entity is registered!
        q.find();
        fail();
    } catch (DbOomException doex) {
    // ignore
    }
    assertEquals(2, DbOomManager.getInstance().getTotalTypes());
    assertEquals(0, DbOomManager.getInstance().getTotalTableNames());
    assertEquals(2, DbOomManager.getInstance().getTotalNames());
    DbOomManager.getInstance().registerEntity(Girl.class, true);
    girl = q.find();
    checkGirl1(girl);
    assertEquals(2, DbOomManager.getInstance().getTotalTypes());
    assertEquals(1, DbOomManager.getInstance().getTotalTableNames());
    assertEquals(2, DbOomManager.getInstance().getTotalNames());
    q.close();
    session.closeSession();
    /**
		 * Test fails on HSQLDB 1.8 since generated columns are not supported.
		 */
    session = new DbThreadSession(cp);
    q = new DbOomQuery("insert into GIRL (NAME) values('Janna')");
    q.setGeneratedColumns();
    q.executeUpdate();
    long key = q.getGeneratedKey();
    assertEquals(4, key);
    q.close();
    q = new DbOomQuery("insert into GIRL (NAME) values('Janna2')");
    q.setGeneratedColumns("ID", "TIME");
    q.executeUpdate();
    Long Key = q.findGeneratedKey(Long.class);
    assertEquals(5, Key.longValue());
    q.close();
    q = new DbOomQuery("insert into GIRL (NAME) values('Sasha')");
    q.setGeneratedColumns("ID, TIME");
    q.executeUpdate();
    ResultSet rs = q.getGeneratedColumns();
    assertEquals(1, q.getOpenResultSetCount());
    try {
        while (rs.next()) {
            int id = rs.getInt(1);
            assertEquals(6, id);
            try {
                rs.getInt(2);
                fail();
            } catch (SQLException sex) {
            // ignore
            }
        }
    } catch (SQLException sex) {
        fail(sex.getMessage());
    }
    q.closeResultSet(rs);
    q.close();
    assertEquals(0, q.getOpenResultSetCount());
    session.closeSession();
    session = new DbThreadSession(cp);
    DbOomManager.getInstance().registerEntity(Girl2.class, true);
    Girl2 g2 = new Girl2("Gwen");
    q = DbEntitySql.insert(g2).query();
    assertEquals("insert into GIRL (NAME) values (:girl2.name)", q.getQueryString());
    q.setGeneratedColumns("ID");
    q.executeUpdate();
    DbOomUtil.populateGeneratedKeys(g2, q);
    //g2.id = Integer.valueOf((int) q.getGeneratedKey());
    q.close();
    assertEquals(7, g2.id.intValue());
    g2 = DbEntitySql.findByColumn(Girl2.class, "name", "Gwen").query().find(Girl2.class);
    assertEquals("Gwen", g2.name);
    assertNull(g2.speciality);
    assertNotNull(g2.time);
    assertEquals(7, g2.id.intValue());
    session.closeSession();
    session = new DbThreadSession(cp);
    q = DbSqlBuilder.sql("select * from $T{Girl g} where $g.name like :what order by $g.id").query();
    q.setString("what", "%anna%");
    List<Girl2> girls = q.list(Girl2.class);
    assertEquals(2, girls.size());
    checkGirl4(girls.get(0));
    checkGirl5(girls.get(1));
    q.close();
    session.closeSession();
}
Also used : DbQuery(jodd.db.DbQuery) SQLException(java.sql.SQLException) Girl2(jodd.db.oom.tst.Girl2) DbSession(jodd.db.DbSession) IdName(jodd.db.oom.tst.IdName) DbThreadSession(jodd.db.DbThreadSession) ResultSet(java.sql.ResultSet) Girl(jodd.db.oom.tst.Girl) Test(org.junit.Test)

Example 4 with Girl

use of jodd.db.oom.tst.Girl in project jodd by oblac.

the class DbSqlBuilderTest method testCriteria2.

@Test
public void testCriteria2() {
    Girl girl = new Girl();
    girl.speciality = "piano";
    Girl girl_condition = new Girl();
    girl_condition.speciality = "swim";
    String tableRef = "ggg";
    DbSqlBuilder dsb = sql().$("update ").table(girl, tableRef).set(tableRef, girl).$("where ").match(tableRef, "conditionRef").use("conditionRef", girl_condition);
    String sql = dsb.generateQuery();
    Map<String, ParameterValue> params = dsb.getQueryParameters();
    assertEquals(2, params.size());
    assertEquals("piano", params.get("girl.speciality").getValue());
    assertEquals("swim", params.get("conditionRef.speciality").getValue());
    assertEquals("update GIRL ggg set SPECIALITY=:girl.speciality " + "where (ggg.SPECIALITY=:conditionRef.speciality)", sql);
}
Also used : ParameterValue(jodd.db.oom.sqlgen.ParameterValue) DbSqlBuilder(jodd.db.oom.sqlgen.DbSqlBuilder) BadGirl(jodd.db.oom.tst.BadGirl) Girl(jodd.db.oom.tst.Girl) Test(org.junit.Test)

Example 5 with Girl

use of jodd.db.oom.tst.Girl in project jodd by oblac.

the class DbHintTest method testHint.

@Test
public void testHint() {
    DbSession dbSession = new DbThreadSession(cp);
    // prepare data
    assertEquals(1, DbEntitySql.insert(new Girl(1, "Anna", "swim")).query().executeUpdate());
    assertEquals(1, DbEntitySql.insert(new Girl(2, "Sandra", "piano")).query().executeUpdate());
    assertEquals(1, DbEntitySql.insert(new Boy2(1, "John", 1)).query().executeUpdate());
    // select without hint
    DbOomQuery dbOomQuery = new DbOomQuery(q1);
    Object[] result = dbOomQuery.find(Boy2.class, Girl.class);
    Boy2 boy2 = (Boy2) result[0];
    assertEquals(1, boy2.id);
    assertEquals("John", boy2.name);
    assertEquals(1, boy2.girlId);
    assertNull(boy2.girl);
    Girl girl = (Girl) result[1];
    assertEquals(1, girl.id);
    // select with t-sql hint
    dbOomQuery = new DbOomQuery(q2);
    boy2 = dbOomQuery.find(Boy2.class, Girl.class);
    assertEquals(1, boy2.id);
    assertEquals("John", boy2.name);
    assertEquals(1, boy2.girlId);
    assertNotNull(boy2.girl);
    assertEquals(1, boy2.girl.id);
    assertEquals(0, boy2.totalGirls);
    // select with external hints
    dbOomQuery = new DbOomQuery(q3);
    dbOomQuery.withHints("boy", "boy.girlAlt", "boy.totalGirls");
    boy2 = dbOomQuery.find(Boy2.class, Girl.class, Integer.class);
    assertEquals(1, boy2.id);
    assertEquals("John", boy2.name);
    assertEquals(1, boy2.girlId);
    assertNotNull(boy2.girlAlt);
    assertEquals(1, boy2.girlAlt.id);
    assertEquals(2, boy2.totalGirls);
    // same select with t-sql hints
    dbOomQuery = new DbOomQuery(q4);
    boy2 = dbOomQuery.find(Boy2.class, Girl.class);
    assertEquals(1, boy2.id);
    assertEquals("John", boy2.name);
    assertEquals(1, boy2.girlId);
    assertNotNull(boy2.girlAlt);
    assertEquals(1, boy2.girlAlt.id);
    assertEquals(0, boy2.totalGirls);
    // same select with t-sql hints
    dbOomQuery = new DbOomQuery(q5);
    boy2 = dbOomQuery.find(Boy2.class, Girl.class, Integer.class);
    assertEquals(1, boy2.id);
    assertEquals("John", boy2.name);
    assertEquals(1, boy2.girlId);
    assertNotNull(boy2.girlAlt);
    assertEquals(1, boy2.girlAlt.id);
    assertEquals(2, boy2.totalGirls);
    // same select with t-sql hints
    dbOomQuery = new DbOomQuery(q6);
    boy2 = dbOomQuery.find(Boy2.class, Girl.class);
    assertEquals(1, boy2.id);
    assertEquals("John", boy2.name);
    assertEquals(1, boy2.girlId);
    assertNotNull(boy2.girlAlt);
    assertEquals(1, boy2.girlAlt.id);
    assertNotNull(boy2.girlAlt.name);
    assertNull(boy2.girlAlt.speciality);
    dbSession.closeSession();
}
Also used : DbSession(jodd.db.DbSession) DbThreadSession(jodd.db.DbThreadSession) Boy2(jodd.db.oom.tst.Boy2) Girl(jodd.db.oom.tst.Girl) Test(org.junit.Test)

Aggregations

Girl (jodd.db.oom.tst.Girl)12 Test (org.junit.Test)12 BadGirl (jodd.db.oom.tst.BadGirl)8 DbSession (jodd.db.DbSession)3 DbThreadSession (jodd.db.DbThreadSession)3 DbSqlBuilder (jodd.db.oom.sqlgen.DbSqlBuilder)2 BadBoy (jodd.db.oom.tst.BadBoy)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 DbQuery (jodd.db.DbQuery)1 ParameterValue (jodd.db.oom.sqlgen.ParameterValue)1 Boy2 (jodd.db.oom.tst.Boy2)1 Girl2 (jodd.db.oom.tst.Girl2)1 IdName (jodd.db.oom.tst.IdName)1