00001 {*********************************************************}
00002 { }
00003 { Zeos Database Objects }
00004 { Variables classes and interfaces }
00005 { }
00006 { Originally written by Sergey Seroukhov }
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 ZVariables;
00055
00056 interface
00057
00058 {$I ZCore.inc}
00059
00060 uses SysUtils, Classes, Contnrs, ZCompatibility, ZVariant, ZExpression;
00061
00062 type
00063 {** Implements a variable holder object. }
00064 TZVariable = class (TObject)
00065 private
00066 FName: string;
00067 FValue: TZVariant;
00068 public
00069 constructor Create(const Name: string; const Value: TZVariant);
00070
00071 property Name: string read FName write FName;
00072 property Value: TZVariant read FValue write FValue;
00073 end;
00074
00075 {** Implements a variables list. }
00076 TZVariablesList = class (TInterfacedObject, IZVariablesList)
00077 private
00078 FVariables: TObjectList;
00079 public
00080 constructor Create;
00081 destructor Destroy; override;
00082
00083 function GetCount: Integer;
00084 function GetName(Index: Integer): string;
00085 function GetValue(Index: Integer): TZVariant;
00086 procedure SetValue(Index: Integer; const Value: TZVariant);
00087 function GetValueByName(const Name: string): TZVariant;
00088 procedure SetValueByName(const Name: string; const Value: TZVariant);
00089
00090 procedure Add(const Name: string; const Value: TZVariant);
00091 procedure Remove(const Name: string);
00092 function FindByName(const Name: string): Integer;
00093
00094 procedure ClearValues;
00095 procedure Clear;
00096 end;
00097
00098 implementation
00099
00100 uses ZMessages;
00101
00102 { TZVariable }
00103
00104 {**
00105 Creates a new instance of variable
00106 @param Name a variable name.
00107 @param Value a variable value.
00108 }
00109 constructor TZVariable.Create(const Name: string; const Value: TZVariant);
00110 begin
00111 FName := Name;
00112 FValue := Value;
00113 end;
00114
00115 { TZVariablesList }
00116
00117 {**
00118 Creates this variable list object.
00119 }
00120 constructor TZVariablesList.Create;
00121 begin
00122 FVariables := TObjectList.Create;
00123 end;
00124
00125 {**
00126 Destroys this object and cleanups the memory.
00127 }
00128 destructor TZVariablesList.Destroy;
00129 begin
00130 FVariables.Free;
00131 inherited Destroy;
00132 end;
00133
00134 {**
00135 Finds a variable by specified name.
00136 @param Name a name of the variable.
00137 @returns a found variable index or <code>-1</code> otherwise.
00138 }
00139 function TZVariablesList.FindByName(const Name: string): Integer;
00140 var
00141 I: Integer;
00142 Current: TZVariable;
00143 UpperName: string;
00144 begin
00145 Result := -1;
00146 UpperName := UpperCase(Name);
00147 for I := 0 to FVariables.Count - 1 do
00148 begin
00149 Current := TZVariable(FVariables[I]);
00150 if Current.Name = UpperName then
00151 begin
00152 Result := I;
00153 Break;
00154 end;
00155 end;
00156 end;
00157
00158 {**
00159 Adds a new variable with value.
00160 @param Name a name of the new variable.
00161 @param Value a value for the new variable.
00162 }
00163 procedure TZVariablesList.Add(const Name: string; const Value: TZVariant);
00164 begin
00165 if FindByName(Name) >= 0 then
00166 raise Exception.Create(Format(SVariableAlreadyExists, [Name]));
00167 FVariables.Add(TZVariable.Create(UpperCase(Name), Value));
00168 end;
00169
00170 {**
00171 Removes a variable by specified name.
00172 @param Name a name of variable to be removed.
00173 }
00174 procedure TZVariablesList.Remove(const Name: string);
00175 var
00176 Index: Integer;
00177 begin
00178 Index := FindByName(Name);
00179 if Index >= 0 then
00180 FVariables.Delete(Index);
00181 end;
00182
00183 {**
00184 Clears all variables.
00185 }
00186 procedure TZVariablesList.Clear;
00187 begin
00188 FVariables.Clear;
00189 end;
00190
00191 {**
00192 Clears only variable values.
00193 }
00194 procedure TZVariablesList.ClearValues;
00195 var
00196 I: Integer;
00197 begin
00198 for I := 0 to FVariables.Count - 1 do
00199 TZVariable(FVariables[I]).Value := NullVariant;
00200 end;
00201
00202 {**
00203 Gets a number of registered variables.
00204 @returns a number of all registered variables.
00205 }
00206 function TZVariablesList.GetCount: Integer;
00207 begin
00208 Result := FVariables.Count;
00209 end;
00210
00211 {**
00212 Gets a variable name by it's index.
00213 @param Index a variable index.
00214 @returns a variable name.
00215 }
00216 function TZVariablesList.GetName(Index: Integer): string;
00217 begin
00218 Result := TZVariable(FVariables[Index]).Name;
00219 end;
00220
00221 {**
00222 Gets a variable value by it's index.
00223 @param Index a variable index.
00224 @returns a variable value
00225 }
00226 function TZVariablesList.GetValue(Index: Integer): TZVariant;
00227 begin
00228 Result := TZVariable(FVariables[Index]).Value;
00229 end;
00230
00231 {**
00232 Gets a variable name by it's name.
00233 @param Name a variable name.
00234 @returns a variable value.
00235 }
00236 function TZVariablesList.GetValueByName(const Name: string): TZVariant;
00237 var
00238 Index: Integer;
00239 begin
00240 Index := FindByName(Name);
00241 if Index >= 0 then
00242 Result := TZVariable(FVariables[Index]).Value
00243 else Result := NullVariant;
00244 end;
00245
00246 {**
00247 Sets a variable name by it's index.
00248 @param Index a variable index.
00249 @param Value a variable value.
00250 }
00251 procedure TZVariablesList.SetValue(Index: Integer; const Value: TZVariant);
00252 begin
00253 TZVariable(FVariables[Index]).Value := Value;
00254 end;
00255
00256 {**
00257 Sets a variable name by it's name.
00258 @param Index a variable name.
00259 @param Value a variable value.
00260 }
00261 procedure TZVariablesList.SetValueByName(const Name: string; const Value: TZVariant);
00262 var
00263 Index: Integer;
00264 begin
00265 Index := FindByName(Name);
00266 if Index >= 0 then
00267 TZVariable(FVariables[Index]).Value := Value
00268 else Add(Name, Value);
00269 end;
00270
00271 end.