private void primaryKeyColumns(Connection conn, Table table, ProgressMeasure progress) { requireNonNulls(conn, table, progress); final SqlSupplier<ResultSet> supplier = () -> conn.getMetaData().getPrimaryKeys(null, null, table.getId()); final TableChildMutator<PrimaryKeyColumn> mutator = (pkc, rs) -> { final String columnName = rs.getString("COLUMN_NAME"); pkc.mutator().setId(columnName); pkc.mutator().setName(columnName); pkc.mutator().setOrdinalPosition(rs.getInt("KEY_SEQ")); }; tableChilds( table, PrimaryKeyColumn.class, table.mutator()::addNewPrimaryKeyColumn, supplier, rsChild -> rsChild.getString("COLUMN_NAME"), mutator ); if (!table.isView() && table.primaryKeyColumns().noneMatch(pk -> true)) { LOGGER.warn(format("Table '%s' does not have any primary key.", table.getId())); } }
private static int findNullOffset( final Table table, final Stage<?> stage, final HasComparableOperators<?, ?> field ) { int result = -1; final String onColumnId = field .identifier() .getColumnId(); final List<Column> columns = table.columns() .filter(Column::isEnabled) .collect(toList()); for (int j = 0; j < columns.size(); j++) { final String columnId = columns.get(j).getId(); if (columnId.equals(onColumnId)) { // Compose a null detecting entity mapper result = j; break; } } if (result == -1) { throw new IllegalStateException( "Unable to locate column " + onColumnId + " in table " + table.getId() + " for stage " + stage.toString() + " Columns: " + columns.stream().map(Column::getId).collect(joining(", ")) ); } return result; }
ImmutableTable(ImmutableSchema parent, Map<String, Object> table) { super(parent, table); final Table prototype = new TableImpl(parent, table); this.enabled = prototype.isEnabled(); this.id = prototype.getId(); this.name = prototype.getName(); this.alias = prototype.getAlias(); this.packageName = prototype.getPackageName(); this.view = prototype.isView(); this.columns = unmodifiableList(super.children(COLUMNS, ImmutableColumn::new).collect(toList())); this.indexes = unmodifiableList(super.children(INDEXES, ImmutableIndex::new).collect(toList())); this.foreignKeys = unmodifiableList(super.children(FOREIGN_KEYS, ImmutableForeignKey::new).collect(toList())); this.primaryKeyColumns = unmodifiableList(super.children(PRIMARY_KEY_COLUMNS, ImmutablePrimaryKeyColumn::new).collect(toList())); }
public static Stream<? extends Document> traverseOver(Table table) { return Stream.of( table.columns(), table.columns().flatMap(DocumentDbUtil::traverseOver), table.primaryKeyColumns(), table.primaryKeyColumns().flatMap(DocumentDbUtil::traverseOver), table.indexes(), table.indexes().flatMap(DocumentDbUtil::traverseOver), table.foreignKeys(), table.foreignKeys().flatMap(DocumentDbUtil::traverseOver) ).flatMap(Function.identity()); }
/** * Returns {@code true} if the two specified documents represents the same * element in the database. Two documents are considered same if they have * the same name and type and their parents are considered same. * * @param first the first document * @param second the second document * @return {@code true} if same, else {@code false} */ public static boolean isSame(Table first, Table second) { if (first.getId().equals(second.getId())) { final Schema firstParent = first.getParentOrThrow(); final Schema secondParent = second.getParentOrThrow(); return isSame(firstParent, secondParent); } else { return false; } }
/** * Returns the table name used when calling the * {@code SHOW COLUMNS FROM} statement. * * @param table to use * @return the table name to use */ protected String metaDataTableNameForShowColumns(Table table) { return table.getId(); }
private String sqlColumnList(Predicate<Column> preFilter, Function<String, String> postMapper) { return table.columns() .sorted(comparing(Column::getOrdinalPosition)) .filter(Column::isEnabled) .filter(preFilter) .map(Column::getName) .map(naming::encloseField) .map(postMapper) .collect(joining(",")); }
final String tableName = rsTable.getString("TABLE_NAME"); final String tableType = rsTable.getString("TABLE_TYPE"); table.mutator().setId(tableName); table.mutator().setName(tableName); table.mutator().setView("VIEW".equals(tableType)); if (!table.isView()) { if (table.columns() .filter(col -> col.getAsString(ORIGINAL_TYPE).filter("INTEGER"::equalsIgnoreCase).isPresent()) .noneMatch(col -> table.primaryKeyColumns().anyMatch(pkc -> DocumentDbUtil.isSame(pkc.findColumn().get(), col))) && table.columns().map(Column::getId).noneMatch("rowid"::equalsIgnoreCase)) { final Column column = table.mutator().addNewColumn(); if (table.primaryKeyColumns().anyMatch(pkc -> true)) { final Set<String> oldPks = table.primaryKeyColumns() .map(PrimaryKeyColumn::getId) .collect(toCollection(LinkedHashSet::new)); if (table.indexes() .filter(Index::isUnique) .noneMatch(idx -> oldPks.equals(idx.indexColumns() final Index pkReplacement = table.mutator().addNewIndex(); final String idxName = md5(oldPks.toString()); table.primaryKeyColumns().forEachOrdered(pkc -> { final int ordNo = 1 + (int) pkReplacement.indexColumns().count(); final IndexColumn idxCol = mutator.addNewIndexColumn();
private void foreignKeys(Connection conn, Table table, ProgressMeasure progress) { requireNonNulls(conn, table); final Schema schema = table.getParentOrThrow(); final SqlSupplier<ResultSet> supplier = () -> conn.getMetaData().getImportedKeys(null, null, table.getId()); table, ForeignKey.class, table.mutator()::addNewForeignKey, supplier, rsChild -> rsChild.getInt("KEY_SEQ") == 1 ? "" table.foreignKeys() .filter(fk -> fksThatNeedNewNames.contains(fk.getId())) .forEach(fk -> { .collect(toSet()); final Optional<? extends Index> found = table.indexes() .filter(idx -> idx.indexColumns() .map(IndexColumn::getId) "Found a foreign key in table '%s' with no name. " + "Assigning it a random name '%s'", table.getId(), randName));
.abstract_() .setSupertype(SimpleParameterizedType.create( table.isView() ? AbstractViewManager.class : AbstractManager.class, .add(Constructor.of().protected_() .add("this.tableIdentifier = " + TableIdentifier.class.getSimpleName() + ".of(" + Stream.of(getSupport().dbmsOrThrow().getId(), getSupport().schemaOrThrow().getId(), getSupport().tableOrThrow().getId()) .map(s -> "\"" + s + "\"").collect(joining(", ")) + ");") () -> table.primaryKeyColumns() .sorted(comparing(PrimaryKeyColumn::getOrdinalPosition)) .filter(HasEnabled::test)
private void indexes(Connection conn, Table table, ProgressMeasure progress) { requireNonNulls(conn, table, progress); if (table.isView() && IGNORE_VIEW_INDEXES) return; final SqlSupplier<ResultSet> supplier = () -> conn.getMetaData().getIndexInfo(null, null, table.getId(), false, APPROXIMATE_INDEX); final TableChildMutator<Index> mutator = (index, rs) -> { final String indexName = rs.getString("INDEX_NAME"); index.mutator().setId(indexName); index.mutator().setName(indexName); index.mutator().setUnique(!rs.getBoolean("NON_UNIQUE")); final IndexColumn indexColumn = index.mutator().addNewIndexColumn(); final String columnName = rs.getString("COLUMN_NAME"); indexColumn.mutator().setId(columnName); indexColumn.mutator().setName(columnName); indexColumn.mutator().setOrdinalPosition(rs.getInt("ORDINAL_POSITION")); indexColumn.mutator().setOrderType(getOrderType(rs)); }; final SqlPredicate<ResultSet> filter = rs -> rs.getString("INDEX_NAME") != null; tableChilds( table, Index.class, table.mutator()::addNewIndex, supplier, rsChild -> rsChild.getString("INDEX_NAME"), mutator, filter ); }
requireNonNulls(connection, table); final Schema schema = table.getParentOrThrow(); + "Type name %s, data type %d, decimal digits %d." + "Fallback to JDBC-type %s", table.getId(), column.getId(), md.getTypeName(), tableChilds(Column.class, table.mutator()::addNewColumn, supplier, mutator, progressListener);
/** * Returns the full name used in the database for the specified * {@link Table}. This is typically constructed by combining the schema and * the table name with a separator, but that might be different in different * implementations. * * @param table the table to retrieve the name of * @return the full name */ default String fullNameOf(Table table) { final Schema schema = table.getParentOrThrow(); return fullNameOf(schema.getName(), table.getName()); }
if (table.isView()) { return; final Schema schema = table.getParentOrThrow(); final SqlSupplier<ResultSet> supplier = () -> connection.getMetaData().getIndexInfo( }; tableChilds(Index.class, table.mutator()::addNewIndex, supplier, mutator, filter, progressListener);
).set(Value.ofInvocation(TableIdentifier.class, "of", Stream.<HasAlias>of( table.getParentOrThrow().getParentOrThrow(), table.getParentOrThrow(), table ).map(HasName::getName) getSupport().entityType()) )).set(Value.ofReference("unmodifiableList(asList(" + nl() + indent( table.columns() .sorted(comparing(Column::getOrdinalPosition)) .filter(HasEnabled::isEnabled)
/** * Debug method used to track type mappers of a project. May be of future use if one perhaps would venture to * investigate why we get several copies of the dbms from the database */ private void printTypeMappers(String heading, Project p) { System.out.println(heading); p.dbmses().map(d -> (Dbms) d).forEach(dbms -> { dbms.schemas().map(s -> (Schema) s).forEach(schema -> { schema.tables().map(t -> (Table) t).forEach(table -> { table.columns().map(c -> (Column) c).filter(c -> c.getTypeMapper().isPresent()).forEach(column -> { String mapperName = column.getTypeMapper().get(); if (mapperName.endsWith("PrimitiveTypeMapper")) { mapperName = "Primitive"; } System.out.println(" - " + dbms.getName() + ":" + schema.getName() + "/" + table.getName() + "." + column.getName() + " mapped by " + mapperName); }); }); }); }); }
private String sqlPrimaryKeyColumnList(Function<String, String> postMapper) { requireNonNull(postMapper); return table.primaryKeyColumns() .sorted(comparing(PrimaryKeyColumn::getOrdinalPosition)) .map(this::findColumn) .map(Column::getName) .map(naming::encloseField) .map(postMapper) .collect(joining(" AND ")); }
protected void foreignKeys(Connection connection, Table table, ProgressMeasure progressListener) { requireNonNulls(connection, table); final Schema schema = table.getParentOrThrow(); final SqlSupplier<ResultSet> supplier = () -> connection.getMetaData().getImportedKeys( }; tableChilds(ForeignKey.class, table.mutator()::addNewForeignKey, supplier, mutator, progressListener);
table.indexes() .filter(i -> i.indexColumns().count() == 1) .filter(Index::isUnique) .anyMatch(col -> isSame(column, col)) || ( table.primaryKeyColumns().count() == 1 && table.primaryKeyColumns() .map(PrimaryKeyColumn::findColumn) .filter(Optional::isPresent)
icon = ((Table) node).isView() ? TABLE_LINK : TABLE; } else {