Defining Function Instances

This chapter describes how to define function instances.

Previous Topic

Next Topic

Function Instance Definition

Functions and operators (jointly referred to in this chapter as "functions") can be used in many contexts. For example, you can use the operator "+" to add numbers, concatenate strings, or to add two user-defined data type values. The use of a function within a given context is called a function instance.

The definition of a function instance links a function definition and the function's arguments to a procedure (user-written executable procedure, not a database procedure) and a result (type and length). For every new function that you define, you must provide the DBMS Server with function instance definitions for its use. You must also provide the function instance definitions that define the use of your new data type with the functions with which you intend to use it.

Previous Topic

Next Topic

Structure IIADD_FI_DFN

Function instances are defined by filling in the structure IIADD_FI_DFN, once for each instance. IIADD_FI_DFN is included in the header file provided with Object Management Extension. The fields in this structure are as follows:

Previous Topic

Next Topic

Length Definition of Result Data Type

The length of a result data type is defined by fid_rlength in II_ADD_FI_DFN or an external lenspec routine. This length is used by the DBMS Server when manipulating values internally. The length component of the actual data value (II_DATA_VALUE db_length) should not be changed within the function itself. If the length defined by fid_rlength or the lenspec routine differs from the data value's db_length, then errors may result or data values may be incorrectly interpreted.

The same is true of scale and precision in the case of DECIMAL. In this case, the fid_rprec or lenspec is used and the db_prec of the data value should not be changed.

The following macros, defined in $II_SYSTEM/ingres/files/iiadd.h, can be used to manipulate DECIMAL length values:

Previous Topic

Next Topic

External Lenspec Routine—Return Result Length of Specified Value

The lenspec routine returns the result length for the specified value in the db_length field of the dvr parameter. This routine is called by the DBMS Server when a function instance's fid_rltype field is set to II_RES_EXTERN. Lenspec also returns II_STATUS.

The parameters for the lenspec routine are as follows:

The II_DATA_VALUE parameters are used only to pass type and length information. The db_data field in the II_DATA_VALUE structure must be ignored.

Previous Topic

Next Topic

Complementary Function Instances

To optimize query performance, the DBMS Server often inverts the sense of a comparison operation. To make this possible, you must define a complementary function instance for every function instance of fid_optype II_COMPARISON that you define.

For example, if you define a function instance for the equals (=) operator, you must also define a function instance for its complement, not equals (!=). Note that the complement of ">=" is "<", and the complement of "<=" is ">".

Complementing function instance definitions must agree. That is, if A is a function instance whose complement is B (A's fid_cmplmnt = B's fid_opid), the complement of B must be A (B's fid_cmplmnt = A's fid_opid).

Previous Topic

Next Topic

Sorting of the Function Instance Definition Array

For the DBMS Server to merge the new function instance definitions with existing function instance definitions, the array of function instance definitions that you create must be specified in sorted order.

The array must be sorted by the number value of the fid_optype field. All comparisons (fid_optype = II_COMPARISON) must be first, followed by all operators (fid_optype = II_OPERATOR), normal functions (fid_type = II_NORMAL), and finally, the coercion functions (fid_optype = II_COERCION).

In each operator type, sort the function instances by the numeric value of the fid_opid field. For example, with the operator type II_COMPARISON, the function instances are sorted in the following order:

  1. 'not equal' function instances
  2. 'less than' function instances
  3. 'less than/equal to' function instances
  4. 'equal' function instances
  5. 'greater than' function instances
  6. 'greater than/equal to' function instances

Previous Topic

Next Topic

Methods for Defining Function Instances for Large Objects

Object Management Extension provides two ways to define function instances for large objects. You can use the Ingres-supplied filter functions or you can manipulate the large objects directly.

Previous Topic

Next Topic

Ingres-supplied Filter Functions

Of the callback functions, two have been paired to allow for function instances that must traverse a large object. Ingres provides skeleton code that allows you to pass the object through your filter.

To use a filter function, the calling instance sets up the workspace by calling the function supplied to initialize the workspace. This function does all the work necessary to allow for the simple traversal of the input object. After this, your filter function is called for each segment and is passed an II_DATA_VALUE, which provides space for manipulation of the input and output segments, a function to call for each segment, a workspace to be used by this routine, and a continuation indicator.

The continuation indicator states whether this is the first or last call to the filter. The indicator is specified by the use of the II_C_BEGIN_MASK and the II_C_END_MASK, indicating that this is the beginning or end of the resulting object.

The function to be called is called with pointers to II_DATA_VALUE, which describes input and output segments. The routine is expected to convert the input into the output segment. When processing the data, the function indicates the disposition of the current segment by filling in the value of the adw_shared.shd_exp_action field of the workspace.

As the routine moves through the object, it is expected to keep the adw_shared.shd_l1_chk field filled with the current length of the large object that is the result. The routine that is called back does not necessarily know if it will be called again.

The expected action indicator conveys information between the filter routine and the function instance routine. The indicator is set to ADW_START before the first segment routine call. Thereafter, the indicator is examined at the return of the call to determine the next action necessary for the function instance routine to perform.

The possible values and responses are as follows:

If the routine returns ADW_GET_DATA and there is no more data to get, the function instance routine disposes of the current segment and assumes that the work of the routine is complete. The routine is not called again.

This calling sequence means that the routine never knows if it will be called again. For this reason, the adw_l0_chk and adw_l1_chk values must be correct, and the output segment from your filter must always be a valid segment, even when the filter is not complete.

Of the fields in the workspace, only those in the shared section are interpreted by the filter function. The shd_exp_action, shd_l0_chk, and shd_l1_chk fields must be set as described above. The other fields in the II_SHARED structure must be left unchanged.

The remaining fields in the workspace are available for each function instance's use.

Previous Topic

Next Topic

Direct Manipulation of Large Objects

In some cases, (for example, two-pass algorithms,) defining a function instance does not work. For these situations, your code can get the peripheral object segments directly through the use of the large object handler. The handler routine performs a variety of operations on large objects.

The handler is called with two arguments. The first is the operation code, which tells the handler which operation to perform. The second argument is a peripheral object control block, II_POP_CB. This control block structure is used to pass information to and from the caller of the handler. The first part of this structure is a standard header used in the DBMS Server.

The exact description of this control block structure can be found in the iiadd.h header file.

The pop_type field must always be set to II_POP_TYPE. The pop_length field must always be set to the size of the structure. The pop_s_reserved and pop_l_reserved fields must not be set or altered. These fields are used by the DBMS Server memory management routines. You can set the other fields as required.

The rest of the II_POP_CB control block consists of the following fields:

Previous Topic

Next Topic

II_INFORMATION Operation—Return Maximum Length of Peripheral Object Segments

This operation returns the maximum length (in bytes) for each segment of a peripheral object.

Inputs

The input fields for the II_INFORMATION operation are the parts of the peripheral control block structure that tell the DBMS Server what information to pass and how to pass the information. The inputs are as follows:

Outputs

The output fields for the II_INFORMATION operation describe the results the DBMS Server returns. The outputs are as follows:

Returns

II_STATUS

Previous Topic

Next Topic

II_GET Operation—Get Next Segment

This operation gets the next segment of some object. The object is represented to II_GET by a coupon. This operation makes use of the saved information in the peripheral control block, which is passed in the pop_cb's user argument parameter.

Inputs

The input fields for the II_GET operation are the parts of the peripheral control block structure that tell the DBMS Server what and how to pass the information. The inputs are as follows:

Outputs

The output fields for the II_GET operation describe the results the DBMS Server returns. The outputs are as follows:

Returns

II_STATUS

Previous Topic

Next Topic

II_PUT Operation—Add a New Segment

This operation adds a new segment to the end of a new peripheral object. This operation takes, as input, the segments of a peripheral object, and returns a completed coupon. When there are multiple segments in an object, the coupon is not complete until the last invocation of the II_PUT operation.

The underlying data type must be provided by the caller, and describes how the underlying data must be represented. See the pop_underdv field in the table below. For example, long varchar objects are stored as varchar segments.

Inputs

The input fields for the II_PUT operation are the parts of the peripheral control block structure that tell the DBMS Server what and how to pass the information. The inputs are as follows:

Outputs

The output fields for the II_PUT operation describe the results returned by the DBMS Server. The outputs are as follows:

Returns

II_STATUS

Previous Topic

Next Topic

II_COPY Operation—Move a Peripheral Object

This operation moves a peripheral object by performing gets and puts.

Inputs

The input fields for the II_COPY operation are the parts of the peripheral control block structure that tell the DBMS Server what and how to pass the information. The inputs are as follows:

Outputs

The output fields for the II_COPY operation describe the results returned by the DBMS Server. The outputs are as follows:

Returns

II_STATUS


© 2007 Ingres Corporation. All rights reserved.