Search in sources :

Example 1 with FieldElement

use of com.squareup.protoparser.FieldElement 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;
}
Also used : Table(org.teiid.metadata.Table) FieldElement(com.squareup.protoparser.FieldElement) ArrayList(java.util.ArrayList) MessageElement(com.squareup.protoparser.MessageElement) Column(org.teiid.metadata.Column) DataType(com.squareup.protoparser.DataType) TranslatorException(org.teiid.translator.TranslatorException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 2 with FieldElement

use of com.squareup.protoparser.FieldElement in project teiid by teiid.

the class ProtobufMetadataProcessor method addTable.

private Table addTable(MetadataFactory mf, List<MessageElement> messageTypes, List<EnumElement> enumTypes, MessageElement messageElement, String columnPrefix, HashSet<String> ignoreTables) throws TranslatorException {
    String tableName = messageElement.name();
    if (mf.getSchema().getTable(tableName) != null) {
        return mf.getSchema().getTable(tableName);
    }
    if (ignoreTables.contains(tableName)) {
        return null;
    }
    Table table = mf.addTable(tableName);
    table.setSupportsUpdate(true);
    table.setNameInSource(messageElement.qualifiedName());
    table.setAnnotation(messageElement.documentation());
    for (FieldElement fieldElement : messageElement.fields()) {
        addColumn(mf, messageTypes, enumTypes, columnPrefix, table, fieldElement, ignoreTables, false);
    }
    return table;
}
Also used : Table(org.teiid.metadata.Table) FieldElement(com.squareup.protoparser.FieldElement)

Aggregations

FieldElement (com.squareup.protoparser.FieldElement)2 Table (org.teiid.metadata.Table)2 DataType (com.squareup.protoparser.DataType)1 MessageElement (com.squareup.protoparser.MessageElement)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Column (org.teiid.metadata.Column)1 TranslatorException (org.teiid.translator.TranslatorException)1