/* * (non-Javadoc) * * @see * com.google.code.shardbatis.converter.AbstractSqlConverter#doConvert(net * .sf.jsqlparser.statement.Statement, java.lang.Object, java.lang.String) */ @Override protected Statement doConvert(Statement statement, Object params, String mapperId) { if (!(statement instanceof Update)) { throw new IllegalArgumentException("The argument statement must is instance of Update."); } Update update = (Update) statement; String name = update.getTable().getName(); update.getTable().setName(this.convertTableName(name, params, mapperId)); return update; }
public void deParse(Update update) { buffer .append("UPDATE ") .append(PlainSelect.getStringList(update.getTables(), true, false)) .append(" SET "); for (int i = 0; i < update.getColumns().size(); i++) { Column column = update.getColumns().get(i); buffer.append(column.getFullyQualifiedName()).append(" = "); Expression expression = update.getExpressions().get(i); expression.accept(expressionVisitor); if (i < update.getColumns().size() - 1) { buffer.append(", "); } } if (update.getFromItem() != null) { buffer.append(" FROM ").append(update.getFromItem()); if (update.getJoins() != null) { for (Join join : update.getJoins()) { if (join.isSimple()) { buffer.append(", ").append(join); } else { buffer.append(" ").append(join); } } } } if (update.getWhere() != null) { buffer.append(" WHERE "); update.getWhere().accept(expressionVisitor); } }
public Result getResult() throws SQLException { // we must have a selected database Database selectedDatabase = conn.getSelectedDatabase(); if (selectedDatabase == null) { throw new SQLException("No database selected."); } // check the users permission // if(!conn.getUser().canCreateTable) // throw new SQLException("Permission denied. You must have the CREATE TABLE privilege."); // get schema Schema schema = selectedDatabase.getSchema("public"); if (schema == null) { throw new SQLException("No such schema " + schema.getName()); } // see if the table exists Table table = schema.getTable(sql.getTable().toString()); if (table == null) { // temporary table? TemporaryTable tt = conn.getTemporaryTable(sql.getTable().toString()); if (tt != null) { table = schema.getTable(tt.internalName); } if (table == null) { throw new SQLException( "Table " + schema.getName() + "." + sql.getTable().toString() + " does not exist"); } } // parse expression operations try { // extract WHERE clause, making sure it is not empty net.sf.jsqlparser.expression.Expression whereClause = sql.getWhere(); if (whereClause == null) { whereClause = new LongValue("1"); } // parse the expression net.eagledb.server.planner.Expression ex = new net.eagledb.server.planner.Expression(table, selectedDatabase, whereClause); PageOperation[] op = ex.parse(true); // validate the column names and create expressions net.eagledb.server.planner.Expression[] cols = new net.eagledb.server.planner.Expression[sql.getColumns().size()]; // ArrayList<PageOperation[]> colsop = new ArrayList<PageOperation[]>(); int[] columnID = new int[sql.getColumns().size()]; int i = 0; for (Object o : sql.getColumns()) { if (!table.attributeExists(o.toString())) { throw new SQLException("No such column '" + o + "'"); } columnID[i] = table.getAttributeLocation(o.toString()); cols[i] = new net.eagledb.server.planner.Expression( table, selectedDatabase, (net.sf.jsqlparser.expression.Expression) sql.getExpressions().get(i)); // colsop.add(cols[i].parse(false)); ++i; } // create the executation plan Plan p = new Plan(); // find limits int limitOffset = 0, limit = Integer.MAX_VALUE; /*if(sql.getLimit() != null) { limitOffset = (int) sql.getLimit().getOffset(); limit = (int) sql.getLimit().getRowCount(); }*/ p.addPlanItem( new FullTableScan( conn, table, 0, whereClause.toString(), op, ex.buffers, limitOffset, limit)); // execute plan p.executeUpdate(columnID, cols, conn.transactionID); return new Result(ResultCode.SUCCESS); } catch (ExpressionException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return new Result(ResultCode.FAILED); }