/** * 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(); }
/** * Returns the table name used when calling the * connection.getMetaData().getPrimaryKeys() method. * * @param table to use * @return the table name used when calling the * connection.getMetaData().getPrimaryKeys() method */ protected String metaDataTableNameForPrimaryKeys(Table table) { return table.getId(); }
/** * Returns the table name used when calling the * connection.getMetaData().getColumns() method. * * @param table to use * @return the table name used when calling * connection.getMetaData().getColumns() method */ protected String metaDataTableNameForColumns(Table table) { return table.getId(); }
/** * Returns the table name used when calling the * connection.getMetaData().getIndexes() method. * * @param table to use * @return the table name used when calling * connection.getMetaData().getIndexes() method */ protected String metaDataTableNameForIndexes(Table table) { return table.getId(); }
/** * Returns the table name used when calling the * connection.getMetaData().getImportedKeys() method. * * @param table to use * @return the table name used when calling the * connection.getMetaData().getImportedKeys() method */ protected String metaDataTableNameForForeignKeys(Table table) { return table.getId(); }
@Override protected String getJavadocRepresentText() { return "The main interface for entities of the {@code " + getDocument().getId() + "}-table in the database."; } }
public static Optional<? extends Table> referencedTableIfPresent(Project project, String dbmsId, String schemaId, String tableId) { return referencedSchemaIfPresent(project, dbmsId, schemaId) .flatMap(schema -> schema.tables().filter(table -> tableId.equals(table.getId())) .findAny() ); }
public static Table referencedTable(Project project, String dbmsId, String schemaId, String tableId) { return referencedSchema(project, dbmsId, schemaId) .tables().filter(table -> tableId.equals(table.getId())) .findAny().orElseThrow(() -> new SpeedmentConfigException( "Could not find referenced " + Table.class.getSimpleName() + " with name '" + tableId + "'." )); }
/** * A helper method for accessing the foreign {@link Table} referenced by * this key. * * @return the foreign {@link Table} referenced by this */ default Optional<? extends Table> findForeignTable() { final Optional<Schema> schema = ancestors() .filter(Schema.class::isInstance) .map(Schema.class::cast) .findFirst(); return schema.flatMap(s -> s.tables() .filter(tab -> tab.getId().equals(getForeignTableName())) .findAny() ); }
/** * 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; } }
private SpeedmentException couldNotFindLocalColumnException() { return new SpeedmentException( "Could not find referenced local column '" + fkc.getId() + "' in table '" + fkc.getParent().flatMap(ForeignKey::getParent).get().getId() + "'." ); }
/** * Counts the number of elements in the current table by querying the * database. * * @param dbmsOperationHandler the operation handler * @param naming the naming convention in use * @param dbms the dbms * @param table the table * * @return the number of elements in the table */ public static long sqlCount(DbmsOperationHandler dbmsOperationHandler, DatabaseNamingConvention naming, Dbms dbms, Table table) { return dbmsOperationHandler.executeQuery(dbms, "SELECT COUNT(*) FROM " + sqlTableReference(naming, table), Collections.emptyList(), rs -> rs.getLong(1) ).findAny().orElseThrow(() -> new NoSuchElementException("Unable to get sql count for table " + table.getId() + " in dbms " + dbms.getId())); }
/** * Returns a list of all the enum constants in a particular column. * The list is created each time this method is called and is therefore * safe to edit without affecting the column. * <p> * If no enum constants was specified in the column, an exception is * thrown. * * @param column the column to retreive the constants from * @return list of the constants */ public static List<String> enumConstantsOf(Column column) { return Stream.of(column.getEnumConstants() .orElseThrow(() -> new RuntimeException( "Column '" + column.getId() + "' in table '" + column.getParentOrThrow().getId() + "' was marked as an enum but no enum constants was specified." )) .split(",") ).sorted().collect(toList()); }
@Override protected String getJavadocRepresentText() { return "The generated base for the {@link " + getSupport().entityType().getTypeName() + "}-interface representing entities of the {@code " + getDocument().getId() + "}-table in the database."; }
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())); }
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())); } }
protected Type typeOfPK() { final long pks = primaryKeyColumns().count(); if (pks == 0) { return DefaultType.list(DefaultType.WILDCARD); } final Column firstColumn = columnsFromPks() .findFirst().orElseThrow(() -> new SpeedmentTranslatorException( "Table '" + table().get().getId() + "' did not contain any primary key columns." )); Type firstType = typeMappers.get(firstColumn).getJavaType(firstColumn); if (DefaultType.isPrimitive(firstType)) { firstType = DefaultType.wrapperFor(firstType); } if (pks == 1) { return firstType; } else if (columnsFromPks() .map(c -> typeMappers.get(c).getJavaType(c)) .allMatch(firstType::equals)) { return DefaultType.list(firstType); } else { return DefaultType.list(DefaultType.WILDCARD); } }
protected void primaryKeyColumns(Connection connection, Table table, ProgressMeasure progressListener) { requireNonNulls(connection, table); final Schema schema = table.getParentOrThrow(); final SqlSupplier<ResultSet> supplier = () -> connection.getMetaData().getPrimaryKeys(jdbcCatalogLookupName(schema), jdbcSchemaLookupName(schema), metaDataTableNameForPrimaryKeys(table) ); final AbstractDbmsOperationHandler.TableChildMutator<PrimaryKeyColumn, ResultSet> mutator = (primaryKeyColumn, rs) -> { final String columnName = rs.getString("COLUMN_NAME"); primaryKeyColumn.mutator().setId(columnName); primaryKeyColumn.mutator().setName(columnName); primaryKeyColumn.mutator().setOrdinalPosition(rs.getInt("KEY_SEQ")); }; tableChilds(PrimaryKeyColumn.class, table.mutator()::addNewPrimaryKeyColumn, supplier, mutator, progressListener); if (!table.isView() && table.primaryKeyColumns().noneMatch(pk -> true)) { LOGGER.warn("Table '" + table.getId() + "' does not have any primary key."); } }
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 ); }