ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/runtime/IBCustomDataSet.pas
(Generate patch)

Comparing ibx/trunk/runtime/IBCustomDataSet.pas (file contents):
Revision 101 by tony, Thu Jan 18 14:37:18 2018 UTC vs.
Revision 105 by tony, Thu Jan 18 14:37:32 2018 UTC

# Line 315 | Line 315 | type
315      FFieldName: string;
316      FGeneratorName: string;
317      FIncrement: integer;
318 +    FQuery: TIBSQL;
319 +    function GetDatabase: TIBDatabase;
320 +    function GetTransaction: TIBTransaction;
321 +    procedure SetDatabase(AValue: TIBDatabase);
322 +    procedure SetGeneratorName(AValue: string);
323      procedure SetIncrement(const AValue: integer);
324 +    procedure SetTransaction(AValue: TIBTransaction);
325 +    procedure SetQuerySQL;
326    protected
327 <    function GetNextValue(ADatabase: TIBDatabase; ATransaction: TIBTransaction): integer;
327 >    function GetNextValue: integer;
328    public
329      constructor Create(Owner: TIBCustomDataSet);
330 +    destructor Destroy; override;
331      procedure Apply;
332      property Owner: TIBCustomDataSet read FOwner;
333 +    property Database: TIBDatabase read GetDatabase write SetDatabase;
334 +    property Transaction: TIBTransaction read GetTransaction write SetTransaction;
335    published
336 <    property Generator: string read FGeneratorName write FGeneratorName;
336 >    property Generator: string read FGeneratorName write SetGeneratorName;
337      property Field: string read FFieldName write FFieldName;
338      property Increment: integer read FIncrement write SetIncrement default 1;
339      property ApplyOnEvent: TIBGeneratorApplyOnEvent read FApplyOnEvent write FApplyOnEvent;
# Line 2069 | Line 2079 | var
2079    LocalBool: wordBool;
2080    LocalInt64: Int64;
2081    LocalCurrency: Currency;
2072  p: PRecordData;
2082    ColData: ISQLData;
2083   begin
2075  p := PRecordData(Buffer);
2084    LocalData := nil;
2085 <  with p^.rdFields[FieldIndex], FFieldColumns^[FieldIndex] do
2085 >  with PRecordData(Buffer)^.rdFields[FieldIndex], FFieldColumns^[FieldIndex] do
2086    begin
2087      QryResults.GetData(ColumnIndex,fdIsNull,fdDataLength,LocalData);
2088      if not fdIsNull then
# Line 2717 | Line 2725 | begin
2725      FQSelect.Database := Value;
2726      FQModify.Database := Value;
2727      FDatabaseInfo.Database := Value;
2728 +    FGeneratorField.Database := Value;
2729    end;
2730   end;
2731  
# Line 2885 | Line 2894 | begin
2894      FQRefresh.Transaction := Value;
2895      FQSelect.Transaction := Value;
2896      FQModify.Transaction := Value;
2897 +    FGeneratorField.Transaction := Value;
2898    end;
2899   end;
2900  
# Line 5058 | Line 5068 | end;
5068  
5069   procedure TIBGenerator.SetIncrement(const AValue: integer);
5070   begin
5071 +  if FIncrement = AValue then Exit;
5072    if AValue < 0 then
5073 <     raise Exception.Create('A Generator Increment cannot be negative');
5074 <  FIncrement := AValue
5073 >    IBError(ibxeNegativeGenerator,[]);
5074 >  FIncrement := AValue;
5075 >  SetQuerySQL;
5076   end;
5077  
5078 < function TIBGenerator.GetNextValue(ADatabase: TIBDatabase;
5067 <  ATransaction: TIBTransaction): integer;
5078 > procedure TIBGenerator.SetTransaction(AValue: TIBTransaction);
5079   begin
5080 <  with TIBSQL.Create(nil) do
5081 <  try
5082 <    Database := ADatabase;
5083 <    Transaction := ATransaction;
5084 <    if not assigned(Database) then
5085 <       IBError(ibxeCannotSetDatabase,[]);
5086 <    if not assigned(Transaction) then
5087 <       IBError(ibxeCannotSetTransaction,[]);
5088 <    with Transaction do
5089 <      if not InTransaction then StartTransaction;
5090 <    SQL.Text := Format('Select Gen_ID(%s,%d) as ID From RDB$Database',[FGeneratorName,Increment]);
5091 <    Prepare;
5080 >  FQuery.Transaction := AValue;
5081 > end;
5082 >
5083 > procedure TIBGenerator.SetQuerySQL;
5084 > begin
5085 >  FQuery.SQL.Text := Format('Select Gen_ID(%s,%d) From RDB$Database',[FGeneratorName,Increment]);
5086 > end;
5087 >
5088 > function TIBGenerator.GetDatabase: TIBDatabase;
5089 > begin
5090 >  Result := FQuery.Database;
5091 > end;
5092 >
5093 > function TIBGenerator.GetTransaction: TIBTransaction;
5094 > begin
5095 >  Result := FQuery.Transaction;
5096 > end;
5097 >
5098 > procedure TIBGenerator.SetDatabase(AValue: TIBDatabase);
5099 > begin
5100 >  FQuery.Database := AValue;
5101 > end;
5102 >
5103 > procedure TIBGenerator.SetGeneratorName(AValue: string);
5104 > begin
5105 >  if FGeneratorName = AValue then Exit;
5106 >  FGeneratorName := AValue;
5107 >  SetQuerySQL;
5108 > end;
5109 >
5110 > function TIBGenerator.GetNextValue: integer;
5111 > begin
5112 >  with FQuery do
5113 >  begin
5114 >    Transaction.Active := true;
5115      ExecQuery;
5116      try
5117 <      Result := FieldByName('ID').AsInteger
5117 >      Result := Fields[0].AsInteger
5118      finally
5119        Close
5120      end;
5087  finally
5088    Free
5121    end;
5122   end;
5123  
# Line 5093 | Line 5125 | constructor TIBGenerator.Create(Owner: T
5125   begin
5126    FOwner := Owner;
5127    FIncrement := 1;
5128 +  FQuery := TIBSQL.Create(nil);
5129 + end;
5130 +
5131 + destructor TIBGenerator.Destroy;
5132 + begin
5133 +  if assigned(FQuery) then FQuery.Free;
5134 +  inherited Destroy;
5135   end;
5136  
5137  
5138   procedure TIBGenerator.Apply;
5139   begin
5140 <  if (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then
5141 <    Owner.FieldByName(FFieldName).AsInteger := GetNextValue(Owner.Database,Owner.Transaction);
5140 >  if assigned(Database) and assigned(Transaction) and
5141 >       (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then
5142 >    Owner.FieldByName(FFieldName).AsInteger := GetNextValue;
5143   end;
5144  
5145  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines