Skip to content

Commit

Permalink
TEIID-3370: adding the insert/update/delete support
Browse files Browse the repository at this point in the history
  • Loading branch information
rareddy committed Oct 7, 2015
1 parent fa98f76 commit 817b5e0
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 143 deletions.
Expand Up @@ -118,11 +118,16 @@ protected TranslatorException buildError(BinaryWSProcedureExecution execution) {
// do some error handling
try {
Blob blob = (Blob)execution.getOutputParameterValues().get(0);
JsonDeserializer parser = new JsonDeserializer(false);
ODataError error = parser.toError(blob.getBinaryStream());
return new TranslatorException(ODataPlugin.Util.gs(
ODataPlugin.Event.TEIID17013, execution.getResponseCode(),
error.getCode(), error.getMessage(), error.getInnerError()));
if (blob != null) {
JsonDeserializer parser = new JsonDeserializer(false);
ODataError error = parser.toError(blob.getBinaryStream());
return new TranslatorException(ODataPlugin.Util.gs(
ODataPlugin.Event.TEIID17013, execution.getResponseCode(),
error.getCode(), error.getMessage(), error.getInnerError()));
} else {
return new TranslatorException(ODataPlugin.Util.gs(
ODataPlugin.Event.TEIID17031));
}
}
catch (Throwable t) {
return new TranslatorException(t);
Expand Down Expand Up @@ -168,7 +173,7 @@ protected BinaryWSProcedureExecution invokeHTTP(String method,

protected Map<String, List<String>> getDefaultHeaders() {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Accept", Arrays.asList(ContentType.JSON_NO_METADATA.toContentTypeString())); //$NON-NLS-1$
headers.put("Accept", Arrays.asList(ContentType.JSON.toContentTypeString())); //$NON-NLS-1$
headers.put("Content-Type", Arrays.asList(
ContentType.APPLICATION_JSON.toContentTypeString())); //$NON-NLS-1$ //$NON-NLS-2$
if (this.executionContext != null) {
Expand Down
Expand Up @@ -79,6 +79,7 @@ public class ODataExecutionFactory extends ExecutionFactory<ConnectionFactory, W
private boolean supportsOdataCount;
private boolean supportsOdataSkip;
private boolean supportsOdataTop;
private boolean supportsUpdates = true;
private XMLMetadata serviceMatadata;

public ODataExecutionFactory() {
Expand All @@ -92,8 +93,8 @@ public ODataExecutionFactory() {
setSupportsOdataCount(true);
setSupportsOdataFilter(true);
setSupportsOdataOrderBy(true);
setSupportsOdataSkip(true);
setSupportsOdataTop(true);
setSupportsOdataSkip(false); // based on document based on cursoring, this will not be correct
setSupportsOdataTop(false);

registerFunctionModifier(SourceSystemFunctions.CONVERT, new AliasModifier("cast")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.LOCATE, new AliasModifier("indexof")); //$NON-NLS-1$
Expand Down Expand Up @@ -186,7 +187,11 @@ public ProcedureExecution createProcedureExecution(Call command,
public UpdateExecution createUpdateExecution(Command command,
ExecutionContext executionContext, RuntimeMetadata metadata,
WSConnection connection) throws TranslatorException {
return new ODataUpdateExecution(command, this, executionContext,metadata, connection);
if (supportsUpdates()) {
return new ODataUpdateExecution(command, this, executionContext,metadata, connection);
} else {
throw new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17030));
}
}

@Override
Expand Down Expand Up @@ -288,7 +293,18 @@ public boolean supportsOdataTop() {

public void setSupportsOdataTop(boolean supports) {
this.supportsOdataTop = supports;
}
}

@TranslatorProperty(display="Supports Updates",
description="True, if(PUT,PATCH,DELETE) operations supported",
advanced=true)
public boolean supportsUpdates() {
return supportsUpdates;
}

public void setSupportsUpdates(boolean supports) {
this.supportsUpdates = supports;
}

@Override
public boolean supportsCompareCriteriaEquals() {
Expand Down
Expand Up @@ -178,15 +178,16 @@ private String odataType(String type, String runtimeType) {
public void visit(ColumnReference obj) {
Column column = obj.getMetadataObject();
// check if the column on psedo column, then move it to the parent.
String pseudo = getPseudo(column);
String pseudo = ODataMetadataProcessor.getPseudo(column);

this.exprType.push(odataType(column.getNativeType(), column.getRuntimeType()));

ODataDocumentNode schemaElement = this.query.getSchemaElement((Table)column.getParent());
if (pseudo != null) {
try {
Table columnParent = (Table)column.getParent();
Table pseudoColumnParent = this.metadata.getTable(getMerge(columnParent));
Table pseudoColumnParent = this.metadata.getTable(
ODataMetadataProcessor.getMerge(columnParent));
schemaElement = this.query.getSchemaElement(pseudoColumnParent);
} catch (TranslatorException e) {
this.exceptions.add(e);
Expand All @@ -213,15 +214,7 @@ public void visit(ColumnReference obj) {
}
}
}

private String getPseudo(Column column) {
return column.getProperty(ODataMetadataProcessor.PSEUDO, false);
}

private String getMerge(Table table) {
return table.getProperty(ODataMetadataProcessor.MERGE, false);
}


protected boolean isInfixFunction(String function) {
return infixFunctions.containsKey(function);
}
Expand Down
Expand Up @@ -49,6 +49,7 @@
import org.apache.olingo.commons.api.edm.provider.CsdlSingleton;
import org.teiid.metadata.BaseColumn.NullType;
import org.teiid.metadata.Column;
import org.teiid.metadata.Column.SearchType;
import org.teiid.metadata.ExtensionMetadataProperty;
import org.teiid.metadata.KeyRecord;
import org.teiid.metadata.MetadataFactory;
Expand Down Expand Up @@ -269,6 +270,14 @@ private void addProperty(MetadataFactory mf, XMLMetadata metadata,
}
}

static String getPseudo(Column column) {
return column.getProperty(ODataMetadataProcessor.PSEUDO, false);
}

static String getMerge(Table table) {
return table.getProperty(ODataMetadataProcessor.MERGE, false);
}

static boolean isComplexType(Table table) {
ODataType type = ODataType.valueOf(table.getProperty(ODataMetadataProcessor.ODATA_TYPE, false));
return type == ODataType.COMPLEX || type == ODataType.COMPLEX_COLLECTION;
Expand Down Expand Up @@ -512,7 +521,6 @@ record = parentTable.getPrimaryKey();
Column c = mf.getSchema().getTable(childTable.getName()).getColumnByName(targetColumnName);
if (c == null) {
c = addColumn(mf, childTable, column, targetColumnName);
c.setSelectable(false);
c.setProperty(PSEUDO, column.getName());
} else {
targetColumnName = column.getName();
Expand All @@ -522,7 +530,6 @@ record = parentTable.getPrimaryKey();
Column c = mf.getSchema().getTable(childTable.getName()).getColumnByName(column.getName());
if (c == null) {
c = addColumn(mf, childTable, column, targetColumnName);
c.setSelectable(false);
c.setProperty(PSEUDO, column.getName());
} else {
targetColumnName = column.getName();
Expand Down Expand Up @@ -619,7 +626,14 @@ private Column addPropertyAsColumn(MetadataFactory mf, Table table,
c.setProperty("SRID", property.getSrid().toString());
}
if (!property.getType().equals("Edm.String")) {
c.setProperty("NATIVE_TYPE", property.getType());
if (property.isCollection()) {
c.setNativeType("Collection("+property.getType()+")");
} else {
c.setNativeType(property.getType());
}
}
if (property.getType().equals("Edm.String") && property.isCollection()) {
c.setNativeType("Collection("+property.getType()+")");
}
return c;
}
Expand Down Expand Up @@ -667,6 +681,9 @@ private Column buildColumn(MetadataFactory mf, Table table, CsdlProperty propert
String columnName = property.getName();
Column c = mf.addColumn(columnName, ODataTypeManager.teiidType(property.getType(),
property.isCollection()), table);
if(property.isCollection()) {
c.setSearchType(SearchType.Unsearchable);
}
c.setUpdatable(true);
return c;
}
Expand Down
Expand Up @@ -65,5 +65,6 @@ public static enum Event implements BundleUtil.Event{
TEIID17028,
TEIID17029,
TEIID17030,
TEIID17031
}
}
Expand Up @@ -133,10 +133,6 @@ else if (obj.getRightItem() instanceof Join) {
append(obj.getRightItem());
Table left = ((NamedTable)obj.getLeftItem()).getMetadataObject();
try {
if (ODataMetadataProcessor.isComplexType(left) ||
ODataMetadataProcessor.isNavigationType(left)) {
throw new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17027, left.getName()));
}
updated = this.odataQuery.addNavigation(obj.getCondition(), obj.getJoinType(), left);
obj.setCondition(updated);
if (updated != null) {
Expand Down Expand Up @@ -175,7 +171,6 @@ public void visit(Limit obj) {
this.odataQuery.setTop(new Integer(obj.getRowLimit()));
}
}


@Override
public void visit(OrderBy obj) {
Expand All @@ -188,7 +183,12 @@ public void visit(SortSpecification obj) {
this.orderBy.append(Tokens.COMMA);
}
ColumnReference column = (ColumnReference)obj.getExpression();
this.orderBy.append(column.getMetadataObject().getName());
try {
Column c = normalizePseudoColumn(column.getMetadataObject());
this.orderBy.append(c.getName());
} catch (TranslatorException e) {
this.exceptions.add(e);
}
// default is ascending
if (obj.getOrdering() == Ordering.DESC) {
this.orderBy.append(Tokens.SPACE).append(DESC.toLowerCase());
Expand All @@ -212,7 +212,11 @@ public void visit(DerivedColumn obj) {
this.exceptions.add(new TranslatorException(ODataPlugin.Util
.gs(ODataPlugin.Event.TEIID17006, column.getName())));
}
this.projectedColumns.add(column);
try {
this.projectedColumns.add(normalizePseudoColumn(column));
} catch (TranslatorException e) {
this.exceptions.add(e);
}
}
else if (obj.getExpression() instanceof AggregateFunction) {
AggregateFunction func = (AggregateFunction)obj.getExpression();
Expand All @@ -227,6 +231,21 @@ else if (obj.getExpression() instanceof AggregateFunction) {
this.exceptions.add(new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17008)));
}
}

private Column normalizePseudoColumn(Column column) throws TranslatorException {
String pseudo = ODataMetadataProcessor.getPseudo(column);
if (pseudo != null) {
try {
Table columnParent = (Table)column.getParent();
Table pseudoColumnParent = this.metadata.getTable(
ODataMetadataProcessor.getMerge(columnParent));
return pseudoColumnParent.getColumnByName(pseudo);
} catch (TranslatorException e) {
this.exceptions.add(e);
}
}
return column;
}

public void append(LanguageObject obj) {
visitNode(obj);
Expand Down

0 comments on commit 817b5e0

Please sign in to comment.