-
-
Notifications
You must be signed in to change notification settings - Fork 2
incendium.db.execute_non_query
César Román edited this page Oct 21, 2021
·
16 revisions
Execute a stored procedure against the connection.
Used for UPDATE, INSERT, and DELETE statements.
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 (list[InParam]): A Dictionary containing all
INPUT
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.
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.
import traceback
from incendium import constants, db, exceptions, util
from incendium.db import InParam
from java.lang import Exception as JException
def insert():
# Initialize variables.
transaction_id = system.db.beginTransaction(timeout=30000)
try:
# Build params.
params = [
InParam("int_param", system.db.INTEGER, 1),
InParam("decimal_param", system.db.DECIMAL, 1.0000),
InParam("varchar_param", system.db.VARCHAR, "VARCHAR value"),
InParam("nvarchar_param", system.db.NVARCHAR, "NVARCHAR value"),
InParam("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 as 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.format(
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)