Skip to content

incendium.db.get_output_params

César Román edited this page Apr 30, 2024 · 15 revisions

Description

Get the OUT[PUT] parameters from the Stored Procedure.

Syntax

incendium.db.get_output_params(stored_procedure, output, [database], [transaction], [params])

Args:

  • stored_procedure (str): The name of the stored procedure to execute.
  • output (list[OutParam]): A list containing all OUTPUT parameters as OutParam objects.
  • database (str): The name of the database connection to execute against. If omitted or "", the project's default database connection will be used. Optional.
  • transaction (str): A transaction identifier. If omitted, the call will be executed in its own transaction. Optional.
  • params (list[InParam]): A list containing all INPUT parameters as InParam objects. Optional.

Returns:

  • dict: A Python dictionary of OUTPUT parameters.

Recommendations

None.

Code Examples

The incendium.db.check function is the best example of usage.

import system.db
from incendium import db
from incendium.db import InParam, OutParam


def check(stored_procedure, params=None):
    """Execute a stored procedure that returns a flag (TRUE or FALSE).

    Args:
        stored_procedure (`str`): The name of the stored procedure
        params (`list`[`InParam`]): A list containing all INPUT parameters
            as InParam objects. Optional.

    Returns:
        bool: The flag.
    """
    output = [OutParam("flag", system.db.BIT)]
    output_params = db.get_output_params(stored_procedure, output=output, params=params)

    return output_params["flag"]

Another option would be to retrieve a newly inserted identity where we don't care about the number of rows that were updated. For the latter, please check [o_execute_non_query].

import system.date
import system.db
from incendium import db
from incendium.db import InParam, OutParam


def save(transaction_id=None):
    """Insert or Update data.

    Args:
        transaction_id (`str`): A transaction identifier.
    """
    # Initialize INPUT parameters.
    params = [
        InParam("id", system.db.INTEGER, 1),
        InParam("name", system.db.VARCHAR, "Name"),
        InParam("created_date", system.db.TIMESTAMP, system.date.now()),
        InParam(
            "created_t_stamp",
            system.db.BIGINT,
            system.date.toMillis(system.date.now()),
        ),
    ]
    # Stored procedure includes the following parameter:
    # @new_id INT OUTPUT
    # To set @new_id, run this statement:
    # SET @new_id = SCOPE_IDENTITY();
    output = [OutParam("new_id", system.db.INTEGER)]

    output_params = db.get_output_params(
        "schema.stored_procedure",
        transaction=transaction_id,
        output=output,
        params=params,
    )

    return output_params["new_id"]

o_execute_non_query

Clone this wiki locally