use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method buildUpdateStatement.
private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update s, boolean returnValues) throws StatementExecutionException {
if (s.getTables().size() != 1) {
throw new StatementExecutionException("unsupported multi-table update " + s);
}
net.sf.jsqlparser.schema.Table fromTable = s.getTables().get(0);
String tableSpace = fromTable.getSchemaName();
String tableName = fromTable.getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
}
Table table = tableManager.getTable();
for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
}
if (table.isPrimaryKeyColumn(c.getColumnName())) {
throw new StatementExecutionException("updates of fields on the PK (" + Arrays.toString(table.primaryKey) + ") are not supported. Please perform a DELETE and than an INSERT");
}
}
List<CompiledSQLExpression> compiledSQLExpressions = new ArrayList<>();
for (Expression e : s.getExpressions()) {
compiledSQLExpressions.add(SQLExpressionCompiler.compileExpression(null, e));
}
RecordFunction function = new SQLRecordFunction(table, s.getColumns(), compiledSQLExpressions);
// Perform a scan and then update each row
SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
if (where != null) {
Expression expressionWhere = s.getWhere();
discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
}
DMLStatement st = new UpdateStatement(tableSpace, tableName, null, function, where).setReturnValues(returnValues);
return ExecutionPlan.simple(st);
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method buildDeleteStatement.
private ExecutionPlan buildDeleteStatement(String defaultTableSpace, Delete s, boolean returnValues) throws StatementExecutionException {
net.sf.jsqlparser.schema.Table fromTable = s.getTable();
String tableSpace = fromTable.getSchemaName();
String tableName = fromTable.getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
}
Table table = tableManager.getTable();
// Perform a scan and then delete each row
SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
if (where != null) {
Expression expressionWhere = s.getWhere();
discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
}
DMLStatement st = new DeleteStatement(tableSpace, tableName, null, where).setReturnValues(returnValues);
return ExecutionPlan.simple(st);
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLPlanner method findConstraintOnColumn.
private static Expression findConstraintOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
if (where instanceof AndExpression) {
AndExpression and = (AndExpression) where;
Expression keyOnLeft = findConstraintOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
if (keyOnLeft != null) {
return keyOnLeft;
}
Expression keyOnRight = findConstraintOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
if (keyOnRight != null) {
return keyOnRight;
}
} else if (expressionType.isAssignableFrom(where.getClass())) {
Expression keyDirect = validateColumnConstaintToExpression(where, columnName, tableAlias, expressionType);
if (keyDirect != null) {
return keyDirect;
}
}
return null;
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLProjection method addExpressionsForFunctionArguments.
private static void addExpressionsForFunctionArguments(ColumnReferencesDiscovery discovery, List<OutputColumn> output, Table table) throws StatementExecutionException {
List<net.sf.jsqlparser.schema.Column> columns = discovery.getColumnsByTable().get(table.name);
if (columns != null) {
for (Expression e : columns) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e;
String columnName = c.getColumnName();
boolean found = false;
for (OutputColumn outputColumn : output) {
if (columnName.equalsIgnoreCase(outputColumn.column.name)) {
found = true;
break;
} else if (outputColumn.directColumnReference != null && outputColumn.directColumnReference.getColumnName().equalsIgnoreCase(columnName)) {
found = true;
break;
}
}
if (!found) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("invalid column name " + c.getColumnName());
}
output.add(new OutputColumn(null, Column.column(columnName, column.type), c, null));
}
}
}
}
use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class CompiledInExpression method create.
public static CompiledInExpression create(InExpression in, String validatedTableAlias) {
if (in.getLeftItemsList() != null) {
throw new StatementExecutionException("Unsupported operand " + in.getClass() + " with a non-expression left argument (" + in + ")");
}
CompiledSQLExpression left = compileExpression(validatedTableAlias, in.getLeftExpression());
if (left == null) {
return null;
}
if (in.getRightItemsList() instanceof ExpressionList) {
List<CompiledSQLExpression> expList = new ArrayList<>();
ExpressionList exps = (ExpressionList) in.getRightItemsList();
for (Expression exp : exps.getExpressions()) {
CompiledSQLExpression newExp = compileExpression(validatedTableAlias, exp);
if (newExp == null) {
return null;
}
expList.add(newExp);
}
return new CompiledInExpression(in.isNot(), left, expList, null);
}
if (in.getRightItemsList() instanceof SubSelect) {
SubSelect ss = (SubSelect) in.getRightItemsList();
if (!(ss.getSelectBody() instanceof PlainSelect)) {
throw new StatementExecutionException("unsupported operand " + in.getClass() + " with subquery of type " + ss.getClass() + "(" + ss + ")");
}
return new CompiledInExpression(in.isNot(), left, null, ss);
}
throw new StatementExecutionException("unsupported operand " + in.getClass() + " with argument of type " + in.getRightItemsList().getClass() + "(" + in + ")");
}
Aggregations