- Type Parameters:
R- The record type of the table being inserted into
- All Superinterfaces:
Attachable,AttachableQueryPart,ConditionProvider,DMLQuery<R>,Flow.Publisher<Integer>,Insert<R>,Publisher<Integer>,org.reactivestreams.Publisher<Integer>,Query,QueryPart,RowCountQuery,Serializable,Statement,StoreQuery<R>
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 TypeMethodDescriptionvoidaddConditions(Collection<? extends Condition> conditions) Adds new conditions to the query, connecting them to existing conditions withOperator.AND.voidaddConditions(Condition condition) Adds a new condition to the query, connecting it to existing conditions withOperator.AND.voidaddConditions(Condition... conditions) Adds new conditions to the query, connecting them to existing conditions withOperator.AND.voidaddConditions(Operator operator, Collection<? extends Condition> conditions) Adds new conditions to the query, connecting them to existing conditions with the provided operator.voidaddConditions(Operator operator, Condition condition) Adds a new condition to the query, connecting it to existing conditions with the provided operator.voidaddConditions(Operator operator, Condition... conditions) Adds new conditions to the query, connecting them to existing conditions with the provided operator.voidShort for callingnewRecord()andStoreQuery.setRecord(Record).<T> voidaddValueForUpdate(Field<T> field, Field<T> value) Add a value to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.<T> voidaddValueForUpdate(Field<T> field, T value) Add a value to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.voidaddValuesForUpdate(Map<?, ?> map) Add multiple values to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.The record holding returned values as specified by any of theStoreQuery.setReturning()methods.The records holding returned values as specified by any of theStoreQuery.setReturning()methods.voidAdds a new Record to the insert statement for multi-record insertsvoidonConflict(Collection<? extends Field<?>> fields) Whether aON CONFLICTclause should be added to thisINSERTstatement.voidonConflict(Field<?>... fields) Whether aON CONFLICTclause should be added to thisINSERTstatement.voidonConflictOnConstraint(Constraint constraint) Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement.voidonConflictOnConstraint(Name constraint) Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement.voidonConflictOnConstraint(UniqueKey<R> constraint) Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement.voidonConflictWhere(Condition condition) Adds a new condition theonConflict(Field...)clause.voidonDuplicateKeyIgnore(boolean flag) Whether anON DUPLICATE KEY IGNOREclause should be added to thisINSERTstatement.voidonDuplicateKeyUpdate(boolean flag) Whether aON DUPLICATE KEY UPDATEclause should be added to thisINSERTstatement.voidSet an empty record with theDEFAULT VALUESclause.voidsetRecordForUpdate(R record) Add multiple values to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.voidConfigure theINSERTorUPDATEstatement to return all fields inR.voidsetReturning(Collection<? extends SelectFieldOrAsterisk> fields) Configure theINSERTorUPDATEstatement to return a list of fields inR.voidsetReturning(Identity<R, ?> identity) Configure theINSERTorUPDATEstatement to return the generated identity value.voidsetReturning(SelectFieldOrAsterisk... fields) Configure theINSERTorUPDATEstatement to return a list of fields inR.voidsetSelect(Collection<? extends Field<?>> fields, Select<?> select) Use aSELECTstatement as the source of values for theINSERTstatement.voidUse aSELECTstatement as the source of values for theINSERTstatement.Methods inherited from interface org.jooq.Attachable
attach, configuration, detachMethods inherited from interface org.jooq.AttachableQueryPart
getBindValues, getParam, getParams, getSQL, getSQLMethods inherited from interface org.reactivestreams.Publisher
subscribeMethods inherited from interface org.jooq.Query
bind, bind, cancel, execute, executeAsync, executeAsync, isExecutable, keepStatement, poolable, queryTimeout
-
Method Details
-
newRecord
Adds a new Record to the insert statement for multi-record insertsCalling this method will cause subsequent calls to
StoreQuery.addValue(Field, Object)(and similar) to fill the next record.If this call is not followed by
StoreQuery.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.
-
addRecord
Short for callingnewRecord()andStoreQuery.setRecord(Record).- Parameters:
record- The record to add to this insert statement.
-
onConflict
Whether aON CONFLICTclause should be added to thisINSERTstatement.When setting this flag to
true, be sure to also add values "for update" using theaddValueForUpdate(Field, Field)methods.- See Also:
-
onConflict
Whether aON CONFLICTclause should be added to thisINSERTstatement.When setting this flag to
true, be sure to also add values "for update" using theaddValueForUpdate(Field, Field)methods.- See Also:
-
onConflictOnConstraint
@Support({AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLSERVER,SYBASE,YUGABYTEDB}) void onConflictOnConstraint(Name constraint) Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement. -
onConflictOnConstraint
@Support({AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLSERVER,SYBASE,YUGABYTEDB}) void onConflictOnConstraint(Constraint constraint) Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement. -
onConflictOnConstraint
Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement. -
onDuplicateKeyUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,SYBASE,TERADATA,YUGABYTEDB}) void onDuplicateKeyUpdate(boolean flag) Whether aON DUPLICATE KEY UPDATEclause should be added to thisINSERTstatement.When setting this flag to
true, be sure to also add values "for update" using theaddValueForUpdate(Field, Field)methods.The
ON DUPLICATE KEY UPDATEflag is mutually exclusive with theON DUPLICATE KEY IGNOREflag (seeonDuplicateKeyIgnore(boolean). Setting one will unset the other- See Also:
-
onDuplicateKeyIgnore
Whether anON DUPLICATE KEY IGNOREclause should be added to thisINSERTstatement.This clause is not actually supported in this form by any database, but can be emulated as such:
Dialect Emulation SQLDialect.MYSQLandSQLDialect.MARIADBINSERT IGNORE INTO …SQLDialect.POSTGRES_9_5andSQLDialect.SQLITEINSERT INTO … ON CONFLICT DO NOTHINGSQLDialect.DB2
SQLDialect.HSQLDB
SQLDialect.ORACLE
SQLDialect.SQLSERVER
SQLDialect.SYBASEMERGE 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 UPDATEflag is mutually exclusive with theON DUPLICATE KEY IGNOREflag (seeonDuplicateKeyIgnore(boolean). Setting one will unset the other -
addValueForUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,SYBASE,TERADATA,YUGABYTEDB}) <T> void addValueForUpdate(Field<T> field, T value) Add a value to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.- See Also:
-
addValueForUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,SYBASE,TERADATA,YUGABYTEDB}) <T> void addValueForUpdate(Field<T> field, Field<T> value) Add a value to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.- See Also:
-
addValuesForUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,SYBASE,TERADATA,YUGABYTEDB}) void addValuesForUpdate(Map<?, ?> map) Add multiple values to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.Please assure that key/value pairs have matching
<T>types. Values can either be of type<T>orField<T>- See Also:
-
setRecordForUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD_3_0,H2,HANA,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,SYBASE,TERADATA,YUGABYTEDB}) void setRecordForUpdate(R record) Add multiple values to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.This works like
StoreQuery.setRecord(Record).- Parameters:
record- The record to add to this insert statement.
-
onConflictWhere
@Support({AURORA_POSTGRES,POSTGRES_9_5,SQLITE,YUGABYTEDB}) void onConflictWhere(Condition condition) Adds a new condition theonConflict(Field...)clause.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Parameters:
condition- The condition
-
addConditions
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DERBY,DUCKDB,EXASOL,H2,HANA,MARIADB,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,YUGABYTEDB}) void addConditions(Condition condition) Adds a new condition to the query, connecting it to existing conditions withOperator.AND.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Specified by:
addConditionsin interfaceConditionProvider- Parameters:
condition- The condition
-
addConditions
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DERBY,DUCKDB,EXASOL,H2,HANA,MARIADB,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,YUGABYTEDB}) void addConditions(Condition... conditions) Adds new conditions to the query, connecting them to existing conditions withOperator.AND.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Specified by:
addConditionsin interfaceConditionProvider- Parameters:
conditions- The condition
-
addConditions
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DERBY,DUCKDB,EXASOL,H2,HANA,MARIADB,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,YUGABYTEDB}) void addConditions(Collection<? extends Condition> conditions) Adds new conditions to the query, connecting them to existing conditions withOperator.AND.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Specified by:
addConditionsin interfaceConditionProvider- Parameters:
conditions- The condition
-
addConditions
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DERBY,DUCKDB,EXASOL,H2,HANA,MARIADB,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,YUGABYTEDB}) void addConditions(Operator operator, Condition condition) Adds a new condition to the query, connecting it to existing conditions with the provided operator.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Specified by:
addConditionsin interfaceConditionProvider- Parameters:
condition- The condition
-
addConditions
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DERBY,DUCKDB,EXASOL,H2,HANA,MARIADB,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,YUGABYTEDB}) void addConditions(Operator operator, Condition... conditions) Adds new conditions to the query, connecting them to existing conditions with the provided operator.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Specified by:
addConditionsin interfaceConditionProvider- Parameters:
conditions- The condition
-
addConditions
@Support({AURORA_MYSQL,AURORA_POSTGRES,COCKROACHDB,CUBRID,DERBY,DUCKDB,EXASOL,H2,HANA,MARIADB,MYSQL,ORACLE,POSTGRES_9_5,SNOWFLAKE,SQLITE,SQLSERVER,YUGABYTEDB}) void addConditions(Operator operator, Collection<? extends Condition> conditions) Adds new conditions to the query, connecting them to existing conditions with the provided operator.This is for use with
SQLDialect.POSTGRES'sonConflict(Field...)clause.- Specified by:
addConditionsin interfaceConditionProvider- Parameters:
conditions- The condition
-
setDefaultValues
@Support({ACCESS,ASE,AURORA_MYSQL,AURORA_POSTGRES,BIGQUERY,COCKROACHDB,CUBRID,DB2,DERBY,DUCKDB,EXASOL,FIREBIRD,H2,HANA,HSQLDB,IGNITE,INFORMIX,INGRES,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES,REDSHIFT,SNOWFLAKE,SQLDATAWAREHOUSE,SQLITE,SQLSERVER,SYBASE,TERADATA,VERTICA,YUGABYTEDB}) void setDefaultValues()Set an empty record with theDEFAULT VALUESclause. -
setSelect
Use aSELECTstatement as the source of values for theINSERTstatement. -
setSelect
Use aSELECTstatement as the source of values for theINSERTstatement. -
setReturning
Configure theINSERTorUPDATEstatement to return all fields inR.This feature works with
INSERTstatements for all SQL dialects- Specified by:
setReturningin interfaceStoreQuery<R extends Record>- See Also:
-
setReturning
Configure theINSERTorUPDATEstatement to return the generated identity value.This feature works with
INSERTstatements for all SQL dialects- Specified by:
setReturningin interfaceStoreQuery<R extends Record>- Parameters:
identity- The table's identity- See Also:
-
setReturning
Configure theINSERTorUPDATEstatement to return a list of fields inR.This feature works with
INSERTstatements for all SQL dialects- Specified by:
setReturningin interfaceStoreQuery<R extends Record>- Parameters:
fields- Fields to be returned- See Also:
-
setReturning
Configure theINSERTorUPDATEstatement to return a list of fields inR.This feature works with
INSERTstatements for all SQL dialects- Specified by:
setReturningin interfaceStoreQuery<R extends Record>- Parameters:
fields- Fields to be returned- See Also:
-
getReturnedRecord
The record holding returned values as specified by any of theStoreQuery.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 … RETURNINGandUPDATE … RETURNINGclauses - 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
@@identityorlast_inserted_rowid()values. Those values are fetched in a separateSELECTstatement. If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.
This feature works with
INSERTstatements for all SQL dialects- Specified by:
getReturnedRecordin interfaceStoreQuery<R extends Record>- Returns:
- The returned value as specified by any of the
StoreQuery.setReturning()methods. This may returnnullin case jOOQ could not retrieve any generated keys from the JDBC driver. - See Also:
- Firebird and Postgres have native support for
-
getReturnedRecords
The records holding returned values as specified by any of theStoreQuery.setReturning()methods.This implemented differently for every dialect:
- Firebird and Postgres have native support for
INSERT … RETURNINGandUPDATE … RETURNINGclauses - 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
@@identityorlast_inserted_rowid()values. Those values are fetched in a separateSELECTstatement. 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 typeRecord. This means that only actual columns inRcan be returned. For a more generic set of column expressions, useStoreQuery.getResult()instead.This feature works with
INSERTstatements for all SQL dialects- Specified by:
getReturnedRecordsin interfaceStoreQuery<R extends Record>- Returns:
- The returned values as specified by any of the
StoreQuery.setReturning()methods. Note:- Not all databases / JDBC drivers support returning several values on multi-row inserts!
- This may return an empty
Resultin case jOOQ could not retrieve any generated keys from the JDBC driver.
- Firebird and Postgres have native support for
-