private void InitKeyInfo(DataRow row, DatabaseMetaData dbMetaData, String catalog, String schema, String table) {
string column = (string)row [(int)SCHEMA_TABLE.BaseColumnName];
row [(int)SCHEMA_TABLE.IsUnique] = false;
row [(int)SCHEMA_TABLE.IsKey] = false;
row [(int)SCHEMA_TABLE.IsIdentity] = false;
row [(int)SCHEMA_TABLE.IsRowVersion] = false;
if ((_command.Behavior & CommandBehavior.KeyInfo) == 0)
return;
if(table == null || column == null || dbMetaData == null)
return;
ResultSet versionCol = dbMetaData.getVersionColumns(catalog, schema, table);
try {
while(versionCol.next()) {
if(versionCol.getString("COLUMN_NAME") == column) {
if (DatabaseMetaData__Finals.versionColumnPseudo == versionCol.getShort("PSEUDO_COLUMN")) {
row [(int)SCHEMA_TABLE.IsIdentity] = true;
row [(int)SCHEMA_TABLE.IsRowVersion] = true;
}
}
}
}
finally {
versionCol.close();
}
ResultSet primaryKeys = dbMetaData.getPrimaryKeys(catalog,schema,table);
bool primaryKeyExists = false;
int columnCount = 0;
try {
while(primaryKeys.next()) {
columnCount++;
if(primaryKeys.getString("COLUMN_NAME") == column) {
row [(int)SCHEMA_TABLE.IsKey] = true;
primaryKeyExists = true;
}
}
// column constitutes a key by itself, so it should be marked as unique
if ((columnCount == 1) && (((bool)row [(int)SCHEMA_TABLE.IsKey]) == true)) {
row [(int)SCHEMA_TABLE.IsUnique] = true;
}
}
finally {
primaryKeys.close();
}
ResultSet indexInfoRes = dbMetaData.getIndexInfo(catalog,schema,table,true,false);
string currentIndexName = null;
columnCount = 0;
bool belongsToCurrentIndex = false;
bool atFirstIndex = true;
bool uniqueKeyExists = false;
try {
while(indexInfoRes.next()) {
if (indexInfoRes.getShort("TYPE") == DatabaseMetaData__Finals.tableIndexStatistic) {
// index of type tableIndexStatistic identifies table statistics - ignore it
continue;
}
uniqueKeyExists = true;
string iname = indexInfoRes.getString("INDEX_NAME");
if (currentIndexName == iname) {
// we're within the rows of the same index
columnCount++;
}
else {
// we jump to row of new index
if (belongsToCurrentIndex && columnCount == 1) {
// there is a constraint of type UNIQUE that applies only to this column
row [(int)SCHEMA_TABLE.IsUnique] = true;
}
if (currentIndexName != null) {
atFirstIndex = false;
}
currentIndexName = iname;
columnCount = 1;
belongsToCurrentIndex = false;
}
if(indexInfoRes.getString("COLUMN_NAME") == column) {
// FIXME : this will cause "spare" columns marked as IsKey. Needs future investigation.
// only the first index we met should be marked as a key
//if (atFirstIndex) {
row [(int)SCHEMA_TABLE.IsKey] = true;
//}
belongsToCurrentIndex = true;
}
}
// the column appears in the last index, which is single-column
if (belongsToCurrentIndex && columnCount == 1) {
// there is a constraint of type UNIQUE that applies only to this column
row [(int)SCHEMA_TABLE.IsUnique] = true;
}
}
finally {
indexInfoRes.close();
}
if(!primaryKeyExists && !uniqueKeyExists) {
ResultSet bestRowId = dbMetaData.getBestRowIdentifier(catalog, schema, table, DatabaseMetaData__Finals.bestRowTemporary, false);
try {
while(bestRowId.next()) {
if(bestRowId.getString("COLUMN_NAME") == column)
row [(int)SCHEMA_TABLE.IsKey] = true;
}
}
finally {
bestRowId.close();
}
}
}