public class Upsert extends H2SqlPart

Generates an upsert statement for H2.


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)
      Constructs an Upsert for the specified table.
      Parameters:
      table - the table name
  • 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()
      Returns the table name.
      Returns:
      the table name
    • nextRow

      public Upsert nextRow()
      Starts a new row for upserting.
      Returns:
      this instance
    • add

      public Upsert add(String columnName, Object value)
      Adds a column-value pair to the current row.
      Parameters:
      columnName - the column name
      value - the value to upsert
      Returns:
      this instance
    • getRows

      public List<InsertRow> getRows()
      Returns the list of rows.
      Returns:
      the list of rows
    • getLastRow

      public InsertRow getLastRow()
      Returns the last row.
      Returns:
      the last row
    • 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