Search in sources :

Example 11 with DbSession

use of jodd.db.DbSession in project jodd by oblac.

the class DbHint2Test method tearDown.

@Override
public void tearDown() throws Exception {
    DbSession session = new DbSession(cp);
    executeUpdate(session, "drop table GIRL if exists");
    executeUpdate(session, "drop table BOY if exists");
    executeUpdate(session, "drop table ROOM if exists");
    session.closeSession();
    super.tearDown();
}
Also used : DbSession(jodd.db.DbSession)

Example 12 with DbSession

use of jodd.db.DbSession 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)

Example 13 with DbSession

use of jodd.db.DbSession in project jodd by oblac.

the class LiveDatabaseTest method workoutEntity.

// ---------------------------------------------------------------- workout
protected void workoutEntity() {
    DbSession session = new DbSession();
    Tester tester = new Tester();
    tester.setName("one");
    tester.setValue(Integer.valueOf(7));
    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.insert(tester));
    dbOomQuery.setGeneratedKey();
    dbOomQuery.executeUpdate();
    assertDb(session, "{1,one,7}");
    long key = dbOomQuery.getGeneratedKey();
    tester.setId(Long.valueOf(key));
    dbOomQuery.close();
    assertEquals(1, tester.getId().longValue());
    tester.setName("seven");
    DbOomQuery.query(session, DbEntitySql.updateAll(tester)).executeUpdate();
    assertDb(session, "{1,seven,7}");
    tester.setName("SEVEN");
    DbOomQuery.query(session, DbEntitySql.update(tester)).executeUpdate();
    assertDb(session, "{1,SEVEN,7}");
    tester.setName("seven");
    DbOomQuery.query(session, DbEntitySql.updateColumn(tester, "name")).executeUpdate();
    assertDb(session, "{1,seven,7}");
    tester = new Tester();
    tester.setId(Long.valueOf(2));
    tester.setName("two");
    tester.setValue(Integer.valueOf(2));
    DbOomQuery.query(session, DbEntitySql.insert(tester)).executeUpdate();
    assertDb(session, "{1,seven,7}{2,two,2}");
    long count = DbOomQuery.query(session, DbEntitySql.count(Tester.class)).executeCount();
    assertEquals(2, count);
    tester = DbOomQuery.query(session, DbEntitySql.findById(Tester.class, 2)).find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());
    tester = DbOomQuery.query(session, DbEntitySql.findById(Tester.class, 2).aliasColumnsAs(ColumnAliasType.COLUMN_CODE)).find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());
    tester = DbOomQuery.query(session, DbEntitySql.findById(Tester.class, 2).aliasColumnsAs(ColumnAliasType.TABLE_REFERENCE)).find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());
    tester = DbOomQuery.query(session, DbEntitySql.findById(Tester.class, 2).aliasColumnsAs(ColumnAliasType.TABLE_NAME)).find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());
    tester = DbOomQuery.query(session, DbEntitySql.findById(Tester.class, 2).aliasColumnsAs(// fixes POSTGRESQL
    ColumnAliasType.COLUMN_CODE)).find();
    assertEquals("{2,two,2}", tester.toString());
    tester = new Tester();
    tester.setName("seven");
    tester = DbOomQuery.query(session, DbEntitySql.find(tester)).find(Tester.class);
    assertEquals("{1,seven,7}", tester.toString());
    DbOomQuery.query(session, DbEntitySql.findByColumn(Tester.class, "name", "seven")).find(Tester.class);
    assertEquals("{1,seven,7}", tester.toString());
    DbOomQuery.query(session, DbEntitySql.deleteById(Tester.class, 1)).executeUpdate();
    count = DbOomQuery.query(session, DbEntitySql.count(Tester.class)).executeCount();
    assertEquals(1, count);
    session.closeSession();
}
Also used : DbSession(jodd.db.DbSession) Tester(jodd.db.oom.tst.Tester)

Example 14 with DbSession

use of jodd.db.DbSession in project jodd by oblac.

the class LiveMapperDbTest method loadEntry.

protected void loadEntry(Tester2 tester2) {
    DbSession session = new DbSession();
    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.findById(Tester2.class, tester2.id));
    Tester2 tester21 = dbOomQuery.find(Tester2.class);
    assertNotNull(tester21);
    assertEquals(tester2.id, tester21.id);
    assertEquals(tester2.value, tester21.value);
    assertEquals(tester2.name, tester21.name);
    assertEquals(tester2.time, tester21.time);
    assertEquals(tester2.time2, tester21.time2);
    session.closeSession();
}
Also used : DbSession(jodd.db.DbSession)

Example 15 with DbSession

use of jodd.db.DbSession in project jodd by oblac.

the class DbHsqldbTestCase method setUp.

@Before
public void setUp() throws Exception {
    cp = new CoreConnectionPool();
    cp.setDriver("org.hsqldb.jdbcDriver");
    cp.setUrl("jdbc:hsqldb:mem:test");
    cp.setUser("sa");
    cp.setPassword("");
    cp.init();
    dbtxm = new DbJtxTransactionManager(cp);
    // initial data
    DbSession session = new DbSession(cp);
    executeUpdate(session, "drop table BOY if exists");
    executeUpdate(session, "drop table GIRL if exists");
    String sql = "create table GIRL (" + "ID			integer		not null," + "NAME		varchar(20)	not null," + "SPECIALITY	varchar(20)	null," + "primary key (ID)" + ')';
    executeUpdate(session, sql);
    sql = "create table BOY (" + "ID			integer	not null," + "GIRL_ID	integer	null," + "NAME	varchar(20)	null," + "primary key (ID)," + "FOREIGN KEY (GIRL_ID) REFERENCES GIRL (ID)" + ')';
    executeUpdate(session, sql);
    session.closeSession();
}
Also used : DbSession(jodd.db.DbSession) CoreConnectionPool(jodd.db.pool.CoreConnectionPool) DbJtxTransactionManager(jodd.db.jtx.DbJtxTransactionManager) Before(org.junit.Before)

Aggregations

DbSession (jodd.db.DbSession)16 Test (org.junit.Test)10 DbThreadSession (jodd.db.DbThreadSession)7 DbQuery (jodd.db.DbQuery)3 Girl (jodd.db.oom.tst.Girl)3 ResultSet (java.sql.ResultSet)2 JDateTime (jodd.datetime.JDateTime)2 DbSqlBuilder (jodd.db.oom.sqlgen.DbSqlBuilder)2 BigDecimal (java.math.BigDecimal)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1 QueryMapper (jodd.db.QueryMapper)1 DbJtxTransactionManager (jodd.db.jtx.DbJtxTransactionManager)1 Boy2 (jodd.db.oom.tst.Boy2)1 Boy4 (jodd.db.oom.tst.Boy4)1 Enumerator (jodd.db.oom.tst.Enumerator)1 Girl2 (jodd.db.oom.tst.Girl2)1