Java Code Examples for net.sf.jsqlparser.statement.update.Update#getWhere()

The following examples show how to use net.sf.jsqlparser.statement.update.Update#getWhere() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TablesNamesFinder.java    From evosql with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(Update update) {
    for (Table table : update.getTables()) {
        tables.add(table.getName());
    }
    if (update.getExpressions() != null) {
        for (Expression expression : update.getExpressions()) {
            expression.accept(this);
        }
    }

    if (update.getFromItem() != null) {
        update.getFromItem().accept(this);
    }

    if (update.getJoins() != null) {
        for (Join join : update.getJoins()) {
            join.getRightItem().accept(this);
        }
    }

    if (update.getWhere() != null) {
        update.getWhere().accept(this);
    }
}
 
Example 2
Source File: CRUDParseUtils.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
public static void parseUpdate(String sql, Table table, Entry entry, Condition condition)
        throws JSQLParserException, FrontException {
    Statement statement = CCJSqlParserUtil.parse(sql);
    Update update = (Update) statement;

    // parse table name
    List<net.sf.jsqlparser.schema.Table> tables = update.getTables();
    String tableName = tables.get(0).getName();
    table.setTableName(tableName);

    // parse cloumns
    List<Column> columns = update.getColumns();
    List<Expression> expressions = update.getExpressions();
    int size = expressions.size();
    String[] values = new String[size];
    for (int i = 0; i < size; i++) {
        values[i] = expressions.get(i).toString();
    }
    for (int i = 0; i < columns.size(); i++) {
        entry.put(trimQuotes(columns.get(i).toString()), trimQuotes(values[i]));
    }

    // parse where clause
    Expression where = update.getWhere();
    if (where != null) {
        BinaryExpression expr2 = (BinaryExpression) (where);
        handleExpression(condition, expr2);
    }
    Limit limit = update.getLimit();
    parseLimit(condition, limit);
}
 
Example 3
Source File: AbstractCloudSpannerStatement.java    From spanner-jdbc with MIT License 5 votes vote down vote up
/**
 * Transform the given UPDATE-statement into an "INSERT INTO TAB1 (...) SELECT ... FROM TAB1 WHERE
 * ... ON DUPLICATE KEY UPDATE"
 * 
 * @param update The UPDATE-statement
 * @return An SQL-statement equal to the UPDATE-statement but in INSERT form
 * @throws SQLException if a database exception occurs while getting the table meta data or if the
 *         statement tries to update the primary key value
 */
protected String createInsertSelectOnDuplicateKeyUpdateStatement(Update update)
    throws SQLException {
  String tableName = unquoteIdentifier(update.getTables().get(0).getName());
  TableKeyMetaData table = getConnection().getTable(tableName);
  List<String> keyColumns = table.getKeyColumns();
  List<String> updateColumns = update.getColumns().stream().map(Column::getColumnName)
      .map(String::toUpperCase).collect(Collectors.toList());
  List<String> quotedKeyColumns =
      keyColumns.stream().map(this::quoteIdentifier).collect(Collectors.toList());
  List<String> quotedAndQualifiedKeyColumns =
      keyColumns.stream().map(x -> quoteIdentifier(tableName) + "." + quoteIdentifier(x))
          .collect(Collectors.toList());

  List<String> quotedUpdateColumns =
      updateColumns.stream().map(this::quoteIdentifier).collect(Collectors.toList());
  List<String> expressions =
      update.getExpressions().stream().map(Object::toString).collect(Collectors.toList());
  if (updateColumns.stream().anyMatch(keyColumns::contains)) {
    String invalidCols =
        updateColumns.stream().filter(keyColumns::contains).collect(Collectors.joining());
    throw new CloudSpannerSQLException(
        "UPDATE of a primary key value is not allowed, cannot UPDATE the column(s) "
            + invalidCols,
        Code.INVALID_ARGUMENT);
  }

  StringBuilder res = new StringBuilder();
  res.append("INSERT INTO ").append(quoteIdentifier(tableName)).append("\n(");
  res.append(String.join(", ", quotedKeyColumns)).append(", ");
  res.append(String.join(", ", quotedUpdateColumns)).append(")");
  res.append("\nSELECT ").append(String.join(", ", quotedAndQualifiedKeyColumns)).append(", ");
  res.append(String.join(", ", expressions));
  res.append("\nFROM ").append(quoteIdentifier(tableName));
  if (update.getWhere() != null)
    res.append("\n").append("WHERE ").append(update.getWhere().toString());
  res.append("\nON DUPLICATE KEY UPDATE");

  return res.toString();
}
 
Example 4
Source File: TableRenameVisitor.java    From compass with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(Update update)
{
    for (Table table : update.getTables())
    {
    	table.accept(this);
    }
    if (update.getExpressions() != null)
    {
        for (Expression expression : update.getExpressions())
        {
            expression.accept(this);
        }
    }

    if (update.getFromItem() != null)
    {
        update.getFromItem().accept(this);
    }

    if (update.getJoins() != null)
    {
        for (Join join : update.getJoins())
        {
            join.getRightItem().accept(this);
        }
    }

    if (update.getWhere() != null)
    {
        update.getWhere().accept(this);
    }
}