The BASED ON clause lets you extract from the database the
data type and size of a field and then use it to declare host
language types. These are Pascal TYPE(s), and C typedef(s). When
you preprocess your program, the RDML preprocessor assigns the
data type and size attributes associated with the field to the
variable or function you declare using the the BASED ON clause.
See the DECLARE_VARIABLE statement for information on declaring
host language variables.
1 – Examples
The following programs demonstrate the use of the BASED ON clause
to declare types. Both the C and Pascal programs use the BASED
ON clause to declare the function, job_name. These programs pass
the value of the field JOB_CODE to the function. The function
determines the job title associated with the job code and passes
the job title back to the calling program. Note that in the C
program a program variable, temp_job_name, is required so that
the function can return the job title to the calling program.
In Pascal, function values are returned to the calling program
automatically.
1.1 – C Example
#include <stdio.h>
DATABASE PERS = FILENAME "PERSONNEL";
typedef BASED ON JOBS.JOB_CODE job_code_type;
typedef BASED ON JOBS.JOB_TITLE job_title_type;
DECLARE_VARIABLE temp_job_name SAME AS JOBS.JOB_TITLE;
job_title_type job_name(job)
job_code_type job;
{ /* begin function */
READY PERS;
START_TRANSACTION READ_ONLY;
FOR FIRST 1 J IN JOBS
WITH J.JOB_CODE = job
strcpy (temp_job_name, J.JOB_TITLE);
END_FOR;
COMMIT;
FINISH;
return temp_job_name;
} /* end of function */
main ()
{
printf ("%s\n",job_name("APGM"));
}
1.2 – Pascal Example
program based_on_clause (INPUT,OUTPUT);
DATABASE PERS = FILENAME 'PERSONNEL';
type
job_code_type = BASED ON JOBS.JOB_CODE;
job_title_type = BASED ON JOBS.JOB_TITLE;
function job_name (job : JOB_CODE_TYPE ) : JOB_TITLE_TYPE;
begin {* function *}
READY PERS;
START_TRANSACTION READ_ONLY;
FOR FIRST 1 J IN JOBS
WITH J.JOB_CODE = job
job_name := J.JOB_TITLE;
END_FOR;
COMMIT;
FINISH;
end; {* function *}
begin {* main *}
writeln (job_name ('APGM'));
end.
2 – Format
(B)0[m[4mBASED[m [4mON[m qqqqqwqq>qqqqqqqqqqqqqqqqqqqqwqqqqqqqqk
mqq> db-handle qq> . qqqj x
x
lqqqqqqqqqqqqqqqqqqqqqq<qqqqqqqqqqqqqqqqqqqqqqqj
x
mqqqqq> relation-name qqqq> . qqqq> field-name
2.1 – Format arguments
db-handle Database handle. A host variable used
to refer to a specific database you have
invoked. For more information see the
entry on the Database Handle clause.
relation-name The name of a relation in the database.
field-name The name of a field in a relation. For
example, once you have defined E as
the context variable for the EMPLOYEES
relation, E.LAST_NAME is a value
expression that refers to a value from
the LAST_NAME field of EMPLOYEES.