zeoslib  UNKNOWN
 All Files
ZVariables.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { Variables classes and interfaces }
5 { }
6 { Originally written by Sergey Seroukhov }
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 ZVariables;
53 
54 interface
55 
56 {$I ZCore.inc}
57 
58 uses SysUtils, Classes, Contnrs, ZCompatibility, ZVariant, ZExpression;
59 
60 type
61  {** Implements a variable holder object. }
62  TZVariable = class (TObject)
63  private
64  FName: string;
65  FValue: TZVariant;
66  public
67  constructor Create(const Name: string; const Value: TZVariant);
68 
69  property Name: string read FName write FName;
70  property Value: TZVariant read FValue write FValue;
71  end;
72 
73  {** Implements a variables list. }
74  TZVariablesList = class (TInterfacedObject, IZVariablesList)
75  private
76  FVariables: TObjectList;
77  public
78  constructor Create;
79  destructor Destroy; override;
80 
81  function GetCount: Integer;
82  function GetName(Index: Integer): string;
83  function GetValue(Index: Integer): TZVariant;
84  procedure SetValue(Index: Integer; const Value: TZVariant);
85  function GetValueByName(const Name: string): TZVariant;
86  procedure SetValueByName(const Name: string; const Value: TZVariant);
87 
88  procedure Add(const Name: string; const Value: TZVariant);
89  procedure Remove(const Name: string);
90  function FindByName(const Name: string): Integer;
91 
92  procedure ClearValues;
93  procedure Clear;
94  end;
95 
96 implementation
97 
98 uses ZMessages;
99 
100 { TZVariable }
101 
102 {**
103  Creates a new instance of variable
104  @param Name a variable name.
105  @param Value a variable value.
106 }
107 constructor TZVariable.Create(const Name: string; const Value: TZVariant);
108 begin
109  FName := Name;
110  FValue := Value;
111 end;
112 
113 { TZVariablesList }
114 
115 {**
116  Creates this variable list object.
117 }
118 constructor TZVariablesList.Create;
119 begin
120  FVariables := TObjectList.Create(True);
121 end;
122 
123 {**
124  Destroys this object and cleanups the memory.
125 }
126 destructor TZVariablesList.Destroy;
127 begin
128  FVariables.Clear;
129  FreeAndNil(FVariables);
130  inherited Destroy;
131 end;
132 
133 {**
134  Finds a variable by specified name.
135  @param Name a name of the variable.
136  @returns a found variable index or <code>-1</code> otherwise.
137 }
138 function TZVariablesList.FindByName(const Name: string): Integer;
139 var
140  I: Integer;
141  Current: TZVariable;
142  UpperName: string;
143 begin
144  Result := -1;
145  UpperName := UpperCase(Name);
146  for I := 0 to FVariables.Count - 1 do
147  begin
148  Current := TZVariable(FVariables[I]);
149  if Current.Name = UpperName then
150  begin
151  Result := I;
152  Break;
153  end;
154  end;
155 end;
156 
157 {**
158  Adds a new variable with value.
159  @param Name a name of the new variable.
160  @param Value a value for the new variable.
161 }
162 procedure TZVariablesList.Add(const Name: string; const Value: TZVariant);
163 begin
164  if FindByName(Name) >= 0 then
165  raise Exception.Create(Format(SVariableAlreadyExists, [Name]));
166  FVariables.Add(TZVariable.Create(UpperCase(Name), Value));
167 end;
168 
169 {**
170  Removes a variable by specified name.
171  @param Name a name of variable to be removed.
172 }
173 procedure TZVariablesList.Remove(const Name: string);
174 var
175  Index: Integer;
176 begin
177  Index := FindByName(Name);
178  if Index >= 0 then
179  FVariables.Delete(Index);
180 end;
181 
182 {**
183  Clears all variables.
184 }
185 procedure TZVariablesList.Clear;
186 begin
187  FVariables.Clear;
188 end;
189 
190 {**
191  Clears only variable values.
192 }
193 procedure TZVariablesList.ClearValues;
194 var
195  I: Integer;
196 begin
197  for I := 0 to FVariables.Count - 1 do
198  TZVariable(FVariables[I]).Value := NullVariant;
199 end;
200 
201 {**
202  Gets a number of registered variables.
203  @returns a number of all registered variables.
204 }
205 function TZVariablesList.GetCount: Integer;
206 begin
207  Result := FVariables.Count;
208 end;
209 
210 {**
211  Gets a variable name by it's index.
212  @param Index a variable index.
213  @returns a variable name.
214 }
215 function TZVariablesList.GetName(Index: Integer): string;
216 begin
217  Result := TZVariable(FVariables[Index]).Name;
218 end;
219 
220 {**
221  Gets a variable value by it's index.
222  @param Index a variable index.
223  @returns a variable value
224 }
225 function TZVariablesList.GetValue(Index: Integer): TZVariant;
226 begin
227  Result := TZVariable(FVariables[Index]).Value;
228 end;
229 
230 {**
231  Gets a variable name by it's name.
232  @param Name a variable name.
233  @returns a variable value.
234 }
235 function TZVariablesList.GetValueByName(const Name: string): TZVariant;
236 var
237  Index: Integer;
238 begin
239  Index := FindByName(Name);
240  if Index >= 0 then
241  Result := TZVariable(FVariables[Index]).Value
242  else
243  Result := NullVariant;
244 end;
245 
246 {**
247  Sets a variable name by it's index.
248  @param Index a variable index.
249  @param Value a variable value.
250 }
251 procedure TZVariablesList.SetValue(Index: Integer; const Value: TZVariant);
252 begin
253  TZVariable(FVariables[Index]).Value := Value;
254 end;
255 
256 {**
257  Sets a variable name by it's name.
258  @param Index a variable name.
259  @param Value a variable value.
260 }
261 procedure TZVariablesList.SetValueByName(const Name: string; const Value: TZVariant);
262 var
263  Index: Integer;
264 begin
265  Index := FindByName(Name);
266  if Index >= 0 then
267  TZVariable(FVariables[Index]).Value := Value
268  else
269  Add(Name, Value);
270 end;
271 
272 end.