use of net.sf.jsqlparser.expression.Expression 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;
}
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method findFiltersOnPrimaryKey.
private Expression findFiltersOnPrimaryKey(Table table, String tableAlias, Expression where) throws StatementExecutionException {
List<Expression> expressions = new ArrayList<>();
for (String pk : table.primaryKey) {
Expression condition = findConstraintExpressionOnColumn(where, pk, tableAlias, BinaryExpression.class);
if (condition == null) {
break;
}
expressions.add(condition);
}
if (expressions.isEmpty()) {
// no match at all, there is no direct constraint on PK
return null;
} else {
return composeSimpleAndExpressions(expressions);
}
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method findIndexAccess.
private static SQLRecordKeyFunction findIndexAccess(Expression where, String[] columnsToMatch, ColumnsList table, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
List<Expression> expressions = new ArrayList<>();
List<String> columns = new ArrayList<>();
for (String pk : columnsToMatch) {
Expression condition = findConstraintOnColumn(where, pk, tableAlias, expressionType);
if (condition == null) {
break;
}
columns.add(pk);
expressions.add(condition);
}
if (expressions.isEmpty()) {
// no match at all, there is no direct constraint on PK
return null;
}
return new SQLRecordKeyFunction(table, columns, expressions);
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method validateColumnConstaintToExpression.
private static Expression validateColumnConstaintToExpression(Expression testExpression, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
Expression result = null;
if (expressionType.isAssignableFrom(testExpression.getClass())) {
BinaryExpression e = (BinaryExpression) testExpression;
if (e.getLeftExpression() instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e.getLeftExpression();
boolean okAlias = true;
if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(tableAlias)) {
okAlias = false;
}
if (okAlias && columnName.equalsIgnoreCase(c.getColumnName()) && SQLRecordPredicate.isConstant(e.getRightExpression())) {
return e.getRightExpression();
}
} else if (e.getLeftExpression() instanceof AndExpression) {
result = findConstraintOnColumn(e.getLeftExpression(), columnName, tableAlias, expressionType);
if (result != null) {
return result;
}
} else if (e.getRightExpression() instanceof AndExpression) {
result = findConstraintOnColumn(e.getRightExpression(), columnName, tableAlias, expressionType);
if (result != null) {
return result;
}
}
}
return result;
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method findConstraintExpressionOnColumn.
private Expression findConstraintExpressionOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
if (where instanceof AndExpression) {
AndExpression and = (AndExpression) where;
Expression keyOnLeft = findConstraintExpressionOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
if (keyOnLeft != null) {
return keyOnLeft;
}
Expression keyOnRight = findConstraintExpressionOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
if (keyOnRight != null) {
return keyOnRight;
}
} else if (expressionType.isAssignableFrom(where.getClass())) {
Expression keyDirect = validateColumnConstaintExpressionToExpression(where, columnName, tableAlias, expressionType);
if (keyDirect != null) {
return keyDirect;
}
}
return null;
}
Aggregations