Search in sources :

Example 1 with SqlBoxContext

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");
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Example 2 with SqlBoxContext

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;
}
Also used : Method(java.lang.reflect.Method) TableModel(com.github.drinkjava2.jdialects.model.TableModel)

Example 3 with SqlBoxContext

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);
}
Also used : EntityNet(com.github.drinkjava2.jsqlbox.entitynet.EntityNet) List(java.util.List) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Map(java.util.Map)

Example 4 with SqlBoxContext

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));
    }
}
Also used : SqlBoxContextConfig(com.github.drinkjava2.jsqlbox.SqlBoxContextConfig) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Example 5 with SqlBoxContext

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));
    }
}
Also used : SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Aggregations

SqlBoxContext (com.github.drinkjava2.jsqlbox.SqlBoxContext)23 Test (org.junit.Test)18 TableModel (com.github.drinkjava2.jdialects.model.TableModel)7 Map (java.util.Map)6 HikariDataSource (com.zaxxer.hikari.HikariDataSource)5 Method (java.lang.reflect.Method)5 ColumnModel (com.github.drinkjava2.jdialects.model.ColumnModel)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 SqlBoxContextConfig (com.github.drinkjava2.jsqlbox.SqlBoxContextConfig)2 Before (org.junit.Before)2 AbstractUser (activerecordtext.AbstractUser)1 TextedUser (activerecordtext.TextedUser)1 Team (com.demo.model.Team)1 DataSourceBox (com.github.drinkjava2.config.DataSourceConfig.DataSourceBox)1 Type (com.github.drinkjava2.jdialects.Type)1 GenerationType (com.github.drinkjava2.jdialects.annotation.jpa.GenerationType)1 IdGenerator (com.github.drinkjava2.jdialects.id.IdGenerator)1 IdentityIdGenerator (com.github.drinkjava2.jdialects.id.IdentityIdGenerator)1