use of net.sf.jsqlparser.expression.StringValue in project herddb by diennea.
the class SQLExpressionCompiler method compileExpression.
// this method never returns NULL
public static CompiledSQLExpression compileExpression(String validatedTableAlias, Expression exp) {
if (exp instanceof BinaryExpression) {
CompiledSQLExpression compiled = compileSpecialBinaryExpression(validatedTableAlias, exp);
if (compiled != null) {
return compiled;
}
}
if (exp instanceof net.sf.jsqlparser.schema.Column) {
return compileColumnExpression(validatedTableAlias, exp);
} else if (exp instanceof StringValue) {
return new ConstantExpression(RawString.of(((StringValue) exp).getValue()));
} else if (exp instanceof LongValue) {
try {
return new ConstantExpression(((LongValue) exp).getValue());
} catch (NumberFormatException largeNumber) {
return new ConstantExpression(Double.valueOf(((LongValue) exp).getStringValue()));
}
} else if (exp instanceof DoubleValue) {
return new ConstantExpression(((DoubleValue) exp).getValue());
} else if (exp instanceof TimestampValue) {
return new ConstantExpression(((TimestampValue) exp).getValue());
} else if (exp instanceof NullValue) {
return new ConstantExpression(null);
} else if (exp instanceof TimeKeyExpression) {
TimeKeyExpression ext = (TimeKeyExpression) exp;
if (CURRENT_TIMESTAMP.equalsIgnoreCase(ext.getStringValue())) {
return new ConstantExpression(new java.sql.Timestamp(System.currentTimeMillis()));
} else {
throw new StatementExecutionException("unhandled expression " + exp);
}
} else if (exp instanceof JdbcParameter) {
int index = ((JdbcParameter) exp).getIndex() - 1;
return new JdbcParameterExpression(index);
} else if (exp instanceof AndExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAndExpression(a, b, c));
} else if (exp instanceof OrExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledOrExpression(a, b, c));
} else if (exp instanceof Function) {
return CompiledFunction.create((Function) exp, validatedTableAlias);
} else if (exp instanceof Addition) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAddExpression(a, b, c));
} else if (exp instanceof Subtraction) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledSubtractExpression(a, b, c));
} else if (exp instanceof Multiplication) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMultiplyExpression(a, b, c));
} else if (exp instanceof Division) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledDivideExpression(a, b, c));
} else if (exp instanceof Parenthesis) {
Parenthesis p = (Parenthesis) exp;
CompiledSQLExpression inner = compileExpression(validatedTableAlias, p.getExpression());
return new CompiledParenthesisExpression(p.isNot(), inner);
} else if (exp instanceof EqualsTo) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledEqualsExpression(a, b, c));
} else if (exp instanceof NotEqualsTo) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledNotEqualsExpression(a, b, c));
} else if (exp instanceof MinorThan) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenExpression(a, b, c));
} else if (exp instanceof MinorThanEquals) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenEqualsExpression(a, b, c));
} else if (exp instanceof GreaterThan) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenExpression(a, b, c));
} else if (exp instanceof GreaterThanEquals) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenEqualsExpression(a, b, c));
} else if (exp instanceof LikeExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledLikeExpression(a, b, c));
} else if (exp instanceof Between) {
return CompiledBetweenExpression.create(validatedTableAlias, (Between) exp);
} else if (exp instanceof SignedExpression) {
SignedExpression s = (SignedExpression) exp;
CompiledSQLExpression inner = compileExpression(validatedTableAlias, s.getExpression());
return new CompiledSignedExpression(s.getSign(), inner);
} else if (exp instanceof InExpression) {
InExpression in = (InExpression) exp;
return CompiledInExpression.create(in, validatedTableAlias);
} else if (exp instanceof IsNullExpression) {
IsNullExpression i = (IsNullExpression) exp;
CompiledSQLExpression left = compileExpression(validatedTableAlias, i.getLeftExpression());
return new CompiledIsNullExpression(i.isNot(), left);
} else if (exp instanceof CaseExpression) {
return CompiledCaseExpression.create(validatedTableAlias, (CaseExpression) exp);
}
throw new StatementExecutionException("unsupported operand " + exp.getClass() + ", expression is " + exp);
}
use of net.sf.jsqlparser.expression.StringValue in project herddb by diennea.
the class HerdDBCLI method rewriteQuery.
private static QueryWithParameters rewriteQuery(String query, TableSpaceMapper mapper, boolean frommysqldump) throws ScriptException {
try {
List<Object> parameters = new ArrayList<>();
if (frommysqldump && query.startsWith("INSERT INTO")) {
// this is faster than CCJSqlParserUtil and will allow the cache to work at "client-side" too
QueryWithParameters rewriteSimpleInsertStatement = MySqlDumpInsertStatementRewriter.rewriteSimpleInsertStatement(query);
if (rewriteSimpleInsertStatement != null) {
query = rewriteSimpleInsertStatement.query;
parameters.addAll(rewriteSimpleInsertStatement.jdbcParameters);
String schema = mapper == null ? null : mapper.getTableSpace(rewriteSimpleInsertStatement.tableName);
return new QueryWithParameters(query, rewriteSimpleInsertStatement.tableName, parameters, schema);
}
}
String _query = query;
net.sf.jsqlparser.statement.Statement stmt = PARSER_CACHE.get(_query, () -> {
return CCJSqlParserUtil.parse(_query);
});
if (stmt instanceof Insert) {
boolean somethingdone = false;
Insert insert = (Insert) stmt;
ItemsList itemlist = insert.getItemsList();
if (itemlist instanceof ExpressionList) {
ExpressionList list = (ExpressionList) itemlist;
List<Expression> expressions = list.getExpressions();
for (int i = 0; i < expressions.size(); i++) {
Expression e = expressions.get(i);
boolean done = false;
if (e instanceof StringValue) {
StringValue sv = (StringValue) e;
parameters.add(sv.getValue());
done = true;
} else if (e instanceof LongValue) {
LongValue sv = (LongValue) e;
parameters.add(sv.getValue());
done = true;
} else if (e instanceof NullValue) {
NullValue sv = (NullValue) e;
parameters.add(null);
done = true;
} else if (e instanceof TimestampValue) {
TimestampValue sv = (TimestampValue) e;
parameters.add(sv.getValue());
done = true;
} else if (e instanceof DoubleValue) {
DoubleValue sv = (DoubleValue) e;
parameters.add(sv.getValue());
done = true;
}
if (done) {
somethingdone = true;
expressions.set(i, new JdbcParameter());
}
}
if (somethingdone) {
StringBuilder queryResult = new StringBuilder();
InsertDeParser deparser = new InsertDeParser(new ExpressionDeParser(null, queryResult), null, queryResult);
deparser.deParse(insert);
query = queryResult.toString();
}
} else if (itemlist instanceof MultiExpressionList) {
MultiExpressionList mlist = (MultiExpressionList) itemlist;
List<ExpressionList> lists = mlist.getExprList();
for (ExpressionList list : lists) {
List<Expression> expressions = list.getExpressions();
for (int i = 0; i < expressions.size(); i++) {
Expression e = expressions.get(i);
boolean done = false;
if (e instanceof StringValue) {
StringValue sv = (StringValue) e;
parameters.add(sv.getValue());
done = true;
} else if (e instanceof LongValue) {
LongValue sv = (LongValue) e;
parameters.add(sv.getValue());
done = true;
} else if (e instanceof NullValue) {
NullValue sv = (NullValue) e;
parameters.add(null);
done = true;
} else if (e instanceof TimestampValue) {
TimestampValue sv = (TimestampValue) e;
parameters.add(sv.getValue());
done = true;
} else if (e instanceof DoubleValue) {
DoubleValue sv = (DoubleValue) e;
parameters.add(sv.getValue());
done = true;
}
if (done) {
somethingdone = true;
expressions.set(i, new JdbcParameter());
}
}
}
if (somethingdone) {
StringBuilder queryResult = new StringBuilder();
InsertDeParser deparser = new InsertDeParser(new ExpressionDeParser(null, queryResult), null, queryResult);
deparser.deParse(insert);
query = queryResult.toString();
}
}
String schema = mapper == null ? null : mapper.getTableSpace(stmt);
return new QueryWithParameters(query, null, parameters, schema);
} else {
String schema = mapper == null ? null : mapper.getTableSpace(stmt);
return new QueryWithParameters(query, null, Collections.emptyList(), schema);
}
} catch (ExecutionException err) {
System.out.println("error for query: " + query + " -> " + err.getCause());
return null;
}
}
Aggregations