use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class LoggerTest method doSqlBoxLoggerTest.
@Test
public void doSqlBoxLoggerTest() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
dataSource.setDriverClassName("org.h2.Driver");
// change to your user & password
dataSource.setUsername("sa");
dataSource.setPassword("");
SqlBoxContext.setGlobalAllowShowSql(true);
SqlBoxContext ctx = new SqlBoxContext(dataSource);
for (String ddl : ctx.getDialect().toDropAndCreateDDL(LoggerTest.class)) ctx.quiteExecute(ddl);
LoggerTest t = new LoggerTest();
t.setName("Tom");
ctx.insert(t);
SqlBoxContext.setGlobalAllowShowSql(false);
SqlBoxContext.getGlobalLogger().info("Logger test message3 output ok");
SqlBoxContext.getGlobalLogger().info("Logger test message4 output ok");
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class SqlBoxUtils method createSqlBox.
/**
* Create a SqlBox by given entity or entityClass
*/
public static SqlBox createSqlBox(SqlBoxContext ctx, Class<?> entityOrBoxClass) {
Class<?> boxClass = null;
if (entityOrBoxClass == null)
throw new SqlBoxException("Bean Or SqlBox class can not be null");
if (SqlBox.class.isAssignableFrom(entityOrBoxClass))
boxClass = entityOrBoxClass;
if (boxClass == null)
boxClass = ClassCacheUtils.checkClassExist(entityOrBoxClass.getName() + SqlBoxContext.getGlobalsqlboxsuffix());
if (boxClass == null)
boxClass = ClassCacheUtils.checkClassExist(entityOrBoxClass.getName() + "$" + entityOrBoxClass.getSimpleName() + SqlBoxContext.getGlobalsqlboxsuffix());
if (boxClass != null && !SqlBox.class.isAssignableFrom((Class<?>) boxClass))
boxClass = null;
SqlBox box = null;
if (boxClass == null) {
box = new SqlBox();
box.setTableModel(TableModelUtils.entity2Model(entityOrBoxClass));
box.setEntityClass(entityOrBoxClass);
} else {
try {
box = (SqlBox) boxClass.newInstance();
TableModel model = box.getTableModel();
if (model == null) {
model = TableModelUtils.entity2Model(entityOrBoxClass);
box.setTableModel(model);
}
Method configMethod = null;
try {
// NOSONAR
configMethod = boxClass.getMethod("config", TableModel.class);
} catch (Exception e) {
// NOSONAR
}
if (configMethod != null)
configMethod.invoke(box, model);
} catch (Exception e) {
throw new SqlBoxException("Can not create SqlBox instance: " + entityOrBoxClass, e);
}
}
if (box.getContext() == null)
box.setContext(ctx);
return box;
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class EntityListHandler method handleResult.
@Override
public Object handleResult(QueryRunner query, Object result) {
List<Map<String, Object>> list = (List) sqlMapListHandler.handleResult(query, result);
EntityNet net = ((SqlBoxContext) query).netCreate(list);
return net.getAllEntityList(targetClass);
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext 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));
}
}
use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.
the class UsuageAndSpeedTest method xXxxStyle.
@Test
public void xXxxStyle() {
SqlBoxContext ctx = new SqlBoxContext(dataSource);
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));
}
}
Aggregations