Changeset 359 for tools/dbc_export/USqlDatabase.pas
- Timestamp:
- Mar 5, 2010, 8:46:18 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/dbc_export/USqlDatabase.pas
r335 r359 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; … … 57 59 UserName: string; 58 60 Password: string; 61 Encoding: string; 59 62 Table: string; 60 63 RepeatLastAction: Boolean; … … 62 65 procedure CreateDatabase; 63 66 procedure CreateTable(Name: string); 64 procedure CreateColumn( ATable, ColumnName: string; ColumnType: TTypeKind);67 procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind); 65 68 function Query(Data: string): TDbRows; 66 69 function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows; 67 70 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);71 procedure Insert(ATable: string; Data: TAssociativeArray); 72 procedure Update(ATable: string; Data: TAssociativeArray; Condition: string = '1'); 73 procedure Replace(ATable: string; Data: TAssociativeArray); 71 74 procedure Connect; 72 75 procedure Disconnect; … … 75 78 property LastErrorNumber: Integer read GetLastErrorNumber; 76 79 property Connected: Boolean read GetConnected; 77 property OnError: TNotifyEvent read FOnError write FOnError;78 80 constructor Create; 79 81 destructor Destroy; override; … … 84 86 function MySQLFloatToStr(F: Real): string; 85 87 function MySQLStrToFloat(S: string): Real; 86 function MySQLDateToDateTime(Date: string): TDateTime;87 88 88 89 implementation … … 107 108 { TDataModule2 } 108 109 109 function MySQLDateToDateTime(Date: string): TDateTime;110 begin111 Result := 0;112 end;113 114 110 function MySQLFloatToStr(F: Real): string; 115 111 var … … 117 113 begin 118 114 S := FloatToStr(F); 119 if Pos(',', S) > 0 then S[Pos(',',S)] := '.';115 if Pos(',', S) > 0 then S[Pos(',',S)] := '.'; 120 116 Result := S; 121 117 end; … … 123 119 function MySQLStrToFloat(S: string): Real; 124 120 begin 125 if Pos('.', S) > 0 then S[Pos('.',S)] := ',';121 if Pos('.', S) > 0 then S[Pos('.',S)] := ','; 126 122 Result := StrToFloat(S); 127 123 end; … … 143 139 end else FConnected := False; 144 140 CheckError; 145 Rows := Query('SET NAMES cp1250');141 Rows := Query('SET NAMES ' + Encoding); 146 142 Rows.Free; 147 143 end; 148 144 149 procedure TSqlDatabase.Insert(ATable: string; Data: TAssoc Array);145 procedure TSqlDatabase.Insert(ATable: string; Data: TAssociativeArray); 150 146 var 151 147 DbNames: string; … … 153 149 I: Integer; 154 150 Value: string; 155 DbR ows: TDbRows;151 DbResult: TDbRows; 156 152 begin 157 153 Table := ATable; 158 154 DbNames := ''; 159 155 DbValues := ''; 160 for I := 0 to Data.Count -1 do begin156 for I := 0 to Data.Count - 1 do begin 161 157 Value := Data.ValuesAtIndex[I]; 162 158 StringReplace(Value, '"', '\"', [rfReplaceAll]); … … 167 163 System.Delete(DbNames, 1, 1); 168 164 System.Delete(DbValues, 1, 1); 169 DbR ows:= Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');170 DbR ows.Free;165 DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 166 DbResult.Free; 171 167 end; 172 168 … … 176 172 DbResult: PMYSQL_RES; 177 173 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 174 begin 182 175 //DebugLog('SqlDatabase query: '+Data); … … 194 187 if Assigned(DbResult) then begin 195 188 Result.Count := mysql_num_rows(DbResult); 196 for I := 0 to Result.Count -1 do begin189 for I := 0 to Result.Count - 1 do begin 197 190 DbRow := mysql_fetch_row(DbResult); 198 Result[I] := TAssoc Array.Create;191 Result[I] := TAssociativeArray.Create; 199 192 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]); 193 for II := 0 to mysql_num_fields(DbResult) - 1 do begin 194 Add(mysql_fetch_field_direct(DbResult, II)^.Name + 195 NameValueSeparator + PChar((DbRow + II)^)); 202 196 end; 203 197 end; … … 215 209 end; 216 210 217 procedure TSqlDatabase.Replace(ATable: string; Data: TAssoc Array);211 procedure TSqlDatabase.Replace(ATable: string; Data: TAssociativeArray); 218 212 var 219 213 DbNames: string; … … 221 215 Value: string; 222 216 I: Integer; 223 DbR ows: TDbRows;217 DbResult: TDbRows; 224 218 begin 225 219 Table := ATable; 226 220 DbNames := ''; 227 221 DbValues := ''; 228 for I := 0 to Data.Count -1 do begin222 for I := 0 to Data.Count - 1 do begin 229 223 Value := Data.ValuesAtIndex[I]; 230 224 StringReplace(Value, '"', '\"', [rfReplaceAll]); … … 235 229 System.Delete(DbNames, 1, 1); 236 230 System.Delete(DbValues, 1, 1); 237 DbR ows:= Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')');238 DbR ows.Free;231 DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 232 DbResult.Free; 239 233 end; 240 234 … … 245 239 end; 246 240 247 procedure TSqlDatabase.Update(ATable: string; Data: TAssoc Array; Condition: string = '1');241 procedure TSqlDatabase.Update(ATable: string; Data: TAssociativeArray; Condition: string = '1'); 248 242 var 249 243 DbValues: string; 250 244 Value: string; 251 245 I: Integer; 252 DbR ows: TDbRows;246 DbResult: TDbRows; 253 247 begin 254 248 Table := ATable; 255 249 DbValues := ''; 256 for I := 0 to Data.Count -1 do begin250 for I := 0 to Data.Count - 1 do begin 257 251 Value := Data.ValuesAtIndex[I]; 258 252 StringReplace(Value, '"', '\"', [rfReplaceAll]); … … 261 255 end; 262 256 System.Delete(DbValues, 1, 1); 263 DbR ows:= Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition);264 D BRows.Free;257 DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition); 258 DbResult.Free; 265 259 end; 266 260 … … 272 266 { TAssocArray } 273 267 274 procedure TAssoc Array.AddKeyValue(Key, Value: string);268 procedure TAssociativeArray.AddKeyValue(Key, Value: string); 275 269 begin 276 270 Add(Key + NameValueSeparator + Value); 277 271 end; 278 272 279 constructor TAssoc Array.Create;273 constructor TAssociativeArray.Create; 280 274 begin 281 275 NameValueSeparator := '|'; 282 276 end; 283 277 284 destructor TAssoc Array.Destroy;278 destructor TAssociativeArray.Destroy; 285 279 begin 286 280 inherited; 287 281 end; 288 282 289 function TAssoc Array.GetAllValues: string;283 function TAssociativeArray.GetAllValues: string; 290 284 var 291 285 I: Integer; 292 286 begin 293 287 Result := ''; 294 for I := 0 to Count -1 do begin288 for I := 0 to Count - 1 do begin 295 289 Result := Result + Names[I] + '=' + ValuesAtIndex[I] + ','; 296 290 end; 297 291 end; 298 292 299 function TAssoc Array.GetValues(Index: string): string;293 function TAssociativeArray.GetValues(Index: string): string; 300 294 begin 301 295 Result := inherited Values[Index]; 302 296 end; 303 297 304 function TAssoc Array.GetValuesAtIndex(Index: Integer): string;298 function TAssociativeArray.GetValuesAtIndex(Index: Integer): string; 305 299 begin 306 300 Result := inherited Values[Names[Index]]; … … 309 303 procedure TSqlDatabase.Delete(ATable: string; Condition: string = '1'); 310 304 var 311 DbR ows: TDbRows;305 DbResult: TDbRows; 312 306 begin 313 307 Table := ATable; 314 DbR ows := Query('DELETE FROM `' + Table + '` WHERE ' + Condition);315 DbR ows.Free;308 DbResult = Query('DELETE FROM `' + Table + '` WHERE ' + Condition); 309 DbResult.Free; 316 310 end; 317 311 … … 331 325 inherited; 332 326 FSession := nil; 333 end; 334 335 procedure TAssocArray.SetValues(Index: string; const Value: string); 327 Encoding := 'utf8'; 328 end; 329 330 procedure TAssociativeArray.SetValues(Index: string; const Value: string); 336 331 begin 337 332 inherited Values[Index] := Value; … … 344 339 I: Integer; 345 340 begin 346 for I := 0 to Count - 1 do Data[I].Free; 341 for I := 0 to Count - 1 do 342 Data[I].Free; 347 343 inherited; 348 344 end; 349 345 350 function TDbRows.GetData(Index: Integer): TAssoc Array;346 function TDbRows.GetData(Index: Integer): TAssociativeArray; 351 347 begin 352 348 Result := Items[Index]; 353 349 end; 354 350 355 procedure TDbRows.SetData(Index: Integer; const Value: TAssoc Array);351 procedure TDbRows.SetData(Index: Integer; const Value: TAssociativeArray); 356 352 begin 357 353 Items[Index] := Value; … … 376 372 begin 377 373 Result := LastErrorNumber <> 0; 378 if Result and Assigned(OnError) then OnError(Self); 374 if Result then 375 raise EQueryError.Create('Database query error: "' + LastErrorMessage + '"'); 379 376 end; 380 377 … … 394 391 begin 395 392 Query('CREATE TABLE `' + Name + '`' + 396 ' (` id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));');397 end; 398 399 procedure TSqlDatabase.CreateColumn( ATable, ColumnName: string;393 ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));'); 394 end; 395 396 procedure TSqlDatabase.CreateColumn(Table, ColumnName: string; 400 397 ColumnType: TTypeKind); 401 398 const
Note:
See TracChangeset
for help on using the changeset viewer.