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);
}
}
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();
}
}
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;
}
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();
}
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);
}
Aggregations