public class Upsert extends MySqlPart

Generates an upsert statement for MySQL.


Sample Generated SQL:

INSERT INTO `tableName` (`col1`, `col2`)
VALUES (val1, val2),
       (val1, val2,
       (val1, val2)
ON DUPLICATE KEY UPDATE `col1` = VALUES(`col1`), `col2` = VALUES(`col2`);
  • Constructor Details

    • Upsert

      public Upsert(String table)
  • Method Details

    • setPretty

      public Upsert setPretty(boolean pretty)
      Indicates if line breaks and indentations should be added to output upsert statement (default false).

      Example output (pretty = true):
      INSERT INTO `tableName` (`col1`, `col2`)
      VALUES (val1, val2),
             (val1, val2),
             (val1, val2)
      ON DUPLICATE KEY UPDATE `col1` = VALUES(`col1`), `col2` = VALUES(`col2`);

      Example output (pretty = false):
      INSERT INTO `tableName` (`col1`, `col2`) VALUES (val1, val2), (val1, val2), (val1, val2) ON DUPLICATE KEY UPDATE `col1` = VALUES(`col1`), `col2` = VALUES(`col2`);
      Overrides:
      setPretty in class SqlPart
    • getTable

      public String getTable()
    • nextRow

      public Upsert nextRow()
      Starts a new row for subsequent add(String, Object) calls; the first row already exists after construction.
    • add

      public Upsert add(String columnName, Object value)
      Adds a column value (an SQL literal, written as-is) to the current row; only the first row's column names are used.
    • getRows

      public List<InsertRow> getRows()
    • getLastRow

      public InsertRow getLastRow()
    • collectSqlFragment

      public void collectSqlFragment(CodeWriter writer)
      Description copied from class: SqlPart
      Writes this part's SQL to the writer; used by SqlPart.getSqlFragment() and by enclosing parts.
      Specified by:
      collectSqlFragment in class SqlPart