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; |
2727 |
|
FQSelect.Database := Value; |
2728 |
|
FQModify.Database := Value; |
2729 |
|
FDatabaseInfo.Database := Value; |
2730 |
+ |
FGeneratorField.Database := Value; |
2731 |
|
end; |
2732 |
|
end; |
2733 |
|
|
2896 |
|
FQRefresh.Transaction := Value; |
2897 |
|
FQSelect.Transaction := Value; |
2898 |
|
FQModify.Transaction := Value; |
2899 |
+ |
FGeneratorField.Transaction := Value; |
2900 |
|
end; |
2901 |
|
end; |
2902 |
|
|
5070 |
|
|
5071 |
|
procedure TIBGenerator.SetIncrement(const AValue: integer); |
5072 |
|
begin |
5073 |
+ |
if FIncrement = AValue then Exit; |
5074 |
|
if AValue < 0 then |
5075 |
< |
raise Exception.Create('A Generator Increment cannot be negative'); |
5076 |
< |
FIncrement := AValue |
5075 |
> |
IBError(ibxeNegativeGenerator,[]); |
5076 |
> |
FIncrement := AValue; |
5077 |
> |
SetQuerySQL; |
5078 |
|
end; |
5079 |
|
|
5080 |
< |
function TIBGenerator.GetNextValue(ADatabase: TIBDatabase; |
5067 |
< |
ATransaction: TIBTransaction): integer; |
5080 |
> |
procedure TIBGenerator.SetTransaction(AValue: TIBTransaction); |
5081 |
|
begin |
5082 |
< |
with TIBSQL.Create(nil) do |
5083 |
< |
try |
5084 |
< |
Database := ADatabase; |
5085 |
< |
Transaction := ATransaction; |
5086 |
< |
if not assigned(Database) then |
5087 |
< |
IBError(ibxeCannotSetDatabase,[]); |
5088 |
< |
if not assigned(Transaction) then |
5089 |
< |
IBError(ibxeCannotSetTransaction,[]); |
5090 |
< |
with Transaction do |
5091 |
< |
if not InTransaction then StartTransaction; |
5092 |
< |
SQL.Text := Format('Select Gen_ID(%s,%d) as ID From RDB$Database',[FGeneratorName,Increment]); |
5093 |
< |
Prepare; |
5082 |
> |
FQuery.Transaction := AValue; |
5083 |
> |
end; |
5084 |
> |
|
5085 |
> |
procedure TIBGenerator.SetQuerySQL; |
5086 |
> |
begin |
5087 |
> |
FQuery.SQL.Text := Format('Select Gen_ID(%s,%d) From RDB$Database',[FGeneratorName,Increment]); |
5088 |
> |
end; |
5089 |
> |
|
5090 |
> |
function TIBGenerator.GetDatabase: TIBDatabase; |
5091 |
> |
begin |
5092 |
> |
Result := FQuery.Database; |
5093 |
> |
end; |
5094 |
> |
|
5095 |
> |
function TIBGenerator.GetTransaction: TIBTransaction; |
5096 |
> |
begin |
5097 |
> |
Result := FQuery.Transaction; |
5098 |
> |
end; |
5099 |
> |
|
5100 |
> |
procedure TIBGenerator.SetDatabase(AValue: TIBDatabase); |
5101 |
> |
begin |
5102 |
> |
FQuery.Database := AValue; |
5103 |
> |
end; |
5104 |
> |
|
5105 |
> |
procedure TIBGenerator.SetGeneratorName(AValue: string); |
5106 |
> |
begin |
5107 |
> |
if FGeneratorName = AValue then Exit; |
5108 |
> |
FGeneratorName := AValue; |
5109 |
> |
SetQuerySQL; |
5110 |
> |
end; |
5111 |
> |
|
5112 |
> |
function TIBGenerator.GetNextValue: integer; |
5113 |
> |
begin |
5114 |
> |
with FQuery do |
5115 |
> |
begin |
5116 |
> |
Transaction.Active := true; |
5117 |
|
ExecQuery; |
5118 |
|
try |
5119 |
< |
Result := FieldByName('ID').AsInteger |
5119 |
> |
Result := Fields[0].AsInteger |
5120 |
|
finally |
5121 |
|
Close |
5122 |
|
end; |
5087 |
– |
finally |
5088 |
– |
Free |
5123 |
|
end; |
5124 |
|
end; |
5125 |
|
|
5127 |
|
begin |
5128 |
|
FOwner := Owner; |
5129 |
|
FIncrement := 1; |
5130 |
+ |
FQuery := TIBSQL.Create(nil); |
5131 |
+ |
end; |
5132 |
+ |
|
5133 |
+ |
destructor TIBGenerator.Destroy; |
5134 |
+ |
begin |
5135 |
+ |
if assigned(FQuery) then FQuery.Free; |
5136 |
+ |
inherited Destroy; |
5137 |
|
end; |
5138 |
|
|
5139 |
|
|
5140 |
|
procedure TIBGenerator.Apply; |
5141 |
|
begin |
5142 |
< |
if (FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then |
5143 |
< |
Owner.FieldByName(FFieldName).AsInteger := GetNextValue(Owner.Database,Owner.Transaction); |
5142 |
> |
if assigned(Database) and assigned(Transaction) and |
5143 |
> |
(FGeneratorName <> '') and (FFieldName <> '') and Owner.FieldByName(FFieldName).IsNull then |
5144 |
> |
Owner.FieldByName(FFieldName).AsInteger := GetNextValue; |
5145 |
|
end; |
5146 |
|
|
5147 |
|
|