org.zodiac.fastorm.rdb.metadata.TableOrViewMetadata Maven / Gradle / Ivy
The newest version!
package org.zodiac.fastorm.rdb.metadata;
import org.zodiac.commons.util.lang.FastThreadLocalStringUtil;
import org.zodiac.fastorm.core.meta.Feature;
import org.zodiac.fastorm.core.meta.FeatureSupportedMetadata;
import org.zodiac.fastorm.core.meta.ObjectMetadata;
import org.zodiac.fastorm.core.meta.ObjectType;
import org.zodiac.fastorm.rdb.events.ContextKeyValue;
import org.zodiac.fastorm.rdb.events.ContextKeys;
import org.zodiac.fastorm.rdb.events.EventContext;
import org.zodiac.fastorm.rdb.events.EventListener;
import org.zodiac.fastorm.rdb.events.EventType;
import org.zodiac.fastorm.rdb.metadata.dialect.Dialect;
import org.zodiac.fastorm.rdb.metadata.key.ForeignKeyBuilder;
import org.zodiac.fastorm.rdb.metadata.key.ForeignKeyMetadata;
import org.zodiac.fastorm.rdb.metadata.key.LazyForeignKeyMetadata;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* DQL. Object Metadata: tTables or views.
*
*/
public interface TableOrViewMetadata extends ObjectMetadata, FeatureSupportedMetadata {
@Override
ObjectType getObjectType();
/**
* Current database dialect.
*
* @return Dialect.
*/
Dialect getDialect();
/**
* @return The schema where metadata is located
*/
RDBSchemaMetadata getSchema();
/**
* @return All columns of the current table or view
*/
List getColumns();
/**
* @return Retrieve all columns and include associated table pairs of columns.
*/
List findColumns();
/**
* Retrieve the columns of the current table or view.
*
* @param name Column name or alias.
* @return Optional
* @see RDBColumnMetadata#getName()
* @see RDBColumnMetadata#getAlias()
*/
Optional getColumn(String name);
/**
* Immediately retrieve the columns of the current table or view, and throw an exception if they do not exist: {@link IllegalStateException} .
*
* @param name Column name
* @return RDBColumnMetadata
*/
default RDBColumnMetadata getColumnNow(String name) {
return getColumn(name)
.orElseThrow(() -> new IllegalStateException("column not found:" + name + " in " + getFullName()));
}
/**
* Search for columns, which can be found by associating table to column or other table to column relationships through foreign keys.
*
* @param name Column full name or alias. For example: user.name, schema1. user.name .
* @return Optional
*/
Optional findColumn(String name);
/**
* Get all foreign keys.
*
* @return Foreign keys.
*/
List getForeignKeys();
/**
* Retrieve foreign keys based on the association table
*
* @param targetName The association table.
* @return Optional.
*/
Optional getForeignKey(String targetName);
/**
* Add foreign key metadata.
*
* @param metadata ForeignKeyMetadata
* @see ForeignKeyBuilder
* @see TableOrViewMetadata#addForeignKey(ForeignKeyBuilder)
*/
void addForeignKey(ForeignKeyMetadata metadata);
/**
*Add foreign key metadata with the builder.
*
* @param builder The {@link ForeignKeyBuilder} .
* @return Added metadata.
* @see LazyForeignKeyMetadata
*/
ForeignKeyMetadata addForeignKey(ForeignKeyBuilder builder);
/**
* Triggering event.
*
* @param eventType The {@link EventType} .
* @param keyValues Event context key value content.
*/
default void fireEvent(EventType eventType, ContextKeyValue>... keyValues) {
fireEvent(eventType, ctx -> ctx.set(keyValues));
}
/**
* Trigger event, if there is a trigger present
*
* @param eventType The {@link EventType} .
* @param contextConsumer Contextual consumer.
*/
default void fireEvent(EventType eventType, Consumer contextConsumer) {
EventListener eventListener = this.findFeatureOrElse(EventListener.ID, null);
if (eventListener != null) {
EventContext context = EventContext.create();
context.set(ContextKeys.table, this);
contextConsumer.accept(context);
eventListener.onEvent(eventType, context);
}
}
default String getRealName() {
return getName();
}
default String getFullName() {
return FastThreadLocalStringUtil.concat(
getSchema().getName(),
".",
getRealName()
);
}
default String getQuoteName() {
return getDialect().quote(getSchema().getName(), false)
+ "."
+ getDialect().quote(getRealName(), false);
}
@Override
default T findFeatureOrElse(String id, Supplier orElse) {
T current = getFeatureOrElse(id, null);
if (null != current) {
return current;
}
RDBSchemaMetadata schema = getSchema();
if (schema != null) {
return schema.findFeatureOrElse(id, null);
}
return orElse == null ? null : orElse.get();
}
default List findFeatures(Predicate predicate) {
return Stream.concat(getSchema().getFeatureList().stream(), getFeatureList().stream())
.filter(predicate)
.collect(Collectors.toList());
}
default List findFeatures() {
return findFeatures((feature -> true));
}
/**
* Merge the table structure with the specified table structure.
*
* @param metadata The {@link TableOrViewMetadata} .
*/
void merge(TableOrViewMetadata metadata);
/**
* Replace the table structure.
*
* @param metadata The {@link TableOrViewMetadata} .
*/
void replace(TableOrViewMetadata metadata);
}