use of org.teiid.metadata.Table in project teiid by teiid.
the class TestTriggerActions method testUpdateWithChanging.
@Test
public void testUpdateWithChanging() throws Exception {
TransformationMetadata metadata = TestUpdateValidator.example1();
TestUpdateValidator.createView("select 1 as x, 2 as y", metadata, GX);
Table t = metadata.getMetadataStore().getSchemas().get(VM1).getTables().get(GX);
t.setDeletePlan("");
t.setUpdatePlan("FOR EACH ROW BEGIN update pm1.g1 set e2 = case when changing.y then new.y end where e2 = old.y; END");
t.setInsertPlan("");
String sql = "update gx set y = 5";
FakeDataManager dm = new FakeDataManager();
FakeDataStore.addTable("pm1.g1", dm, metadata);
CommandContext context = createCommandContext();
BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
List<?>[] expected = new List[] { Arrays.asList(1) };
helpProcess(plan, context, dm, expected);
assertEquals("UPDATE pm1.g1 SET e2 = 5 WHERE e2 = 2", dm.getQueries().get(0));
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class MongoDBMetadataProcessor method addColumn.
private Column addColumn(MetadataFactory metadataFactory, Table table, String columnKey, Object value) {
Column column = null;
if (columnKey.equals(ID)) {
if (value instanceof BasicDBObject) {
BasicDBObject compositeKey = (BasicDBObject) value;
for (String key : compositeKey.keySet()) {
column = addColumn(metadataFactory, table, key, compositeKey.get(key));
column.setUpdatable(true);
}
}
}
if (!columnKey.equals(ID) && value instanceof BasicDBObject) {
// embedded doc - one to one
Table childTable = addTable(metadataFactory, columnKey, (BasicDBObject) value, table);
if (childTable != null) {
childTable.setProperty(MERGE, table.getName());
childTable.setProperty(ASSOSIATION, MergeDetails.Association.ONE.name());
}
} else if (value instanceof BasicDBList) {
// embedded doc, list one to many
if (((BasicDBList) value).get(0) instanceof BasicDBObject) {
Table childTable = addTable(metadataFactory, columnKey, (BasicDBObject) ((BasicDBList) value).get(0), table);
if (childTable != null) {
childTable.setProperty(MERGE, table.getName());
childTable.setProperty(ASSOSIATION, MergeDetails.Association.MANY.name());
}
} else {
column = table.getColumnByName(columnKey);
if (column == null) {
// $NON-NLS-1$
column = metadataFactory.addColumn(columnKey, TypeFacility.RUNTIME_NAMES.OBJECT + "[]", table);
} else if (!column.getRuntimeType().equals(TypeFacility.RUNTIME_NAMES.OBJECT + "[]")) {
// $NON-NLS-1$
// type conflict
MetadataFactory.setDataType(TypeFacility.RUNTIME_NAMES.OBJECT, column, metadataFactory.getDataTypes(), false);
column.setNativeType(null);
}
column.setSearchType(SearchType.Unsearchable);
}
} else if (value instanceof DBRef) {
Object obj = ((DBRef) value).getId();
column = addColumn(metadataFactory, table, columnKey, obj);
String ref = ((DBRef) value).getCollectionName();
// $NON-NLS-1$
metadataFactory.addForeignKey("FK_" + columnKey, Arrays.asList(columnKey), ref, table);
} else {
column = table.getColumnByName(columnKey);
String dataType = getDataType(value);
if (column == null) {
column = metadataFactory.addColumn(columnKey, dataType, table);
setNativeType(column, value);
} else if (!column.getRuntimeType().equals(getDataType(value))) {
// type conflict
if (STRING_COMPATIBLE_TYPES.contains(column.getRuntimeType()) && STRING_COMPATIBLE_TYPES.contains(dataType)) {
MetadataFactory.setDataType(TypeFacility.RUNTIME_NAMES.STRING, column, metadataFactory.getDataTypes(), false);
} else {
MetadataFactory.setDataType(TypeFacility.RUNTIME_NAMES.OBJECT, column, metadataFactory.getDataTypes(), false);
}
column.setNativeType(null);
column.setSearchType(SearchType.Unsearchable);
}
}
// create a PK out of _id
if (columnKey.equals(ID)) {
if (value instanceof BasicDBObject) {
BasicDBObject compositeKey = (BasicDBObject) value;
ArrayList<String> columns = new ArrayList<String>();
for (String key : compositeKey.keySet()) {
columns.add(key);
}
// $NON-NLS-1$
metadataFactory.addPrimaryKey("PK0", columns, table);
} else {
// $NON-NLS-1$
metadataFactory.addPrimaryKey("PK0", Arrays.asList(ID), table);
}
}
return column;
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class MongoDBMetadataProcessor method addTable.
private Table addTable(MetadataFactory metadataFactory, String tableName, BasicDBObject row, Table parent) {
Table table = null;
if (metadataFactory.getSchema().getTable(tableName) != null) {
table = metadataFactory.getSchema().getTable(tableName);
}
Set<String> keys = row.keySet();
if (keys != null && !keys.isEmpty()) {
if (table == null) {
table = metadataFactory.addTable(tableName);
table.setSupportsUpdate(true);
if (parent != null) {
// $NON-NLS-1$
FullyQualifiedName rn = new FullyQualifiedName("embedded", tableName);
String parentfqn = parent.getProperty(FQN, false);
table.setProperty(FQN, parentfqn + FullyQualifiedName.SEPARATOR + rn.toString());
} else {
// $NON-NLS-1$
FullyQualifiedName fqn = new FullyQualifiedName("collection", tableName);
table.setProperty(FQN, fqn.toString());
}
}
for (String columnKey : keys) {
Object value = row.get(columnKey);
Column column = addColumn(metadataFactory, table, columnKey, value);
if (column != null) {
column.setUpdatable(true);
}
}
return table;
}
return null;
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class MongoDBUpdateExecution method removeParentKey.
private void removeParentKey(MongoDocument document, BasicDBObject row) throws TranslatorException {
Table source = document.getTable();
Table target = document.getMergeTable();
for (ForeignKey fk : source.getForeignKeys()) {
if (fk.getReferenceTableName().equals(target.getName())) {
for (int i = 0; i < fk.getColumns().size(); i++) {
if (row != null) {
row.remove(fk.getColumns().get(i).getName());
}
}
}
}
}
use of org.teiid.metadata.Table in project teiid by teiid.
the class IQueryToLdapSearchParser method getSearchScopeFromFromItem.
private int getSearchScopeFromFromItem(NamedTable fromItem) {
// $NON-NLS-1$
String searchScopeString = "";
int searchScope = LDAPConnectorConstants.ldapDefaultSearchScope;
// TODO: Re-use the getExpressionString method if possible, rather than
// rewriting the same code twice.
Table group = fromItem.getMetadataObject();
String nameInSource = group.getNameInSource();
// string instead so we can safely call split on it
if (nameInSource == null) {
// $NON-NLS-1$
nameInSource = "";
}
// now split it on ? to find the part of it that specifies search scope
// $NON-NLS-1$
String[] nameInSourceArray = nameInSource.split("\\?");
if (nameInSourceArray.length >= 2) {
searchScopeString = nameInSourceArray[1];
}
// try the default in the connector properties
if (searchScopeString.equals("")) {
// $NON-NLS-1$
SearchDefaultScope searchDefaultScope = this.executionFactory.getSearchDefaultScope();
if (searchDefaultScope != null) {
searchScopeString = searchDefaultScope.name();
}
// protect against getting null back from the property
if (searchScopeString == null) {
// $NON-NLS-1$
searchScopeString = "";
}
}
if (searchScopeString.equals("SUBTREE_SCOPE")) {
// $NON-NLS-1$
searchScope = SearchControls.SUBTREE_SCOPE;
} else if (searchScopeString.equals("ONELEVEL_SCOPE")) {
// $NON-NLS-1$
searchScope = SearchControls.ONELEVEL_SCOPE;
} else if (searchScopeString.equals("OBJECT_SCOPE")) {
// $NON-NLS-1$
searchScope = SearchControls.OBJECT_SCOPE;
}
return searchScope;
}
Aggregations