Changeset 37 for trunk/Common/USqlDatabase.pas
- Timestamp:
- Oct 30, 2010, 7:36:25 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/USqlDatabase.pas
r34 r37 8 8 9 9 uses 10 SysUtils, Classes, Dialogs, mysql50, TypInfo, Contnrs, UStringListEx; 10 SysUtils, Classes, Dialogs, mysql50, TypInfo, UStringListEx, 11 ListObject, DictionaryStringString; 11 12 12 13 type … … 20 21 TSetClientCapabilities = set of TClientCapabilities; 21 22 22 T AssociativeArray = class(TStringList)23 TDbRows = class(TListObject) 23 24 private 24 function GetValues(Index: string): string; 25 function GetValuesAtIndex(Index: Integer): string; 26 procedure SetValues(Index: string; const Value: string); 25 function GetData(Index: Integer): TDictionaryStringString; 26 procedure SetData(Index: Integer; const Value: TDictionaryStringString); 27 27 public 28 constructor Create; 29 destructor Destroy; override; 30 function GetAllValues: string; 31 procedure AddKeyValue(Key, Value: string); 32 property ValuesAtIndex[Index: Integer]: string read GetValuesAtIndex; 33 property Values[Index: string]: string read GetValues write SetValues; default; 34 end; 35 36 TDbRows = class(TObjectList) 37 private 38 function GetData(Index: Integer): TAssociativeArray; 39 procedure SetData(Index: Integer; const Value: TAssociativeArray); 40 public 41 property Data[Index: Integer]: TAssociativeArray read GetData write SetData; default; 28 property Data[Index: Integer]: TDictionaryStringString read GetData write SetData; default; 42 29 destructor Destroy; override; 43 30 end; … … 68 55 function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows; 69 56 procedure Delete(ATable: string; Condition: string = '1'); 70 procedure Insert(ATable: string; Data: T AssociativeArray);71 procedure Update(ATable: string; Data: T AssociativeArray; Condition: string = '1');72 procedure Replace(ATable: string; Data: T AssociativeArray);57 procedure Insert(ATable: string; Data: TDictionaryStringString); 58 procedure Update(ATable: string; Data: TDictionaryStringString; Condition: string = '1'); 59 procedure Replace(ATable: string; Data: TDictionaryStringString); 73 60 procedure Connect; 74 61 procedure Disconnect; … … 186 173 end; 187 174 188 procedure TSqlDatabase.Insert(ATable: string; Data: T AssociativeArray);175 procedure TSqlDatabase.Insert(ATable: string; Data: TDictionaryStringString); 189 176 var 190 177 DbNames: string; … … 198 185 DbValues := ''; 199 186 for I := 0 to Data.Count - 1 do begin 200 Value := Data. ValuesAtIndex[I];187 Value := Data.Items[I].Value; 201 188 StringReplace(Value, '"', '\"', [rfReplaceAll]); 202 189 if Value = 'NOW()' then DbValues := DbValues + ',' + Value 203 190 else DbValues := DbValues + ',"' + Value + '"'; 204 DbNames := DbNames + ',`' + Data. Names[I] + '`';191 DbNames := DbNames + ',`' + Data.Keys[I] + '`'; 205 192 end; 206 193 System.Delete(DbNames, 1, 1); … … 233 220 for I := 0 to Result.Count - 1 do begin 234 221 DbRow := mysql_fetch_row(DbResult); 235 Result[I] := T AssociativeArray.Create;222 Result[I] := TDictionaryStringString.Create; 236 223 with Result[I] do begin 237 224 for II := 0 to mysql_num_fields(DbResult) - 1 do begin 238 Add(mysql_fetch_field_direct(DbResult, II)^.Name +239 NameValueSeparator +PChar((DbRow + II)^));225 Add(mysql_fetch_field_direct(DbResult, II)^.Name, 226 PChar((DbRow + II)^)); 240 227 end; 241 228 end; … … 245 232 end; 246 233 247 procedure TSqlDatabase.Replace(ATable: string; Data: T AssociativeArray);234 procedure TSqlDatabase.Replace(ATable: string; Data: TDictionaryStringString); 248 235 var 249 236 DbNames: string; … … 257 244 DbValues := ''; 258 245 for I := 0 to Data.Count - 1 do begin 259 Value := Data. ValuesAtIndex[I];246 Value := Data.Items[I].Value; 260 247 StringReplace(Value, '"', '\"', [rfReplaceAll]); 261 248 if Value = 'NOW()' then DbValues := DbValues + ',' + Value 262 249 else DbValues := DbValues + ',"' + Value + '"'; 263 DbNames := DbNames + ',`' + Data. Names[I] + '`';250 DbNames := DbNames + ',`' + Data.Keys[I] + '`'; 264 251 end; 265 252 System.Delete(DbNames, 1, 1); … … 278 265 end; 279 266 280 procedure TSqlDatabase.Update(ATable: string; Data: T AssociativeArray; Condition: string = '1');267 procedure TSqlDatabase.Update(ATable: string; Data: TDictionaryStringString; Condition: string = '1'); 281 268 var 282 269 DbValues: string; … … 288 275 DbValues := ''; 289 276 for I := 0 to Data.Count - 1 do begin 290 Value := Data. ValuesAtIndex[I];277 Value := Data.Items[I].Value; 291 278 StringReplace(Value, '"', '\"', [rfReplaceAll]); 292 279 if Value = 'NOW()' then DbValues := DbValues + ',' + Value 293 else DbValues := DbValues + ',' + Data. Names[I] + '=' + '"' + Value + '"';280 else DbValues := DbValues + ',' + Data.Keys[I] + '=' + '"' + Value + '"'; 294 281 end; 295 282 System.Delete(DbValues, 1, 1); … … 395 382 end; 396 383 397 { TAssociativeArray } 398 399 procedure TAssociativeArray.AddKeyValue(Key, Value: string); 400 begin 401 Add(Key + NameValueSeparator + Value); 402 end; 403 404 constructor TAssociativeArray.Create; 405 begin 406 NameValueSeparator := '|'; 407 end; 408 409 destructor TAssociativeArray.Destroy; 384 { TDbRows } 385 386 destructor TDbRows.Destroy; 410 387 begin 411 388 inherited; 412 389 end; 413 390 414 function TAssociativeArray.GetAllValues: string; 415 var 416 I: Integer; 417 begin 418 Result := ''; 419 for I := 0 to Count - 1 do begin 420 Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ','; 421 end; 422 end; 423 424 function TAssociativeArray.GetValues(Index: string): string; 425 begin 426 Result := inherited Values[Index]; 427 end; 428 429 function TAssociativeArray.GetValuesAtIndex(Index: Integer): string; 430 begin 431 Result := inherited Values[Names[Index]]; 432 end; 433 434 procedure TAssociativeArray.SetValues(Index: string; const Value: string); 435 begin 436 inherited Values[Index] := Value; 437 end; 438 439 { TDbRows } 440 441 destructor TDbRows.Destroy; 442 begin 443 inherited; 444 end; 445 446 function TDbRows.GetData(Index: Integer): TAssociativeArray; 447 begin 448 Result := TAssociativeArray(Items[Index]); 449 end; 450 451 procedure TDbRows.SetData(Index: Integer; const Value: TAssociativeArray); 391 function TDbRows.GetData(Index: Integer): TDictionaryStringString; 392 begin 393 Result := TDictionaryStringString(Items[Index]); 394 end; 395 396 procedure TDbRows.SetData(Index: Integer; const Value: TDictionaryStringString); 452 397 begin 453 398 Items[Index] := Value;
Note:
See TracChangeset
for help on using the changeset viewer.