Data Type Conversion
An Ada variable declaration must be compatible with the Ingres value it represents. Numeric Ingres values can be set by and retrieved into numeric variables, and Ingres character values can be set by and retrieved into character string variables.
Data type conversion occurs automatically for different numeric types, such as from floating-point Ingres database column values into integer Ada variables, and for character strings, such as from varying-length Ingres character fields into fixed-length Ada character string buffers.
Ingres does not automatically convert between numeric and character types. You must use the Ingres type conversion operators, the Ingres ascii function, or an Ada conversion procedure for this purpose.
The following table shows the default type compatibility for each Ingres data type. Note that some Ada types do not match exactly and, consequently, may go through some runtime conversion.
Ingres TYPES and Corresponding Ada Data Types
Runtime Numeric Type Conversion
The Ingres runtime system provides automatic data type conversion between numeric-type values in the database and forms system and numeric Ada variables. The standard type conversion rules (according to standard VAX rules) are followed. For example, if you assign a float variable to an integer-valued field, the digits after the decimal point of the variable's value are truncated. Runtime errors are generated for overflow on conversion.
The Ingres money type is represented as long_float, an 8-byte floating-point value.
Runtime Character Type Conversion
Automatic conversion occurs between Ingres character string values and Ada character string variables. There are four string-valued Ingres objects that can interact with character string variables. They are Ingres names, such as form and column names, database columns of type c, char, text or varchar, and form fields of type character. Several considerations apply when dealing with character string conversions, both to and from Ingres.
The conversion of Ada character string variables used to represent Ingres names is simple: trailing blanks are truncated from the variables, because the blanks make no sense in that context. For example, the string literals "empform" and "empform" refer to the same form.
The conversion of other Ingres objects is a little more complicated. First, the storage of character data in Ingres differs according to whether the medium of storage is a database column of type c or character, a database column of type text or varchar, or a character form field. Ingres pads columns of type c or character with blanks to their declared length. Conversely, it does not add blanks to the data in columns of type text or varchar or in form fields.
Second, EQUEL assumes that the convention is to blank-pad fixed-length character strings. Character string variables not blank-padded may be storing ASCII nulls or data left over from a previous assignment. For example, the character string "abc" may be stored in an Ada string(1..5) variable as the string "abc " followed by two blanks.
When character data is retrieved from a Ingres database column or form field into an Ada character string variable and the variable is longer than the value being retrieved, the variable is padded with blanks. If the variable is shorter than the value being retrieved, the value is truncated. You should always ensure that the variable is at least as long as the column or field, in order to avoid truncation of data.
When inserting character data into an Ada Ingres database column or form field from an Ada variable, note the following conventions:
• When data is inserted from an Ada variable into a database column of type c or character and the column is longer than the variable, the column is padded with blanks. If the column is shorter than the variable, the data is truncated to the length of the column.
• When data is inserted from an Ada variable into a database column of type text and the column is longer than the variable, no padding of the column takes place. Furthermore, by default, all trailing blanks in the data are truncated before the data is inserted into the text or varchar column. For example, when a string "abc" stored in an Ada string(1..5) variable as "abc " (refer to above) is inserted into the text or varchar column, the two trailing blanks are removed and only the string "abc" is stored in the database column. To retain such trailing blanks, you can use the EQUEL notrim function. It has the following syntax:
notrim(stringvar)
where stringvar is a character string variable. An example demonstrating this feature follows later. When used with repeat queries, the notrim syntax is:
@notrim(stringvar)
If the text or varchar column is shorter than the variable, the data is truncated to the length of the column.
• When data is inserted from an ADA variable into a character form field and the field is longer than the variable, no padding of the field takes place. In addition, all trailing blanks in the data are truncated before the data is inserted into the field. If the field is shorter than the data (even after all trailing blanks have been truncated), the data is truncated to the length of the field.
When comparing character data in an Ingres database column with character data in an ADA variable, note the following convention:
• When comparing data in c, character, or varchar database columns with data in a character variable, all trailing blanks are ignored. Trailing blanks are significant in text. Initial and embedded blanks are significant in character, text, and varchar; they are ignored in c.
As described above, the conversion of character string data between Ingres objects and ADA variables often involves the trimming or padding of trailing blanks, with resultant change to the data. If trailing blanks have significance in your application, give careful consideration to the effect of any data conversion. For information on the significance of blanks when comparing with various Ingres character types, see the QUEL Reference Guide.
The Ingres date data type is represented as a 25-byte character string.
The program fragment in the following example demonstrates the notrim function and the truncation rules explained above.
-- Assume that a table called "textchar" has been created with
-- the following CREATE statement:
-- CREATE textchar
-- (row = integer4,
-- data = text(10)) -- Note the text data type
## with EQUEL;
...
## row: Integer;
## data: String(1..7) := (1..7 => ' ');
...
data(1..3) := "abc "; -- Holds "abc" followed by 4 blanks
-- The following APPEND adds the string "abc" (blanks truncated)
## APPEND TO textchar (#row = 1, #data = data)
-- This statement adds the string "abc ", with 4 trailing
-- blanks left intact by using the NOTRIM function.
## APPEND TO textchar (#row = 2, #data = notrim(data))
-- This RETRIEVE will retrieve row #2, because the NOTRIM
-- function left trailing blanks in the "data" variable
-- in the last APPEND statement.
## RETRIEVE (row = textchar.#row)
## WHERE length (textchar.#data) = 7
put("Row found = ");
put(row);