Top Description Methods
org.jooq

public Interface InsertQuery<R extends org.jooq.Record>

extends StoreQuery<R extends Record>, Insert<R extends Record>, ConditionProvider
Known Direct Implementers
org.jooq.impl.InsertQueryImpl
Type Parameters
<R>
The record type of the table being inserted into
Static Imports
org.jooq.SQLDialect.CUBRID, .SQLDialect.DERBY, .SQLDialect.FIREBIRD, .SQLDialect.H2, .SQLDialect.HSQLDB, .SQLDialect.MARIADB, .SQLDialect.MYSQL, .SQLDialect.POSTGRES, .SQLDialect.SQLITE, .SQLDialect.YUGABYTEDB

Deprecated

as a consequence of ConditionProvider being deprecated. See corresponding docs for further information.

An INSERT statement (model API).

This type is the model API representation of a Insert statement, which can be mutated after creation. The advantage of this API compared to the DSL API is a more simple approach to writing dynamic SQL.

Instances can be created using DSLContext#insertQuery(Table) and overloads.

Author
Lukas Eder

Method Summary

Modifier and TypeMethod and Description
public void
addConditions(Condition
The condition
condition
)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated as a consequence of Condition being deprecated.
Adds a new condition to the query, connecting it to existing conditions with Operator#AND.
public void
addConditions(Condition...
The condition
conditions
)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated as a consequence of Condition being deprecated.
Adds new conditions to the query, connecting them to existing conditions with Operator#AND.
public void
addConditions(Collection<? extends Condition>
The condition
conditions
)

Redeclares org.jooq.ConditionProvider.addConditions.

Adds new conditions to the query, connecting them to existing conditions with Operator#AND.
public void
addConditions(Operator operator, Condition
The condition
condition
)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated as a consequence of Condition being deprecated.
Adds a new condition to the query, connecting it to existing conditions with the provided operator.
public void
addConditions(Operator operator, Condition...
The condition
conditions
)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated as a consequence of Condition being deprecated.
Adds new conditions to the query, connecting them to existing conditions with the provided operator.
public void
addConditions(Operator operator, Collection<? extends Condition>
The condition
conditions
)

Redeclares org.jooq.ConditionProvider.addConditions.

Adds new conditions to the query, connecting them to existing conditions with the provided operator.
public void
addRecord(R
The record to add to this insert statement.
record
)

Short for calling newRecord(); setRecord(record);

public <T> void
addValueForUpdate(Field<T> field, T value)

Deprecated as a consequence of Field being deprecated.
Add a value to the ON DUPLICATE KEY UPDATE clause of this INSERT statement, where this is supported.
public <T> void
addValueForUpdate(Field<T> field, Field<T> value)

Deprecated as a consequence of Field being deprecated.
Add a value to the ON DUPLICATE KEY UPDATE clause of this INSERT statement, where this is supported.
public void
addValuesForUpdate(Map<?, ?> map)

Add multiple values to the ON DUPLICATE KEY UPDATE clause of this INSERT statement, where this is supported.

public R
getReturnedRecord()

Redeclares org.jooq.StoreQuery.getReturnedRecord.

The record holding returned values as specified by any of the setReturning() methods.
public @NotNull Result<R>
getReturnedRecords()

Redeclares org.jooq.StoreQuery.getReturnedRecords.

The records holding returned values as specified by any of the setReturning() methods.
public void
newRecord()

Adds a new Record to the insert statement for multi-record inserts

Calling this method will cause subsequent calls to addValue(Field, Object) (and similar) to fill the next record.

public void
onConflict(Field<?>... fields)

Deprecated as a consequence of Field being deprecated.
Whether a ON CONFLICT clause should be added to this INSERT statement.
public void
onConflict(Collection<? extends Field<?>> fields)

Whether a ON CONFLICT clause should be added to this INSERT statement.

public void
onConflictOnConstraint(Name constraint)

Whether use a ON CONFLICT or ON CONFLICT ON CONSTRAINT clause in this INSERT statement.

public void
onConflictOnConstraint(Constraint constraint)

Deprecated as a consequence of Constraint being deprecated.
Whether use a ON CONFLICT or ON CONFLICT ON CONSTRAINT clause in this INSERT statement.
public void
onConflictOnConstraint(UniqueKey<R> constraint)

Whether use a ON CONFLICT or ON CONFLICT ON CONSTRAINT clause in this INSERT statement.

public void
onConflictWhere(Condition
The condition
condition
)

Deprecated as a consequence of Condition being deprecated.
Adds a new condition the onConflict(Field...) clause.
public void
onDuplicateKeyIgnore(boolean flag)

Whether an ON DUPLICATE KEY IGNORE clause should be added to this INSERT statement.

public void
onDuplicateKeyUpdate(boolean flag)

Whether a ON DUPLICATE KEY UPDATE clause should be added to this INSERT statement.

public void
setDefaultValues()

Set an empty record with the DEFAULT VALUES clause.

public void
setReturning()

Redeclares org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return all fields in R.
public void
setReturning(Identity<R, ?>
The table's identity
identity
)

Redeclares org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return the generated identity value.
public void
setReturning(SelectFieldOrAsterisk...
Fields to be returned
fields
)

Redeclares org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return a list of fields in R.
public void
setReturning(Collection<? extends SelectFieldOrAsterisk>
Fields to be returned
fields
)

Redeclares org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return a list of fields in R.
public void
setSelect(Field<?>[] fields, Select<?> select)

Deprecated as a consequence of Field being deprecated.
Use a SELECT statement as the source of values for the INSERT statement.
public void
setSelect(Collection<? extends Field<?>> fields, Select<?> select)

Use a SELECT statement as the source of values for the INSERT statement.

Inherited from org.jooq.StoreQuery:
addValueaddValueaddValuesgetResultsetRecord

Method Detail

addConditionsback to summary
public void addConditions(Condition condition)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated

as a consequence of Condition being deprecated. See corresponding docs for further information.

Adds a new condition to the query, connecting it to existing conditions with Operator#AND.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
condition:Condition

The condition

Annotations
@Override
@Support:CUBRID, DERBY, H2, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
addConditionsback to summary
public void addConditions(Condition... conditions)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated

as a consequence of Condition being deprecated. See corresponding docs for further information.

Adds new conditions to the query, connecting them to existing conditions with Operator#AND.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
conditions:Condition[]

The condition

Annotations
@Override
@Support:CUBRID, DERBY, H2, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
addConditionsback to summary
public void addConditions(Collection<? extends Condition> conditions)

Redeclares org.jooq.ConditionProvider.addConditions.

Adds new conditions to the query, connecting them to existing conditions with Operator#AND.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
conditions:Collection<? extends Condition>

The condition

Annotations
@Override
@Support:CUBRID, DERBY, H2, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
addConditionsback to summary
public void addConditions(Operator operator, Condition condition)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated

as a consequence of Condition being deprecated. See corresponding docs for further information.

Adds a new condition to the query, connecting it to existing conditions with the provided operator.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
condition:Condition

The condition

Annotations
@Override
@Support:CUBRID, DERBY, H2, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
addConditionsback to summary
public void addConditions(Operator operator, Condition... conditions)

Redeclares org.jooq.ConditionProvider.addConditions.

Deprecated

as a consequence of Condition being deprecated. See corresponding docs for further information.

Adds new conditions to the query, connecting them to existing conditions with the provided operator.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
conditions:Condition[]

The condition

Annotations
@Override
@Support:CUBRID, DERBY, H2, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
addConditionsback to summary
public void addConditions(Operator operator, Collection<? extends Condition> conditions)

Redeclares org.jooq.ConditionProvider.addConditions.

Adds new conditions to the query, connecting them to existing conditions with the provided operator.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
conditions:Collection<? extends Condition>

The condition

Annotations
@Override
@Support:CUBRID, DERBY, H2, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
addRecordback to summary
public void addRecord(R record)

Short for calling newRecord(); setRecord(record);

Parameters
record:R

The record to add to this insert statement.

Annotations
@Support
addValueForUpdateback to summary
public <T> void addValueForUpdate(Field<T> field, T value)

Deprecated

as a consequence of Field being deprecated. See corresponding docs for further information.

Add a value to the ON DUPLICATE KEY UPDATE clause of this INSERT statement, where this is supported.

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
See Also
InsertOnDuplicateStep#onDuplicateKeyUpdate()
addValueForUpdateback to summary
public <T> void addValueForUpdate(Field<T> field, Field<T> value)

Deprecated

as a consequence of Field being deprecated. See corresponding docs for further information.

Add a value to the ON DUPLICATE KEY UPDATE clause of this INSERT statement, where this is supported.

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
See Also
InsertOnDuplicateStep#onDuplicateKeyUpdate()
addValuesForUpdateback to summary
public void addValuesForUpdate(Map<?, ?> map)

Add multiple values to the ON DUPLICATE KEY UPDATE clause of this INSERT statement, where this is supported.

Please assure that key/value pairs have matching <T> types. Values can either be of type <T> or Field<T>

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
See Also
InsertOnDuplicateStep#onDuplicateKeyUpdate()
getReturnedRecordback to summary
public R getReturnedRecord()

Redeclares org.jooq.StoreQuery.getReturnedRecord.

Doc from org.jooq.StoreQuery.getReturnedRecord.

The record holding returned values as specified by any of the setReturning() methods.

If the insert statement returns several records, this is the same as calling getReturnedRecords().get(0)

This implemented differently for every dialect:

  • Firebird and Postgres have native support for INSERT … RETURNING and UPDATE … RETURNING clauses
  • HSQLDB, Oracle, and DB2 JDBC drivers allow for retrieving any table column as "generated key" in one statement
  • Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as "generated key". If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.
  • Sybase and SQLite allow for retrieving IDENTITY values as @@identity or last_inserted_rowid() values. Those values are fetched in a separate SELECT statement. If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.

This feature works with INSERT statements for all SQL dialects

Returns:R

The returned value as specified by any of the setReturning() methods. This may return null in case jOOQ could not retrieve any generated keys from the JDBC driver.

Annotations
@Override
@Nullable
@Support
getReturnedRecordsback to summary
public @NotNull Result<R> getReturnedRecords()

Redeclares org.jooq.StoreQuery.getReturnedRecords.

Doc from org.jooq.StoreQuery.getReturnedRecords.

The records holding returned values as specified by any of the setReturning() methods.

This implemented differently for every dialect:

  • Firebird and Postgres have native support for INSERT … RETURNING and UPDATE … RETURNING clauses
  • HSQLDB, Oracle, and DB2 JDBC drivers allow for retrieving any table column as "generated key" in one statement
  • Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as "generated key". If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.
  • Sybase and SQLite allow for retrieving IDENTITY values as @@identity or last_inserted_rowid() values. Those values are fetched in a separate SELECT statement. If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.

[#5070] Due to an early API design flaw, this method historically returns the type R, not a more generic type Record. This means that only actual columns in R can be returned. For a more generic set of column expressions, use getResult() instead.

This feature works with INSERT statements for all SQL dialects

Returns:@NotNull Result<R>

The returned values as specified by any of the setReturning() methods. Note:

  • Not all databases / JDBC drivers support returning several values on multi-row inserts!
  • This may return an empty Result in case jOOQ could not retrieve any generated keys from the JDBC driver.
Annotations
@Override
@NotNull
@Support
newRecordback to summary
public void newRecord()

Adds a new Record to the insert statement for multi-record inserts

Calling this method will cause subsequent calls to addValue(Field, Object) (and similar) to fill the next record.

If this call is not followed by addValue(Field, Object) calls, then this call has no effect.

If this call is done on a fresh insert statement (without any values yet), then this call has no effect either.

Annotations
@Support
onConflictback to summary
public void onConflict(Field<?>... fields)

Deprecated

as a consequence of Field being deprecated. See corresponding docs for further information.

Whether a ON CONFLICT clause should be added to this INSERT statement.

When setting this flag to true, be sure to also add values "for update" using the addValueForUpdate(Field, Field) methods.

Annotations
@Support
See Also
InsertOnDuplicateStep#onDuplicateKeyUpdate()
onConflictback to summary
public void onConflict(Collection<? extends Field<?>> fields)

Whether a ON CONFLICT clause should be added to this INSERT statement.

When setting this flag to true, be sure to also add values "for update" using the addValueForUpdate(Field, Field) methods.

Annotations
@Support
See Also
InsertOnDuplicateStep#onDuplicateKeyUpdate()
onConflictOnConstraintback to summary
public void onConflictOnConstraint(Name constraint)

Whether use a ON CONFLICT or ON CONFLICT ON CONSTRAINT clause in this INSERT statement.

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTEDB
onConflictOnConstraintback to summary
public void onConflictOnConstraint(Constraint constraint)

Deprecated

as a consequence of Constraint being deprecated. See corresponding docs for further information.

Whether use a ON CONFLICT or ON CONFLICT ON CONSTRAINT clause in this INSERT statement.

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, YUGABYTEDB
onConflictOnConstraintback to summary
public void onConflictOnConstraint(UniqueKey<R> constraint)

Whether use a ON CONFLICT or ON CONFLICT ON CONSTRAINT clause in this INSERT statement.

Annotations
@Support
onConflictWhereback to summary
public void onConflictWhere(Condition condition)

Deprecated

as a consequence of Condition being deprecated. See corresponding docs for further information.

Adds a new condition the onConflict(Field...) clause.

This is for use with SQLDialect#POSTGRES's onConflict(Field...) clause.

Parameters
condition:Condition

The condition

Annotations
@Support:POSTGRES, SQLITE, YUGABYTEDB
onDuplicateKeyIgnoreback to summary
public void onDuplicateKeyIgnore(boolean flag)

Whether an ON DUPLICATE KEY IGNORE clause should be added to this INSERT statement.

This clause is not actually supported in this form by any database, but can be emulated as such:

Dialect Emulation
SQLDialect#MYSQL and SQLDialect#MARIADB
INSERT IGNORE INTO …
SQLDialect#POSTGRES_9_5 and SQLDialect#SQLITE
INSERT INTO … ON CONFLICT DO NOTHING
SQLDialect#DB2
SQLDialect#HSQLDB
SQLDialect#ORACLE
SQLDialect#SQLSERVER
SQLDialect#SYBASE
MERGE INTO [dst]
USING ([values])
ON [dst.key] = [values.key]
WHEN NOT MATCHED THEN INSERT ..
All the others
INSERT INTO [dst] ( ... )
SELECT [values]
WHERE NOT EXISTS (
  SELECT 1
  FROM [dst]
  WHERE [dst.key] = [values.key]
)

The ON DUPLICATE KEY UPDATE flag is mutually exclusive with the ON DUPLICATE KEY IGNORE flag (see onDuplicateKeyIgnore(boolean). Setting one will unset the other

Annotations
@Support
onDuplicateKeyUpdateback to summary
public void onDuplicateKeyUpdate(boolean flag)

Whether a ON DUPLICATE KEY UPDATE clause should be added to this INSERT statement.

When setting this flag to true, be sure to also add values "for update" using the addValueForUpdate(Field, Field) methods.

The ON DUPLICATE KEY UPDATE flag is mutually exclusive with the ON DUPLICATE KEY IGNORE flag (see onDuplicateKeyIgnore(boolean). Setting one will unset the other

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
See Also
InsertOnDuplicateStep#onDuplicateKeyUpdate()
setDefaultValuesback to summary
public void setDefaultValues()

Set an empty record with the DEFAULT VALUES clause.

Annotations
@Support:CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB
setReturningback to summary
public void setReturning()

Redeclares org.jooq.StoreQuery.setReturning.

Doc from org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return all fields in R.

This feature works with INSERT statements for all SQL dialects

Annotations
@Override
@Support
setReturningback to summary
public void setReturning(Identity<R, ?> identity)

Redeclares org.jooq.StoreQuery.setReturning.

Doc from org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return the generated identity value.

This feature works with INSERT statements for all SQL dialects

Parameters
identity:Identity<R, ?>

The table's identity

Annotations
@Override
@Support
setReturningback to summary
public void setReturning(SelectFieldOrAsterisk... fields)

Redeclares org.jooq.StoreQuery.setReturning.

Doc from org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return a list of fields in R.

This feature works with INSERT statements for all SQL dialects

Parameters
fields:SelectFieldOrAsterisk[]

Fields to be returned

Annotations
@Override
@Support
setReturningback to summary
public void setReturning(Collection<? extends SelectFieldOrAsterisk> fields)

Redeclares org.jooq.StoreQuery.setReturning.

Doc from org.jooq.StoreQuery.setReturning.

Configure the INSERT or UPDATE statement to return a list of fields in R.

This feature works with INSERT statements for all SQL dialects

Parameters
fields:Collection<? extends SelectFieldOrAsterisk>

Fields to be returned

Annotations
@Override
@Support
setSelectback to summary
public void setSelect(Field<?>[] fields, Select<?> select)

Deprecated

as a consequence of Field being deprecated. See corresponding docs for further information.

Use a SELECT statement as the source of values for the INSERT statement.

Annotations
@Support
setSelectback to summary
public void setSelect(Collection<? extends Field<?>> fields, Select<?> select)

Use a SELECT statement as the source of values for the INSERT statement.

Annotations
@Support