home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Runimage / Delphi50 / Source / Vcl / ibupdatesql.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  5.7 KB  |  193 lines

  1. {********************************************************}
  2. {                                                        }
  3. {       Borland Delphi Visual Component Library          }
  4. {       InterBase Express core components                }
  5. {                                                        }
  6. {       Copyright (c) 1998-1999 Inprise Corporation      }
  7. {                                                        }
  8. {    InterBase Express is based in part on the product   }
  9. {    Free IB Components, written by Gregory H. Deatz for }
  10. {    Hoagland, Longo, Moran, Dunst & Doukas Company.     }
  11. {    Free IB Components is used under license.           }
  12. {                                                        }
  13. {********************************************************}
  14.  
  15. unit IBUpdateSQL;
  16.  
  17. interface
  18.  
  19. uses Windows, SysUtils, Graphics, Classes, Controls, Db, StdVCL,
  20.      IB, IBCustomDataSet, IBQuery;
  21.  
  22. type
  23. { TIBUpdateSQL }
  24.  
  25.   TIBUpdateSQL = class(TIBDataSetUpdateObject)
  26.   private
  27.     FDataSet: TIBCustomDataSet;
  28.     FQueries: array[TUpdateKind] of TIBQuery;
  29.     FSQLText: array[TUpdateKind] of TStrings;
  30.     function GetQuery(UpdateKind: TUpdateKind): TIBQuery;
  31.     function GetSQLIndex(Index: Integer): TStrings;
  32.     procedure SetSQL(UpdateKind: TUpdateKind; Value: TStrings);
  33.     procedure SetSQLIndex(Index: Integer; Value: TStrings);
  34.   protected
  35.     function GetSQL(UpdateKind: TUpdateKind): TStrings; override;
  36.     function GetDataSet: TIBCustomDataSet; override;
  37.     procedure SetDataSet(ADataSet: TIBCustomDataSet); override;
  38.     procedure SQLChanged(Sender: TObject);
  39.   public
  40.     constructor Create(AOwner: TComponent); override;
  41.     destructor Destroy; override;
  42.     procedure Apply(UpdateKind: TUpdateKind); override;
  43.     procedure ExecSQL(UpdateKind: TUpdateKind);
  44.     procedure SetParams(UpdateKind: TUpdateKind);
  45.     property DataSet;
  46.     property Query[UpdateKind: TUpdateKind]: TIBQuery read GetQuery;
  47.     property SQL[UpdateKind: TUpdateKind]: TStrings read GetSQL write SetSQL;
  48.   published
  49.     property ModifySQL: TStrings index 0 read GetSQLIndex write SetSQLIndex;
  50.     property InsertSQL: TStrings index 1 read GetSQLIndex write SetSQLIndex;
  51.     property DeleteSQL: TStrings index 2 read GetSQLIndex write SetSQLIndex;
  52.   end;
  53.  
  54. implementation
  55.  
  56. { TIBUpdateSQL }
  57.  
  58. constructor TIBUpdateSQL.Create(AOwner: TComponent);
  59. var
  60.   UpdateKind: TUpdateKind;
  61. begin
  62.   inherited Create(AOwner);
  63.   for UpdateKind := Low(TUpdateKind) to High(TUpdateKind) do
  64.   begin
  65.     FSQLText[UpdateKind] := TStringList.Create;
  66.     TStringList(FSQLText[UpdateKind]).OnChange := SQLChanged;
  67.   end;
  68. end;
  69.  
  70. destructor TIBUpdateSQL.Destroy;
  71. var
  72.   UpdateKind: TUpdateKind;
  73. begin
  74.   if Assigned(FDataSet) and (FDataSet.UpdateObject = Self) then
  75.     FDataSet.UpdateObject := nil;
  76.   for UpdateKind := Low(TUpdateKind) to High(TUpdateKind) do
  77.     FSQLText[UpdateKind].Free;
  78.   inherited Destroy;
  79. end;
  80.  
  81. procedure TIBUpdateSQL.ExecSQL(UpdateKind: TUpdateKind);
  82. begin
  83.   with Query[UpdateKind] do
  84.   begin
  85.     Prepare;
  86.     ExecSQL;
  87.     if RowsAffected <> 1 then IBError(ibxeUpdateFailed, [nil]);
  88.   end;
  89. end;
  90.  
  91. function TIBUpdateSQL.GetQuery(UpdateKind: TUpdateKind): TIBQuery;
  92. begin
  93.   if not Assigned(FQueries[UpdateKind]) then
  94.   begin
  95.     FQueries[UpdateKind] := TIBQuery.Create(Self);
  96.     FQueries[UpdateKind].SQL.Assign(FSQLText[UpdateKind]);
  97.     if (FDataSet is TIBCustomDataSet) then
  98.     begin
  99.       FQueries[UpdateKind].Database := TIBCustomDataSet(FDataSet).DataBase;
  100.       FQueries[UpdateKind].Transaction := TIBCustomDataSet(FDataSet).Transaction;
  101.     end;
  102.   end;
  103.   Result := FQueries[UpdateKind];
  104. end;
  105.  
  106. function TIBUpdateSQL.GetSQL(UpdateKind: TUpdateKind): TStrings;
  107. begin
  108.   Result := FSQLText[UpdateKind];
  109. end;
  110.  
  111. function TIBUpdateSQL.GetSQLIndex(Index: Integer): TStrings;
  112. begin
  113.   Result := FSQLText[TUpdateKind(Index)];
  114. end;
  115.  
  116. function TIBUpdateSQL.GetDataSet: TIBCustomDataSet;
  117. begin
  118.   Result := FDataSet;
  119. end;
  120.  
  121. procedure TIBUpdateSQL.SetDataSet(ADataSet: TIBCustomDataSet);
  122. begin
  123.   FDataSet := ADataSet;
  124. end;
  125.  
  126. procedure TIBUpdateSQL.SetSQL(UpdateKind: TUpdateKind; Value: TStrings);
  127. begin
  128.   FSQLText[UpdateKind].Assign(Value);
  129. end;
  130.  
  131. procedure TIBUpdateSQL.SetSQLIndex(Index: Integer; Value: TStrings);
  132. begin
  133.   SetSQL(TUpdateKind(Index), Value);
  134. end;
  135.  
  136. procedure TIBUpdateSQL.SQLChanged(Sender: TObject);
  137. var
  138.   UpdateKind: TUpdateKind;
  139. begin
  140.   for UpdateKind := Low(TUpdateKind) to High(TUpdateKind) do
  141.     if Sender = FSQLText[UpdateKind] then
  142.     begin
  143.       if Assigned(FQueries[UpdateKind]) then
  144.       begin
  145.         FQueries[UpdateKind].Params.Clear;
  146.         FQueries[UpdateKind].SQL.Assign(FSQLText[UpdateKind]);
  147.       end;
  148.       Break;
  149.     end;
  150. end;
  151.  
  152. procedure TIBUpdateSQL.SetParams(UpdateKind: TUpdateKind);
  153. var
  154.   I: Integer;
  155.   Old: Boolean;
  156.   Param: TParam;
  157.   PName: string;
  158.   Field: TField;
  159.   Value: Variant;
  160. begin
  161.   if not Assigned(FDataSet) then Exit;
  162.   with Query[UpdateKind] do
  163.   begin
  164.     for I := 0 to Params.Count - 1 do
  165.     begin
  166.       Param := Params[I];
  167.       PName := Param.Name;
  168.       Old := CompareText(Copy(PName, 1, 4), 'OLD_') = 0; {do not localize}
  169.       if Old then
  170.         System.Delete(PName, 1, 4);
  171.       Field := FDataSet.FindField(PName);
  172.       if not Assigned(Field) then
  173.         Continue;
  174.       if Old then
  175.         Param.AssignFieldValue(Field, Field.OldValue) else
  176.       begin
  177.         Value := Field.NewValue;
  178.         if VarIsEmpty(Value) then
  179.           Value := Field.OldValue;
  180.         Param.AssignFieldValue(Field, Value);
  181.       end;
  182.     end;
  183.   end;
  184. end;
  185.  
  186. procedure TIBUpdateSQL.Apply(UpdateKind: TUpdateKind);
  187. begin
  188.   SetParams(UpdateKind);
  189.   ExecSQL(UpdateKind);
  190. end;
  191.  
  192. end.
  193.