zeoslib  UNKNOWN
 All Files
ZSequence.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { Database Sequence Component }
5 { }
6 { Originally written by Stefan Glienke }
7 { }
8 {*********************************************************}
9 
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
12 { }
13 { License Agreement: }
14 { }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
20 { }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
39 { }
40 { }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
45 { }
46 { http://www.sourceforge.net/projects/zeoslib. }
47 { }
48 { }
49 { Zeos Development Group. }
50 {********************************************************@}
51 
52 unit ZSequence;
53 
54 interface
55 
56 {$I ZComponent.inc}
57 
58 uses
59  SysUtils, Classes, {$IFDEF MSEgui}mclasses,{$ENDIF}
60  ZDbcIntfs, ZConnection;
61 
62 type
63  {** Represents a component which wraps a sequence to database. }
64  TZSequence = class(TComponent)
65  private
66  FSequence: IZSequence;
67  FConnection: TZConnection;
68  FSequenceName: string;
69  FBlockSize: Integer;
70  procedure SetConnection(const Value: TZConnection);
71  procedure SetBlockSize(const Value: Integer);
72  procedure SetSequenceName(const Value: string);
73  protected
74  procedure Notification(AComponent: TComponent;
75  Operation: TOperation); override;
76  function GetSequence: IZSequence;
77  public
78  constructor Create(AOwner: TComponent); override;
79  // Modified by cipto 8/2/2007 12:09:21 PM
80  destructor Destroy; override;
81 
82  function GetCurrentValue: Int64;
83  function GetNextValue: Int64;
84  function GetCurrentValueSQL: string; // Get the Sequence as SQL-Text
85  function GetNextValueSQL: string; // Get the Sequence as SQL-Text
86  // Modified by cipto 8/2/2007 10:04:41 AM
87  procedure CloseSequence;
88  published
89  property BlockSize: Integer read FBlockSize write SetBlockSize default 1;
90  property Connection: TZConnection read FConnection write SetConnection;
91  property SequenceName: string read FSequenceName write SetSequenceName;
92  end;
93 
94 implementation
95 
96 { TZSequence }
97 
98 procedure TZSequence.CloseSequence;
99 begin
100  if Assigned(FSequence) then
101  FSequence:=nil;
102 end;
103 
104 constructor TZSequence.Create(AOwner: TComponent);
105 begin
106  inherited;
107  FBlockSize := 1;
108 end;
109 
110 {**
111  Gets the current unique key generated by this sequence.
112  @param the next generated unique key.
113 }
114 destructor TZSequence.Destroy;
115 begin
116  // Modified by cipto 8/3/2007 10:35:41 AM
117  if Assigned(FConnection) then
118  FConnection.UnregisterSequence(self);
119  inherited;
120 end;
121 
122 function TZSequence.GetCurrentValue: Int64;
123 begin
124  GetSequence;
125  if Assigned(FSequence) then
126  Result := FSequence.GetCurrentValue
127  else
128  Result := 0;
129 end;
130 
131 function TZSequence.GetCurrentValueSQL: string;
132 begin
133  GetSequence;
134  if Assigned(FSequence) then begin
135  Result := FSequence.GetCurrentValueSQL;
136  end else begin
137  Result := 'IMPLEMENT';
138  end;
139 end;
140 
141 {**
142  Gets the next unique key generated by this sequence.
143  @param the next generated unique key.
144 }
145 function TZSequence.GetNextValue: Int64;
146 begin
147  GetSequence;
148  if Assigned(FSequence) then
149  Result := FSequence.GetNextValue
150  else
151  Result := 0;
152 end;
153 
154 function TZSequence.GetNextValueSQL: string;
155 begin
156  GetSequence;
157  if Assigned(FSequence) then begin
158  Result := FSequence.GetNextValueSQL;
159  end else begin
160  Result := 'IMPLEMENT';
161  end;
162 end;
163 
164 function TZSequence.GetSequence: IZSequence;
165 begin
166  if not Assigned(FSequence) then
167  begin
168  if Assigned(FConnection) and Assigned(FConnection.DbcConnection) then
169  FSequence := FConnection.DbcConnection.CreateSequence(
170  FSequenceName, FBlockSize);
171  end;
172  Result := FSequence;
173 end;
174 
175 {**
176  Processes component notifications.
177  @param AComponent a changed component object.
178  @param Operation a component operation code.
179 }
180 procedure TZSequence.Notification(AComponent: TComponent;
181  Operation: TOperation);
182 begin
183  inherited Notification(AComponent, Operation);
184 
185  if (Operation = opRemove) and (AComponent = FConnection) then
186  begin
187  // Modified by cipto 8/2/2007 12:07:57 PM
188  FConnection := nil;
189  if Assigned(FSequence) then
190  FSequence := nil;
191  end;
192 end;
193 
194 procedure TZSequence.SetBlockSize(const Value: Integer);
195 begin
196  FBlockSize := Value;
197  GetSequence;
198  if Assigned(FSequence) then
199  FSequence.SetBlockSize(FBlockSize);
200 end;
201 
202 procedure TZSequence.SetConnection(const Value: TZConnection);
203 begin
204  if FConnection <> Value then
205  begin
206  if Assigned(FSequence) then
207  FSequence := nil;
208  FConnection := Value;
209  // Modified by cipto 8/2/2007 11:59:58 AM
210  if Assigned(FConnection) then
211  FConnection.RegisterSequence(self);
212  GetSequence;
213  end;
214 end;
215 
216 procedure TZSequence.SetSequenceName(const Value: string);
217 begin
218  FSequenceName := Value;
219  GetSequence;
220  if Assigned(FSequence) then
221  FSequence.SetName(FSequenceName);
222 end;
223 
224 end.