-
- Type Parameters:
R
- The record type of the table being inserted into
- All Superinterfaces:
Attachable
,java.lang.AutoCloseable
,java.util.concurrent.Flow.Publisher<java.lang.Integer>
,Insert<R>
,org.reactivestreams.Publisher<java.lang.Integer>
,Query
,QueryPart
,RowCountQuery
,java.io.Serializable
,Statement
,StoreQuery<R>
public interface InsertQuery<R extends Record> extends StoreQuery<R>, Insert<R>
AnINSERT
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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
addConditions(java.util.Collection<? extends Condition> conditions)
Adds new conditions to the query, connecting them to existing conditions withOperator.AND
.void
addConditions(Condition condition)
Adds a new condition to the query, connecting it to existing conditions withOperator.AND
.void
addConditions(Condition... conditions)
Adds new conditions to the query, connecting them to existing conditions withOperator.AND
.void
addConditions(Operator operator, java.util.Collection<? extends Condition> conditions)
Adds new conditions to the query, connecting them to existing conditions with the provided operator.void
addConditions(Operator operator, Condition condition)
Adds a new condition to the query, connecting it to existing conditions with the provided operator.void
addConditions(Operator operator, Condition... conditions)
Adds new conditions to the query, connecting them to existing conditions with the provided operator.void
addRecord(R record)
Short for callingnewRecord(); setRecord(record);
<T> void
addValueForUpdate(Field<T> field, Field<T> value)
Add a value to theON DUPLICATE KEY UPDATE
clause of thisINSERT
statement, where this is supported.<T> void
addValueForUpdate(Field<T> field, T value)
Add a value to theON DUPLICATE KEY UPDATE
clause of thisINSERT
statement, where this is supported.void
addValuesForUpdate(java.util.Map<?,?> map)
Add multiple values to theON DUPLICATE KEY UPDATE
clause of thisINSERT
statement, where this is supported.R
getReturnedRecord()
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.void
newRecord()
Adds a new Record to the insert statement for multi-record insertsvoid
onConflict(java.util.Collection<? extends Field<?>> fields)
Whether aON CONFLICT
clause should be added to thisINSERT
statement.void
onConflict(Field<?>... fields)
Whether aON CONFLICT
clause should be added to thisINSERT
statement.void
onConflictOnConstraint(Constraint constraint)
Whether use aON CONFLICT
orON CONFLICT ON CONSTRAINT
clause in thisINSERT
statement.void
onConflictOnConstraint(Name constraint)
Whether use aON CONFLICT
orON CONFLICT ON CONSTRAINT
clause in thisINSERT
statement.void
onConflictOnConstraint(UniqueKey<R> constraint)
Whether use aON CONFLICT
orON CONFLICT ON CONSTRAINT
clause in thisINSERT
statement.void
onDuplicateKeyIgnore(boolean flag)
Whether anON DUPLICATE KEY IGNORE
clause should be added to thisINSERT
statement.void
onDuplicateKeyUpdate(boolean flag)
Whether aON DUPLICATE KEY UPDATE
clause should be added to thisINSERT
statement.void
setDefaultValues()
Set an empty record with theDEFAULT VALUES
clause.void
setReturning()
Configure theINSERT
orUPDATE
statement to return all fields inR
.void
setReturning(java.util.Collection<? extends SelectFieldOrAsterisk> fields)
Configure theINSERT
orUPDATE
statement to return a list of fields inR
.void
setReturning(Identity<R,?> identity)
Configure theINSERT
orUPDATE
statement to return the generated identity value.void
setReturning(SelectFieldOrAsterisk... fields)
Configure theINSERT
orUPDATE
statement to return a list of fields inR
.void
setSelect(Field<?>[] fields, Select<?> select)
Use aSELECT
statement as the source of values for theINSERT
statement.-
Methods inherited from interface org.jooq.Attachable
attach, configuration, detach
-
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 CONFLICT
clause should be added to thisINSERT
statement.When setting this flag to
true
, be sure to also add values "for update" using theaddValueForUpdate(Field, Field)
methods.
-
onConflict
@Support void onConflict(java.util.Collection<? extends Field<?>> fields)
Whether aON CONFLICT
clause should be added to thisINSERT
statement.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 CONFLICT
orON CONFLICT ON CONSTRAINT
clause in thisINSERT
statement.
-
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 CONFLICT
orON CONFLICT ON CONSTRAINT
clause in thisINSERT
statement.
-
onConflictOnConstraint
@Support void onConflictOnConstraint(UniqueKey<R> constraint)
Whether use aON CONFLICT
orON CONFLICT ON CONSTRAINT
clause in thisINSERT
statement.
-
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 UPDATE
clause should be added to thisINSERT
statement.When setting this flag to
true
, be sure to also add values "for update" using theaddValueForUpdate(Field, Field)
methods.The
ON DUPLICATE KEY UPDATE
flag is mutually exclusive with theON DUPLICATE KEY IGNORE
flag (seeonDuplicateKeyIgnore(boolean)
. Setting one will unset the other
-
onDuplicateKeyIgnore
@Support void onDuplicateKeyIgnore(boolean flag)
Whether anON DUPLICATE KEY IGNORE
clause should be added to thisINSERT
statement.This clause is not actually supported in this form by any database, but can be emulated as such:
Dialect Emulation SQLDialect.MYSQL
andSQLDialect.MARIADB
INSERT IGNORE INTO ..
SQLDialect.POSTGRES_9_5
andSQLDialect.SQLITE
INSERT INTO .. ON CONFLICT DO NOTHING
SQLDialect.CUBRID
INSERT INTO .. ON DUPLICATE KEY UPDATE [any-field] = [any-field]
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 theON DUPLICATE KEY IGNORE
flag (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 UPDATE
clause of thisINSERT
statement, 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 UPDATE
clause of thisINSERT
statement, 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(java.util.Map<?,?> map)
Add multiple values to theON DUPLICATE KEY UPDATE
clause of thisINSERT
statement, 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(java.util.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, java.util.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 VALUES
clause.
-
setSelect
@Support void setSelect(Field<?>[] fields, Select<?> select)
Use aSELECT
statement as the source of values for theINSERT
statement.
-
setReturning
@Support void setReturning()
Configure theINSERT
orUPDATE
statement to return all fields inR
.This feature works with
INSERT
statements for all SQL dialects- Specified by:
setReturning
in interfaceStoreQuery<R extends Record>
- See Also:
StoreQuery.getReturnedRecords()
-
setReturning
@Support void setReturning(Identity<R,?> identity)
Configure theINSERT
orUPDATE
statement to return the generated identity value.This feature works with
INSERT
statements for all SQL dialects- Specified by:
setReturning
in interfaceStoreQuery<R extends Record>
- Parameters:
identity
- The table's identity- See Also:
StoreQuery.getReturnedRecords()
-
setReturning
@Support void setReturning(SelectFieldOrAsterisk... fields)
Configure theINSERT
orUPDATE
statement to return a list of fields inR
.This feature works with
INSERT
statements for all SQL dialects- Specified by:
setReturning
in interfaceStoreQuery<R extends Record>
- Parameters:
fields
- Fields to be returned- See Also:
StoreQuery.getReturnedRecords()
-
setReturning
@Support void setReturning(java.util.Collection<? extends SelectFieldOrAsterisk> fields)
Configure theINSERT
orUPDATE
statement to return a list of fields inR
.This feature works with
INSERT
statements for all SQL dialects- Specified by:
setReturning
in 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 .. RETURNING
andUPDATE .. 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
orlast_inserted_rowid()
values. Those values are fetched in a separateSELECT
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- Specified by:
getReturnedRecord
in interfaceStoreQuery<R extends Record>
- Returns:
- The returned value as specified by any of the
StoreQuery.setReturning()
methods. This may returnnull
in 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 .. RETURNING
andUPDATE .. 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
orlast_inserted_rowid()
values. Those values are fetched in a separateSELECT
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 typeRecord
. This means that only actual columns inR
can be returned. For a more generic set of column expressions, useStoreQuery.getResult()
instead.This feature works with
INSERT
statements for all SQL dialects- Specified by:
getReturnedRecords
in 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
Result
in case jOOQ could not retrieve any generated keys from the JDBC driver.
- Firebird and Postgres have native support for
-
-