Step 4. Master-detail link. Use of a special prefix "MAS_" for parameter naming.
Now we can pass to creation of the master-detail link. For this we drop on the form the following components:
DBGrid2: TDBGrid;
DataSource2: TDataSource;
pFIBDataSet2: TpFIBDataSet;
ReadTransaction2: TpFIBTransaction;
WriteTransaction2: TpFIBTransaction;
Now let's link them in the same way as the previous group of component. Then we write SelectSQL for pFIBDataSet2:
SELECT * FROM EMPLOYEE
WHERE DEPT_NO = ?DEPT_NO
It is obvious that with the help of a detail-query we want to select only those employees, who work in the current department. The value of the ??DEPT_NO? parameter must be taken from the DEPT_NO field of the DEPARTMENT table. For making it automatic set pFIBDataSet2.DataSource equal DataSource1. Now we generate modifying queries for pFIBDataSet2 in the same way as earlier for pFIBDataSet1.
After automatic generation we need to make some changes in the got queries. That is in particular a query for InsertSQL:
INSERT INTO EMPLOYEE(
EMP_NO, FIRST_NAME, LAST_NAME, PHONE_EXT, HIRE_DATE,
DEPT_NO,
JOB_CODE, JOB_GRADE, JOB_COUNTRY, SALARY
)
VALUES(
?EMP_NO, ?FIRST_NAME, ?LAST_NAME, ?PHONE_EXT, ?HIRE_DATE,
?DEPT_NO,
?JOB_CODE, ?JOB_GRADE, ?JOB_COUNTRY, ?SALARY
)
It is obvious that after adding a new employee we should set the ??DEPT_NO? parameter by the current value of the DEPT_NO field from the DEPARTMENT table. FIBPlus allows doing it automatically with the help of the prefix �MAS_�:
INSERT INTO EMPLOYEE(
EMP_NO, FIRST_NAME, LAST_NAME, PHONE_EXT, HIRE_DATE,
DEPT_NO,
JOB_CODE, JOB_GRADE, JOB_COUNTRY, SALARY
)
VALUES(
?EMP_NO, ?FIRST_NAME, ?LAST_NAME, ?PHONE_EXT, ?HIRE_DATE,
?MAS_DEPT_NO,
?JOB_CODE, ?JOB_GRADE, ?JOB_COUNTRY, ?SALARY
)
Now we explicitly set that the value for EMPLOYEE.DEPT_NO should be taken from the DEPT_NO field of the DEPARMENT table, which is a master table for the EMPLOYEE one. The same changes must be made for UpdateSQL:
UPDATE EMPLOYEE SET
FIRST_NAME = ?FIRST_NAME,
LAST_NAME = ?LAST_NAME,
PHONE_EXT = ?PHONE_EXT,
HIRE_DATE = ?HIRE_DATE,
DEPT_NO = ?MAS_DEPT_NO,
JOB_CODE = ?JOB_CODE,
JOB_GRADE = ?JOB_GRADE,
JOB_COUNTRY = ?JOB_COUNTRY,
SALARY = ?SALARY
WHERE
EMP_NO = ?OLD_EMP_NO
Now let's make some code changes in the procedure:
procedure TForm1.Button1Click(Sender: TObject);
begin
pFIBDatabase1.DBName := DBNameE.Text;
pFIBDatabase1.ConnectParams.UserName := UserNameE.Text;
pFIBDatabase1.ConnectParams.Password := PasswordE.Text;
pFIBDatabase1.Open;
pFIBDataSet1.Open;
pFIBDataSet2.Open;
end;
Now we can run an application. While navigating up and down on DBGrid1 you can see automatic contents changes in DBGrid2. The master-detail link now works:
Figure 14.
See the full example code.
|