use of net.sf.jsqlparser.expression.operators.relational.MultiExpressionList in project herddb by diennea.
the class SQLPlanner method buildInsertStatement.
private ExecutionPlan buildInsertStatement(String defaultTableSpace, Insert s, boolean returnValues) throws StatementExecutionException {
String tableSpace = s.getTable().getSchemaName();
String tableName = s.getTable().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();
ItemsList itemlist = s.getItemsList();
if (itemlist instanceof ExpressionList) {
int index = 0;
List<Expression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
ExpressionList list = (ExpressionList) itemlist;
if (s.getColumns() != null) {
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);
}
Expression expression;
try {
expression = list.getExpressions().get(index++);
} catch (IndexOutOfBoundsException badQuery) {
throw new StatementExecutionException("bad number of VALUES in INSERT clause");
}
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(c);
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
} else {
for (Column column : table.columns) {
Expression expression = list.getExpressions().get(index++);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(new net.sf.jsqlparser.schema.Column(column.name));
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
}
RecordFunction keyfunction;
if (keyValueExpression.isEmpty() && table.auto_increment) {
keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
} else {
if (keyValueExpression.size() != table.primaryKey.length) {
throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
}
keyfunction = new SQLRecordKeyFunction(table, keyExpressionToColumn, keyValueExpression);
}
RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
try {
return ExecutionPlan.simple(new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues));
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
} else if (itemlist instanceof MultiExpressionList) {
if (returnValues) {
throw new StatementExecutionException("cannot 'return values' on multi-values insert");
}
MultiExpressionList multilist = (MultiExpressionList) itemlist;
List<InsertStatement> inserts = new ArrayList<>();
for (ExpressionList list : multilist.getExprList()) {
List<Expression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
int index = 0;
if (s.getColumns() != null) {
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);
}
Expression expression;
try {
expression = list.getExpressions().get(index++);
} catch (IndexOutOfBoundsException badQuery) {
throw new StatementExecutionException("bad number of VALUES in INSERT clause");
}
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(c);
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
} else {
for (Column column : table.columns) {
Expression expression = list.getExpressions().get(index++);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(expression);
}
valuesColumns.add(new net.sf.jsqlparser.schema.Column(column.name));
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
}
}
RecordFunction keyfunction;
if (keyValueExpression.isEmpty() && table.auto_increment) {
keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
} else {
if (keyValueExpression.size() != table.primaryKey.length) {
throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
}
keyfunction = new SQLRecordKeyFunction(table, keyExpressionToColumn, keyValueExpression);
}
RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
InsertStatement insert = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction);
inserts.add(insert);
}
try {
return ExecutionPlan.multiInsert(inserts);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
} else {
List<Expression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
Select select = s.getSelect();
ExecutionPlan datasource = buildSelectStatement(defaultTableSpace, select, true, -1);
if (s.getColumns() == null) {
throw new StatementExecutionException("for INSERT ... SELECT you have to declare the columns to be filled in (use INSERT INTO TABLE(c,c,c,) SELECT .....)");
}
IntHolder holder = new IntHolder(1);
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);
}
JdbcParameter readFromResultSetAsJdbcParameter = new JdbcParameter();
readFromResultSetAsJdbcParameter.setIndex(holder.value++);
if (table.isPrimaryKeyColumn(column.name)) {
keyExpressionToColumn.add(column.name);
keyValueExpression.add(readFromResultSetAsJdbcParameter);
}
valuesColumns.add(c);
valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, readFromResultSetAsJdbcParameter));
}
RecordFunction keyfunction;
if (keyValueExpression.isEmpty() && table.auto_increment) {
keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
} else {
if (keyValueExpression.size() != table.primaryKey.length) {
throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
}
keyfunction = new SQLRecordKeyFunction(table, keyExpressionToColumn, keyValueExpression);
}
RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
try {
return ExecutionPlan.dataManipulationFromSelect(new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues), datasource);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
}
}
use of net.sf.jsqlparser.expression.operators.relational.MultiExpressionList in project JSqlParser by JSQLParser.
the class InsertDeParser method visit.
@Override
public void visit(MultiExpressionList multiExprList) {
buffer.append(" VALUES ");
for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
buffer.append("(");
for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
Expression expression = iter.next();
expression.accept(expressionVisitor);
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (it.hasNext()) {
buffer.append(", ");
}
}
}
use of net.sf.jsqlparser.expression.operators.relational.MultiExpressionList in project JSqlParser by JSQLParser.
the class ReplaceDeParser method visit.
@Override
public void visit(MultiExpressionList multiExprList) {
buffer.append("VALUES ");
for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
buffer.append("(");
for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
Expression expression = iter.next();
expression.accept(expressionVisitor);
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (it.hasNext()) {
buffer.append(", ");
}
}
}
use of net.sf.jsqlparser.expression.operators.relational.MultiExpressionList in project JSqlParser by JSQLParser.
the class UpsertDeParser method visit.
@Override
public void visit(MultiExpressionList multiExprList) {
buffer.append(" VALUES ");
for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
buffer.append("(");
for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
Expression expression = iter.next();
expression.accept(expressionVisitor);
if (iter.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (it.hasNext()) {
buffer.append(", ");
}
}
}
use of net.sf.jsqlparser.expression.operators.relational.MultiExpressionList 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