00001 {*********************************************************}
00002 { }
00003 { Zeos Database Objects }
00004 { Database Sequence Component }
00005 { }
00006 { Originally written by Stefan Glienke }
00007 { }
00008 {*********************************************************}
00009
00010 {@********************************************************}
00011 { Copyright (c) 1999-2006 Zeos Development Group }
00012 { }
00013 { License Agreement: }
00014 { }
00015 { This library is distributed in the hope that it will be }
00016 { useful, but WITHOUT ANY WARRANTY; without even the }
00017 { implied warranty of MERCHANTABILITY or FITNESS FOR }
00018 { A PARTICULAR PURPOSE. See the GNU Lesser General }
00019 { Public License for more details. }
00020 { }
00021 { The source code of the ZEOS Libraries and packages are }
00022 { distributed under the Library GNU General Public }
00023 { License (see the file COPYING / COPYING.ZEOS) }
00024 { with the following modification: }
00025 { As a special exception, the copyright holders of this }
00026 { library give you permission to link this library with }
00027 { independent modules to produce an executable, }
00028 { regardless of the license terms of these independent }
00029 { modules, and to copy and distribute the resulting }
00030 { executable under terms of your choice, provided that }
00031 { you also meet, for each linked independent module, }
00032 { the terms and conditions of the license of that module. }
00033 { An independent module is a module which is not derived }
00034 { from or based on this library. If you modify this }
00035 { library, you may extend this exception to your version }
00036 { of the library, but you are not obligated to do so. }
00037 { If you do not wish to do so, delete this exception }
00038 { statement from your version. }
00039 { }
00040 { }
00041 { The project web site is located on: }
00042 { http:
00043 { http:
00044 { svn:
00045 { }
00046 { http:
00047 { http:
00048 { }
00049 { }
00050 { }
00051 { Zeos Development Group. }
00052 {********************************************************@}
00053
00054 unit ZSequence;
00055
00056 interface
00057
00058 {$I ZComponent.inc}
00059
00060 uses
00061 SysUtils, Classes, ZDbcIntfs, ZConnection;
00062
00063 type
00064 {** Represents a component which wraps a sequence to database. }
00065 TZSequence = class(TComponent)
00066 private
00067 FSequence: IZSequence;
00068 FConnection: TZConnection;
00069 FSequenceName: string;
00070 FBlockSize: Integer;
00071 procedure SetConnection(const Value: TZConnection);
00072 procedure SetBlockSize(const Value: Integer);
00073 procedure SetSequenceName(const Value: string);
00074 protected
00075 procedure Notification(AComponent: TComponent;
00076 Operation: TOperation); override;
00077 function GetSequence: IZSequence;
00078 public
00079 constructor Create(AOwner: TComponent); override;
00080
00081 destructor Destroy; override;
00082
00083 function GetCurrentValue: Int64;
00084 function GetNextValue: Int64;
00085 function GetCurrentValueSQL: string;
00086 function GetNextValueSQL: string;
00087
00088 procedure CloseSequence;
00089 published
00090 property BlockSize: Integer read FBlockSize write SetBlockSize default 1;
00091 property Connection: TZConnection read FConnection write SetConnection;
00092 property SequenceName: string read FSequenceName write SetSequenceName;
00093 end;
00094
00095 implementation
00096
00097 { TZSequence }
00098
00099 procedure TZSequence.CloseSequence;
00100 begin
00101 if Assigned(FSequence) then
00102 FSequence:=nil;
00103 end;
00104
00105 constructor TZSequence.Create(AOwner: TComponent);
00106 begin
00107 inherited;
00108 FBlockSize := 1;
00109 end;
00110
00111 {**
00112 Gets the current unique key generated by this sequence.
00113 @param the next generated unique key.
00114 }
00115 destructor TZSequence.Destroy;
00116 begin
00117
00118 if Assigned(FConnection) then
00119 FConnection.UnregisterSequence(self);
00120 inherited;
00121 end;
00122
00123 function TZSequence.GetCurrentValue: Int64;
00124 begin
00125 GetSequence;
00126 if Assigned(FSequence) then
00127 Result := FSequence.GetCurrentValue
00128 else
00129 Result := 0;
00130 end;
00131
00132 function TZSequence.GetCurrentValueSQL: string;
00133 begin
00134 GetSequence;
00135 if Assigned(FSequence) then begin
00136 Result := FSequence.GetCurrentValueSQL;
00137 end else begin
00138 Result := 'IMPLEMENT';
00139 end;
00140 end;
00141
00142 {**
00143 Gets the next unique key generated by this sequence.
00144 @param the next generated unique key.
00145 }
00146 function TZSequence.GetNextValue: Int64;
00147 begin
00148 GetSequence;
00149 if Assigned(FSequence) then
00150 Result := FSequence.GetNextValue
00151 else
00152 Result := 0;
00153 end;
00154
00155 function TZSequence.GetNextValueSQL: string;
00156 begin
00157 GetSequence;
00158 if Assigned(FSequence) then begin
00159 Result := FSequence.GetNextValueSQL;
00160 end else begin
00161 Result := 'IMPLEMENT';
00162 end;
00163 end;
00164
00165 function TZSequence.GetSequence: IZSequence;
00166 begin
00167 if not Assigned(FSequence) then
00168 begin
00169 if Assigned(FConnection) and Assigned(FConnection.DbcConnection) then
00170 FSequence := FConnection.DbcConnection.CreateSequence(
00171 FSequenceName, FBlockSize);
00172 end;
00173 Result := FSequence;
00174 end;
00175
00176 {**
00177 Processes component notifications.
00178 @param AComponent a changed component object.
00179 @param Operation a component operation code.
00180 }
00181 procedure TZSequence.Notification(AComponent: TComponent;
00182 Operation: TOperation);
00183 begin
00184 inherited Notification(AComponent, Operation);
00185
00186 if (Operation = opRemove) and (AComponent = FConnection) then
00187 begin
00188
00189 FConnection := nil;
00190 if Assigned(FSequence) then
00191 FSequence := nil;
00192 end;
00193 end;
00194
00195 procedure TZSequence.SetBlockSize(const Value: Integer);
00196 begin
00197 FBlockSize := Value;
00198 GetSequence;
00199 if Assigned(FSequence) then
00200 FSequence.SetBlockSize(FBlockSize);
00201 end;
00202
00203 procedure TZSequence.SetConnection(const Value: TZConnection);
00204 begin
00205 if FConnection <> Value then
00206 begin
00207 if Assigned(FSequence) then
00208 FSequence := nil;
00209 FConnection := Value;
00210
00211 if Assigned(FConnection) then
00212 FConnection.RegisterSequence(self);
00213 GetSequence;
00214 end;
00215 end;
00216
00217 procedure TZSequence.SetSequenceName(const Value: string);
00218 begin
00219 FSequenceName := Value;
00220 GetSequence;
00221 if Assigned(FSequence) then
00222 FSequence.SetName(FSequenceName);
00223 end;
00224
00225 end.