Package org.apache.derby.iapi.sql.conn

Examples of org.apache.derby.iapi.sql.conn.StatementContext


  public void insertRow() throws SQLException {
        synchronized (getConnectionSynchronization()) {
            checksBeforeInsert();
            setupContextStack();
            LanguageConnectionContext lcc = getEmbedConnection().getLanguageConnection();
            StatementContext statementContext = null;
            try {
                /*
                 * construct the insert statement
                 *
                 * If no values have been supplied for a column, it will be set
                 * to the column's default value, if any.
                 * If no default value had been defined, the default value of a
                 * nullable column is set to NULL.
                 */

                boolean foundOneColumnAlready = false;
                StringBuffer insertSQL = new StringBuffer("INSERT INTO ");
                StringBuffer valuesSQL = new StringBuffer("VALUES (");
                CursorActivation activation = lcc.lookupCursorActivation(getCursorName());

                ExecCursorTableReference targetTable =
                        activation.getPreparedStatement().getTargetTable();
                // got the underlying (schema.)table name
                insertSQL.append(getFullBaseTableName(targetTable));

                insertSQL.append(" (");
                // in this for loop we are constructing list of column-names
                // and values (?) ,... part of the insert sql
                for (int i=1; i<=resultDescription.getColumnCount(); i++) {
                    if (foundOneColumnAlready) {
                        insertSQL.append(",");
                        valuesSQL.append(",");
                    }
                    // using quotes around the column name
                    // to preserve case sensitivity
                    insertSQL.append(quoteSqlIdentifier(
                            resultDescription.getColumnDescriptor(i).getName()));
                    if (columnGotUpdated[i-1]) {
                        valuesSQL.append("?");
                    } else {
                        valuesSQL.append("DEFAULT");
                    }
                    foundOneColumnAlready = true;
                }
                insertSQL.append(") ");
                valuesSQL.append(") ");
                insertSQL.append(valuesSQL);

                // Context used for preparing, don't set any timeout (use 0)
                statementContext = lcc.pushStatementContext(
                        isAtomic,
                        false,
                        insertSQL.toString(),
                        null,
                        false,
                        0L);
                org.apache.derby.iapi.sql.PreparedStatement ps =
                        lcc.prepareInternalStatement(insertSQL.toString());
                Activation act = ps.getActivation(lcc, false);

                statementContext.setActivation(act);

                // in this for loop we are assigning values for parameters
                //in sql constructed earlier VALUES (?, ..)
                for (int i=1, paramPosition=0; i<=resultDescription.getColumnCount(); i++) {
                    // if the column got updated, do following
View Full Code Here


        // Check that the cursor is not positioned on insertRow
        checkNotOnInsertRow();
       
        setupContextStack();
        LanguageConnectionContext lcc = getEmbedConnection().getLanguageConnection();
        StatementContext statementContext = null;
        try {
            if (currentRowHasBeenUpdated == false) //nothing got updated on this row
                return; //nothing to do since no updates were made to this row

            //now construct the update where current of sql
            boolean foundOneColumnAlready = false;
            StringBuffer updateWhereCurrentOfSQL = new StringBuffer("UPDATE ");
            CursorActivation activation = lcc.lookupCursorActivation(getCursorName());


            ExecCursorTableReference targetTable = activation.getPreparedStatement().getTargetTable();
            updateWhereCurrentOfSQL.append(getFullBaseTableName(targetTable));//got the underlying (schema.)table name
            updateWhereCurrentOfSQL.append(" SET ");
     
            for (int i=1; i<=resultDescription.getColumnCount(); i++) { //in this for loop we are constructing columnname=?,... part of the update sql
                if (columnGotUpdated[i-1]) { //if the column got updated, do following
                    if (foundOneColumnAlready)
                        updateWhereCurrentOfSQL.append(",");
                    //using quotes around the column name to preserve case sensitivity
                    updateWhereCurrentOfSQL.append(quoteSqlIdentifier(
                            resultDescription.getColumnDescriptor(i).getName()) + "=?");
                    foundOneColumnAlready = true;
                }
            }
            //using quotes around the cursor name to preserve case sensitivity
            updateWhereCurrentOfSQL.append(" WHERE CURRENT OF " +
                    quoteSqlIdentifier(getCursorName()));

            // Context used for preparing, don't set any timeout (use 0)
            statementContext = lcc.pushStatementContext(isAtomic, false, updateWhereCurrentOfSQL.toString(), null, false, 0L);
            org.apache.derby.iapi.sql.PreparedStatement ps = lcc.prepareInternalStatement(updateWhereCurrentOfSQL.toString());
            Activation act = ps.getActivation(lcc, false);

            statementContext.setActivation(act);

            //in this for loop we are assigning values for parameters in sql constructed earlier with columnname=?,...
            for (int i=1, paramPosition=0; i<=resultDescription.getColumnCount(); i++) {
                if (columnGotUpdated[i-1])  //if the column got updated, do following
                    act.getParameterValueSet().getParameterForSet(paramPosition++).setValue(updateRow.getColumn(i));
View Full Code Here

            checkNotOnInsertRow();

            setupContextStack();
           
            LanguageConnectionContext lcc = getEmbedConnection().getLanguageConnection();
            StatementContext statementContext = null;
           
            //now construct the delete where current of sql
            try {
                StringBuffer deleteWhereCurrentOfSQL = new StringBuffer("DELETE FROM ");
                CursorActivation activation = lcc.lookupCursorActivation(getCursorName());
                deleteWhereCurrentOfSQL.append(getFullBaseTableName(activation.getPreparedStatement().getTargetTable()));//get the underlying (schema.)table name
                //using quotes around the cursor name to preserve case sensitivity
                deleteWhereCurrentOfSQL.append(" WHERE CURRENT OF " +
                        quoteSqlIdentifier(getCursorName()));
               
                // Context used for preparing, don't set any timeout (use 0)
                statementContext = lcc.pushStatementContext(isAtomic, false, deleteWhereCurrentOfSQL.toString(), null, false, 0L);
                org.apache.derby.iapi.sql.PreparedStatement ps = lcc.prepareInternalStatement(deleteWhereCurrentOfSQL.toString());
                // Get activation, so that we can get the warning from it
                Activation act = ps.getActivation(lcc, false);

                statementContext.setActivation(act);

                // Don't set any timeout when deleting rows (use 0)
                //execute delete where current of sql
                org.apache.derby.iapi.sql.ResultSet rs =
          ps.executeSubStatement(activation, act, true, 0L);
View Full Code Here

           * error. (Cache the LanguageConnectionContext)
           */
          LanguageConnectionContext lcc = getEmbedConnection()
              .getLanguageConnection();
                    // No timeout for this operation (use 0)
          StatementContext statementContext =
                        lcc.pushStatementContext(isAtomic,
             concurrencyOfThisResultSet==java.sql.ResultSet.CONCUR_READ_ONLY,
             getSQLText(),
                                                 getParameterValueSet(),
                                                 false, 0L);
View Full Code Here

        }

        /* Add the dependency to the StatementContext, so that
         * it can be cleared on a pre-execution error.
         */
        StatementContext sc = (StatementContext) cm.getContext(org.apache.derby.iapi.reference.ContextId.LANG_STATEMENT);
        sc.addDependency(dy);
      }
      else
      {
        /* Add a stored dependency */
        LanguageConnectionContext  lcc = getLanguageConnectionContext(cm);
View Full Code Here

        if (runTimeStatsOn) {
            if (! isTopResultSet) {
                 // This is simply for RunTimeStats.  We first need to get the
                 // subquery tracking array via the StatementContext
                StatementContext sc = activation.getLanguageConnectionContext().
                    getStatementContext();
                subqueryTrackingArray = sc.getSubqueryTrackingArray();
            }

            nextTime += getElapsedMillis(beginTime);
        }
        return result;
View Full Code Here

    {
      if (! isTopResultSet)
      {
        /* This is simply for RunTimeStats */
        /* We first need to get the subquery tracking array via the StatementContext */
        StatementContext sc = activation.getLanguageConnectionContext().getStatementContext();
        subqueryTrackingArray = sc.getSubqueryTrackingArray();
      }
      nextTime += getElapsedMillis(beginTime);
    }
      return result;
  }
View Full Code Here

        ps.setSource(sps.getText());
        ps.setSPSAction();
      }

      // save the active statement context for exception handling purpose
      StatementContext active_sc = lcc.getStatementContext();
     
      /*
      ** Execute the activation.  If we have an error, we
      ** are going to go to some extra work to pop off
      ** our statement context.  This is because we are
      ** a nested statement (we have 2 activations), but
      ** we aren't a nested connection, so we have to
      ** pop off our statementcontext to get error handling 
      ** to work correctly.  This is normally a no-no, but
      ** we are an unusual case.
      */
      try
      {
                // This is a substatement; for now, we do not set any timeout
                // for it. We might change this behaviour later, by linking
                // timeout to its parent statement's timeout settings.
        ResultSet rs = ps.executeSubStatement
          (activation, spsActivation, false, 0L);
                if( rs.returnsRows())
                {
                    // Fetch all the data to ensure that functions in the select list or values statement will
                    // be evaluated and side effects will happen. Why else would the trigger action return
                    // rows, but for side effects?
                    // The result set was opened in ps.execute()
                    while( rs.getNextRow() != null)
                    {
                    }
                }
                rs.close();
      }
      catch (StandardException e)
      {
        /*
        ** When a trigger SPS action is executed and results in
        ** an exception, the system needs to clean up the active
        ** statement context(SC) and the trigger execution context
        ** (TEC) in language connection context(LCC) properly (e.g.: 
        ** "Maximum depth triggers exceeded" exception); otherwise,
        ** this will leave old TECs lingering and may result in
        ** subsequent statements within the same connection to throw
        ** the same exception again prematurely. 
        **   
        ** A new statement context will be created for the SPS before
        ** it is executed.  However, it is possible for some
        ** StandardException to be thrown before a new statement
        ** context is pushed down to the context stack; hence, the
        ** trigger executor needs to ensure that the current active SC
        ** is associated with the SPS, so that it is cleaning up the
        ** right statement context in LCC.
        **   
        ** When the active SC is cleaned up, the TEC will be removed
        ** from LCC and the SC object will be popped off from the LCC
        ** as part of cleanupOnError logic. 
         */
       
        /* retrieve the current active SC */
        StatementContext sc = lcc.getStatementContext();
       
        /* make sure that the cleanup is on the new SC */
        if (active_sc != sc)
        {
          sc.cleanupOnError(e);
        }
       
        /* Handle dynamic recompiles */
        if (e.getMessageId().equals(SQLState.LANG_STATEMENT_NEEDS_RECOMPILE))
        {
View Full Code Here

     * LanguageConnectionContext.
     */
    beginTime = getCurrentTimeMillis();
    beginExecutionTime = beginTime;

    StatementContext sc = lcc.getStatementContext();
    sc.setTopResultSet(this, (NoPutResultSet[]) null);

    // Pick up any materialized subqueries
    subqueryTrackingArray = sc.getSubqueryTrackingArray();
  }
View Full Code Here

     */
  public void checkCancellationFlag()
        throws
            StandardException
  {
        StatementContext localStatementContext = getLanguageConnectionContext().getStatementContext();           
        if (localStatementContext == null) {
            return;
        }

        if (localStatementContext.isCancelled()) {
            throw StandardException.newException(SQLState.LANG_STATEMENT_CANCELLED_OR_TIMED_OUT);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.derby.iapi.sql.conn.StatementContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.