use of com.squareup.protoparser.DataType in project teiid by teiid.
the class ProtobufMetadataProcessor method isEnum.
private boolean isEnum(List<MessageElement> messageTypes, List<EnumElement> enumTypes, DataType type) {
if (type instanceof DataType.NamedType) {
for (EnumElement element : enumTypes) {
if (element.name().equals(((DataType.NamedType) type).name())) {
return true;
}
}
// enum does not nest, messages nest
for (MessageElement element : messageTypes) {
List<MessageElement> childMessageTypes = filter(element.nestedElements(), MessageElement.class);
List<EnumElement> childEnumTypes = filter(element.nestedElements(), EnumElement.class);
if (isEnum(childMessageTypes, childEnumTypes, type)) {
return true;
}
}
}
return false;
}
use of com.squareup.protoparser.DataType in project teiid by teiid.
the class ProtobufMetadataProcessor method addColumn.
private Column addColumn(MetadataFactory mf, List<MessageElement> messageTypes, List<EnumElement> enumTypes, String parentTableColumn, Table table, FieldElement fieldElement, HashSet<String> ignoreTables, boolean nested) throws TranslatorException {
DataType type = fieldElement.type();
String annotation = fieldElement.documentation();
String columnName = fieldElement.name();
String teiidType = null;
boolean searchable = true;
if (isEnum(messageTypes, enumTypes, type)) {
teiidType = ProtobufDataManager.teiidType(type, isCollection(fieldElement), true);
} else if (isMessage(messageTypes, type)) {
// this is nested table. If the nested table has PK, then we will configure external
// if not we will consider this as embedded with primary table.
String nestedName = ((DataType.NamedType) type).name();
MessageElement nestedMessageElement = getMessage(messageTypes, nestedName);
if (nestedMessageElement == null) {
throw new TranslatorException(InfinispanPlugin.Event.TEIID25001, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25001, nestedName, columnName));
}
// this is one-2-many
if (isCollection(fieldElement)) {
Table nestedTable = addTable(mf, messageTypes, enumTypes, nestedMessageElement, parentTableColumn == null ? columnName : parentTableColumn + Tokens.DOT + columnName, ignoreTables);
if (table.getPrimaryKey() != null) {
// add additional column to represent the relationship
Column parentColumn = table.getPrimaryKey().getColumns().get(0);
String psedoColumnName = table.getName() + "_" + parentColumn.getName();
Column addedColumn = mf.addColumn(psedoColumnName, parentColumn.getRuntimeType(), nestedTable);
addedColumn.setNameInSource(parentColumn.getName());
addedColumn.setUpdatable(true);
addedColumn.setProperty(PSEUDO, columnName);
addedColumn.setSearchType(SearchType.Searchable);
List<String> keyColumns = new ArrayList<String>();
keyColumns.add(addedColumn.getName());
List<String> refColumns = new ArrayList<String>();
refColumns.add(parentColumn.getName());
// $NON-NLS-1$
mf.addForeignKey("FK_" + table.getName().toUpperCase(), keyColumns, refColumns, table.getName(), nestedTable);
// since this nested table can not be reached directly, put a access
// pattern on it.
nestedTable.setProperty(MERGE, table.getFullName());
nestedTable.setProperty(PARENT_TAG, Integer.toString(fieldElement.tag()));
nestedTable.setProperty(PARENT_COLUMN_NAME, columnName);
} else {
ignoreTables.add(nestedName);
LogManager.logInfo(LogConstants.CTX_CONNECTOR, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25006, nestedName));
}
} else {
ignoreTables.add(nestedMessageElement.name());
// inline all the columns from the message and return
for (FieldElement nestedElement : nestedMessageElement.fields()) {
Column nestedColumn = addColumn(mf, messageTypes, enumTypes, columnName, table, nestedElement, ignoreTables, true);
nestedColumn.setNameInSource(nestedElement.name());
nestedColumn.setProperty(MESSAGE_NAME, nestedMessageElement.qualifiedName());
nestedColumn.setProperty(PARENT_TAG, Integer.toString(fieldElement.tag()));
nestedColumn.setProperty(PARENT_COLUMN_NAME, columnName);
}
}
return null;
} else {
teiidType = findFromAnnotation(AT_TEIID, annotation, "type");
if (teiidType == null) {
teiidType = ProtobufDataManager.teiidType(type, isCollection(fieldElement), false);
} else {
// these are artificial types not defined in protobuf, so not eligible for pushdown based
// comparisons so that need to be marked as such.
searchable = false;
}
}
Column c = null;
if (nested) {
c = mf.addColumn(parentTableColumn + "_" + columnName, teiidType, table);
} else {
c = mf.addColumn(columnName, teiidType, table);
}
c.setNativeType(fieldElement.type().toString());
c.setUpdatable(true);
c.setNullType(fieldElement.label() == Label.REQUIRED ? NullType.No_Nulls : NullType.Nullable);
c.setProperty(TAG, Integer.toString(fieldElement.tag()));
String length = findFromAnnotation(AT_TEIID, annotation, "length");
if (length != null) {
c.setLength(Integer.parseInt(length));
}
String precision = findFromAnnotation(AT_TEIID, annotation, "precision");
if (precision != null) {
c.setPrecision(Integer.parseInt(precision));
}
String scale = findFromAnnotation(AT_TEIID, annotation, "scale");
if (scale != null) {
c.setScale(Integer.parseInt(scale));
}
// process default value
if (fieldElement.getDefault() != null) {
if (isEnum(messageTypes, enumTypes, type)) {
String ordinal = getEnumOrdinal(messageTypes, enumTypes, ((DataType.NamedType) type).name(), fieldElement.getDefault().value().toString());
if (ordinal != null) {
c.setDefaultValue(ordinal);
}
} else {
c.setDefaultValue(fieldElement.getDefault().value().toString());
}
}
// process annotations
if (table.getAnnotation() != null) {
if (table.getAnnotation().contains("@Indexed")) {
c.setSearchType(SearchType.Searchable);
}
}
if (annotation != null && !annotation.isEmpty()) {
// this is induced annotation already represented in Teiid metadata remove it.
if (annotation.contains("@Teiid(")) {
int start = annotation.indexOf("@Teiid(");
int end = annotation.indexOf(")", start + 7);
annotation = annotation.substring(0, start) + annotation.substring(end + 1);
}
if (!annotation.isEmpty()) {
c.setAnnotation(annotation);
}
if (annotation.contains("@IndexedField(index=false")) {
c.setSearchType(SearchType.Unsearchable);
}
if (annotation.contains("@Id")) {
List<String> pkNames = new ArrayList<String>();
pkNames.add(fieldElement.name());
mf.addPrimaryKey("PK_" + fieldElement.name().toUpperCase(), pkNames, table);
}
}
if (!searchable) {
c.setSearchType(SearchType.Unsearchable);
}
return c;
}
Aggregations