Search in sources :

Example 11 with NutDao

use of org.nutz.dao.impl.NutDao in project nutz by nutzam.

the class SimpleDaoTest method test_fastinsert_speed.

// @Test
// public void test_map_blob() throws FileNotFoundException {
// if (dao.exists("t_test_map_blob")) {
// dao.drop("t_test_map_blob");
// Lang.quiteSleep(1000);
// }
// dao.execute(Sqls.create("create table t_test_map_blob(id
// VARCHAR(60),filecontent blob)"));
//
// NutMap map = new NutMap().setv(".table", "t_test_map_blob");
// map.put("id", R.UU32());
// map.put("filecontent", new
// FileInputStream("W:\\usb3.0_intel_1.0.10.255_w7.zip"));
//
// dao.insert(map);
//
// Record re = dao.fetch("t_test_map_blob", Cnd.NEW());
// assertNotNull(re);
// System.out.println(re.get("filecontent").getClass());
// System.out.println(new String((byte[])re.get("filecontent")));
//
//// assertEquals("你好", new String((byte[])re.get("filecontent")));
// }
//@Test
public void test_fastinsert_speed() {
    SimpleDataSource ds = new SimpleDataSource();
    ds.setJdbcUrl("jdbc:mysql://localhost/nutztest");
    ds.setUsername("root");
    ds.setPassword("root");
    dao = new NutDao(ds);
    // 删表重建
    dao.create(Pet.class, true);
    Lang.sleep(1000);
    Stopwatch sw = Stopwatch.begin();
    // 生成10*2000个对象
    List<List<Pet>> list = new ArrayList<List<Pet>>();
    for (int i = 0; i < 10; i++) {
        List<Pet> pets = new ArrayList<Pet>();
        for (int j = 0; j < 2000; j++) {
            Pet pet = Pet.create(R.UU32());
            pets.add(pet);
        }
        list.add(pets);
    }
    sw.stop();
    System.out.println("生成对象的耗时: " + sw);
    for (final List<Pet> tmp : list) {
        sw = Stopwatch.begin();
        Trans.exec(new Atom() {

            public void run() {
                dao.fastInsert(tmp);
            }
        });
        sw.stop();
        System.out.println("fastInsert插入2000个对象的耗时" + sw);
    }
    dao.create(Pet.class, false);
    for (int i = 0; i < 10; i++) {
        try {
            final int t = i;
            Connection conn = ds.getConnection();
            conn.setAutoCommit(false);
            sw = Stopwatch.begin();
            System.out.println(System.currentTimeMillis());
            PreparedStatement ps = conn.prepareStatement("INSERT INTO t_pet(name,alias) VALUES(?,?)");
            System.out.println(System.currentTimeMillis());
            for (int j = 0; j < 2000; j++) {
                ps.setString(1, "pet_" + t + "_" + j);
                ps.setString(2, "");
                //                    ps.setInt(3, 30);
                //                    ps.setInt(4, 0);
                //                    ps.setDate(5, null);
                //                    ps.setFloat(6, 0);
                ps.addBatch();
            }
            System.out.println(System.currentTimeMillis());
            ps.executeBatch();
            conn.commit();
            sw.stop();
            System.out.println(sw);
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) SQLException(java.sql.SQLException) Stopwatch(org.nutz.lang.Stopwatch) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Atom(org.nutz.trans.Atom) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) List(java.util.List) ArrayList(java.util.ArrayList) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet)

Example 12 with NutDao

use of org.nutz.dao.impl.NutDao in project nutz by nutzam.

the class SimpleDaoTest method test_get_sql.

@Test
public void test_get_sql() throws Throwable {
    NutDao dao = new NutDao(ioc.get(javax.sql.DataSource.class));
    final List<String> sqls = new ArrayList<String>();
    final Method m = NutStatement.class.getDeclaredMethod("toStatement", Object[][].class, String.class);
    m.setAccessible(true);
    dao.setExecutor(new DaoExecutor() {

        public void exec(Connection conn, DaoStatement st) {
            String psql = st.toPreparedStatement();
            sqls.add(psql);
            // 事实上根据参数矩阵(getParamMatrix)和Sql语句(toPreparedStatement)就能逐一替换生成
            try {
                String ssql = (String) m.invoke(st, st.getParamMatrix(), psql);
                sqls.add(ssql);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    dao.insert(Pet.create("hi"));
    for (String sql : sqls) {
        System.out.println(sql);
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) DaoStatement(org.nutz.dao.sql.DaoStatement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) DaoExecutor(org.nutz.dao.impl.DaoExecutor) Method(java.lang.reflect.Method) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) Test(org.junit.Test)

Example 13 with NutDao

use of org.nutz.dao.impl.NutDao in project nutz by nutzam.

the class SimpleDaoTest method test_migration.

@Test
public void test_migration() {
    dao.execute(Sqls.create("drop table t_pet"));
    Entity<Pet> en = dao.getEntity(Pet.class);
    NutDao dao = (NutDao) this.dao;
    JdbcExpert expert = dao.getJdbcExpert();
    MappingField mf = en.getField("age");
    String str = "create table t_pet (" + mf.getColumnName() + " " + expert.evalFieldType(mf) + "," + mf.getColumnName() + "_2" + " " + expert.evalFieldType(mf) + ")";
    dao.execute(Sqls.create(str));
    Daos.migration(dao, Pet.class, !dao.meta().isSQLite(), !dao.meta().isSQLite());
}
Also used : NutDao(org.nutz.dao.impl.NutDao) JdbcExpert(org.nutz.dao.jdbc.JdbcExpert) MappingField(org.nutz.dao.entity.MappingField) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 14 with NutDao

use of org.nutz.dao.impl.NutDao in project nutz by nutzam.

the class FastClassFactoryTest method test_fastclass_for_datasource.

@Test
public void test_fastclass_for_datasource() {
    FastClassFactory.get(Dao.class);
    FastClassFactory.get(DruidDataSource.class);
    FastClass fc = FastClassFactory.get(DaoSupport.class);
    fc = FastClassFactory.get(NutDao.class);
    fc.invoke(new NutDao(), "execute", new Class[] { Sql.class }, new Object[] { null });
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Test(org.junit.Test)

Aggregations

NutDao (org.nutz.dao.impl.NutDao)14 Test (org.junit.Test)7 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)4 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)4 Atom (org.nutz.trans.Atom)4 ArrayList (java.util.ArrayList)3 DataSource (javax.sql.DataSource)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 Dao (org.nutz.dao.Dao)2 DaoException (org.nutz.dao.DaoException)2 JdbcExpert (org.nutz.dao.jdbc.JdbcExpert)2 Sql (org.nutz.dao.sql.Sql)2 Pet (org.nutz.dao.test.meta.Pet)2 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1