Newer
Older
*/
private static int[] getStats(final TAPMetadata meta){
int[] counts = new int[]{1,5,0,0,0};
int[] stdColCounts = new int[]{4,6,13,5,3}; // 4,6 because of the addition of `dbname` ; 13 because of the addition of `dbname` and `arraysize` (tap-1.1)
for(int c = 0; c < stdColCounts.length; c++)
counts[2] += stdColCounts[c];
Iterator<TAPSchema> itSchemas = meta.iterator();
while(itSchemas.hasNext()){
TAPSchema schema = itSchemas.next();
boolean isTapSchema = (schema.getADQLName().equalsIgnoreCase(STDSchema.TAPSCHEMA.toString()));
if (!isTapSchema)
counts[0]++;
Iterator<TAPTable> itTables = schema.iterator();
while(itTables.hasNext()){
TAPTable table = itTables.next();
if (isTapSchema && TAPMetadata.resolveStdTable(table.getADQLName()) != null){
int ind = h2JDBCConnection.getCreationOrder(TAPMetadata.resolveStdTable(table.getADQLName()));
counts[2] -= stdColCounts[ind];
counts[1]++;
Iterator<DBColumn> itColumns = table.iterator();
while(itColumns.hasNext()){
itColumns.next();
counts[2]++;
}
Iterator<TAPForeignKey> itKeys = table.getForeignKeys();
while(itKeys.hasNext()){
TAPForeignKey fk = itKeys.next();
counts[3]++;
counts[4] += fk.getNbRelations();
}
}
}
return counts;
}
/**
* <p>Get the effective counts after a call of {@link JDBCConnection#setTAPSchema(TAPMetadata)}.</p>
* <p>Counts are computed directly from the DB using the given connection; the same connection used to set the TAP schema in {@link JDBCConnection#setTAPSchema(TAPMetadata)}.</p>
* @param conn
* @param meta Metadata, in order to get the standard TAP tables' name.
* @return An integer array with the following values: [0]=nbSchemas, [1]=nbTables, [2]=nbColumns, [3]=nbKeys and [4]=nbKeyColumns.
*/
private static int[] getStats(final JDBCConnection conn, final TAPMetadata meta){
int[] counts = new int[5];
Statement stmt = null;
stmt = conn.connection.createStatement();
TAPSchema tapSchema = meta.getSchema(STDSchema.TAPSCHEMA.toString());
String schemaPrefix = formatIdentifier(tapSchema.getDBName(), conn.translator.isCaseSensitive(IdentifierField.SCHEMA));
gmantele
committed
if (!conn.supportsSchema || schemaPrefix == null)
schemaPrefix = "";
else
gmantele
committed
schemaPrefix += ".";
boolean tCaseSensitive = conn.translator.isCaseSensitive(IdentifierField.TABLE);
TAPTable tapTable = tapSchema.getTable(STDTable.SCHEMAS.toString());
counts[0] = count(stmt, schemaPrefix + formatIdentifier(tapTable.getDBName(), tCaseSensitive), tapSchema.getADQLName() + "." + tapTable.getADQLName());
tapTable = tapSchema.getTable(STDTable.TABLES.toString());
counts[1] = count(stmt, schemaPrefix + formatIdentifier(tapTable.getDBName(), tCaseSensitive), tapSchema.getADQLName() + "." + tapTable.getADQLName());
tapTable = tapSchema.getTable(STDTable.COLUMNS.toString());
counts[2] = count(stmt, schemaPrefix + formatIdentifier(tapTable.getDBName(), tCaseSensitive), tapSchema.getADQLName() + "." + tapTable.getADQLName());
tapTable = tapSchema.getTable(STDTable.KEYS.toString());
counts[3] = count(stmt, schemaPrefix + formatIdentifier(tapTable.getDBName(), tCaseSensitive), tapSchema.getADQLName() + "." + tapTable.getADQLName());
tapTable = tapSchema.getTable(STDTable.KEY_COLUMNS.toString());
counts[4] = count(stmt, schemaPrefix + formatIdentifier(tapTable.getDBName(), tCaseSensitive), tapSchema.getADQLName() + "." + tapTable.getADQLName());
}catch(SQLException se){
fail("Can not create a statement!");
if (stmt != null)
stmt.close();
}catch(SQLException ex){}
}
return counts;
}
private static int count(final Statement stmt, final String qualifiedTableName, final String adqlTableName){
ResultSet rs = null;
rs = stmt.executeQuery("SELECT COUNT(*) FROM " + qualifiedTableName + ";");
rs.next();
return rs.getInt(1);
e.printStackTrace(System.err);
fail("Can not count! Maybe " + qualifiedTableName + " (in ADQL: " + adqlTableName + ") does not exist.");
return -1;
close(rs);
}
}
private static void close(final ResultSet rs){
if (rs == null)
return;
rs.close();
}catch(SQLException se){}
}
private static void close(final Statement stmt){
try{
if (stmt != null)
stmt.close();
}catch(SQLException se){}
}
private static void close(final InputStream io){
try{
if (io != null)
io.close();
}catch(IOException ioe){}