![]() | Programming Guide | ![]() |
Adds or replaces a bind value in a DBMS EXECUTE statement for SQL generation
#include <tmusubs.h>int dm_gen_change_execute_using(char *arg, char *bind_parm, char *bind_val, int occ, int relative, int flag);
arg- Reserved for future use.
bind_parm- Specifies the bind parameter.; if a null pointer or empty string, the clause is not built.
bind_val- Specifies the bind value; if a null pointer or an empty string, the clause is not built.
occ- Specifies the occurrence number.
relative- Specifies how to use the occurrence number with one of the following values:
DM_GEN_ABSOLUTE_OCCUR
DM_GEN_RELATIVE_TO_PARENT
DM_GEN_RELATIVE TO CHILDflag- Specifies the type of change to make with one of the following constants:
DM_GEN_APPEND- When
flagis set to this value,bind_valis added to end of theUSINGclause. This produces the following statement:DBMS WITH CURSOR cursor EXECUTE USING
existing_parentTV_binds,
existing_childTV_binds,
bind_parm = bind_val[occ]DM_GEN_PREPEND- When
flagis set to this value,bind_valis added to the beginning of theUSINGclause. This produces the following statement:DBMS WITH CURSORcursorEXECUTE USING
bind_parm = bind_val[occ],
existing_parentTV_binds,
existing_childTV_bindsDM_GEN_REPLACE_ALL- When
flagis set to this value,bind_valreplaces the previousUSINGclause. This produces the following statement:DBMS WITH CURSORcursorEXECUTE USING
bind_parm = bind_val[occ]- If
flagis set to this value and the other arguments are empty strings, theUSINGclause is removed.
dm_gen_change_execute_usinglets you edit theUSINGclause of aDBMS EXECUTEstatement. The data structure for theSELECTstatement, which is built by a call to dm_gen_sql_info (generally in theTM_SEL_GENevent), must already exist before this function is called. Note that this function must be called once for each bind value you wish to change.Often, a call to
dm_gen_change_execute_usingfollows a call to the function dm_gen_change_select_where. If new parameters are added to theWHEREclause's search conditions, those parameters must also be added to theEXECUTE USINGstatement.This function can be implemented as part of a transaction manager event function that processes the
TM_SEL_BUILD_PERFORMevent. If you are modifying the select processing for a server view, call thedm_gen_change_execute_usingfunction from an event function attached to the first parent table view in the server view.To view a sample event function written in JPL, refer to the example in this section. For more information on writing transaction event functions, refer to Chapter 32, "Writing Transaction Event Functions," in Application Development Guide.
The settings for
relativeandoccurrencedetermine the value forocc, the occurrence number used in the statement.If
relativeis set toDM_GEN_RELATIVE_TO_PARENTorDM_GEN_RELATIVE_TO_CHILD, the current occurrence in the parent or child table view is used as the basis for the occurrence number. Then, the setting foroccurrenceis checked. Ifoccurrenceis 0, the current occurrence in that table view is used in the statement. Ifoccurrenceis greater than 0, the occurrence is calculated by adding the specified occurrence to the current occurrence.If you only need to substitute an occurrence number in the statement processing, set
relativetoDM_GEN_ABSOLUTE_OCCURand setoccurrenceto be greater than 0.
# JPL Procedure:
# Generate IN clause using binding parameters.
# Function property is set to titles_exec.
proc titles_exec (event)
if (event == TM_SEL_BUILD_PERFORM)
{
vars retval(5), occ(3), i(3), in_buffer(255), comma(1)
occ = @widget("qbe_titleid")->num_occurrences
# If the array "qbe_titleid" contains data,
# build a SQL "in" clause.
if (occ > 0)# First loop through qbe_titleid and build an IN clause
# in the form "title_id" in (::p1, ::p2, ::p3).
{
for i=1 while i <= occ
{
if (qbe_titleid[i] != "")
{
%.0 i = i
in_buffer = in_buffer ## comma ## "\:\:p" ## i \
comma = ","
}
}
in_buffer = "title_id in (" ## in_buffer ## ")"
retval = dm_gen_change_select_where \
("", in_buffer, DM_GEN_APPEND)# Now loop through qbe_titleid and change the EXECUTE
# USING statement. This could be done in the previous loop.
# It is separated for clarity.
for i=1 while i <= occ
{
if (qbe_titleid[i] != "")
{
%.0 i = i
retval=dm_gen_change_execute_using \
('', "p:i", "qbe_titleid", i, \
DM_GEN_ABSOLUTE_OCCUR, DM_GEN_APPEND)
}
}
if (retval != 0)
return TM_FAILURE
}
}
return TM_PROCEED
The following example uses the current occurrence in the parent table view to specify the occurrence number. The parent table view in this sequential link is a list of customers. When you enter one of the
rental_statuscodes for a customer in theqbe_statusfield, the rentals for that customer which match that status populate the child table view.# JPL Procedure:
# Generate WHERE and EXECUTE USING clause using occurrence
# in parent table view. The Function property for rentals
# table view is set to rentals_hook.proc rentals_hook(event)
{
vars whexp(100) retval(5)
if (event==TM_SEL_BUILD_PERFORM)
{
# Build the following: correlation.rental_status = ::qbe1
whexp=dm_gen_get_tv_alias(sm_tm_pinquire(TM_TV_NAME)) \
## ".rental_status" \
## "=" \
## "::::qbe1"
# Add it to the WHERE clause.
retval = dm_gen_change_select_where("", whexp,\
DM_GEN_APPEND)
# Append to the EXECUTE USING clause in the form:
# qbe1 = qbe_stat[<occ>]
# where occ is the same occurrence number as the current
# occurrence in parent table view.
retval = dm_gen_change_execute_using\
("", "qbe1", "qbe_stat", \
0, DM_GEN_RELATIVE_TO_PARENT, DM_GEN_APPEND)
}
return TM_PROCEED
}