Skip to content

incendium.db.execute_non_query

Cesar Roman edited this page Sep 23, 2020 · 16 revisions

Description

Executes a stored procedure against the connection and returns the number of rows affected.

Used for UPDATE, INSERT, and DELETE statements.

Syntax

incendium.db.execute_non_query(stored_procedure, database, params, transaction)

Args:

  • stored_procedure (str): The name of the stored procedure to execute.
  • database (str): The name of the database connection to execute against. If omitted or "", the project's default database connection will be used. Optional.
  • params (dict): A Dictionary containing all parameters. Optional.
  • transaction (str): A transaction identifier. If omitted, the call will be executed in its own transaction. Optional.

Returns:

  • int: The number of rows modified by the stored procedure, or -1 if not applicable.

Recommendations

We recommend using transactions for all DELETE, INSERT, and UPDATE statements, since in some situations you may be modifying more than one database table at a time.

Code Examples

# Imports.
import traceback

from incendium import (constants, db, exceptions, util)
from java.lang import Exception as JException

def insert():
    # Initialize variables.
    transaction_id = system.db.beginTransaction(timeout=30000)
    
    try:
        # Build params.
        params = {
            'int_param': [system.db.INTEGER, 1],
            'decimal_param': [system.db.DECIMAL, 1.0000],
            'varchar_param': [system.db.VARCHAR, 'VARCHAR value'],
            'nvarchar_param': [system.db.NVARCHAR, 'NVARCHAR value'],
            'datetime_param': [system.db.TIMESTAMP, system.date.now()]
        }
        # Call stored procedure.
        # TODO: Do something with the update_count value returned by execute_non_query
        update_count = db.execute_non_query('schema.stored_procedure', params, transaction_id)
        # Commit transaction.
        system.db.commitTransaction(transaction_id)
    except JException, e:
        # system.db functions throw java.lang.Exception
        # Rollback transaction.
        system.db.rollbackTransaction(transaction_id)
        # Get error message to raise ApplicationError.
        message = constants.UNEXPECTED_ERROR_CAUSED_BY % (
            util.get_function_name(), # Function's name.
            '\n'.join(traceback.format_exc().splitlines()), # Preserved traceback.
            e.cause
        )
        # Raise ApplicationError.
        raise exceptions.ApplicationError(message, e, e.cause) # Handle this at the event calling this function.
    finally:
        # Close transaction.
        system.db.closeTransaction(transaction_id)
Clone this wiki locally