use of org.teiid.language.Literal in project teiid by teiid.
the class AddDiffModifier method translate.
@Override
public List<?> translate(Function function) {
if (add) {
// $NON-NLS-1$
function.setName("dateadd");
} else {
// $NON-NLS-1$
function.setName("datediff");
}
Literal intervalType = (Literal) function.getParameters().get(0);
String interval = ((String) intervalType.getValue()).toUpperCase();
String newInterval = INTERVAL_MAP.get(interval);
if (newInterval != null) {
intervalType.setValue(newInterval);
return null;
}
if (supportsQuarter && interval.equals(NonReserved.SQL_TSI_QUARTER)) {
// $NON-NLS-1$
intervalType.setValue("QUARTER");
return null;
}
if (add) {
if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
// $NON-NLS-1$
intervalType.setValue("MILLISECOND");
Expression[] args = new Expression[] { function.getParameters().get(1), factory.createLiteral(1000000, TypeFacility.RUNTIME_TYPES.INTEGER) };
// $NON-NLS-1$
function.getParameters().set(1, factory.createFunction("/", args, TypeFacility.RUNTIME_TYPES.INTEGER));
} else if (interval.equals(NonReserved.SQL_TSI_QUARTER)) {
intervalType.setValue(ExtractFunctionModifier.DAY);
Expression[] args = new Expression[] { function.getParameters().get(1), factory.createLiteral(91, TypeFacility.RUNTIME_TYPES.INTEGER) };
// $NON-NLS-1$
function.getParameters().set(1, factory.createFunction("*", args, TypeFacility.RUNTIME_TYPES.INTEGER));
} else {
intervalType.setValue(ExtractFunctionModifier.DAY);
Expression[] args = new Expression[] { function.getParameters().get(1), factory.createLiteral(7, TypeFacility.RUNTIME_TYPES.INTEGER) };
// $NON-NLS-1$
function.getParameters().set(1, factory.createFunction("*", args, TypeFacility.RUNTIME_TYPES.INTEGER));
}
return null;
}
if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
// $NON-NLS-1$
intervalType.setValue("MILLISECOND");
// $NON-NLS-1$
return Arrays.asList(function, " * 1000000");
} else if (interval.equals(NonReserved.SQL_TSI_QUARTER)) {
intervalType.setValue(ExtractFunctionModifier.DAY);
// $NON-NLS-1$
return Arrays.asList(function, " / 91");
}
intervalType.setValue(ExtractFunctionModifier.DAY);
// $NON-NLS-1$
return Arrays.asList(function, " / 7");
}
use of org.teiid.language.Literal in project teiid by teiid.
the class MySQL5ExecutionFactory method start.
@Override
public void start() throws TranslatorException {
super.start();
registerFunctionModifier(SourceSystemFunctions.CHAR, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
// $NON-NLS-1$ //$NON-NLS-2$
return Arrays.asList("char(", function.getParameters().get(0), " USING ASCII)");
}
});
registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Literal intervalType = (Literal) function.getParameters().get(0);
String interval = ((String) intervalType.getValue()).toUpperCase();
if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
// $NON-NLS-1$
intervalType.setValue("MICROSECOND");
Expression[] args = new Expression[] { function.getParameters().get(1), getLanguageFactory().createLiteral(1000, TypeFacility.RUNTIME_TYPES.INTEGER) };
// $NON-NLS-1$
function.getParameters().set(1, getLanguageFactory().createFunction("/", args, TypeFacility.RUNTIME_TYPES.INTEGER));
}
return null;
}
});
// $NON-NLS-1$ //$NON-NLS-2$
addPushDownFunction("mysql", "timestampdiff", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.TIMESTAMP, TypeFacility.RUNTIME_NAMES.TIMESTAMP);
registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
Literal intervalType = (Literal) function.getParameters().get(0);
String interval = ((String) intervalType.getValue()).toUpperCase();
if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
// $NON-NLS-1$
intervalType.setValue("MICROSECOND");
// $NON-NLS-1$
return Arrays.asList(function, " * 1000");
}
return null;
}
});
// $NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.ST_SRID, new AliasModifier("SRID"));
}
use of org.teiid.language.Literal in project teiid by teiid.
the class ConcatFunctionModifier method translate.
@Override
public List<?> translate(Function function) {
Expression a = function.getParameters().get(0);
Expression b = function.getParameters().get(1);
List<Condition> crits = new ArrayList<Condition>();
Literal nullValue = langFactory.createLiteral(null, TypeFacility.RUNTIME_TYPES.STRING);
if (isNull(a)) {
return Arrays.asList(nullValue);
} else if (!isNotNull(a)) {
crits.add(langFactory.createIsNullCriteria(a, false));
}
if (isNull(b)) {
return Arrays.asList(nullValue);
} else if (!isNotNull(b)) {
crits.add(langFactory.createIsNullCriteria(b, false));
}
Condition crit = null;
if (crits.isEmpty()) {
return null;
} else if (crits.size() == 1) {
crit = crits.get(0);
} else {
crit = langFactory.createAndOr(Operator.OR, crits.get(0), crits.get(1));
}
List<SearchedWhenClause> cases = Arrays.asList(langFactory.createSearchedWhenCondition(crit, nullValue));
return Arrays.asList(langFactory.createSearchedCaseExpression(cases, function, TypeFacility.RUNTIME_TYPES.STRING));
}
use of org.teiid.language.Literal in project teiid by teiid.
the class DirectSpreadsheetQueryExecution method execute.
@Override
public void execute() throws TranslatorException {
String worksheet = null;
Integer limit = null;
Integer offset = null;
String toQuery = query;
List<String> parts = StringUtil.tokenize(query, ';');
for (String var : parts) {
int index = var.indexOf('=');
if (index == -1) {
continue;
}
String key = var.substring(0, index).trim();
String value = var.substring(index + 1).trim();
if (key.equalsIgnoreCase(WORKSHEET)) {
worksheet = value;
} else if (key.equalsIgnoreCase(QUERY)) {
StringBuilder buffer = new StringBuilder();
SQLStringVisitor.parseNativeQueryParts(value, arguments, buffer, new SQLStringVisitor.Substitutor() {
@Override
public void substitute(Argument arg, StringBuilder builder, int index) {
Literal argumentValue = arg.getArgumentValue();
SpreadsheetSQLVisitor visitor = new SpreadsheetSQLVisitor(connection.getSpreadsheetInfo());
visitor.visit(argumentValue);
builder.append(visitor.getTranslatedSQL());
}
});
toQuery = buffer.toString();
} else if (key.equalsIgnoreCase(LIMIT)) {
limit = Integer.parseInt(value);
} else if (key.equalsIgnoreCase(OFFEST)) {
offset = Integer.parseInt(value);
}
}
this.rowIterator = this.connection.executeQuery(worksheet, toQuery, offset, limit, executionContext.getBatchSize()).iterator();
}
use of org.teiid.language.Literal in project teiid by teiid.
the class TestMongoDBDirectQueryExecution method testDirect.
@Test
public void testDirect() throws Exception {
Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
DBCollection dbCollection = Mockito.mock(DBCollection.class);
DB db = Mockito.mock(DB.class);
Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
Mockito.stub(connection.getDatabase()).toReturn(db);
AggregationOutput output = Mockito.mock(AggregationOutput.class);
Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
Argument arg = new Argument(Direction.IN, null, String.class, null);
arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
Argument arg2 = new Argument(Direction.IN, null, String.class, null);
arg2.setArgumentValue(new Literal("foo", String.class));
ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
execution.execute();
List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Aggregations