Changeset 38 for trunk/Common/USqlDatabase.pas
- Timestamp:
- Nov 13, 2010, 4:32:15 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/USqlDatabase.pas
r37 r38 3 3 {$mode Delphi}{$H+} 4 4 5 // Upraveno: 8.9.20105 // Upraveno: 28.10.2010 6 6 7 7 interface … … 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); … … 167 167 168 168 try 169 Rows := Query('SET NAMES ' + Encoding); 169 Rows := TDbRows.Create; 170 Query(Rows, 'SET NAMES ' + Encoding); 170 171 finally 171 172 Rows.Free; … … 194 195 System.Delete(DbValues, 1, 1); 195 196 try 196 DbResult := Query('INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 197 DbResult := TDbRows.Create; 198 Query(DbResult, 'INSERT INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 197 199 finally 198 200 DbResult.Free; … … 200 202 end; 201 203 202 function TSqlDatabase.Query(Data: string): TDbRows;204 procedure TSqlDatabase.Query(DbRows: TDbRows; Data: string); 203 205 var 204 206 I, II: Integer; … … 206 208 DbRow: MYSQL_ROW; 207 209 begin 210 DbRows.Clear; 208 211 //DebugLog('SqlDatabase query: '+Data); 209 212 RepeatLastAction := False; 210 213 LastQuery := Data; 211 Result := TDbRows.Create;212 214 mysql_query(FSession, PChar(Data)); 213 215 if LastErrorNumber <> 0 then begin … … 217 219 DbResult := mysql_store_result(FSession); 218 220 if Assigned(DbResult) then begin 219 Result.Count := mysql_num_rows(DbResult);220 for I := 0 to Result.Count - 1 do begin221 DbRows.Count := mysql_num_rows(DbResult); 222 for I := 0 to DbRows.Count - 1 do begin 221 223 DbRow := mysql_fetch_row(DbResult); 222 Result[I] := TDictionaryStringString.Create;223 with Result[I] do begin224 DbRows[I] := TDictionaryStringString.Create; 225 with DbRows[I] do begin 224 226 for II := 0 to mysql_num_fields(DbResult) - 1 do begin 225 227 Add(mysql_fetch_field_direct(DbResult, II)^.Name, … … 253 255 System.Delete(DbValues, 1, 1); 254 256 try 255 DbResult := Query('REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 257 DbResult := TDbRows.Create; 258 Query(DbResult, 'REPLACE INTO `' + Table + '` (' + DbNames + ') VALUES (' + DbValues + ')'); 256 259 finally 257 260 DbResult.Free; … … 259 262 end; 260 263 261 function TSqlDatabase.Select(ATable: string; Filter: string = '*'; Condition: string = '1'): TDbRows;264 procedure TSqlDatabase.Select(DbRows: TDbRows; ATable: string; Filter: string = '*'; Condition: string = '1'); 262 265 begin 263 266 Table := ATable; 264 Result := Query('SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition);267 Query(DbRows, 'SELECT ' + Filter + ' FROM `' + Table + '` WHERE ' + Condition); 265 268 end; 266 269 … … 282 285 System.Delete(DbValues, 1, 1); 283 286 try 284 DbResult := Query('UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition); 287 DbResult := TDbRows.Create; 288 Query(DbResult, 'UPDATE `' + Table + '` SET (' + DbValues + ') WHERE ' + Condition); 285 289 finally 286 290 DbResult.Free; … … 299 303 Table := ATable; 300 304 try 301 DbResult := Query('DELETE FROM `' + Table + '` WHERE ' + Condition); 305 DbResult := TDbRows.Create; 306 Query(DbResult, 'DELETE FROM `' + Table + '` WHERE ' + Condition); 302 307 finally 303 308 DbResult.Free; … … 341 346 var 342 347 TempDatabase: string; 348 DbRows: TDbRows; 343 349 begin 344 350 TempDatabase := Database; 345 351 Database := 'mysql'; 346 352 Connect; 347 Query('CREATE DATABASE ' + TempDatabase); 353 try 354 DbRows := TDbRows.Create; 355 Query(DbRows, 'CREATE DATABASE ' + TempDatabase); 356 finally 357 DbRows.Free; 358 end; 348 359 Disconnect; 349 360 Database := TempDatabase; … … 351 362 352 363 procedure TSqlDatabase.CreateTable(Name: string); 353 begin 354 Query('CREATE TABLE `' + Name + '`' + 355 ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));'); 364 var 365 DbRows: TDbRows; 366 begin 367 try 368 DbRows := TDbRows.Create; 369 Query(DbRows, 'CREATE TABLE `' + Name + '`' + 370 ' (`Id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`));'); 371 finally 372 DbRows.Free; 373 end; 356 374 end; 357 375 … … 361 379 ColTypes: array[0..17] of string = ('', 'INT', 'CHAR', 'INT', 'DOUBLE', 362 380 'VARCHAR(255)', 'SET', 'INT', '', '', 'TEXT', 'TEXT', '', '', '', '', '', ''); 363 begin 364 Query('ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' + 365 ColTypes[Integer(ColumnType)] + ' NOT NULL'); 381 var 382 DbRows: TDbRows; 383 begin 384 try 385 DbRows := TDbRows.Create; 386 Query(DbRows, 'ALTER TABLE `' + Table + '` ADD `' + ColumnName + '` ' + 387 ColTypes[Integer(ColumnType)] + ' NOT NULL'); 388 finally 389 DbRows.Free; 390 end; 366 391 end; 367 392
Note:
See TracChangeset
for help on using the changeset viewer.