Changeset 106
- Timestamp:
- Dec 31, 2010, 10:03:27 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Network/MysqlClient/USqlDatabase.pas
r81 r106 3 3 {$mode Delphi}{$H+} 4 4 5 // Upraveno: 28.10.20105 // Modified: 2010-12-24 6 6 7 7 interface 8 8 9 9 uses 10 SysUtils, Classes, Dialogs, mysql50, TypInfo, UStringListEx,11 ListObject, DictionaryStringString;10 SysUtils, Classes, Dialogs, mysql50, TypInfo, 11 SpecializedDictionary, SpecializedList; 12 12 13 13 type … … 52 52 procedure CreateTable(Name: string); 53 53 procedure CreateColumn(Table, ColumnName: string; ColumnType: TTypeKind); 54 function Query(Data: string): TDbRows;55 function Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;54 procedure Query(DbRows: TDbRows; Data: string); 55 procedure Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1'); 56 56 procedure Delete(ATable: string; Condition: string = '1'); 57 57 procedure Insert(ATable: string; Data: TDictionaryStringString); … … 113 113 end; 114 114 115 function StrToStr(Value: string): string; 116 begin 117 Result := Value; 118 end; 119 115 120 function SQLToDateTime(Value: string): TDateTime; 116 121 var 117 Parts: T StringListEx;118 DateParts: T StringListEx;119 TimeParts: T StringListEx;120 begin 121 try 122 Parts := T StringListEx.Create;123 DateParts := T StringListEx.Create;124 TimeParts := T StringListEx.Create;125 126 Parts.Explode( ' ', Value);127 DateParts.Explode( '-', Parts[0]);122 Parts: TListString; 123 DateParts: TListString; 124 TimeParts: TListString; 125 begin 126 try 127 Parts := TListString.Create; 128 DateParts := TListString.Create; 129 TimeParts := TListString.Create; 130 131 Parts.Explode(Value, ' ', StrToStr); 132 DateParts.Explode(Parts[0], '-', StrToStr); 128 133 Result := EncodeDate(StrToInt(DateParts[0]), StrToInt(DateParts[1]), 129 134 StrToInt(DateParts[2])); 130 135 if Parts.Count > 1 then begin 131 TimeParts.Explode( ':', Parts[1]);136 TimeParts.Explode(Parts[1], ':', StrToStr); 132 137 Result := Result + EncodeTime(StrToInt(TimeParts[0]), StrToInt(TimeParts[1]), 133 138 StrToInt(TimeParts[2]), 0); … … 167 172 168 173 try 169 Rows := Query('SET NAMES ' + Encoding); 174 Rows := TDbRows.Create; 175 Query(Rows, 'SET NAMES ' + Encoding); 170 176 finally 171 177 Rows.Free; … … 194 200 System.Delete(DbValues, 1, 1); 195 201 try 196 DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 202 DbResult := TDbRows.Create; 203 Query(DbResult, 'INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 197 204 finally 198 205 DbResult.Free; … … 200 207 end; 201 208 202 function TSqlDatabase.Query(Data: string): TDbRows;209 procedure TSqlDatabase.Query(DbRows: TDbRows; Data: string); 203 210 var 204 211 I, II: Integer; … … 206 213 DbRow: MYSQL_ROW; 207 214 begin 215 DbRows.Clear; 208 216 //DebugLog('SqlDatabase query: '+Data); 209 217 RepeatLastAction := False; 210 218 LastQuery := Data; 211 Result := TDbRows.Create;212 219 mysql_query(FSession, PChar(Data)); 213 220 if LastErrorNumber <> 0 then begin … … 217 224 DbResult := mysql_store_result(FSession); 218 225 if Assigned(DbResult) then begin 219 Result.Count := mysql_num_rows(DbResult);220 for I := 0 to Result.Count - 1 do begin226 DbRows.Count := mysql_num_rows(DbResult); 227 for I := 0 to DbRows.Count - 1 do begin 221 228 DbRow := mysql_fetch_row(DbResult); 222 Result[I] := TDictionaryStringString.Create;223 with Result[I] do begin229 DbRows[I] := TDictionaryStringString.Create; 230 with DbRows[I] do begin 224 231 for II := 0 to mysql_num_fields(DbResult) - 1 do begin 225 232 Add(mysql_fetch_field_direct(DbResult, II)^.Name, … … 253 260 System.Delete(DbValues, 1, 1); 254 261 try 255 DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 262 DbResult := TDbRows.Create; 263 Query(DbResult, 'REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 256 264 finally 257 265 DbResult.Free; … … 259 267 end; 260 268 261 function TSqlDatabase.Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;269 procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1'); 262 270 begin 263 271 Table := ATable; 264 Result := Query('SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);272 Query(DbRows, 'SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition); 265 273 end; 266 274 … … 282 290 System.Delete(DbValues, 1, 1); 283 291 try 284 DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition); 292 DbResult := TDbRows.Create; 293 Query(DbResult, 'UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition); 285 294 finally 286 295 DbResult.Free; … … 299 308 Table := ATable; 300 309 try 301 DbResult := Query('DELETE FROM `' + Table + '` WHERE ' + Condition); 310 DbResult := TDbRows.Create; 311 Query(DbResult, 'DELETE FROM `' + Table + '` WHERE ' + Condition); 302 312 finally 303 313 DbResult.Free; … … 341 351 var 342 352 TempDatabase: string; 353 DbRows: TDbRows; 343 354 begin 344 355 TempDatabase := Database; 345 356 Database := 'mysql'; 346 357 Connect; 347 Query('CREATE DATABASE ' + TempDatabase); 358 try 359 DbRows := TDbRows.Create; 360 Query(DbRows, 'CREATE DATABASE ' + TempDatabase); 361 finally 362 DbRows.Free; 363 end; 348 364 Disconnect; 349 365 Database := TempDatabase; … … 351 367 352 368 procedure TSqlDatabase.CreateTable(Name: string); 353 begin 354 Query('CREATE TABLE `' + Name + '`' + 355 ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));'); 369 var 370 DbRows: TDbRows; 371 begin 372 try 373 DbRows := TDbRows.Create; 374 Query(DbRows, 'CREATE TABLE `' + Name + '`' + 375 ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));'); 376 finally 377 DbRows.Free; 378 end; 356 379 end; 357 380 … … 361 384 ColTypes: array[0..17] of string = ('', 'INT', 'CHAR', 'INT', 'DOUBLE', 362 385 'VARCHAR(255)', 'SET', 'INT', '', '', 'TEXT', 'TEXT', '', '', '', '', '', ''); 363 begin 364 Query('ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' + 365 ColTypes[Integer(ColumnType)] + ' NOT NULL'); 386 var 387 DbRows: TDbRows; 388 begin 389 try 390 DbRows := TDbRows.Create; 391 Query(DbRows, 'ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' + 392 ColTypes[Integer(ColumnType)] + ' NOT NULL'); 393 finally 394 DbRows.Free; 395 end; 366 396 end; 367 397
Note:
See TracChangeset
for help on using the changeset viewer.