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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations