Skip to content

Instantly share code, notes, and snippets.

@Tom9729
Created January 8, 2015 05:43
Show Gist options
  • Save Tom9729/2e73037eff85fe9eddff to your computer and use it in GitHub Desktop.
Save Tom9729/2e73037eff85fe9eddff to your computer and use it in GitHub Desktop.
public List<?> translate(LanguageObject obj, ExecutionContext context) {
List<?> parts = null;
if (obj instanceof Function) {
Function function = (Function)obj;
if (functionModifiers != null) {
String name = function.getName();
while (true) {
FunctionModifier modifier = functionModifiers.get(name);
if (modifier != null) {
parts = modifier.translate(function);
break;
}
int index = name.indexOf('.');
if (index < 0 || index == name.length() - 1) {
break;
}
name = name.substring(index + 1);
}
}
} else if (obj instanceof Command) {
parts = translateCommand((Command)obj, context);
} else if (obj instanceof Limit) {
parts = translateLimit((Limit)obj, context);
} else if (obj instanceof ColumnReference) {
ColumnReference elem = (ColumnReference)obj;
if (isTrimStrings() && elem.getType() == TypeFacility.RUNTIME_TYPES.STRING && elem.getMetadataObject() != null
&& ("char".equalsIgnoreCase(elem.getMetadataObject().getNativeType()) || "nchar".equalsIgnoreCase(elem.getMetadataObject().getNativeType()))) { //$NON-NLS-1$ //$NON-NLS-2$
return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.RTRIM, new Expression[] {elem}, TypeFacility.RUNTIME_TYPES.STRING));
}
} else if (obj instanceof DerivedColumn) {
DerivedColumn dc = (DerivedColumn) obj;
Expression expr = dc.getExpression();
// For GEOMETRY SELECT expressions: force source to do
// conversion from proprietary format to SRID + WKB blob.
if (expr.getType() == TypeFacility.RUNTIME_TYPES.GEOMETRY) {
return translateGeometrySelect(dc);
}
} else if (obj instanceof Literal) {
Literal l = (Literal) obj;
// For GEOMETRY literals: force source to do conversion
// from WKB blob + SRID to proprietary format.
if (l.getType() == TypeFacility.RUNTIME_TYPES.GEOMETRY) {
return translateGeometryLiteral(l);
}
}
return parts;
}
public List<?> translateGeometryLiteral(Literal l) {
GeometryType geom = (GeometryType) l.getValue();
return Arrays.asList(
getLanguageFactory().createFunction(SourceSystemFunctions.ST_GEOMFROMWKB,
new Expression[] {
new Literal(geom, TypeFacility.RUNTIME_TYPES.BLOB),
new Literal(geom.getSrid(), TypeFacility.RUNTIME_TYPES.INTEGER)
},
TypeFacility.RUNTIME_TYPES.GEOMETRY
));
}
public List<?> translateGeometrySelect(DerivedColumn dc) {
return Arrays.asList(
new DerivedColumn(
dc.getAlias(),
getLanguageFactory().createFunction(SourceSystemFunctions.ST_ASTEIIDFORMAT,
new Expression[] {
dc.getExpression()
},
TypeFacility.RUNTIME_TYPES.GEOMETRY
)
));
}
public GeometryType retrieveGeometryValue(Object value) {
return null;
}
@Tom9729
Copy link
Author

Tom9729 commented Jan 8, 2015

RetrieveGeometryValue could be overridden in one of the sub-translators if a special strategy is required (like reading EWKB or doing some reflection magic with vendor classes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment