Search in sources :

Example 46 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project mybatis-3 by mybatis.

the class ResultSetLogger method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
    try {
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, params);
        }
        Object o = method.invoke(rs, params);
        if ("next".equals(method.getName())) {
            if (((Boolean) o)) {
                rows++;
                if (isTraceEnabled()) {
                    ResultSetMetaData rsmd = rs.getMetaData();
                    final int columnCount = rsmd.getColumnCount();
                    if (first) {
                        first = false;
                        printColumnHeaders(rsmd, columnCount);
                    }
                    printColumnValues(columnCount);
                }
            } else {
                debug("     Total: " + rows, false);
            }
        }
        clearColumnInfo();
        return o;
    } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 47 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project druid by druid-io.

the class DruidAvaticaHandlerTest method getRows.

private static List<Map<String, Object>> getRows(final ResultSet resultSet, final Set<String> returnKeys) throws SQLException {
    try {
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final List<Map<String, Object>> rows = Lists.newArrayList();
        while (resultSet.next()) {
            final Map<String, Object> row = Maps.newHashMap();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                if (returnKeys == null || returnKeys.contains(metaData.getColumnLabel(i + 1))) {
                    row.put(metaData.getColumnLabel(i + 1), resultSet.getObject(i + 1));
                }
            }
            rows.add(row);
        }
        return rows;
    } finally {
        resultSet.close();
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 48 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project sharding-jdbc by dangdangdotcom.

the class AbstractResultSetAdapter method generateColumnLabelIndexMap.

private Map<String, Integer> generateColumnLabelIndexMap() throws SQLException {
    ResultSetMetaData resultSetMetaData = resultSets.get(0).getMetaData();
    Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount());
    for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
        result.put(resultSetMetaData.getColumnLabel(i), i);
    }
    return result;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) CaseInsensitiveMap(org.apache.commons.collections4.map.CaseInsensitiveMap)

Example 49 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TxnDbUtil method queryToString.

public static String queryToString(String query, boolean includeHeader) throws Exception {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    StringBuilder sb = new StringBuilder();
    try {
        conn = getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        ResultSetMetaData rsmd = rs.getMetaData();
        if (includeHeader) {
            for (int colPos = 1; colPos <= rsmd.getColumnCount(); colPos++) {
                sb.append(rsmd.getColumnName(colPos)).append("   ");
            }
            sb.append('\n');
        }
        while (rs.next()) {
            for (int colPos = 1; colPos <= rsmd.getColumnCount(); colPos++) {
                sb.append(rs.getObject(colPos)).append("   ");
            }
            sb.append('\n');
        }
    } finally {
        closeResources(conn, stmt, rs);
    }
    return sb.toString();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 50 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project nutz by nutzam.

the class ExtDaoInvocationHandler method migration.

/**
     * 为数据表自动增减字段
     *
     * @param dao
     *            Dao实例
     * @param klass
     *            映射Pojo
     * @param add
     *            是否允许添加字段
     * @param del
     *            是否允许删除字段
     * @param checkIndex
     *            是否检查索引
     * @param tableName
     *            动态表名上下文
     */
public static void migration(Dao dao, final Class<?> klass, final boolean add, final boolean del, final boolean checkIndex, final Object tableName) {
    final JdbcExpert expert = dao.getJdbcExpert();
    if (tableName != null && Strings.isNotBlank(tableName.toString())) {
        dao = ext(dao, tableName);
    }
    final Entity<?> en = dao.getEntity(klass);
    if (!dao.exists(klass))
        return;
    final List<Sql> sqls = new ArrayList<Sql>();
    final Set<String> _indexs = new HashSet<String>();
    dao.run(new ConnCallback() {

        public void invoke(Connection conn) throws Exception {
            expert.setupEntityField(conn, en);
            Statement stat = null;
            ResultSet rs = null;
            ResultSetMetaData meta = null;
            try {
                // 获取数据库元信息
                stat = conn.createStatement();
                rs = stat.executeQuery("select * from " + en.getTableName() + " where 1 != 1");
                meta = rs.getMetaData();
                Set<String> columnNames = new HashSet<String>();
                int columnCount = meta.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    columnNames.add(meta.getColumnName(i).toLowerCase());
                }
                for (MappingField mf : en.getMappingFields()) {
                    if (mf.isReadonly())
                        continue;
                    String colName = mf.getColumnName();
                    if (columnNames.contains(colName.toLowerCase())) {
                        columnNames.remove(colName.toLowerCase());
                        continue;
                    }
                    if (add) {
                        log.infof("add column[%s] to table[%s]", mf.getColumnName(), en.getTableName());
                        sqls.add(expert.createAddColumnSql(en, mf));
                    }
                }
                if (del) {
                    for (String colName : columnNames) {
                        log.infof("del column[%s] from table[%s]", colName, en.getTableName());
                        Sql sql = Sqls.create("ALTER table $table DROP column $name");
                        sql.vars().set("table", en.getTableName());
                        sql.vars().set("name", colName);
                        sqls.add(sql);
                    }
                }
                // show index from mytable;
                if (checkIndex)
                    _indexs.addAll(expert.getIndexNames(en, conn));
            } catch (SQLException e) {
                if (log.isDebugEnabled())
                    log.debugf("migration Table '%s' fail!", en.getTableName(), e);
            } finally // Close ResultSet and Statement
            {
                Daos.safeClose(stat, rs);
            }
        }
    });
    // 创建索引
    UpdateIndexSql indexSqls = createIndexs(dao, en, _indexs, tableName);
    if (checkIndex) {
        // 因为已删除的字段的索引是没办法删除的 所以要先处理索引 再处理字段
        Sql[] delIndexSqls = indexSqls.getSqlsDel();
        if (!Lang.isEmptyArray(delIndexSqls)) {
            dao.execute(delIndexSqls);
        }
    }
    for (Sql sql : sqls) {
        dao.execute(sql);
    }
    if (checkIndex) {
        Sql[] addIndexSqls = indexSqls.getSqlsAdd();
        if (!Lang.isEmptyArray(addIndexSqls)) {
            dao.execute(addIndexSqls);
        }
    }
    // 创建关联表
    dao.getJdbcExpert().createRelation(dao, en);
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) JdbcExpert(org.nutz.dao.jdbc.JdbcExpert) ConnCallback(org.nutz.dao.ConnCallback) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) MappingField(org.nutz.dao.entity.MappingField) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException) Sql(org.nutz.dao.sql.Sql) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Field (java.lang.reflect.Field)9