Search in sources :

Example 26 with SqlBoxContext

use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.

the class UsuageAndSpeedTest method sqlMapperSqlAnnotaion.

@Test
public void sqlMapperSqlAnnotaion() {
    SqlBoxContext ctx = new SqlBoxContext(dataSource);
    // use global default context
    SqlBoxContext.setGlobalSqlBoxContext(ctx);
    UserMapper user = new UserMapper();
    for (int i = 0; i < REPEAT_TIMES; i++) {
        user.insertOneUser("Sam", "Canada");
        user.updateAllUser("Tom", "China");
        List<Map<String, Object>> users = user.selectUsers("Tom", "China");
        Assert.assertEquals(1, users.size());
        user.deleteUsers("Tom", "China");
        Assert.assertEquals(0, user.ctx().nQueryForLongValue("select count(*) from users"));
    }
}
Also used : SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 27 with SqlBoxContext

use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.

the class BeetlSqlTemplateDemo method doTest.

@Test
public void doTest() {
    HikariDataSource ds = new HikariDataSource();
    // H2 Memory database
    ds.setDriverClassName("org.h2.Driver");
    ds.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
    ds.setUsername("sa");
    ds.setPassword("");
    ConnectionSource source = ConnectionSourceHelper.getSingle(ds);
    DBStyle dbstyle = new H2Style();
    SQLLoader loader = new ClasspathLoader("/sql");
    UnderlinedNameConversion nc = new UnderlinedNameConversion();
    SQLManager sqlManager = new SQLManager(dbstyle, loader, source, nc, new Interceptor[] {});
    // Done BeetlSQL engine
    SqlBoxContext.setGlobalTemplateEngine(new BeetlSqlTempalte(sqlManager));
    // Log output
    SqlBoxContext.setGlobalAllowShowSql(true);
    SqlBoxContext ctx = new SqlBoxContext(ds);
    SqlBoxContext.setGlobalSqlBoxContext(ctx);
    String[] ddlArray = ctx.toDropAndCreateDDL(User.class);
    for (String ddl : ddlArray) ctx.quiteExecute(ddl);
    for (int i = 1; i <= 100; i++) {
        User u = new User();
        u.setName("Foo" + i);
        u.setAge(i);
        u.insert();
    }
    Assert.assertEquals(100, ctx.nQueryForLongValue("select count(*) from users"));
    Map<String, Object> params = new HashMap<>();
    params.put("age", 50);
    List<Map<String, Object>> result = ctx.tQueryForMapList("user.select", params);
    Assert.assertEquals(50, result.size());
    ds.close();
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLLoader(org.beetl.sql.core.SQLLoader) HashMap(java.util.HashMap) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) SQLManager(org.beetl.sql.core.SQLManager) ClasspathLoader(org.beetl.sql.core.ClasspathLoader) ConnectionSource(org.beetl.sql.core.ConnectionSource) UnderlinedNameConversion(org.beetl.sql.core.UnderlinedNameConversion) DBStyle(org.beetl.sql.core.db.DBStyle) H2Style(org.beetl.sql.core.db.H2Style) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 28 with SqlBoxContext

use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.

the class Initializer method onStartup.

public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebAppConfig.class);
    servletContext.addListener(new ContextLoaderListener(ctx));
    ctx.setServletContext(servletContext);
    Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
    // force refresh
    ctx.refresh();
    // SqlBoxContext.setGlobalAllowShowSql(true);
    SqlBoxContext sqlCtx = ctx.getBean(SqlBoxContext.class);
    SqlBoxContext.setGlobalSqlBoxContext(sqlCtx);
    String[] ddls = sqlCtx.toDropAndCreateDDL(Team.class);
    for (String ddl : ddls) sqlCtx.quiteExecute(ddl);
    for (int i = 0; i < 5; i++) new Team().put("name", "Team" + i, "rating", i * 10).insert();
    System.out.println("========== com.jsqlboxdemo.init.Initializer initialized=====");
}
Also used : Dynamic(javax.servlet.ServletRegistration.Dynamic) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Team(com.demo.model.Team) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext)

Example 29 with SqlBoxContext

use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.

the class EntityNetFactory method createEntityNet.

/**
 * Create a EntityNet instance, load data from database buy given loadKeyOnly
 * and configObjects parameters
 *
 * @param ctx
 *            A SqlBoxContext instance
 * @param loadKeyOnly
 *            If true will only load PKey and FKeys field, otherwise load all
 *            columns
 * @param configObjects
 *            netConfigs array, can be entity class, entity, SqlBox or
 *            TableModel instance
 * @return The EntityNet
 */
public static EntityNet createEntityNet(SqlBoxContext ctx, boolean loadKeyOnly, Object... configObjects) {
    if (configObjects == null || configObjects.length == 0)
        throw new EntityNetException("LoadNet() does not support empty netConfigs parameter");
    TableModel[] models = EntityNetUtils.objectConfigsToModels(ctx, configObjects);
    EntityNet net = new EntityNet();
    String starOrSharp = loadKeyOnly ? ".##" : ".**";
    for (TableModel t : models) {
        List<Map<String, Object>> mapList = null;
        String alias = t.getAlias();
        if (StrUtils.isEmpty(alias))
            alias = t.getTableName();
        try {
            mapList = ctx.nQuery(new EntitySqlMapListHandler(t), "select " + alias + starOrSharp + " from " + t.getTableName() + " as " + alias);
        } finally {
            EntityNetUtils.removeBindedTableModel(mapList);
        }
        net.addMapList(mapList, t);
    }
    return net;
}
Also used : Map(java.util.Map) TableModel(com.github.drinkjava2.jdialects.model.TableModel) EntitySqlMapListHandler(com.github.drinkjava2.jsqlbox.handler.EntitySqlMapListHandler)

Example 30 with SqlBoxContext

use of com.github.drinkjava2.jsqlbox.SqlBoxContext in project jSqlBox by drinkjava2.

the class Initializer method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent context) {
    // Initialize BeanBox
    BeanBox.regAopAroundAnnotation(TX.class, TxBox.class);
    BeanBox.regAopAroundAnnotation(Transaction.class, TxBox.class);
    // Initialize Global SqlBoxContext
    SqlBoxContextConfig config = new SqlBoxContextConfig();
    config.setConnectionManager(TinyTxConnectionManager.instance());
    SqlBoxContext ctx = new SqlBoxContext((DataSource) BeanBox.getBean(DataSourceBox.class), config);
    SqlBoxContext.setGlobalSqlBoxContext(ctx);
    // Initialize database
    String[] ddls = ctx.toDropAndCreateDDL(Team.class);
    for (String ddl : ddls) ctx.quiteExecute(ddl);
    for (int i = 0; i < 5; i++) new Team().put("name", "Team" + i, "rating", i * 10).insert();
    Assert.assertEquals(5, ctx.nQueryForLongValue("select count(*) from teams"));
    System.out.println("========== com.jsqlboxdemo.init.Initializer initialized=====");
}
Also used : SqlBoxContextConfig(com.github.drinkjava2.jsqlbox.SqlBoxContextConfig) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Team(model.Team)

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