public boolean isPartOfPrimaryKey(Column column) { KeyRecord pk = ((Table) column.getParent()).getPrimaryKey(); if (pk != null) { for (Column col : pk.getColumns()) { if (col.getName().equals(column.getName())) { return true; } } } return false; }
static List<String> getColumnNames(List<Column> columns) { ArrayList<String> names = new ArrayList<String>(); for (Column c : columns) { names.add(c.getName()); } return names; }
private static HashMap<String, String> buildTableMetadata( String tableName, List<Column> columns, String encoding) { HashMap<String, String> options = new HashMap<String, String>(); options.put(EvaluatorIterator.COLUMNS_COUNT, String.valueOf(columns.size())); options.put(EvaluatorIterator.TABLENAME, tableName); options.put(EvaluatorIterator.ENCODING, encoding); for (int i = 0; i < columns.size(); i++) { Column column = columns.get(i); options.put(EvaluatorIterator.createColumnName(EvaluatorIterator.NAME, i), column.getName()); if (!SQLStringVisitor.getRecordName(column).equals(AccumuloMetadataProcessor.ROWID)) { options.put( EvaluatorIterator.createColumnName(EvaluatorIterator.CF, i), column.getProperty(AccumuloMetadataProcessor.CF, false)); if (column.getProperty(AccumuloMetadataProcessor.CQ, false) != null) { options.put( EvaluatorIterator.createColumnName(EvaluatorIterator.CQ, i), column.getProperty(AccumuloMetadataProcessor.CQ, false)); } if (column.getProperty(AccumuloMetadataProcessor.VALUE_IN, false) != null) { options.put( EvaluatorIterator.createColumnName(EvaluatorIterator.VALUE_IN, i), column.getProperty(AccumuloMetadataProcessor.VALUE_IN, false)); } } options.put( EvaluatorIterator.createColumnName(EvaluatorIterator.DATA_TYPE, i), column.getDatatype().getJavaClassName()); } return options; }
@Override public void visit(In obj) { visitNode(obj.getLeftExpression()); Column c = (Column) this.onGoingExpression.pop(); ArrayList<Object> values = new ArrayList<Object>(); for (Expression expr : obj.getRightExpressions()) { visitNode(expr); values.add(this.onGoingExpression.pop().toString()); } this.filterValues.put(c.getName(), values); }
static boolean sameColumnSet(KeyRecord recordOne, KeyRecord recordTwo) { if (recordOne == null || recordTwo == null) { return false; } List<Column> setOne = recordOne.getColumns(); List<Column> setTwo = recordTwo.getColumns(); if (setOne.size() != setTwo.size()) { return false; } for (int i = 0; i < setOne.size(); i++) { Column one = setOne.get(i); Column two = setTwo.get(i); if (!one.getName().equals(two.getName())) { return false; } } return true; }
@Override public void visit(Comparison obj) { if (obj.getOperator() == Operator.EQ) { visitNode(obj.getLeftExpression()); Column c = (Column) this.onGoingExpression.pop(); visitNode(obj.getRightExpression()); ArrayList<Object> values = new ArrayList<Object>(); values.add(this.onGoingExpression.pop().toString()); this.filterValues.put(c.getName(), values); } else { this.exceptions.add(new TranslatorException("Only Equality supported")); } }
private boolean keyMatches(List<String> names, KeyRecord record) { if (names.size() != record.getColumns().size()) { return false; } Set<String> keyNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); for (Column c : record.getColumns()) { keyNames.add(c.getName()); } for (int i = 0; i < names.size(); i++) { if (!keyNames.contains(names.get(i))) { return false; } } return true; }
private void addColumns(List<Column> columns, boolean includeType) { append(LPAREN); boolean first = true; for (Column c : columns) { if (first) { first = false; } else { append(COMMA).append(SPACE); } if (includeType) { appendColumn(c, true, includeType); appendColumnOptions(c); } else if (c.getParent() instanceof KeyRecord) { // function based column append(c.getNameInSource()); } else { append(SQLStringVisitor.escapeSinglePart(c.getName())); } } append(RPAREN); }
private void visit(Table table, Column column) { append(NEWLINE).append(TAB); if (table.getTableType() == Table.Type.TemporaryTable && column.isAutoIncremented() && column.getNullType() == NullType.No_Nulls && column.getJavaType() == DataTypeManager.DefaultDataClasses.INTEGER) { append(SQLStringVisitor.escapeSinglePart(column.getName())); append(SPACE); append(SERIAL); } else { appendColumn(column, true, true); if (column.isAutoIncremented()) { append(SPACE).append(AUTO_INCREMENT); } } appendDefault(column); // options appendColumnOptions(column); }
private static void buildFunctionImports(MetadataStore metadataStore, List<Builder> edmSchemas) { for (Schema schema : metadataStore.getSchemaList()) { EdmSchema.Builder odataSchema = findSchema(edmSchemas, schema.getName()); EdmEntityContainer.Builder entityContainer = findEntityContainer(edmSchemas, schema.getName()); // procedures for (Procedure proc : schema.getProcedures().values()) { EdmFunctionImport.Builder edmProcedure = EdmFunctionImport.newBuilder(); edmProcedure.setName(proc.getName()); String httpMethod = "POST"; for (ProcedureParameter pp : proc.getParameters()) { if (pp.getName().equals("return")) { httpMethod = "GET"; edmProcedure.setReturnType( ODataTypeManager.odataType(pp.getDatatype().getRuntimeTypeName())); continue; } EdmFunctionParameter.Builder param = EdmFunctionParameter.newBuilder(); param.setName(pp.getName()); param.setType(ODataTypeManager.odataType(pp.getDatatype().getRuntimeTypeName())); if (pp.getType() == ProcedureParameter.Type.In) { param.setMode(Mode.In); } else if (pp.getType() == ProcedureParameter.Type.InOut) { param.setMode(Mode.InOut); } else if (pp.getType() == ProcedureParameter.Type.Out) { param.setMode(Mode.Out); } param.setNullable(pp.getNullType() == NullType.Nullable); edmProcedure.addParameters(param); } // add a complex type for return resultset. ColumnSet<Procedure> returnColumns = proc.getResultSet(); if (returnColumns != null) { httpMethod = "GET"; EdmComplexType.Builder complexType = EdmComplexType.newBuilder(); complexType.setName(proc.getName() + "_" + returnColumns.getName()); complexType.setNamespace(schema.getName()); for (Column c : returnColumns.getColumns()) { EdmProperty.Builder property = EdmProperty.newBuilder(c.getName()) .setType(ODataTypeManager.odataType(c.getDatatype().getRuntimeTypeName())) .setNullable(c.getNullType() == NullType.Nullable); if (c.getDatatype() .getRuntimeTypeName() .equals(DataTypeManager.DefaultDataTypes.STRING)) { property .setFixedLength(c.isFixedLength()) .setMaxLength(c.getLength()) .setUnicode(true); } complexType.addProperties(property); } odataSchema.addComplexTypes(complexType); edmProcedure.setIsCollection(true); edmProcedure.setReturnType( EdmCollectionType.newBuilder() .setCollectionType(complexType) .setKind(CollectionKind.Collection)); } edmProcedure.setHttpMethod(httpMethod); entityContainer.addFunctionImports(edmProcedure); } } }
private static void buildEntityTypes( MetadataStore metadataStore, List<EdmSchema.Builder> edmSchemas) { for (Schema schema : metadataStore.getSchemaList()) { List<EdmEntitySet.Builder> entitySets = new ArrayList<EdmEntitySet.Builder>(); List<EdmEntityType.Builder> entityTypes = new ArrayList<EdmEntityType.Builder>(); for (Table table : schema.getTables().values()) { KeyRecord primaryKey = table.getPrimaryKey(); List<KeyRecord> uniques = table.getUniqueKeys(); if (primaryKey == null && uniques.isEmpty()) { LogManager.logDetail( LogConstants.CTX_ODATA, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16002, table.getFullName())); continue; } EdmEntityType.Builder entityType = EdmEntityType.newBuilder().setName(table.getName()).setNamespace(schema.getName()); // adding key if (primaryKey != null) { for (Column c : primaryKey.getColumns()) { entityType.addKeys(c.getName()); } } else { for (Column c : uniques.get(0).getColumns()) { entityType.addKeys(c.getName()); } } // adding properties for (Column c : table.getColumns()) { EdmProperty.Builder property = EdmProperty.newBuilder(c.getName()) .setType(ODataTypeManager.odataType(c.getDatatype().getRuntimeTypeName())) .setNullable(c.getNullType() == NullType.Nullable); if (c.getDatatype() .getRuntimeTypeName() .equals(DataTypeManager.DefaultDataTypes.STRING)) { property.setFixedLength(c.isFixedLength()).setMaxLength(c.getLength()).setUnicode(true); } entityType.addProperties(property); } // entity set one for one entity type EdmEntitySet.Builder entitySet = EdmEntitySet.newBuilder().setName(table.getName()).setEntityType(entityType); entityType.setNamespace(schema.getName()); entitySets.add(entitySet); // add enitity types for entity schema entityTypes.add(entityType); } // entity container is holder entity sets, association sets, function imports EdmEntityContainer.Builder entityContainer = EdmEntityContainer.newBuilder() .setName(schema.getName()) .setIsDefault(false) .addEntitySets(entitySets); // build entity schema EdmSchema.Builder modelSchema = EdmSchema.newBuilder() .setNamespace(schema.getName()) .addEntityTypes(entityTypes) .addEntityContainers(entityContainer); edmSchemas.add(modelSchema); } }
public void handleInsertSequences(Insert insert) throws TranslatorException { /* * If a missing auto_increment column is modeled with name in source indicating that an Oracle Sequence * then pull the Sequence name out of the name in source of the column. */ if (!(insert.getValueSource() instanceof ExpressionValueSource)) { return; } ExpressionValueSource values = (ExpressionValueSource) insert.getValueSource(); if (insert.getTable().getMetadataObject() == null) { return; } List<Column> allElements = insert.getTable().getMetadataObject().getColumns(); if (allElements.size() == values.getValues().size()) { return; } int index = 0; List<ColumnReference> elements = insert.getColumns(); for (Column element : allElements) { if (!element.isAutoIncremented()) { continue; } String name = element.getNameInSource(); int seqIndex = name.indexOf(SEQUENCE); if (seqIndex == -1) { continue; } boolean found = false; while (index < elements.size()) { if (element.equals(elements.get(index).getMetadataObject())) { found = true; break; } index++; } if (found) { continue; } String sequence = name.substring(seqIndex + SEQUENCE.length()); int delimiterIndex = sequence.indexOf(Tokens.DOT); if (delimiterIndex == -1) { throw new TranslatorException( JDBCPlugin.Event.TEIID11017, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11017, SEQUENCE, name)); } String sequenceGroupName = sequence.substring(0, delimiterIndex); String sequenceElementName = sequence.substring(delimiterIndex + 1); NamedTable sequenceGroup = this.getLanguageFactory().createNamedTable(sequenceGroupName, null, null); ColumnReference sequenceElement = this.getLanguageFactory() .createColumnReference( sequenceElementName, sequenceGroup, null, element.getJavaType()); insert .getColumns() .add( index, this.getLanguageFactory() .createColumnReference( element.getName(), insert.getTable(), element, element.getJavaType())); values.getValues().add(index, sequenceElement); } }
@Test public void testForeignTable() throws Exception { String ddl = "CREATE FOREIGN TABLE G1(\n" + "e1 integer primary key,\n" + "e2 varchar(10) unique,\n" + "e3 date not null unique,\n" + "e4 decimal(12,3) default 12.2 options (searchable 'unsearchable'),\n" + "e5 integer auto_increment INDEX OPTIONS (UUID 'uuid', NAMEINSOURCE 'nis', SELECTABLE 'NO'),\n" + "e6 varchar index default 'hello')\n" + "OPTIONS (CARDINALITY 12, UUID 'uuid2', UPDATABLE 'true', FOO 'BAR', ANNOTATION 'Test Table')"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); Table table = tableMap.get("G1"); assertTrue(table.isPhysical()); assertFalse(table.isVirtual()); assertFalse(table.isSystem()); assertFalse(table.isMaterialized()); assertFalse(table.isDeletePlanEnabled()); assertEquals("uuid2", table.getUUID()); assertEquals(12, table.getCardinality()); assertTrue(table.supportsUpdate()); assertEquals("BAR", table.getProperties().get("FOO")); assertEquals("Test Table", table.getAnnotation()); assertEquals(6, table.getColumns().size()); List<Column> columns = table.getColumns(); Column e1 = columns.get(0); Column e2 = columns.get(1); Column e3 = columns.get(2); Column e4 = columns.get(3); Column e5 = columns.get(4); Column e6 = columns.get(5); assertEquals("e1", e1.getName()); assertEquals("int", e1.getDatatype().getName()); assertEquals("primary key not same", e1, table.getPrimaryKey().getColumns().get(0)); assertEquals("e2", e2.getName()); assertEquals("string", e2.getDatatype().getName()); assertEquals("unique", e2, table.getUniqueKeys().get(0).getColumns().get(0)); assertEquals(NullType.Nullable, e2.getNullType()); assertEquals(10, e2.getLength()); assertEquals(0, e2.getPrecision()); assertEquals("e3", e3.getName()); assertEquals("date", e3.getDatatype().getName()); assertEquals("unique", e3, table.getUniqueKeys().get(1).getColumns().get(0)); assertEquals(NullType.No_Nulls, e3.getNullType()); assertEquals("e4", e4.getName()); assertEquals("bigdecimal", e4.getDatatype().getName()); assertEquals(false, e4.isAutoIncremented()); assertEquals(12, e4.getPrecision()); assertEquals(3, e4.getScale()); assertEquals(SearchType.Unsearchable, e4.getSearchType()); assertEquals("12.2", e4.getDefaultValue()); assertEquals("e5", e5.getName()); assertEquals("int", e5.getDatatype().getName()); assertEquals(true, e5.isAutoIncremented()); assertEquals("uuid", e5.getUUID()); assertEquals("nis", e5.getNameInSource()); assertEquals(false, e5.isSelectable()); assertEquals("index", e5, table.getIndexes().get(0).getColumns().get(0)); assertEquals("e6", e6.getName()); assertEquals("string", e6.getDatatype().getName()); assertEquals("index", e6, table.getIndexes().get(1).getColumns().get(0)); assertEquals("hello", e6.getDefaultValue()); }
@Override public void execute( VDBMetaData vdb, MetadataStore store, ValidatorReport report, MetadataValidator metadataValidator) { for (Schema schema : store.getSchemaList()) { if (vdb.getImportedModels().contains(schema.getName())) { continue; } ModelMetaData model = vdb.getModel(schema.getName()); for (Table t : schema.getTables().values()) { if (t.isVirtual()) { if (t.isMaterialized() && t.getMaterializedTable() != null) { String matTableName = t.getMaterializedTable().getFullName(); int index = matTableName.indexOf(Table.NAME_DELIM_CHAR); if (index == -1) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31088, matTableName, t.getFullName())); } else { String schemaName = matTableName.substring(0, index); Schema matSchema = store.getSchema(schemaName); if (matSchema == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31089, schemaName, matTableName, t.getFullName())); } else { Table matTable = matSchema.getTable(matTableName.substring(index + 1)); if (matTable == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31090, matTableName.substring(index + 1), schemaName, t.getFullName())); } else { t.setMaterializedTable(matTable); } } } } } for (KeyRecord record : t.getAllKeys()) { if (record.getColumns() == null || record.getColumns().isEmpty()) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31149, t.getFullName(), record.getName())); } } List<ForeignKey> fks = t.getForeignKeys(); if (fks == null || fks.isEmpty()) { continue; } for (ForeignKey fk : fks) { // Only applicable to older teiid releases than 8.9 if (fk.getReferenceKey() != null && !metadataValidator.isTeiidOrGreater(Version.TEIID_8_9)) { // ensure derived fields are set fk.setReferenceKey(fk.getReferenceKey()); continue; } String referenceTableName = fk.getReferenceTableName(); Table referenceTable = null; if (fk.getReferenceKey() == null) { if (referenceTableName == null) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31091, t.getFullName())); continue; } // TODO there is an ambiguity here because we don't properly track the name parts // so we have to first check for a table name that may contain . referenceTable = schema.getTable(referenceTableName); } else { referenceTableName = fk.getReferenceKey().getParent().getFullName(); } String referenceSchemaName = schema.getName(); int index = referenceTableName.indexOf(Table.NAME_DELIM_CHAR); if (referenceTable == null) { if (index != -1) { referenceSchemaName = referenceTableName.substring(0, index); Schema referenceSchema = store.getSchema(referenceSchemaName); if (referenceSchema == null) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31093, referenceSchemaName, t.getFullName())); continue; } referenceTable = referenceSchema.getTable(referenceTableName.substring(index + 1)); } if (referenceTable == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31092, t.getFullName(), referenceTableName.substring(index + 1), referenceSchemaName)); continue; } } KeyRecord uniqueKey = null; List<String> referenceColumns = fk.getReferenceColumns(); if (fk.getReferenceKey() != null) { // index metadata logic sets the key prior to having the column names List<Column> cols = fk.getReferenceKey().getColumns(); referenceColumns = new ArrayList<String>(); for (Column col : cols) { referenceColumns.add(col.getName()); } } if (referenceColumns == null || referenceColumns.isEmpty()) { if (referenceTable.getPrimaryKey() == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31094, t.getFullName(), referenceTableName.substring(index + 1), referenceSchemaName)); } else { uniqueKey = referenceTable.getPrimaryKey(); } } else { for (KeyRecord record : referenceTable.getUniqueKeys()) { if (keyMatches(fk.getReferenceColumns(), record)) { uniqueKey = record; break; } } if (uniqueKey == null && referenceTable.getPrimaryKey() != null && keyMatches(fk.getReferenceColumns(), referenceTable.getPrimaryKey())) { uniqueKey = referenceTable.getPrimaryKey(); } } if (uniqueKey == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31095, t.getFullName(), referenceTableName.substring(index + 1), referenceSchemaName, fk.getReferenceColumns())); } else { fk.setReferenceKey(uniqueKey); } } } } }