use of java.sql.ResultSetMetaData in project hibernate-orm by hibernate.
the class AutoDiscoveryTest method testDialectGetColumnAliasExtractor.
@Test
public void testDialectGetColumnAliasExtractor() throws Exception {
Session session = openSession();
final SessionImplementor sessionImplementor = (SessionImplementor) session;
session.beginTransaction();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement ps = sessionImplementor.getJdbcCoordinator().getStatementPreparer().prepareStatement(QUERY_STRING);
ResultSet rs = sessionImplementor.getJdbcCoordinator().getResultSetReturn().extract(ps);
try {
ResultSetMetaData metadata = rs.getMetaData();
String column1Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 1);
String column2Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 2);
Assert.assertFalse("bad dialect.getColumnAliasExtractor impl", column1Alias.equals(column2Alias));
} finally {
sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(rs, ps);
sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(ps);
}
}
});
session.getTransaction().commit();
session.close();
}
use of java.sql.ResultSetMetaData in project jfinal by jfinal.
the class ModelBuilder method build.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static final <T> List<T> build(ResultSet rs, Class<? extends Model> modelClass) throws SQLException, InstantiationException, IllegalAccessException {
List<T> result = new ArrayList<T>();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] labelNames = new String[columnCount + 1];
int[] types = new int[columnCount + 1];
buildLabelNamesAndTypes(rsmd, labelNames, types);
while (rs.next()) {
Model<?> ar = modelClass.newInstance();
Map<String, Object> attrs = ar.getAttrs();
for (int i = 1; i <= columnCount; i++) {
Object value;
if (types[i] < Types.BLOB)
value = rs.getObject(i);
else if (types[i] == Types.CLOB)
value = handleClob(rs.getClob(i));
else if (types[i] == Types.NCLOB)
value = handleClob(rs.getNClob(i));
else if (types[i] == Types.BLOB)
value = handleBlob(rs.getBlob(i));
else
value = rs.getObject(i);
attrs.put(labelNames[i], value);
}
result.add((T) ar);
}
return result;
}
use of java.sql.ResultSetMetaData in project jfinal by jfinal.
the class TableBuilder method doBuild.
@SuppressWarnings("unchecked")
private void doBuild(Table table, Connection conn, Config config) throws SQLException {
table.setColumnTypeMap(config.containerFactory.getAttrsMap());
if (table.getPrimaryKey() == null) {
table.setPrimaryKey(config.dialect.getDefaultPrimaryKey());
}
String sql = config.dialect.forTableBuilderDoBuild(table.getName());
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String colName = rsmd.getColumnName(i);
String colClassName = rsmd.getColumnClassName(i);
Class<?> clazz = javaType.getType(colClassName);
if (clazz != null) {
table.setColumnType(colName, clazz);
} else {
int type = rsmd.getColumnType(i);
if (type == Types.BINARY || type == Types.VARBINARY || type == Types.BLOB) {
table.setColumnType(colName, byte[].class);
} else if (type == Types.CLOB || type == Types.NCLOB) {
table.setColumnType(colName, String.class);
} else {
table.setColumnType(colName, String.class);
}
// core.TypeConverter
// throw new RuntimeException("You've got new type to mapping. Please add code in " + TableBuilder.class.getName() + ". The ColumnClassName can't be mapped: " + colClassName);
}
}
rs.close();
stm.close();
}
use of java.sql.ResultSetMetaData in project jOOQ by jOOQ.
the class DefaultDSLContext method fetchLazy.
@Override
public Cursor<Record> fetchLazy(ResultSet rs, DataType<?>... types) {
try {
Field<?>[] fields = new Field[types.length];
ResultSetMetaData meta = rs.getMetaData();
int columns = meta.getColumnCount();
for (int i = 0; i < types.length && i < columns; i++) {
fields[i] = field(meta.getColumnLabel(i + 1), types[i]);
}
return fetchLazy(rs, fields);
} catch (SQLException e) {
throw new DataAccessException("Error while accessing ResultSet meta data", e);
}
}
use of java.sql.ResultSetMetaData in project groovy by apache.
the class GroovyResultSetExtension method toString.
public String toString() {
try {
StringBuilder sb = new StringBuilder("[");
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount();
for (int i = 1; i <= count; i++) {
sb.append(metaData.getColumnName(i));
sb.append(":");
Object object = resultSet.getObject(i);
if (object != null) {
sb.append(object.toString());
} else {
sb.append("[null]");
}
if (i < count) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
} catch (SQLException e) {
// System.err.println("e.getMessage() = " + e.getMessage());
return super.toString();
}
}
Aggregations