Changeset 279 for branches/web/USqlDatabase.pas
- Timestamp:
- Mar 5, 2010, 8:52:50 PM (15 years ago)
- Location:
- branches/web
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/web
- Property svn:ignore
-
old new 3 3 *.o 4 4 backup 5 6 5 index.cgi 7 8 6 index.compiled 9 10 7 UConfig.pas 8 bin
-
- Property svn:ignore
-
branches/web/USqlDatabase.pas
r142 r279 1 1 unit USqlDatabase; 2 2 3 {$mode delphi}{$H+} 3 {$mode Delphi}{$H+} 4 // Upraveno: 16.12.2009 4 5 5 6 interface 6 7 7 8 uses 8 SysUtils, Classes, mysql50, TypInfo;9 SysUtils, Classes, Dialogs, mysql50, TypInfo; 9 10 10 11 type 12 EQueryError = Exception; 13 11 14 TClientCapabilities = (_CLIENT_LONG_PASSWORD, _CLIENT_FOUND_ROWS, 12 15 _CLIENT_LONG_FLAG, _CLIENT_CONNECT_WITH_DB, _CLIENT_NO_SCHEMA, … … 16 19 TSetClientCapabilities = set of TClientCapabilities; 17 20 18 TAssoc Array = class(TStringList)21 TAssociativeArray = class(TStringList) 19 22 private 20 23 function GetValues(Index: string): string; … … 32 35 TDbRows = class(TList) 33 36 private 34 function GetData(Index: Integer): TAssoc Array;35 procedure SetData(Index: Integer; const Value: TAssoc Array);37 function GetData(Index: Integer): TAssociativeArray; 38 procedure SetData(Index: Integer; const Value: TAssociativeArray); 36 39 public 37 property Data[Index: Integer]: TAssoc Array read GetData write SetData; default;40 property Data[Index: Integer]: TAssociativeArray read GetData write SetData; default; 38 41 destructor Destroy; override; 39 42 end; … … 44 47 FSession: PMYSQL; 45 48 FConnected: Boolean; 46 FOnError: TNotifyEvent;47 49 FDatabase: string; 48 50 function GetConnected: Boolean; … … 52 54 function GetCharset: string; 53 55 procedure SetDatabase(const Value: string); 54 { Private declarations }55 56 public 56 57 Hostname: string; 57 58 UserName: string; 58 59 Password: string; 60 Encoding: string; 59 61 Table: string; 60 62 RepeatLastAction: Boolean; … … 62 64 procedure CreateDatabase; 63 65 procedure CreateTable(Name: string); 64 procedure CreateColumn( ATable, ColumnName: string; ColumnType: TTypeKind);66 procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind); 65 67 function Query(Data: string): TDbRows; 66 68 function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows; 67 69 procedure Delete(ATable: string; Condition: string = '1'); 68 procedure Insert(ATable: string; Data: TAssoc Array);69 procedure Update(ATable: string; Data: TAssoc Array; Condition: string = '1');70 procedure Replace(ATable: string; Data: TAssoc Array);70 procedure Insert(ATable: string; Data: TAssociativeArray); 71 procedure Update(ATable: string; Data: TAssociativeArray; Condition: string = '1'); 72 procedure Replace(ATable: string; Data: TAssociativeArray); 71 73 procedure Connect; 72 74 procedure Disconnect; … … 75 77 property LastErrorNumber: Integer read GetLastErrorNumber; 76 78 property Connected: Boolean read GetConnected; 77 property OnError: TNotifyEvent read FOnError write FOnError;78 79 constructor Create; 79 80 destructor Destroy; override; … … 84 85 function MySQLFloatToStr(F: Real): string; 85 86 function MySQLStrToFloat(S: string): Real; 86 function MySQLDateToDateTime(Date: string): TDateTime;87 87 88 88 implementation … … 105 105 CLIENT_TRANSACTIONS = 8192; // Client knows about transactions 106 106 107 { TDataModule2 }108 109 function MySQLDateToDateTime(Date: string): TDateTime;110 begin111 Result := 0;112 end;113 114 107 function MySQLFloatToStr(F: Real): string; 115 108 var … … 117 110 begin 118 111 S := FloatToStr(F); 119 if Pos(',', S) > 0 then S[Pos(',',S)] := '.';112 if Pos(',', S) > 0 then S[Pos(',',S)] := '.'; 120 113 Result := S; 121 114 end; … … 123 116 function MySQLStrToFloat(S: string): Real; 124 117 begin 125 if Pos('.', S) > 0 then S[Pos('.',S)] := ',';118 if Pos('.', S) > 0 then S[Pos('.',S)] := ','; 126 119 Result := StrToFloat(S); 127 120 end; 121 122 { TSqlDatabase } 128 123 129 124 procedure TSqlDatabase.Connect; … … 143 138 end else FConnected := False; 144 139 CheckError; 145 Rows := Query('SET NAMES cp1250');140 Rows := Query('SET NAMES ' + Encoding); 146 141 Rows.Free; 147 142 end; 148 143 149 procedure TSqlDatabase.Insert(ATable: string; Data: TAssoc Array);144 procedure TSqlDatabase.Insert(ATable: string; Data: TAssociativeArray); 150 145 var 151 146 DbNames: string; … … 153 148 I: Integer; 154 149 Value: string; 155 DbR ows: TDbRows;150 DbResult: TDbRows; 156 151 begin 157 152 Table := ATable; 158 153 DbNames := ''; 159 154 DbValues := ''; 160 for I := 0 to Data.Count -1 do begin155 for I := 0 to Data.Count - 1 do begin 161 156 Value := Data.ValuesAtIndex[I]; 162 157 StringReplace(Value, '"', '\"', [rfReplaceAll]); … … 167 162 System.Delete(DbNames, 1, 1); 168 163 System.Delete(DbValues, 1, 1); 169 DbR ows:= Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');170 DbR ows.Free;164 DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 165 DbResult.Free; 171 166 end; 172 167 … … 176 171 DbResult: PMYSQL_RES; 177 172 DbRow: MYSQL_ROW; 178 type179 PMYSQL_ROW2 = ^TMYSQL_ROW2; // return data as array of strings180 TMYSQL_ROW2 = array[0..MaxInt div SizeOf(pChar) - 1] of pChar;181 173 begin 182 174 //DebugLog('SqlDatabase query: '+Data); … … 194 186 if Assigned(DbResult) then begin 195 187 Result.Count := mysql_num_rows(DbResult); 196 for I := 0 to Result.Count -1 do begin188 for I := 0 to Result.Count - 1 do begin 197 189 DbRow := mysql_fetch_row(DbResult); 198 Result[I] := TAssoc Array.Create;190 Result[I] := TAssociativeArray.Create; 199 191 with Result[I] do begin 200 for II := 0 to mysql_num_fields(DbResult)-1 do begin 201 Add(mysql_fetch_field_direct(DbResult, II)^.name + NameValueSeparator + PMYSQL_ROW2(DbRow)^[II]); 192 for II := 0 to mysql_num_fields(DbResult) - 1 do begin 193 Add(mysql_fetch_field_direct(DbResult, II)^.Name + 194 NameValueSeparator + PChar((DbRow + II)^)); 202 195 end; 203 196 end; … … 215 208 end; 216 209 217 procedure TSqlDatabase.Replace(ATable: string; Data: TAssoc Array);210 procedure TSqlDatabase.Replace(ATable: string; Data: TAssociativeArray); 218 211 var 219 212 DbNames: string; … … 221 214 Value: string; 222 215 I: Integer; 223 DbR ows: TDbRows;216 DbResult: TDbRows; 224 217 begin 225 218 Table := ATable; 226 219 DbNames := ''; 227 220 DbValues := ''; 228 for I := 0 to Data.Count -1 do begin221 for I := 0 to Data.Count - 1 do begin 229 222 Value := Data.ValuesAtIndex[I]; 230 223 StringReplace(Value, '"', '\"', [rfReplaceAll]); … … 235 228 System.Delete(DbNames, 1, 1); 236 229 System.Delete(DbValues, 1, 1); 237 DbR ows:= Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');238 DbR ows.Free;230 DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 231 DbResult.Free; 239 232 end; 240 233 … … 245 238 end; 246 239 247 procedure TSqlDatabase.Update(ATable: string; Data: TAssoc Array; Condition: string = '1');240 procedure TSqlDatabase.Update(ATable: string; Data: TAssociativeArray; Condition: string = '1'); 248 241 var 249 242 DbValues: string; 250 243 Value: string; 251 244 I: Integer; 252 DbR ows: TDbRows;245 DbResult: TDbRows; 253 246 begin 254 247 Table := ATable; 255 248 DbValues := ''; 256 for I := 0 to Data.Count -1 do begin249 for I := 0 to Data.Count - 1 do begin 257 250 Value := Data.ValuesAtIndex[I]; 258 251 StringReplace(Value, '"', '\"', [rfReplaceAll]); … … 261 254 end; 262 255 System.Delete(DbValues, 1, 1); 263 DbR ows:= Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);264 D BRows.Free;256 DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition); 257 DbResult.Free; 265 258 end; 266 259 … … 270 263 end; 271 264 272 { TAssocArray }273 274 procedure TAssocArray.AddKeyValue(Key, Value: string);275 begin276 Add(Key + NameValueSeparator + Value);277 end;278 279 constructor TAssocArray.Create;280 begin281 NameValueSeparator := '|';282 end;283 284 destructor TAssocArray.Destroy;285 begin286 inherited;287 end;288 289 function TAssocArray.GetAllValues: string;290 var291 I: Integer;292 begin293 Result := '';294 for I := 0 to Count-1 do begin295 Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ',';296 end;297 end;298 299 function TAssocArray.GetValues(Index: string): string;300 begin301 Result := inherited Values[Index];302 end;303 304 function TAssocArray.GetValuesAtIndex(Index: Integer): string;305 begin306 Result := inherited Values[Names[Index]];307 end;308 309 265 procedure TSqlDatabase.Delete(ATable: string; Condition: string = '1'); 310 266 var 311 DbR ows: TDbRows;267 DbResult: TDbRows; 312 268 begin 313 269 Table := ATable; 314 DbR ows:= Query('DELETE FROM `' + Table + '` WHERE ' + Condition);315 DbR ows.Free;270 DbResult := Query('DELETE FROM `' + Table + '` WHERE ' + Condition); 271 DbResult.Free; 316 272 end; 317 273 … … 331 287 inherited; 332 288 FSession := nil; 333 end; 334 335 procedure TAssocArray.SetValues(Index: string; const Value: string); 336 begin 337 inherited Values[Index] := Value; 338 end; 339 340 { TDbRows } 341 342 destructor TDbRows.Destroy; 343 var 344 I: Integer; 345 begin 346 for I := 0 to Count - 1 do Data[I].Free; 347 inherited; 348 end; 349 350 function TDbRows.GetData(Index: Integer): TAssocArray; 351 begin 352 Result := Items[Index]; 353 end; 354 355 procedure TDbRows.SetData(Index: Integer; const Value: TAssocArray); 356 begin 357 Items[Index] := Value; 289 Encoding := 'utf8'; 358 290 end; 359 291 … … 376 308 begin 377 309 Result := LastErrorNumber <> 0; 378 if Result and Assigned(OnError) then OnError(Self); 310 if Result then 311 raise EQueryError.Create('Database query error: "' + LastErrorMessage + '"'); 379 312 end; 380 313 … … 394 327 begin 395 328 Query('CREATE TABLE `' + Name + '`' + 396 ' (` id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));');397 end; 398 399 procedure TSqlDatabase.CreateColumn( ATable, ColumnName: string;329 ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));'); 330 end; 331 332 procedure TSqlDatabase.CreateColumn(Table, ColumnName: string; 400 333 ColumnType: TTypeKind); 401 334 const … … 423 356 end; 424 357 358 { TAssociativeArray } 359 360 procedure TAssociativeArray.AddKeyValue(Key, Value: string); 361 begin 362 Add(Key + NameValueSeparator + Value); 363 end; 364 365 constructor TAssociativeArray.Create; 366 begin 367 NameValueSeparator := '|'; 368 end; 369 370 destructor TAssociativeArray.Destroy; 371 begin 372 inherited; 373 end; 374 375 function TAssociativeArray.GetAllValues: string; 376 var 377 I: Integer; 378 begin 379 Result := ''; 380 for I := 0 to Count - 1 do begin 381 Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ','; 382 end; 383 end; 384 385 function TAssociativeArray.GetValues(Index: string): string; 386 begin 387 Result := inherited Values[Index]; 388 end; 389 390 function TAssociativeArray.GetValuesAtIndex(Index: Integer): string; 391 begin 392 Result := inherited Values[Names[Index]]; 393 end; 394 395 procedure TAssociativeArray.SetValues(Index: string; const Value: string); 396 begin 397 inherited Values[Index] := Value; 398 end; 399 400 { TDbRows } 401 402 destructor TDbRows.Destroy; 403 var 404 I: Integer; 405 begin 406 for I := 0 to Count - 1 do 407 Data[I].Free; 408 inherited; 409 end; 410 411 function TDbRows.GetData(Index: Integer): TAssociativeArray; 412 begin 413 Result := Items[Index]; 414 end; 415 416 procedure TDbRows.SetData(Index: Integer; const Value: TAssociativeArray); 417 begin 418 Items[Index] := Value; 419 end; 420 425 421 end. 426 422
Note:
See TracChangeset
for help on using the changeset viewer.