-
- Type Parameters:
R- The record type of the table being inserted into
- All Superinterfaces:
Attachable,AutoCloseable,Flow.Publisher<Integer>,Insert<R>,org.reactivestreams.Publisher<Integer>,Query,QueryPart,RowCountQuery,Serializable,Statement,StoreQuery<R>
public interface InsertQuery<R extends Record> extends StoreQuery<R>, Insert<R>
AnINSERTstatement (model API).This type is the model API representation of a
Insertstatement, 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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddConditions(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.voidaddRecord(R record)Short for callingnewRecord(); 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.RgetReturnedRecord()The record holding returned values as specified by any of theStoreQuery.setReturning()methods.Result<R>getReturnedRecords()The records holding returned values as specified by any of theStoreQuery.setReturning()methods.voidnewRecord()Adds 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.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.voidsetDefaultValues()Set an empty record with theDEFAULT VALUESclause.voidsetReturning()Configure 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.voidsetSelect(Field<?>[] fields, Select<?> select)Use aSELECTstatement as the source of values for theINSERTstatement.-
Methods inherited from interface org.jooq.Attachable
attach, configuration, detach
-
Methods inherited from interface java.util.concurrent.Flow.Publisher
subscribe
-
Methods inherited from interface org.jooq.Query
bind, bind, cancel, close, execute, executeAsync, executeAsync, getBindValues, getParam, getParams, getSQL, getSQL, getSQL, isExecutable, keepStatement, poolable, queryTimeout
-
-
-
-
Method Detail
-
newRecord
@Support void 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
@Support void addRecord(R record)
Short for callingnewRecord(); setRecord(record);- Parameters:
record- The record to add to this insert statement.
-
onConflict
@Support void onConflict(Field<?>... fields)
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.
-
onConflict
@Support void onConflict(Collection<? extends Field<?>> fields)
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.
-
onConflictOnConstraint
@Support({AURORA_POSTGRES,CUBRID,DB2,DERBY,FIREBIRD_3_0,HSQLDB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLSERVER,SYBASE}) void onConflictOnConstraint(Name constraint)
Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement.
-
onConflictOnConstraint
@Support({AURORA_POSTGRES,CUBRID,DB2,DERBY,FIREBIRD_3_0,HSQLDB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLSERVER,SYBASE}) void onConflictOnConstraint(Constraint constraint)
Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement.
-
onConflictOnConstraint
@Support void onConflictOnConstraint(UniqueKey<R> constraint)
Whether use aON CONFLICTorON CONFLICT ON CONSTRAINTclause in thisINSERTstatement.
-
onDuplicateKeyUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,CUBRID,DB2,DERBY,H2,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER,SYBASE,TERADATA}) 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
-
onDuplicateKeyIgnore
@Support void onDuplicateKeyIgnore(boolean flag)
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 NOTHING
SQLDialect.CUBRIDINSERT INTO .. ON DUPLICATE KEY UPDATE [any-field] = [any-field]
SQLDialect.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,CUBRID,DB2,DERBY,H2,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER,SYBASE,TERADATA}) <T> void addValueForUpdate(Field<T> field, T value)
Add a value to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.
-
addValueForUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,CUBRID,DB2,DERBY,H2,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER,SYBASE,TERADATA}) <T> void addValueForUpdate(Field<T> field, Field<T> value)
Add a value to theON DUPLICATE KEY UPDATEclause of thisINSERTstatement, where this is supported.
-
addValuesForUpdate
@Support({AURORA_MYSQL,AURORA_POSTGRES,CUBRID,DB2,DERBY,H2,HSQLDB,INFORMIX,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER,SYBASE,TERADATA}) 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>
-
addConditions
@Support({AURORA_POSTGRES,CUBRID,DERBY,H2,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER}) 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.- Parameters:
condition- The condition
-
addConditions
@Support({AURORA_POSTGRES,CUBRID,DERBY,H2,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER}) 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.- Parameters:
conditions- The condition
-
addConditions
@Support({AURORA_POSTGRES,CUBRID,DERBY,H2,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER}) 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.- Parameters:
conditions- The condition
-
addConditions
@Support({AURORA_POSTGRES,CUBRID,DERBY,H2,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER}) 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.- Parameters:
condition- The condition
-
addConditions
@Support({AURORA_POSTGRES,CUBRID,DERBY,H2,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER}) 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.- Parameters:
conditions- The condition
-
addConditions
@Support({AURORA_POSTGRES,CUBRID,DERBY,H2,ORACLE,POSTGRES_9_5,SQLITE,SQLSERVER}) 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.- Parameters:
conditions- The condition
-
setDefaultValues
@Support({ASE,AURORA_MYSQL,AURORA_POSTGRES,CUBRID,DB2,DERBY,FIREBIRD,H2,HSQLDB,INGRES,MARIADB,MEMSQL,MYSQL,ORACLE,POSTGRES,SQLDATAWAREHOUSE,SQLITE,SQLSERVER,SYBASE,TERADATA}) void setDefaultValues()
Set an empty record with theDEFAULT VALUESclause.
-
setSelect
@Support void setSelect(Field<?>[] fields, Select<?> select)
Use aSELECTstatement as the source of values for theINSERTstatement.
-
setSelect
@Support void setSelect(Collection<? extends Field<?>> fields, Select<?> select)
Use aSELECTstatement as the source of values for theINSERTstatement.
-
setReturning
@Support void 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:
StoreQuery.getReturnedRecords()
-
setReturning
@Support void setReturning(Identity<R,?> identity)
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:
StoreQuery.getReturnedRecords()
-
setReturning
@Support void setReturning(SelectFieldOrAsterisk... fields)
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:
StoreQuery.getReturnedRecords()
-
setReturning
@Support void setReturning(Collection<? extends SelectFieldOrAsterisk> fields)
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:
StoreQuery.getReturnedRecords()
-
getReturnedRecord
@Support R 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:
StoreQuery.getReturnedRecords()
- Firebird and Postgres have native support for
-
getReturnedRecords
@Support Result<R> 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
-
-