Previous Topic

Next Topic

Manage

The manage step provides data management functionality for your application and/or process.

Below is the code for Manage():

procedure TForm1.Manage();
begin
    // populate the tables with data
    Add_CustomerMaster_Records;
    Add_ItemMaster_Records;

    Add_Transactions();

    // display the orders and their items
    Display_CustomerOrders();
    Display_OrderItems();
end;

procedure TForm1.Add_CustomerMaster_Records;
const
    data : array [1..4] of string = (
        '(''1000'', ''92867'', ''CA'', ''1'', ''Bryan Williams'', ''2999 Regency'', ''Orange'')',
        '(''1001'', ''61434'', ''CT'', ''1'', ''Michael Jordan'', ''13 Main'', ''Harford'')',
        '(''1002'', ''73677'', ''GA'', ''1'', ''Joshua Brown'', ''4356 Cambridge'', ''Atlanta'')',
        '(''1003'', ''10034'', ''MO'', ''1'', ''Keyon Dooling'', ''19771 Park Avenue'', ''Columbia'')'
    );
    nRecords : integer = 4;
var
    i : integer;
    str : string;
begin
    try
        SQLConnection1.StartTransaction(td);
        for i := 1 to nRecords do
        begin
            // add one record at time to table
            str := 'INSERT INTO custmast VALUES ' + data[i];
            SQLConnection1.ExecuteDirect(str);
        end;
        SQLConnection1.Commit(td);
    except on E: Exception do Handle_Exception(E);
    end;
end;


procedure TForm1.Add_ItemMaster_Records;
const
    data : array [1..4] of string = (
        '(''10'', ''19.95'', ''1'', ''Hammer'')',
        '( ''3'',  ''9.99'', ''2'', ''Wrench'')',
        '( ''4'',  ''16.59'',''3'', ''Saw'')',
        '( ''1'',  ''3.98'', ''4'', ''Pliers'')'
    );
    nRecords : integer = 4;
var
    i : integer;
    str : string;
begin
    try
        SQLConnection1.StartTransaction(td);
        for i := 1 to nRecords do
        begin
            // add one record at time to table
            str := 'INSERT INTO itemmast VALUES ' + data[i];
            SQLConnection1.ExecuteDirect(str);
        end;
        SQLConnection1.Commit(td);
    except on E: Exception do Handle_Exception(E);
    end;
end;


procedure TForm1.Add_Transactions;
const
    orders : array [1..3, 1..4] of string = (
        ('09/01/2002', '09/05/2002', '1', '1001'),
        ('09/02/2002', '09/06/2002', '2', '9999'), // bad customer number
        ('09/22/2002', '09/26/2002', '3', '1003')
    );
    items : array [1..6, 1..4] of string = (
        ('1', '1', '2', '1'),
        ('1', '2', '1', '2'),
        ('2', '1', '1', '3'),
        ('2', '2', '3', '4'),
        ('3', '1', '2', '3'),
        ('3', '2', '2', '99') // bad item number
    );
    nOrders : integer = 3;
    nItems : integer = 6;
var
    i : integer;
    j : integer;
    str : string;
begin
    j := 1;
    // process orders
    for i := 1 to nOrders do
    begin
        try
            // start a transaction
            SQLConnection1.StartTransaction(td);
            str := 'INSERT INTO custordr VALUES (' +
                '''' + orders[i,1] + ''', ' +
                '''' + orders[i,2] + ''', ' +
                '''' + orders[i,3] + ''', ' +
                '''' + orders[i,4] + ''')';
            // add order record
            SQLConnection1.ExecuteDirect(str);
        except on E: Exception do Handle_Exception(E);
        end;

        // process order items
        while (CompareStr(items[j,1], orders[i,3]) = 0) do
        begin
            try
                str := 'INSERT INTO ordritem VALUES (' +
                    items[j,2] + ', ' +
                    items[j,3] + ', ' +
                    '''' + items[j,1] + ''', ' +
                    '''' + items[j,4] + ''')';
                // add order item record
                SQLConnection1.ExecuteDirect(str);
            except on E: Exception do Handle_Exception(E);
            end;

            // bump to next item
            j := j + 1;

            // exit the while loop on last item
            if (j >= nItems) then
                break;
        end;

        try
            // commit the transaction
            SQLConnection1.Commit(td);
        except on E: Exception do
            Handle_Exception(E);
        end;
    end;
end;

procedure TForm1.Display_CustomerOrders();
begin
    ClientDataSet1.Active := false;
    SQLQuery1.SQL.Clear;
    SQLQuery1.SQL.Add('SELECT * FROM custordr');
    SQLQuery1.Prepared := true;
    SQLQuery1.Open;
    ClientDataSet1.Active := true;
end;

procedure TForm1.Display_OrderItems();
begin
    ClientDataSet2.Active := false;
    SQLQuery2.SQL.Clear;
    SQLQuery2.SQL.Add('SELECT * FROM ordritem');
    SQLQuery2.Prepared := true;
    SQLQuery2.Open;
    ClientDataSet2.Active := true;
end;