Search in sources :

Example 1 with ClasspathLoader

use of org.beetl.sql.core.ClasspathLoader in project nutzboot by nutzam.

the class BeetlSqlStarter method creatSQLManager.

@IocBean(name = "beetlsqlManager")
public SQLManager creatSQLManager(@Inject("refer:beetlsqlDBStyle") DBStyle dbStyle, @Inject("beetlsqlConnectionSource") ConnectionSource ds) {
    // BeetlSql默认/sql/,但NutzBoot的约定是/sqls/,入乡随俗吧
    SQLLoader loader = new ClasspathLoader(conf.get(PROP_PATH, "/sqls/"));
    // TODO 支持更多种类的NameConversion
    NameConversion nameconv = "default".equals(conf.get(PROP_NAME_CONVERSION, "default")) ? new DefaultNameConversion() : new UnderlinedNameConversion();
    // 是否插入debug拦截器呢? 默认启用好了
    if (conf.getBoolean(PROP_DEBUG, true))
        return new SQLManager(dbStyle, loader, ds, nameconv, new Interceptor[] { new DebugInterceptor() });
    return new SQLManager(dbStyle, loader, ds, nameconv);
}
Also used : NameConversion(org.beetl.sql.core.NameConversion) UnderlinedNameConversion(org.beetl.sql.core.UnderlinedNameConversion) DefaultNameConversion(org.beetl.sql.core.DefaultNameConversion) SQLLoader(org.beetl.sql.core.SQLLoader) ClasspathLoader(org.beetl.sql.core.ClasspathLoader) DebugInterceptor(org.beetl.sql.ext.DebugInterceptor) UnderlinedNameConversion(org.beetl.sql.core.UnderlinedNameConversion) DefaultNameConversion(org.beetl.sql.core.DefaultNameConversion) Interceptor(org.beetl.sql.core.Interceptor) DebugInterceptor(org.beetl.sql.ext.DebugInterceptor) SQLManager(org.beetl.sql.core.SQLManager) IocBean(org.nutz.ioc.loader.annotation.IocBean)

Example 2 with ClasspathLoader

use of org.beetl.sql.core.ClasspathLoader 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

ClasspathLoader (org.beetl.sql.core.ClasspathLoader)2 SQLLoader (org.beetl.sql.core.SQLLoader)2 SQLManager (org.beetl.sql.core.SQLManager)2 UnderlinedNameConversion (org.beetl.sql.core.UnderlinedNameConversion)2 SqlBoxContext (com.github.drinkjava2.jsqlbox.SqlBoxContext)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConnectionSource (org.beetl.sql.core.ConnectionSource)1 DefaultNameConversion (org.beetl.sql.core.DefaultNameConversion)1 Interceptor (org.beetl.sql.core.Interceptor)1 NameConversion (org.beetl.sql.core.NameConversion)1 DBStyle (org.beetl.sql.core.db.DBStyle)1 H2Style (org.beetl.sql.core.db.H2Style)1 DebugInterceptor (org.beetl.sql.ext.DebugInterceptor)1 Test (org.junit.Test)1 IocBean (org.nutz.ioc.loader.annotation.IocBean)1