Search in sources :

Example 16 with SqlBoxContext

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

the class UsuageAndSpeedTest method activeRecordDefaultContext.

@Test
public void activeRecordDefaultContext() {
    SqlBoxContext ctx = new SqlBoxContext(dataSource);
    // use global default context
    SqlBoxContext.setGlobalSqlBoxContext(ctx);
    UserAR user = new UserAR();
    for (int i = 0; i < REPEAT_TIMES; i++) {
        user.setName("Sam");
        user.setAddress("Canada");
        user.insert();
        user.setAddress("China");
        user.update();
        UserAR user2 = ctx.load(UserAR.class, "Sam");
        user2.delete();
    }
}
Also used : SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Example 17 with SqlBoxContext

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

the class UsuageAndSpeedTest method init.

@Before
public void init() {
    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 ctx = new SqlBoxContext(dataSource);
    SqlBoxContext.setGlobalSqlBoxContext(null);
    for (String ddl : ctx.getDialect().toDropAndCreateDDL(UserAR.class)) try {
        ctx.nExecute(ddl);
    } catch (Exception e) {
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) SQLException(java.sql.SQLException) Before(org.junit.Before)

Example 18 with SqlBoxContext

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

the class UsuageAndSpeedTest method dataMapperStyle.

@Test
public void dataMapperStyle() {
    SqlBoxContext ctx = new SqlBoxContext(dataSource);
    for (int i = 0; i < REPEAT_TIMES; i++) {
        UserPOJO user = new UserPOJO();
        user.setName("Sam");
        user.setAddress("Canada");
        ctx.insert(user);
        user.setAddress("China");
        ctx.update(user);
        UserPOJO sam2 = ctx.load(UserPOJO.class, "Sam");
        ctx.delete(sam2);
    }
}
Also used : SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Example 19 with SqlBoxContext

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

the class UsuageAndSpeedTest method dbUtilsNoConnMethod.

@Test
public void dbUtilsNoConnMethod() {
    SqlBoxContext ctx = new SqlBoxContext(dataSource);
    for (int i = 0; i < REPEAT_TIMES; i++) {
        try {
            ctx.execute("insert into users (name,address) values(?,?)", "Sam", "Canada");
            ctx.execute("update users set name=?, address=?", "Tom", "China");
            Assert.assertEquals(1L, ctx.queryForObject("select count(*) from users where name=? and address=?", "Tom", "China"));
            ctx.execute("delete from users where name=? or address=?", "Tom", "China");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Also used : SQLException(java.sql.SQLException) SqlBoxContext(com.github.drinkjava2.jsqlbox.SqlBoxContext) Test(org.junit.Test)

Example 20 with SqlBoxContext

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

the class UsuageAndSpeedTest method tXxxStyle.

@Test
public void tXxxStyle() {
    SqlBoxContext ctx2 = new SqlBoxContext(dataSource);
    Map<String, Object> params = new HashMap<String, Object>();
    for (int i = 0; i < REPEAT_TIMES; i++) {
        UserAR sam = new UserAR("Sam", "Canada");
        UserAR tom = new UserAR("Tom", "China");
        params.put("user", sam);
        ctx2.tExecute("insert into users (name, address) values(#{user.name},:user.address)", params);
        params.put("user", tom);
        ctx2.tExecute("update users set name=#{user.name}, address=:user.address", params);
        params.clear();
        params.put("name", "Tom");
        params.put("addr", "China");
        Assert.assertEquals(1L, ctx2.tQueryForObject("select count(*) from users where name=#{name} and address=:addr", params));
        params.put("u", tom);
        ctx2.tExecute("delete from users where name=:u.name or address=#{u.address}", params);
    }
}
Also used : HashMap(java.util.HashMap) 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