Home InterBase Firebird Community About us Search Help English Russian
Everything about Borland InterBase and Firebird
Get Firebird power with FIBPlus
 
Anonymous Login / Register
Site News
Third-party Product News
Programming
Miscellaneous
Encyclopedia
FAQ


Search the site


Subscribe the site news

Sections:
News
E-mail:


InterBase World is a Full Member of Firebird Foundation



Login


Hits 2621585
1715
Hosts 152020
528
Visitors 771122
718

4



Powered by Bitrix Site Manager - Content Management & Portal Solutions

Home / Article archives / Programming

FIBPlus. Master-detail. Step 4. Master-detail link. Use of a special prefix "MAS_" for parameter naming.


02/28/2004  | Views: 4668

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.

Back to the news section


 Last articles 
09/24/2007 IBSurgeon would like to inform you about a series of free Firebird technical seminars

09/23/2007 Firebird System Tables (Parts II+III)

09/20/2007 Firebird Maestro 7.9 released

09/19/2007 And the Winner is —

09/19/2007 Unable to access fireruby lib - with firebird 2.0.x and ror

news archive