Search in sources :

Example 1 with DBStyle

use of org.beetl.sql.core.db.DBStyle 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)

Aggregations

SqlBoxContext (com.github.drinkjava2.jsqlbox.SqlBoxContext)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ClasspathLoader (org.beetl.sql.core.ClasspathLoader)1 ConnectionSource (org.beetl.sql.core.ConnectionSource)1 SQLLoader (org.beetl.sql.core.SQLLoader)1 SQLManager (org.beetl.sql.core.SQLManager)1 UnderlinedNameConversion (org.beetl.sql.core.UnderlinedNameConversion)1 DBStyle (org.beetl.sql.core.db.DBStyle)1 H2Style (org.beetl.sql.core.db.H2Style)1 Test (org.junit.Test)1