/** * Retrieves the primary key for the table * */ private static void getPrimaryKey(DatabaseMetaData md, DatabaseRelationDefinition relation, QuotedIDFactory idfac) throws SQLException { RelationID id = relation.getID(); // Retrieves a description of the given table's primary key columns. They are ordered by COLUMN_NAME (sic!) try (ResultSet rs = md.getPrimaryKeys(null, id.getSchemaName(), id.getTableName())) { extractPrimaryKey(relation, idfac, id, rs); } catch (SQLSyntaxErrorException e) { // WORKAROUND for MySQL connector >= 8.0: // <https://github.com/ontop/ontop/issues/270> try (ResultSet rs = md.getPrimaryKeys(id.getSchemaName(), null, id.getTableName())) { extractPrimaryKey(relation, idfac, id, rs); } } }
/** * Retrieves the unique attributes(s) * @param md * @return * @throws SQLException */ private static void getUniqueAttributes(DatabaseMetaData md, DatabaseRelationDefinition relation, QuotedIDFactory idfac) throws SQLException { RelationID id = relation.getID(); // extracting unique try (ResultSet rs = md.getIndexInfo(null, id.getSchemaName(), id.getTableName(), true, true)) { extractUniqueAttributes(relation, idfac, rs); } catch (Exception e){ // Workaround for MySQL-connector >= 8.0 try (ResultSet rs = md.getIndexInfo(id.getSchemaName(),null, id.getTableName(), true, true)) { extractUniqueAttributes(relation, idfac, rs); } } }
private static String extractPredicateName(RelationDefinition r) { RelationID id = r.getID(); String name = id.getSchemaName(); if (name == null) name = id.getTableName(); else name = name + "." + id.getTableName(); return name; }
private static String extractPredicateName(RelationDefinition r) { RelationID id = r.getID(); String name = id.getSchemaName(); if (name == null) name = id.getTableName(); else name = name + "." + id.getTableName(); return name; }
RelationID id = relation.getID(); try (ResultSet rs = md.getPrimaryKeys(null, id.getSchemaName(), id.getTableName())) { Map<Integer, String> primaryKeyAttributes = new HashMap<>(); String currentName = null;
/** * Retrieves the foreign keys for the table * */ private static void getForeignKeys(DatabaseMetaData md, DatabaseRelationDefinition relation, DBMetadata metadata) throws SQLException { QuotedIDFactory idfac = metadata.getQuotedIDFactory(); RelationID relationId = relation.getID(); try (ResultSet rs = md.getImportedKeys(null, relationId.getSchemaName(), relationId.getTableName())) { extractForeignKeys(relation, metadata, idfac, rs); } catch (Exception ex) { try (ResultSet rs = md.getImportedKeys(relationId.getSchemaName(),null, relationId.getTableName())) { extractForeignKeys(relation, metadata, idfac, rs); } } }
try (ResultSet rs = md.getColumns(null, seedId.getSchemaName(), seedId.getTableName(), null)) { while (rs.next()) { String schema = rs.getString("TABLE_SCHEM");
try (ResultSet rs = md.getColumns(catalog, seedId.getSchemaName(), seedId.getTableName(), null)) { while (rs.next()) { String schema = rs.getString("TABLE_SCHEM");
try (ResultSet rs = md.getImportedKeys(null, relationId.getSchemaName(), relationId.getTableName())) { ForeignKeyConstraint.Builder builder = null; String currentName = null;
try (ResultSet rs = md.getIndexInfo(null, id.getSchemaName(), id.getTableName(), true, true)) { UniqueConstraint.Builder builder = null; String currentName = null;
relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) attrs = RAExpressionAttributes.create(attributes.build(), alias, relation.getID().getSchemalessID());
relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) attrs = RAExpressionAttributes.create(attributes.build(), alias, relation.getID().getSchemalessID());
@Override public void visit(Table tableName) { RelationID id = idfac.createRelationID(tableName.getSchemaName(), tableName.getName()); // construct the predicate using the table name DatabaseRelationDefinition relation = metadata.getDatabaseRelation(id); if (relation == null) throw new InvalidSelectQueryRuntimeException("Table " + id + " not found in metadata", tableName); relationIndex++; RelationID alias = (tableName.getAlias() != null) ? idfac.createRelationID(null, tableName.getAlias().getName()) : relation.getID(); ImmutableMap<QuotedID, Term> attributes = relation.getAttributes().stream() .collect(ImmutableCollectors.toMap(Attribute::getID, attribute -> createVariable(attribute.getID()))); // DEFAULT SCHEMA // TODO: to be improved if ((tableName.getAlias() == null) && relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) result = RAExpressionAttributes.create(attributes, alias, relation.getID().getSchemalessID()); else result = RAExpressionAttributes.create(attributes, alias); }
@Override public void visit(Table tableName) { RelationID id = idfac.createRelationID(tableName.getSchemaName(), tableName.getName()); // construct the predicate using the table name DatabaseRelationDefinition relation = metadata.getDatabaseRelation(id); if (relation == null) throw new InvalidSelectQueryRuntimeException("Table " + id + " not found in metadata", tableName); relationIndex++; RelationID alias = (tableName.getAlias() != null) ? idfac.createRelationID(null, tableName.getAlias().getName()) : relation.getID(); ImmutableMap<QuotedID, Term> attributes = relation.getAttributes().stream() .collect(ImmutableCollectors.toMap(Attribute::getID, attribute -> createVariable(attribute.getID()))); // DEFAULT SCHEMA // TODO: to be improved if ((tableName.getAlias() == null) && relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) result = RAExpressionAttributes.create(attributes, alias, relation.getID().getSchemalessID()); else result = RAExpressionAttributes.create(attributes, alias); }