Search in sources :

Example 1 with NutDao

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

the class CustomizedSqlsTest method test_dynamic_insert.

@Test
public void test_dynamic_insert() {
    pojos.init();
    ((NutDao) dao).setSqlManager(new FileSqlManager("org/nutz/dao/test/sqls/exec.sqls"));
    int platoonId = 23;
    try {
        pojos.initPlatoon(platoonId);
        Sql sql = dao.sqls().create("tank.insert");
        sql.vars().set("id", platoonId);
        sql.params().set("code", "T1").set("weight", 12);
        dao.execute(sql);
        sql = dao.sqls().create("tank.insert");
        sql.vars().set("id", platoonId);
        sql.params().set("code", "T2").set("weight", 13);
        dao.execute(sql);
        sql = dao.sqls().create("tank.insert");
        sql.vars().set("id", platoonId);
        sql.params().set("code", "T3").set("weight", 14);
        dao.execute(sql);
        sql = dao.sqls().create("tank.insert");
        sql.vars().set("id", platoonId);
        sql.params().set("code", "T4").set("weight", 15);
        dao.execute(sql);
        TableName.run(platoonId, new Atom() {

            public void run() {
                assertEquals(4, dao.count(Tank.class));
            }
        });
    } catch (SqlNotFoundException e) {
    } finally {
        pojos.dropPlatoon(platoonId);
    }
}
Also used : SqlNotFoundException(org.nutz.dao.SqlNotFoundException) NutDao(org.nutz.dao.impl.NutDao) Atom(org.nutz.trans.Atom) FileSqlManager(org.nutz.dao.impl.FileSqlManager) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 2 with NutDao

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

the class DaoUp method setDataSource.

/**
 * 主动设置数据源(连接池)
 * @param dataSource 数据源(连接池)
 */
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    setDao(new NutDao(dataSource));
}
Also used : NutDao(org.nutz.dao.impl.NutDao)

Example 3 with NutDao

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

the class AnnotationIocLoaderTest method test_ioc_inject_by_setter.

@Test
public void test_ioc_inject_by_setter() throws ObjectLoadException {
    AnnotationIocLoader loader = new AnnotationIocLoader(getClass().getPackage().getName());
    Logs.get().error(loader.load(null, "issue1060"));
    NutIoc ioc = new NutIoc(loader);
    // 放个假的
    ioc.getIocContext().save("app", "dao", new ObjectProxy(new NutDao()));
    ioc.get(Issue1060.class);
    ioc.depose();
}
Also used : NutIoc(org.nutz.ioc.impl.NutIoc) NutDao(org.nutz.dao.impl.NutDao) ObjectProxy(org.nutz.ioc.ObjectProxy) Test(org.junit.Test)

Example 4 with NutDao

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

the class PsTest method main.

/**
 * 测试新的Dao实现在postgresql下的表现
 */
public static void main(String[] args) throws Throwable {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setUsername("postgres");
    ds.setPassword("123456");
    ds.setUrl("jdbc:postgresql:test");
    ds.setDefaultAutoCommit(false);
    final NutDao dao = new NutDao(ds);
    TableName.run(1, new Atom() {

        public void run() {
            Trans.exec(new Atom() {

                public void run() {
                    dao.create(Country.class, true);
                    System.out.println("------------???");
                    dao.insert(Country.make("A"));
                    try {
                        System.out.println("---------------------");
                        dao.insert(Country.make("A"));
                        System.out.println("+++++++++++++++++++++++++++++++++++");
                    } catch (DaoException e) {
                    }
                    try {
                        dao.insert(Country.make("C"));
                    } catch (Throwable e) {
                        System.out.println("天啊,还是报错!!");
                    }
                }
            });
        }
    });
    Connection conn = ds.getConnection();
    conn.prepareStatement("insert into dao_country(name,detail) values('ABC','CC')").execute();
    try {
        conn.prepareStatement("insert into dao_country(name,detail) values('ABC','CC')").execute();
    } catch (Exception e) {
    }
    conn.prepareStatement("insert into dao_country(name,detail) values('CC','CC')").execute();
    System.out.println("竟然能通过??!!");
    conn.commit();
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Connection(java.sql.Connection) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) Atom(org.nutz.trans.Atom)

Example 5 with NutDao

use of org.nutz.dao.impl.NutDao in project druid by alibaba.

the class TestRollBack method init.

// jdbcUrl = "jdbc:oracle:thin:@a.b.c.d:1521:ocnauto";
// user = "alibaba";
// password = "ccbuauto";
@BeforeClass
public static void init() throws PropertyVetoException, SQLException {
    c3p0 = new ComboPooledDataSource();
    // c3p0.setDriverClass("oracle.jdbc.driver.OracleDriver");
    c3p0.setDriverClass(driver);
    c3p0.setJdbcUrl(url);
    c3p0.setUser(user);
    c3p0.setPassword(password);
    druid = new DruidDataSource();
    druid.setUrl(url);
    druid.setUsername(user);
    druid.setPassword(password);
    druid.setFilters("stat,trace,encoding");
    druid.setDefaultAutoCommit(false);
    dao_c3p0 = new NutDao(c3p0);
    dao_druid = new NutDao(druid);
    if (!dao_c3p0.exists("msg")) {
        // 字段长度5
        dao_c3p0.execute(Sqls.create("create table msg(message varchar(5))"));
    }
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) NutDao(org.nutz.dao.impl.NutDao) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) BeforeClass(org.junit.BeforeClass)

Aggregations

NutDao (org.nutz.dao.impl.NutDao)15 Test (org.junit.Test)8 Connection (java.sql.Connection)4 ArrayList (java.util.ArrayList)4 DataSource (javax.sql.DataSource)4 Atom (org.nutz.trans.Atom)4 SQLException (java.sql.SQLException)3 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)3 Pet (org.nutz.dao.test.meta.Pet)3 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 DaoException (org.nutz.dao.DaoException)2 JdbcExpert (org.nutz.dao.jdbc.JdbcExpert)2 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)2 AbcPet (org.nutz.dao.test.meta.nutzcn.AbcPet)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 PreparedStatement (java.sql.PreparedStatement)1