use of org.apache.ignite.console.agent.db.DbColumn in project ignite by apache.
the class OracleMetadataDialect method tables.
/**
* {@inheritDoc}
*/
@Override
public Collection<DbTable> tables(Connection conn, List<String> schemas, boolean tblsOnly) throws SQLException {
PreparedStatement pkStmt = conn.prepareStatement(SQL_PRIMARY_KEYS);
PreparedStatement uniqueIdxsStmt = conn.prepareStatement(SQL_UNIQUE_INDEXES_KEYS);
PreparedStatement idxStmt = conn.prepareStatement(SQL_INDEXES);
if (schemas.isEmpty())
schemas.add(null);
Set<String> sysSchemas = systemSchemas();
Collection<DbTable> tbls = new ArrayList<>();
try (Statement colsStmt = conn.createStatement()) {
for (String schema : schemas) {
if (systemSchemas().contains(schema) || (schema != null && schema.startsWith("FLOWS_")))
continue;
String sql = String.format(SQL_COLUMNS, tblsOnly ? "INNER JOIN all_tables b on a.table_name = b.table_name and a.owner = b.owner" : "", schema != null ? String.format(" WHERE a.owner = '%s' ", schema) : "");
try (ResultSet colsRs = colsStmt.executeQuery(sql)) {
String prevSchema = "";
String prevTbl = "";
boolean first = true;
Set<String> pkCols = Collections.emptySet();
Collection<DbColumn> cols = new ArrayList<>();
Collection<QueryIndex> idxs = Collections.emptyList();
while (colsRs.next()) {
String owner = colsRs.getString(OWNER_IDX);
String tbl = colsRs.getString(TBL_NAME_IDX);
if (sysSchemas.contains(owner) || (schema != null && schema.startsWith("FLOWS_")))
continue;
boolean changed = !owner.equals(prevSchema) || !tbl.equals(prevTbl);
if (changed) {
if (first)
first = false;
else
tbls.add(table(prevSchema, prevTbl, cols, idxs));
prevSchema = owner;
prevTbl = tbl;
cols = new ArrayList<>();
pkCols = primaryKeys(pkStmt, owner, tbl);
Map.Entry<String, Set<String>> uniqueIdxAsPk = null;
if (pkCols.isEmpty()) {
uniqueIdxAsPk = uniqueIndexAsPk(uniqueIndexes(uniqueIdxsStmt, owner, tbl));
if (uniqueIdxAsPk != null)
pkCols.addAll(uniqueIdxAsPk.getValue());
}
idxs = indexes(idxStmt, owner, tbl, uniqueIdxAsPk != null ? uniqueIdxAsPk.getKey() : null);
}
String colName = colsRs.getString(COL_NAME_IDX);
cols.add(new DbColumn(colName, decodeType(colsRs), pkCols.contains(colName), !"N".equals(colsRs.getString(NULLABLE_IDX)), false));
}
if (!cols.isEmpty())
tbls.add(table(prevSchema, prevTbl, cols, idxs));
}
}
}
return tbls;
}
use of org.apache.ignite.console.agent.db.DbColumn in project ignite by apache.
the class JdbcMetadataDialect method tables.
/**
* {@inheritDoc}
*/
@Override
public Collection<DbTable> tables(Connection conn, List<String> schemas, boolean tblsOnly) throws SQLException {
DatabaseMetaData dbMeta = conn.getMetaData();
Set<String> sys = systemSchemas();
Collection<String> unsignedTypes = unsignedTypes(dbMeta);
if (schemas.isEmpty())
schemas.add(null);
Collection<DbTable> tbls = new ArrayList<>();
for (String toSchema : schemas) {
try (ResultSet tblsRs = dbMeta.getTables(useCatalog() ? toSchema : null, useSchema() ? toSchema : null, "%", tblsOnly ? TABLES_ONLY : TABLES_AND_VIEWS)) {
while (tblsRs.next()) {
String tblCatalog = tblsRs.getString(TBL_CATALOG_IDX);
String tblSchema = tblsRs.getString(TBL_SCHEMA_IDX);
String tblName = tblsRs.getString(TBL_NAME_IDX);
// In case of MySql we should use catalog.
String schema = tblSchema != null ? tblSchema : tblCatalog;
// Skip system schemas.
if (sys.contains(schema))
continue;
Set<String> pkCols = new LinkedHashSet<>();
try (ResultSet pkRs = dbMeta.getPrimaryKeys(tblCatalog, tblSchema, tblName)) {
while (pkRs.next()) pkCols.add(pkRs.getString(PK_COL_NAME_IDX));
}
Map.Entry<String, Set<String>> uniqueIdxAsPk = null;
// If PK not found, trying to use first UNIQUE index as key.
if (pkCols.isEmpty()) {
Map<String, Set<String>> uniqueIdxs = new LinkedHashMap<>();
try (ResultSet idxRs = dbMeta.getIndexInfo(tblCatalog, tblSchema, tblName, true, true)) {
while (idxRs.next()) {
String idxName = idxRs.getString(IDX_NAME_IDX);
String colName = idxRs.getString(IDX_COL_NAME_IDX);
if (idxName == null || colName == null)
continue;
Set<String> idxCols = uniqueIdxs.get(idxName);
if (idxCols == null) {
idxCols = new LinkedHashSet<>();
uniqueIdxs.put(idxName, idxCols);
}
idxCols.add(colName);
}
}
uniqueIdxAsPk = uniqueIndexAsPk(uniqueIdxs);
if (uniqueIdxAsPk != null)
pkCols.addAll(uniqueIdxAsPk.getValue());
}
Collection<DbColumn> cols = new ArrayList<>();
try (ResultSet colsRs = dbMeta.getColumns(tblCatalog, tblSchema, tblName, null)) {
while (colsRs.next()) {
String colName = colsRs.getString(COL_NAME_IDX);
cols.add(new DbColumn(colName, colsRs.getInt(COL_DATA_TYPE_IDX), pkCols.contains(colName), colsRs.getInt(COL_NULLABLE_IDX) == DatabaseMetaData.columnNullable, unsignedTypes.contains(colsRs.getString(COL_TYPE_NAME_IDX))));
}
}
String uniqueIdxAsPkName = uniqueIdxAsPk != null ? uniqueIdxAsPk.getKey() : null;
Map<String, QueryIndex> idxs = new LinkedHashMap<>();
try (ResultSet idxRs = dbMeta.getIndexInfo(tblCatalog, tblSchema, tblName, false, true)) {
while (idxRs.next()) {
String idxName = idxRs.getString(IDX_NAME_IDX);
String colName = idxRs.getString(IDX_COL_NAME_IDX);
// Skip {@code null} names and unique index used as PK.
if (idxName == null || colName == null || idxName.equals(uniqueIdxAsPkName))
continue;
QueryIndex idx = idxs.get(idxName);
if (idx == null) {
idx = index(idxName);
idxs.put(idxName, idx);
}
String askOrDesc = idxRs.getString(IDX_ASC_OR_DESC_IDX);
Boolean asc = askOrDesc == null || "A".equals(askOrDesc);
idx.getFields().put(colName, asc);
}
}
// Remove index that is equals to primary key.
if (!pkCols.isEmpty()) {
for (Map.Entry<String, QueryIndex> entry : idxs.entrySet()) {
QueryIndex idx = entry.getValue();
if (pkCols.equals(idx.getFields().keySet())) {
idxs.remove(entry.getKey());
break;
}
}
}
tbls.add(table(schema, tblName, cols, idxs.values()));
}
}
}
return tbls;
}
Aggregations