Search in sources :

Example 1 with SqlCallback

use of org.nutz.dao.sql.SqlCallback in project ansj_seg by NLPchina.

the class Jdbc2Stream method toStream.

@Override
public InputStream toStream(String path) {
    path = path.substring(7);
    String[] split = path.split("\\|");
    String jdbc = split[0];
    String username = split[1];
    String password = split[2];
    String sqlStr = split[3];
    String logStr = jdbc + "|" + username + "|********|" + sqlStr;
    SimpleDataSource ds = null;
    try {
        ds = new SimpleDataSource();
        ds.setJdbcUrl(jdbc);
        ds.setUsername(username);
        ds.setPassword(password);
        Dao dao = new NutDao(ds);
        Sql sql = Sqls.create(sqlStr);
        Sql execute = dao.execute(sql.setCallback(new SqlCallback() {

            @Override
            public byte[] invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                ByteArrayOutputStream baos = new ByteArrayOutputStream(100 * 1024);
                while (rs.next()) {
                    try {
                        int count = rs.getMetaData().getColumnCount();
                        for (int i = 1; i < count; i++) {
                            baos.write(String.valueOf(rs.getObject(i)).getBytes());
                            baos.write(TAB);
                        }
                        baos.write(String.valueOf(rs.getObject(count)).getBytes());
                        baos.write(LINE);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return baos.toByteArray();
            }
        }));
        return new ByteArrayInputStream((byte[]) execute.getResult());
    } catch (Exception e) {
        throw new LibraryException("err to load by jdbc " + logStr);
    } finally {
        if (ds != null) {
            ds.close();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) LibraryException(org.ansj.exception.LibraryException) Sql(org.nutz.dao.sql.Sql) Dao(org.nutz.dao.Dao) NutDao(org.nutz.dao.impl.NutDao) SqlCallback(org.nutz.dao.sql.SqlCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) ResultSet(java.sql.ResultSet) LibraryException(org.ansj.exception.LibraryException)

Example 2 with SqlCallback

use of org.nutz.dao.sql.SqlCallback in project nutz by nutzam.

the class SqlTemplate method queryForList.

/**
     * 执行一个SQL查询操作,结果为一组对象。
     * 
     * @param sql
     *            包含变量占位符的SQL
     * @param vars
     *            变量map,无参数时,可为null
     * @param params
     *            参数map,无参数时,可为null
     * @param classOfT
     *            对象类型
     * 
     * @return 对象列表,无查询结果时返回长度为0的List对象
     */
public <T> List<T> queryForList(String sql, Map<String, Object> vars, Map<String, Object> params, final Class<T> classOfT) {
    Sql sqlObj = createSqlObj(sql, params);
    sqlObj.setCallback(new SqlCallback() {

        public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
            List<T> list = new ArrayList<T>();
            while (rs.next()) {
                T result = Castors.me().castTo(rs.getObject(1), classOfT);
                list.add(result);
            }
            return list;
        }
    });
    execute(sqlObj, vars, params);
    return sqlObj.getList(classOfT);
}
Also used : SqlCallback(org.nutz.dao.sql.SqlCallback) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Sql(org.nutz.dao.sql.Sql)

Example 3 with SqlCallback

use of org.nutz.dao.sql.SqlCallback in project nutz by nutzam.

the class SqlTemplate method queryForObject.

/**
     * 执行一个SQL查询操作,结果为给定对象类型的对象,适用于明确SQL查询结果的类型。
     * 
     * @param sql
     *            包含变量占位符的SQL
     * @param vars
     *            变量map,无参数时,可为null
     * @param params
     *            参数map,无参数时,可为null
     * @param classOfT
     *            对象类型,SQL查询结果所对应的类型,如Date.class等
     * 
     * @return 对象,无查询结果时返回null
     */
public <T> T queryForObject(String sql, Map<String, Object> vars, Map<String, Object> params, Class<T> classOfT) {
    Sql sqlObj = createSqlObj(sql, params);
    sqlObj.setCallback(new SqlCallback() {

        public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
            if (null != rs && rs.next())
                return rs.getObject(1);
            return null;
        }
    });
    execute(sqlObj, vars, params);
    return sqlObj.getObject(classOfT);
}
Also used : SqlCallback(org.nutz.dao.sql.SqlCallback) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Sql(org.nutz.dao.sql.Sql)

Example 4 with SqlCallback

use of org.nutz.dao.sql.SqlCallback in project nutz by nutzam.

the class CreateDropTableTest method createTableTimestampFieldDefaultNullInMySQL.

@Test
public void createTableTimestampFieldDefaultNullInMySQL() throws Exception {
    boolean isMySql = dao.meta().isMySql();
    // 这个仅仅测试MySQL数据库
    if (isMySql) {
        dao.create(TableWithTimestampInMySql.class, true);
        Sql descTable = Sqls.create(DESC_TABLE_SQL);
        descTable.params().set("tableName", "t_ts");
        descTable.setCallback(new SqlCallback() {

            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                boolean ts1 = false;
                boolean ts2 = false;
                boolean ts3 = false;
                boolean ts4 = false;
                while (rs.next()) {
                    String columnName = rs.getString(1);
                    String isNullable = rs.getString(2);
                    String defaultValue = rs.getString(3);
                    String extra = rs.getString(4);
                    if ("ts1".equals(columnName)) {
                        if ((null == defaultValue || "NULL".equalsIgnoreCase(defaultValue)) && "YES".equalsIgnoreCase(isNullable)) {
                            ts1 = true;
                        }
                    }
                    if ("ts2".equals(columnName)) {
                        if (null != defaultValue && "0000-00-00 00:00:00".equals(defaultValue) && "YES".equalsIgnoreCase(isNullable)) {
                            ts2 = true;
                        }
                    }
                    if ("ts3".equals(columnName)) {
                        if (null != defaultValue && "0000-00-00 00:00:00".equals(defaultValue) && "NO".equalsIgnoreCase(isNullable)) {
                            ts3 = true;
                        }
                    }
                    if ("ts4".equals(columnName)) {
                        if (null != defaultValue && "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue) && "NO".equalsIgnoreCase(isNullable) && "ON UPDATE CURRENT_TIMESTAMP".equalsIgnoreCase(extra)) {
                            ts4 = true;
                        }
                    }
                }
                return ts1 && ts2 && ts3 && ts4;
            }
        });
        dao.execute(descTable);
        boolean isTrue = descTable.getObject(Boolean.class);
        Assert.assertTrue(isTrue);
    }
}
Also used : SqlCallback(org.nutz.dao.sql.SqlCallback) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 5 with SqlCallback

use of org.nutz.dao.sql.SqlCallback in project nutz by nutzam.

the class CustomizedSqlsTest method test_query_without_entity.

@Test
public void test_query_without_entity() {
    pojos.initPet();
    dao.insert(Pet.create(4));
    Sql sql = Sqls.create("SELECT * FROM t_pet $condition");
    sql.setCondition(Cnd.where("name", "like", "pet_%").asc("name"));
    sql.setCallback(new SqlCallback() {

        public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
            List<Pet> pets = new ArrayList<Pet>(4);
            while (rs.next()) pets.add(dao.getObject(Pet.class, rs, null));
            return pets;
        }
    });
    dao.execute(sql);
    List<Pet> pets = sql.getList(Pet.class);
    assertEquals(4, pets.size());
    assertEquals("pet_00", pets.get(0).getName());
    assertEquals("pet_01", pets.get(1).getName());
    assertEquals("pet_02", pets.get(2).getName());
    assertEquals("pet_03", pets.get(3).getName());
}
Also used : SqlCallback(org.nutz.dao.sql.SqlCallback) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Pet(org.nutz.dao.test.meta.Pet) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 Sql (org.nutz.dao.sql.Sql)5 SqlCallback (org.nutz.dao.sql.SqlCallback)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 LibraryException (org.ansj.exception.LibraryException)1 Dao (org.nutz.dao.Dao)1 NutDao (org.nutz.dao.impl.NutDao)1 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)1 NutSql (org.nutz.dao.impl.sql.NutSql)1 Pet (org.nutz.dao.test.meta.Pet)1