org.apache.derby.impl.sql.execute
Class InsertResultSet

java.lang.Object
  extended by org.apache.derby.impl.sql.execute.NoRowsResultSetImpl
      extended by org.apache.derby.impl.sql.execute.DMLWriteResultSet
          extended by org.apache.derby.impl.sql.execute.InsertResultSet
All Implemented Interfaces:
TargetResultSet, ResultSet

 class InsertResultSet
extends DMLWriteResultSet
implements TargetResultSet

Insert the rows from the source into the specified base table. This will cause constraints to be checked and triggers to be executed based on the c's and t's compiled into the insert plan.


Nested Class Summary
(package private)  class InsertResultSet.MyRowAllocator
           
 
Field Summary
private  NumberDataValue[] aiCache
          keeps track of autoincrement values that are generated by getSetAutoincrementValues.
private  ResultSet autoGeneratedKeysResultSet
           
private  TemporaryRowHolderImpl autoGeneratedKeysRowsHolder
           
protected  boolean autoincrementGenerated
          If set to true, implies that this (rep)insertresultset has generated autoincrement values.
private  ConglomerateController bulkHeapCC
           
private  ScanController bulkHeapSC
           
protected  boolean bulkInsert
           
(package private)  boolean bulkInsertPerformed
           
private  boolean bulkInsertReplace
           
private  GeneratedMethod checkGM
           
private  int[][] collation
           
(package private)  InsertConstantAction constants
           
protected  DataDictionary dd
           
private  boolean firstExecute
           
private  boolean firstRow
           
private  RISetChecker fkChecker
           
private  FKInfo[] fkInfoArray
           
private  ExecRow fullTemplate
           
private  GeneratedMethod generationClauses
           
private  boolean hasBeforeRowTrigger
           
private  boolean hasBeforeStatementTrigger
           
private  long heapConglom
           
private  long identityVal
           
private  java.util.Hashtable indexConversionTable
           
private  FormatableBitSet indexedCols
           
private  ExecIndexRow[] indexRows
           
private  boolean[] needToDropSort
           
private  int numOpens
           
private  ColumnOrdering[][] ordering
           
private  ResultDescription resultDescription
           
private  RowLocation rl
           
private  ExecRow row
           
private  RowChanger rowChanger
           
private  TemporaryRowHolderImpl rowHolder
           
private  RowLocationRetRowSource[] rowSources
           
(package private)  NoPutResultSet savedSource
           
private  boolean setIdentity
           
private  SortController[] sorters
           
private  long[] sortIds
           
private  NoPutResultSet sourceResultSet
           
private  BulkTableScanResultSet tableScan
           
private  TransactionController tc
           
protected  TableDescriptor td
           
private  TriggerEventActivator triggerActivator
           
private  TriggerInfo triggerInfo
           
(package private)  boolean userSpecifiedBulkInsert
           
 
Fields inherited from class org.apache.derby.impl.sql.execute.DMLWriteResultSet
baseRowReadMap, constantAction, deferredSparseRow, heapDCOCI, indexDCOCIs, rowCount, streamStorableHeapColIds
 
Fields inherited from class org.apache.derby.impl.sql.execute.NoRowsResultSetImpl
activation, beginExecutionTime, beginTime, endExecutionTime, endTime, lcc
 
Fields inherited from interface org.apache.derby.iapi.sql.ResultSet
CURRENT_RESULTSET_ONLY, ENTIRE_RESULTSET_TREE, ISAFTERLAST, ISBEFOREFIRST, ISFIRST, ISLAST
 
Constructor Summary
InsertResultSet(NoPutResultSet source, GeneratedMethod generationClauses, GeneratedMethod checkGM, Activation activation)
           
 
Method Summary
private  long bulkInsertCore(LanguageConnectionContext lcc, long oldHeapConglom)
           
private  void bulkValidateForeignKeys(TransactionController tc, ContextManager cm)
          Bulk Referential Integrity Checker
private  void bulkValidateForeignKeysCore(TransactionController tc, ContextManager cm, FKInfo fkInfo, long fkConglom, long pkConglom, java.lang.String fkConstraintName)
           
 void changedRow(ExecRow execRow, RowLocation rowLocation)
          Pass a changed row and the row location for that row to the target result set.
 void cleanUp()
          Tells the system to clean up on an error.
 void close()
          Clean up resources and call close on data members.
private  void emptyIndexes(long newHeapConglom, InsertConstantAction constants, TableDescriptor td, DataDictionary dd, ExecRow fullTemplate)
          Empty the indexes after doing a bulk insert replace where the table has 0 rows after the replace.
private  void evaluateCheckConstraints()
          Run the check constraints against the current row.
 void finish()
          Tells the system that there will be no more access to any database information via this result set; in particular, no more calls to open().
private  int[] generatedColumnPositionsArray()
          If user didn't provide columns list for auto-generated columns, then only include columns with auto-generated values in the resultset.
 ResultSet getAutoGeneratedKeysResultset()
          ResultSet for rows inserted into the table (contains auto-generated keys columns only)
private  java.lang.String[] getColumnNames(int[] baseColumnPositions)
           
private  ExecRow getCompactRow(ExecRow inputRow, int[] columnIndexes)
          Take the input row and return a new compact ExecRow using the column positions provided in columnIndexes.
private  boolean getExclusiveTableLock()
          Get an exclusive table lock on the target table (and check to see if the table is populated if this is not a bulk insert replace).
 ResultDescription getResultDescription()
          Returns the description of the inserted rows.
 NumberDataValue getSetAutoincrementValue(int columnPosition, long increment)
          getSetAutoincrementValue will get the autoincrement value of the columnPosition specified for the target table.
private  BulkTableScanResultSet getTableScanResultSet(long conglomId)
          Get me a table scan result set, preferably a bulk table scan, thank you.
private  boolean isSingleRowResultSet()
           
private  boolean isSourceRowResultSet()
           
private  ExecRow makeIndexTemplate(FKInfo fkInfo, ExecRow fullTemplate, ContextManager cm)
          Make a template row with the correct columns.
private  void normalInsertCore(LanguageConnectionContext lcc, boolean firstExecute)
           
 void open()
          Needs to be called before the result set will do anything.
 ExecRow preprocessSourceRow(ExecRow execRow)
          Preprocess the source row.
private  void setEstimatedRowCount(long heapConglom)
          Set the estimated row count for this table.
private  void setUpAllSorts(ExecRow sourceRow, RowLocation rl)
          Set up to update all of the indexes on a table when doing a bulk insert on an empty table.
private  int[] uniqueColumnPositionArray(int[] columnIndexes)
          Remove duplicate columns from the array.
private  void updateAllIndexes(long newHeapConglom, InsertConstantAction constants, TableDescriptor td, DataDictionary dd, ExecRow fullTemplate)
          Update all of the indexes on a table when doing a bulk insert on an empty table.
private  boolean verifyAutoGenColumn(ColumnDescriptor cd)
          Check that the received ColumnDescriptor corresponds to a column for which it is possible to fetch auto-generated keys.
private  void verifyAutoGeneratedColumnsIndexes(int[] columnIndexes)
          Verify that the auto-generated columns list (by position) has valid column positions for the table.
private  void verifyAutoGeneratedColumnsNames(java.lang.String[] columnNames)
          Verify that the auto-generated columns list (by name) has valid column names for the table.
protected  boolean verifyBulkInsert()
          Verify that bulkInsert is allowed on this table.
 
Methods inherited from class org.apache.derby.impl.sql.execute.DMLWriteResultSet
decodeLockMode, getIndexNameFromCID, getNextRowCore, makeDeferredSparseRow, modifiedRowCount
 
Methods inherited from class org.apache.derby.impl.sql.execute.NoRowsResultSetImpl
checkRowPosition, clearCurrentRow, doesCommit, evaluateACheckConstraint, evaluateCheckConstraints, evaluateGenerationClauses, getAbsoluteRow, getActivation, getBeginExecutionTimestamp, getCurrentTimeMillis, getCursorName, getEndExecutionTimestamp, getExecuteTime, getFirstRow, getLastRow, getNextRow, getPreviousRow, getQueryPlanText, getRelativeRow, getRowNumber, getSubqueryTrackingArray, getTimeSpent, getWarnings, isClosed, returnsRows, setAfterLastRow, setBeforeFirstRow, setup
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.derby.iapi.sql.ResultSet
checkRowPosition, clearCurrentRow, getAbsoluteRow, getActivation, getBeginExecutionTimestamp, getCursorName, getEndExecutionTimestamp, getExecuteTime, getFirstRow, getLastRow, getNextRow, getPreviousRow, getRelativeRow, getRowNumber, getSubqueryTrackingArray, getTimeSpent, getWarnings, isClosed, modifiedRowCount, returnsRows, setAfterLastRow, setBeforeFirstRow
 

Field Detail

sourceResultSet

private NoPutResultSet sourceResultSet

savedSource

NoPutResultSet savedSource

constants

InsertConstantAction constants

generationClauses

private GeneratedMethod generationClauses

checkGM

private GeneratedMethod checkGM

heapConglom

private long heapConglom

autoGeneratedKeysResultSet

private ResultSet autoGeneratedKeysResultSet

autoGeneratedKeysRowsHolder

private TemporaryRowHolderImpl autoGeneratedKeysRowsHolder

resultDescription

private ResultDescription resultDescription

rowChanger

private RowChanger rowChanger

tc

private TransactionController tc

row

private ExecRow row

userSpecifiedBulkInsert

boolean userSpecifiedBulkInsert

bulkInsertPerformed

boolean bulkInsertPerformed

bulkInsert

protected boolean bulkInsert

bulkInsertReplace

private boolean bulkInsertReplace

firstRow

private boolean firstRow

needToDropSort

private boolean[] needToDropSort

indexConversionTable

private java.util.Hashtable indexConversionTable

indexedCols

private FormatableBitSet indexedCols

bulkHeapCC

private ConglomerateController bulkHeapCC

dd

protected DataDictionary dd

td

protected TableDescriptor td

indexRows

private ExecIndexRow[] indexRows

fullTemplate

private ExecRow fullTemplate

sortIds

private long[] sortIds

rowSources

private RowLocationRetRowSource[] rowSources

bulkHeapSC

private ScanController bulkHeapSC

ordering

private ColumnOrdering[][] ordering

collation

private int[][] collation

sorters

private SortController[] sorters

rowHolder

private TemporaryRowHolderImpl rowHolder

rl

private RowLocation rl

hasBeforeStatementTrigger

private boolean hasBeforeStatementTrigger

hasBeforeRowTrigger

private boolean hasBeforeRowTrigger

tableScan

private BulkTableScanResultSet tableScan

numOpens

private int numOpens

firstExecute

private boolean firstExecute

fkInfoArray

private FKInfo[] fkInfoArray

triggerInfo

private TriggerInfo triggerInfo

fkChecker

private RISetChecker fkChecker

triggerActivator

private TriggerEventActivator triggerActivator

aiCache

private NumberDataValue[] aiCache
keeps track of autoincrement values that are generated by getSetAutoincrementValues.


autoincrementGenerated

protected boolean autoincrementGenerated
If set to true, implies that this (rep)insertresultset has generated autoincrement values. During refresh for example, the autoincrement values are not generated but sent from the source to target or vice-versa.


identityVal

private long identityVal

setIdentity

private boolean setIdentity
Constructor Detail

InsertResultSet

InsertResultSet(NoPutResultSet source,
                GeneratedMethod generationClauses,
                GeneratedMethod checkGM,
                Activation activation)
          throws StandardException
Throws:
StandardException - Thrown on error
Method Detail

getResultDescription

public ResultDescription getResultDescription()
Returns the description of the inserted rows. REVISIT: Do we want this to return NULL instead?

Specified by:
getResultDescription in interface ResultSet
Overrides:
getResultDescription in class NoRowsResultSetImpl
Returns:
A ResultDescription describing the results of the statement.

changedRow

public void changedRow(ExecRow execRow,
                       RowLocation rowLocation)
                throws StandardException
Description copied from interface: TargetResultSet
Pass a changed row and the row location for that row to the target result set.

Specified by:
changedRow in interface TargetResultSet
Parameters:
execRow - The changed row.
rowLocation - The row location of the row.
Throws:
StandardException - thrown if cursor finish ed.
See Also:
TargetResultSet.changedRow(org.apache.derby.iapi.sql.execute.ExecRow, org.apache.derby.iapi.types.RowLocation)

preprocessSourceRow

public ExecRow preprocessSourceRow(ExecRow execRow)
                            throws StandardException
Preprocess the source row. Apply any check constraints here. Do an inplace cloning of all key columns. For triggers, if we have a before row trigger, we fire it here if we can. This is useful for bulk insert where the store stands between the source and us.

Specified by:
preprocessSourceRow in interface TargetResultSet
Parameters:
execRow - The source row.
Returns:
The preprocessed source row.
Throws:
StandardException - thrown on error

evaluateCheckConstraints

private void evaluateCheckConstraints()
                               throws StandardException
Run the check constraints against the current row. Raise an error if a check constraint is violated.

Throws:
StandardException - thrown on error

open

public void open()
          throws StandardException
Description copied from interface: ResultSet
Needs to be called before the result set will do anything. Need to call before getNextRow(), or for a result set that doesn't return rows, this is the call that will cause all the work to be done.

Specified by:
open in interface ResultSet
Throws:
StandardException - Standard Derby error policy

close

public void close()
           throws StandardException
Clean up resources and call close on data members.

Specified by:
close in interface ResultSet
Overrides:
close in class NoRowsResultSetImpl
Throws:
StandardException - thrown on error

verifyAutoGeneratedColumnsIndexes

private void verifyAutoGeneratedColumnsIndexes(int[] columnIndexes)
                                        throws StandardException
Verify that the auto-generated columns list (by position) has valid column positions for the table.

Throws:
StandardException

generatedColumnPositionsArray

private int[] generatedColumnPositionsArray()
                                     throws StandardException
If user didn't provide columns list for auto-generated columns, then only include columns with auto-generated values in the resultset. Those columns would be ones with default value defined.

Throws:
StandardException

uniqueColumnPositionArray

private int[] uniqueColumnPositionArray(int[] columnIndexes)
                                 throws StandardException
Remove duplicate columns from the array. Then use this array to generate a sub-set of insert resultset to be returned for JDBC3.0 getGeneratedKeys() call.

Throws:
StandardException

verifyAutoGeneratedColumnsNames

private void verifyAutoGeneratedColumnsNames(java.lang.String[] columnNames)
                                      throws StandardException
Verify that the auto-generated columns list (by name) has valid column names for the table. If all the column names are valid, convert column names array to corresponding column positions array Save that column positions array in activation. We do this to simplify the rest of the logic(it only has to deal with column positions here after).

Throws:
StandardException - Thrown on error if invalid column name in the list.

verifyAutoGenColumn

private boolean verifyAutoGenColumn(ColumnDescriptor cd)
Check that the received ColumnDescriptor corresponds to a column for which it is possible to fetch auto-generated keys.


getAutoGeneratedKeysResultset

public ResultSet getAutoGeneratedKeysResultset()
Description copied from interface: ResultSet
ResultSet for rows inserted into the table (contains auto-generated keys columns only)

Specified by:
getAutoGeneratedKeysResultset in interface ResultSet
Overrides:
getAutoGeneratedKeysResultset in class NoRowsResultSetImpl
Returns:
NoPutResultSet NoPutResultSets for rows inserted into the table.
See Also:
ResultSet.getAutoGeneratedKeysResultset()

getSetAutoincrementValue

public NumberDataValue getSetAutoincrementValue(int columnPosition,
                                                long increment)
                                         throws StandardException
getSetAutoincrementValue will get the autoincrement value of the columnPosition specified for the target table. If increment is non-zero we will also update the autoincrement value.

Parameters:
columnPosition - position of the column in the table (1-based)
increment - amount of increment.
Throws:
StandardException - if anything goes wrong.

isSourceRowResultSet

private boolean isSourceRowResultSet()

isSingleRowResultSet

private boolean isSingleRowResultSet()

normalInsertCore

private void normalInsertCore(LanguageConnectionContext lcc,
                              boolean firstExecute)
                       throws StandardException
Throws:
StandardException

getCompactRow

private ExecRow getCompactRow(ExecRow inputRow,
                              int[] columnIndexes)
                       throws StandardException
Take the input row and return a new compact ExecRow using the column positions provided in columnIndexes. Copies references, no cloning.

Throws:
StandardException

bulkInsertCore

private long bulkInsertCore(LanguageConnectionContext lcc,
                            long oldHeapConglom)
                     throws StandardException
Throws:
StandardException

bulkValidateForeignKeys

private void bulkValidateForeignKeys(TransactionController tc,
                                     ContextManager cm)
                              throws StandardException
Bulk Referential Integrity Checker

Throws:
StandardException

bulkValidateForeignKeysCore

private void bulkValidateForeignKeysCore(TransactionController tc,
                                         ContextManager cm,
                                         FKInfo fkInfo,
                                         long fkConglom,
                                         long pkConglom,
                                         java.lang.String fkConstraintName)
                                  throws StandardException
Throws:
StandardException

makeIndexTemplate

private ExecRow makeIndexTemplate(FKInfo fkInfo,
                                  ExecRow fullTemplate,
                                  ContextManager cm)
                           throws StandardException
Make a template row with the correct columns.

Throws:
StandardException

setUpAllSorts

private void setUpAllSorts(ExecRow sourceRow,
                           RowLocation rl)
                    throws StandardException
Set up to update all of the indexes on a table when doing a bulk insert on an empty table.

Throws:
StandardException - thrown on error

updateAllIndexes

private void updateAllIndexes(long newHeapConglom,
                              InsertConstantAction constants,
                              TableDescriptor td,
                              DataDictionary dd,
                              ExecRow fullTemplate)
                       throws StandardException
Update all of the indexes on a table when doing a bulk insert on an empty table.

Throws:
StandardException - thrown on error

cleanUp

public void cleanUp()
             throws StandardException
Description copied from interface: ResultSet
Tells the system to clean up on an error.

Specified by:
cleanUp in interface ResultSet
Throws:
StandardException - Thrown on error
See Also:
ResultSet.cleanUp()

verifyBulkInsert

protected boolean verifyBulkInsert()
                            throws StandardException
Verify that bulkInsert is allowed on this table. The execution time check to see if bulkInsert is allowed simply consists of checking to see if this is not a deferred mode insert and that the table is empty if this is not replace. A side effect of calling this method is to get an exclusive table lock on the table.

Returns:
Whether or not bulkInsert is allowed on this table.
Throws:
StandardException - Thrown on error

getExclusiveTableLock

private boolean getExclusiveTableLock()
                               throws StandardException
Get an exclusive table lock on the target table (and check to see if the table is populated if this is not a bulk insert replace).

Returns:
Whether or not bulkInsert is allowed on this table.
Throws:
StandardException - Thrown on error

setEstimatedRowCount

private void setEstimatedRowCount(long heapConglom)
                           throws StandardException
Set the estimated row count for this table.

Parameters:
heapConglom - Conglomerate number for the heap
Throws:
StandardException - Thrown on failure

emptyIndexes

private void emptyIndexes(long newHeapConglom,
                          InsertConstantAction constants,
                          TableDescriptor td,
                          DataDictionary dd,
                          ExecRow fullTemplate)
                   throws StandardException
Empty the indexes after doing a bulk insert replace where the table has 0 rows after the replace. RESOLVE: This method is ugly! Prior to 2.0, we simply scanned back across the table to build the indexes. We changed this in 2.0 to populate the sorters via a call back as we populated the table. Doing a 0 row replace into a table with indexes is a degenerate case, hence we allow ugly and unoptimized code.

Throws:
StandardException - Thrown on failure

getTableScanResultSet

private BulkTableScanResultSet getTableScanResultSet(long conglomId)
                                              throws StandardException
Get me a table scan result set, preferably a bulk table scan, thank you. If we already have one, reopen it.

Throws:
StandardException

getColumnNames

private java.lang.String[] getColumnNames(int[] baseColumnPositions)

finish

public void finish()
            throws StandardException
Description copied from interface: ResultSet
Tells the system that there will be no more access to any database information via this result set; in particular, no more calls to open(). Will close the result set if it is not already closed.

Specified by:
finish in interface ResultSet
Overrides:
finish in class NoRowsResultSetImpl
Throws:
StandardException - on error

Built on Thu 2010-12-23 20:49:13+0000, from revision ???

Apache Derby V10.6 Internals - Copyright © 2004,2007 The Apache Software Foundation. All Rights Reserved.