4. Working with Classes : How You Can Create a User Class : Examples of Method: Hire Method : Employee Version
 
Share this page                  
Employee Version
In this top-level version of the Hire method, the method script defines the functions described in the following steps. The attributes referenced in this script are set interactively by the user in a frame whose fields are mapped directly to the user class. The method script does the following:
1. Declares a variable to store error status:
method Hire() =
declare
    errstat = integer not null;
enddeclare
2. Checks the employee's hire date. If no date has been assigned, supplies the current date:
if CurObject.HireDate = '' then
   CurObject.HireDate = 'today';
endif;
3. Generates an employee number by increasing the sequence value in v_highest_number:
update v_highest_number set h_number = h_number
+ 1 where tbl_name = 'emp'
4. Selects the highest employee number into the EmpNum attribute for the current employee:
select h_number as :CurObject.EmpNum from
    v_highest_number
where tbl_name = 'emp';
5. Inserts a new row into both the emp and jobs tables and checks for errors:
insert into emp (emp_num, emp_type, last_name,
  first_name, hire_date, term_date, term_reason)
values (:CurObject.EmpNum, :CurObject.Type,
   :CurObject.LastName, :CurObject.FirstName,
   :CurObject.HireDate,
   :CurObject.TerminationDate,
   :CurObject.TerminationReason);
if CurMethod.DBSession.ErrorNumber = 0 then
insert into jobs (emp_num, job_class, pay,
   start_date)
values (:CurObject.EmpNum,
   :CurObject.CurrentClass,
   :CurObject.CurrentPay, :CurObject.HireDate);
endif;
if CurMethod.DBSession.ErrorNumber != 0 then
rollback;
message 'Database error; unable to insert
   employee record.';
endif;
return CurMethod.DBSession.ErrorNumber;
end