String tableName = null;
Path path = null;
StoreType storeType = null;
Options options;
TableStat stat = null;
try {
String sql =
"SELECT " + C_TABLE_ID + ", path, store_type from " + TB_TABLES
+ " WHERE " + C_TABLE_ID + "='" + name + "'";
if (LOG.isDebugEnabled()) {
LOG.debug(sql);
}
stmt = getConnection().createStatement();
res = stmt.executeQuery(sql);
if (!res.next()) { // there is no table of the given name.
return null;
}
tableName = res.getString(C_TABLE_ID).trim();
path = new Path(res.getString("path").trim());
storeType = CatalogUtil.getStoreType(res.getString("store_type").trim());
} catch (SQLException se) {
throw new IOException(se);
} finally {
CatalogUtil.closeSQLWrapper(res, stmt);
}
Schema schema = null;
try {
String sql = "SELECT column_name, data_type, type_length from " + TB_COLUMNS
+ " WHERE " + C_TABLE_ID + "='" + name + "' ORDER by column_id asc";
stmt = getConnection().createStatement();
if (LOG.isDebugEnabled()) {
LOG.debug(sql);
}
res = stmt.executeQuery(sql);
schema = new Schema();
while (res.next()) {
String columnName = tableName + "."
+ res.getString("column_name").trim();
Type dataType = getDataType(res.getString("data_type")
.trim());
int typeLength = res.getInt("type_length");
if (typeLength > 0) {
schema.addColumn(columnName, dataType, typeLength);
} else {
schema.addColumn(columnName, dataType);
}
}
} catch (SQLException se) {
throw new IOException(se);
} finally {
CatalogUtil.closeSQLWrapper(res, stmt);
}
options = Options.create();
try {
String sql = "SELECT key_, value_ from " + TB_OPTIONS
+ " WHERE " + C_TABLE_ID + "='" + name + "'";
stmt = getConnection().createStatement();
if (LOG.isDebugEnabled()) {
LOG.debug(sql);
}
res = stmt.executeQuery(sql);
while (res.next()) {
options.put(
res.getString("key_"),
res.getString("value_"));
}
} catch (SQLException se) {
throw new IOException(se);
} finally {
CatalogUtil.closeSQLWrapper(res, stmt);
}
try {
String sql = "SELECT num_rows, num_bytes from " + TB_STATISTICS
+ " WHERE " + C_TABLE_ID + "='" + name + "'";
if (LOG.isDebugEnabled()) {
LOG.debug(sql);
}
stmt = getConnection().createStatement();
res = stmt.executeQuery(sql);
if (res.next()) {
stat = new TableStat();
stat.setNumRows(res.getLong("num_rows"));
stat.setNumBytes(res.getLong("num_bytes"));
}
} catch (SQLException se) {
throw new IOException(se);
} finally {
CatalogUtil.closeSQLWrapper(res, stmt);