1 {*********************************************************}
3 { Zeos Database Objects }
4 { Variables classes and interfaces }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
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. }
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. }
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) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
58 uses SysUtils, Classes, Contnrs, ZCompatibility, ZVariant, ZExpression;
61 {** Implements a variable holder object. }
62 TZVariable = class (TObject)
67 constructor Create(const Name: string; const Value: TZVariant);
69 property Name: string read FName write FName;
70 property Value: TZVariant read FValue write FValue;
73 {** Implements a variables list. }
74 TZVariablesList = class (TInterfacedObject, IZVariablesList)
76 FVariables: TObjectList;
79 destructor Destroy; override;
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);
88 procedure Add(const Name: string; const Value: TZVariant);
89 procedure Remove(const Name: string);
90 function FindByName(const Name: string): Integer;
92 procedure ClearValues;
103 Creates a new instance of variable
104 @param Name a variable name.
105 @param Value a variable value.
107 constructor TZVariable.Create(const Name: string; const Value: TZVariant);
116 Creates this variable list object.
118 constructor TZVariablesList.Create;
120 FVariables := TObjectList.Create(True);
124 Destroys this object and cleanups the memory.
126 destructor TZVariablesList.Destroy;
129 FreeAndNil(FVariables);
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.
138 function TZVariablesList.FindByName(const Name: string): Integer;
145 UpperName := UpperCase(Name);
146 for I := 0 to FVariables.Count - 1 do
148 Current := TZVariable(FVariables[I]);
149 if Current.Name = UpperName then
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.
162 procedure TZVariablesList.Add(const Name: string; const Value: TZVariant);
164 if FindByName(Name) >= 0 then
165 raise Exception.Create(Format(SVariableAlreadyExists, [Name]));
166 FVariables.Add(TZVariable.Create(UpperCase(Name), Value));
170 Removes a variable by specified name.
171 @param Name a name of variable to be removed.
173 procedure TZVariablesList.Remove(const Name: string);
177 Index := FindByName(Name);
179 FVariables.Delete(Index);
183 Clears all variables.
185 procedure TZVariablesList.Clear;
191 Clears only variable values.
193 procedure TZVariablesList.ClearValues;
197 for I := 0 to FVariables.Count - 1 do
198 TZVariable(FVariables[I]).Value := NullVariant;
202 Gets a number of registered variables.
203 @returns a number of all registered variables.
205 function TZVariablesList.GetCount: Integer;
207 Result := FVariables.Count;
211 Gets a variable name by it's index.
212 @param Index a variable index.
213 @returns a variable name.
215 function TZVariablesList.GetName(Index: Integer): string;
217 Result := TZVariable(FVariables[Index]).Name;
221 Gets a variable value by it's index.
222 @param Index a variable index.
223 @returns a variable value
225 function TZVariablesList.GetValue(Index: Integer): TZVariant;
227 Result := TZVariable(FVariables[Index]).Value;
231 Gets a variable name by it's name.
232 @param Name a variable name.
233 @returns a variable value.
235 function TZVariablesList.GetValueByName(const Name: string): TZVariant;
239 Index := FindByName(Name);
241 Result := TZVariable(FVariables[Index]).Value
243 Result := NullVariant;
247 Sets a variable name by it's index.
248 @param Index a variable index.
249 @param Value a variable value.
251 procedure TZVariablesList.SetValue(Index: Integer; const Value: TZVariant);
253 TZVariable(FVariables[Index]).Value := Value;
257 Sets a variable name by it's name.
258 @param Index a variable name.
259 @param Value a variable value.
261 procedure TZVariablesList.SetValueByName(const Name: string; const Value: TZVariant);
265 Index := FindByName(Name);
267 TZVariable(FVariables[Index]).Value := Value