Changeset 23
- Timestamp:
- Mar 26, 2018, 12:40:10 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DbEngines/UEngineMySQL.pas
r21 r23 16 16 procedure LoadFields(Table: TTable); 17 17 protected 18 procedure LoadTables(Tables: TTables); override;19 18 public 20 19 SqlDatabase: TSqlDatabase; … … 102 101 end; 103 102 104 procedure TDatabaseMySQL.LoadTables(Tables: TTables);105 var106 DbRows: TDbRows;107 NewTable: TTable;108 I: Integer;109 begin110 DbRows := TDbRows.Create;111 try112 SqlDatabase.Query(DbRows, 'SELECT `Id`,`Name`,`Title` FROM `Model`');113 for I := 0 to DbRows.Count - 1 do begin114 NewTable := TTable.Create;115 NewTable.Id := StrToInt(TDictionaryStringString(DbRows[I]).Values['Id']);116 NewTable.DbClient := Self;117 NewTable.Name := TDictionaryStringString(DbRows[I]).Values['Name'];118 NewTable.Caption := TDictionaryStringString(DbRows[I]).Values['Title'];119 LoadFields(NewTable);120 Tables.Add(NewTable);121 end;122 finally123 DbRows.Free;124 end;125 end;126 127 103 procedure TDatabaseMySQL.Query(Text: string; DbRows: TDbRows = nil); 128 104 begin -
trunk/DbEngines/UEngineXML.pas
r22 r23 33 33 function CheckNext(var Source: string; Text: string): Boolean; 34 34 function GetNextPart(var Text: string; Separator: string = ' '): string; 35 function TableCreateIfNotExists(Name: string): TTable; 36 function FieldCreateIfNotExists(TableName, FieldName: string; DataType: TDataType): TField; 35 37 protected 36 38 public 37 39 procedure Query(Text: string; DbRows: TDbRows = nil); override; 38 procedure LoadTables(Tables: TTables); override;39 40 procedure Load; override; 40 41 procedure Save; override; … … 62 63 resourcestring 63 64 SWrongFileFormat = 'Wrong file format'; 65 STableNotFound = 'Table %s not found'; 66 SUnsupportedSqlCommand = 'Unsupported SQL command: %s'; 67 SColumnNotFoundInTable = 'Column %s not found in table %s'; 64 68 65 69 { TDbConnectParamsXml } … … 336 340 Result := Text; 337 341 Text := ''; 342 end; 343 end; 344 345 function TDatabaseXML.TableCreateIfNotExists(Name: string): TTable; 346 begin 347 Result := Tables.SearchByName(Name); 348 if not Assigned(Result) then begin 349 Tables.AddNew(Name); 350 end; 351 end; 352 353 function TDatabaseXML.FieldCreateIfNotExists(TableName, FieldName: string; DataType: TDataType): TField; 354 var 355 Table: TTable; 356 begin 357 TableCreateIfNotExists(TableName); 358 Table := Tables.SearchByName(TableName); 359 Result := Table.Fields.SearchByName(FieldName); 360 if not Assigned(Result) then begin 361 Table.Fields.AddNew(FieldName, DataType); 338 362 end; 339 363 end; … … 359 383 Command := GetNextPart(Text); 360 384 if Command = 'SELECT' then begin 385 DbRows.Count := 0; 361 386 Columns := GetNextPart(Text); 362 387 Expect(Text, 'FROM'); 363 388 TableName := GetNextPart(Text); 364 Table := Tables.SearchByName(TableName); 365 if Assigned(Table) then begin 366 DbRows.Count := 0; 389 if CheckNext(Text, 'WHERE') then begin 390 WhereName := GetNextPart(Text); 391 Command := GetNextPart(Text); 392 if Command = '=' then begin 393 WhereValue := GetNextPart(Text); 394 end else raise Exception.Create('Expression error'); 395 end; 396 if TableName = 'Model' then begin 367 397 if Columns = '*' then begin 368 for I := 0 to Table .Records.Count - 1 do begin398 for I := 0 to Tables.Count - 1 do begin 369 399 NewRecord := TDictionaryStringString.Create; 370 for F := 0 to Table.Fields.Count - 1 do371 NewRecord.Add(TField(Table.Fields[F]).Name, TValue(TRecord(Table.Records[I]).Values[I]).GetString);400 NewRecord.Add('Name', TTable(Tables[I]).Name); 401 NewRecord.Add('Caption', TTable(Tables[I]).Caption); 372 402 DbRows.Add(NewRecord); 373 403 end; … … 375 405 if Columns = 'COUNT(*)' then begin 376 406 NewRecord := TDictionaryStringString.Create; 377 NewRecord.Add('COUNT(*)', IntToStr(Table .Records.Count));407 NewRecord.Add('COUNT(*)', IntToStr(Tables.Count)); 378 408 DbRows.Add(NewRecord); 379 409 end else raise Exception.Create('Unsupported columns ' + Columns + ' specification'); 380 end else raise Exception.Create('Table ' + TableName + ' not found.'); 410 end else 411 if TableName = 'ModelField' then begin 412 if WhereName = 'Model' then 413 Table := Tables.SearchByName(WhereValue) 414 else Table := nil; 415 if Assigned(Table) then begin 416 if Columns = '*' then begin 417 for I := 0 to Table.Fields.Count - 1 do begin 418 NewRecord := TDictionaryStringString.Create; 419 NewRecord.Add('Name', TField(Table.Fields[I]).Name); 420 NewRecord.Add('Caption', TField(Table.Fields[I]).TextBefore); 421 NewRecord.Add('Model', Table.Name); 422 NewRecord.Add('DataType', TField(Table.Fields[I]).DataType.Name); 423 DbRows.Add(NewRecord); 424 end; 425 end else 426 if Columns = 'COUNT(*)' then begin 427 NewRecord := TDictionaryStringString.Create; 428 NewRecord.Add('COUNT(*)', IntToStr(Table.Fields.Count)); 429 DbRows.Add(NewRecord); 430 end else raise Exception.Create('Unsupported columns ' + Columns + ' specification'); 431 end else raise Exception.Create(Format(STableNotFound, [WhereValue])); 432 end else begin 433 Table := Tables.SearchByName(TableName); 434 if Assigned(Table) then begin 435 if Columns = '*' then begin 436 for I := 0 to Table.Records.Count - 1 do begin 437 NewRecord := TDictionaryStringString.Create; 438 for F := 0 to Table.Fields.Count - 1 do 439 NewRecord.Add(TField(Table.Fields[F]).Name, TValue(TRecord(Table.Records[I]).Values[I]).GetString); 440 DbRows.Add(NewRecord); 441 end; 442 end else 443 if Columns = 'COUNT(*)' then begin 444 NewRecord := TDictionaryStringString.Create; 445 NewRecord.Add('COUNT(*)', IntToStr(Table.Records.Count)); 446 DbRows.Add(NewRecord); 447 end else raise Exception.Create('Unsupported columns ' + Columns + ' specification'); 448 end else raise Exception.Create(Format(STableNotFound, [TableName])); 449 end; 381 450 end else 382 451 if Command = 'INSERT' then begin … … 401 470 Expect(Text, ')'); 402 471 if TableName = 'Model' then begin 403 Table := TTable.Create; 404 Table.Name := InsertValues.Values['Name']; 472 Table := Tables.AddNew(InsertValues.Values['Name']); 405 473 Table.Caption := InsertValues.Values['Caption']; 406 Table.DbClient := Self; 407 Tables.Add(Table); 474 end else 475 if TableName = 'ModelField' then begin 476 Table := Tables.SearchByName(InsertValues.Values['Model']); 477 if Assigned(Table) then begin 478 Field := Table.Fields.AddNew(InsertValues.Values['Name'], DbManager.DataTypes.SearchByType(ftString)); 479 Field.TextBefore := InsertValues.Values['Caption']; 480 Field.DataType := DbManager.DataTypes.SearchByName(InsertValues.Values['DataType']); 481 end else raise Exception.Create(Format(STableNotFound, [InsertValues.Values['Model']])); 408 482 end else begin 409 483 Table := Tables.SearchByName(TableName); 410 484 if Assigned(Table) then begin 411 Row := TRecord.Create; 412 Row.Parent := Table; 413 Row.InitValues; 485 Row := Table.Records.AddNew; 414 486 for ValueIndex := 0 to InsertValues.Count - 1 do begin 415 487 Field := Table.Fields.SearchByName(InsertValues.Names[ValueIndex]); 416 FieldIndex := Table.Fields.IndexOf(Field); 417 TValue(Row.Values[FieldIndex]).SetString(InsertValues.ValueFromIndex[ValueIndex]); 488 if Assigned(Field) then begin 489 FieldIndex := Table.Fields.IndexOf(Field); 490 TValue(Row.Values[FieldIndex]).SetString(InsertValues.ValueFromIndex[ValueIndex]); 491 end else raise Exception.Create(Format(SColumnNotFoundInTable, 492 [InsertValues.Names[ValueIndex], TableName])); 418 493 end; 419 Table.Records.Add(Row); 420 end else raise Exception.Create(Format('Table %s not found', [TableName])); 494 end else raise Exception.Create(Format(STableNotFound, [TableName])); 421 495 end; 422 496 InsertValues.Free; … … 436 510 if Assigned(Table) then begin 437 511 Tables.Remove(Table); 438 end else raise Exception.Create(Format('Table %s not found', [whereValue])); 512 end else raise Exception.Create(Format(STableNotFound, [whereValue])); 513 end else 514 if TableName = 'ModelField' then begin 515 Field := Table.Fields.SearchByName(WhereName); 516 Table.Fields.Remove(Field); 439 517 end else begin 440 518 Table := Tables.SearchByName(TableName); … … 444 522 Table.Records.Remove(Row); 445 523 end else raise Exception.Create('Row not found'); 446 end else raise Exception.Create(Format( 'Table %s not found', [TableName]));524 end else raise Exception.Create(Format(STableNotFound, [TableName])); 447 525 end; 448 526 end else 449 raise Exception.Create('Unsupported SQL command ' + Command); 450 end; 451 452 procedure TDatabaseXML.LoadTables(Tables: TTables); 453 begin 454 Tables.Assign(Self.Tables); 527 raise Exception.Create(Format(SUnsupportedSqlCommand, [Command])); 455 528 end; 456 529 … … 461 534 if FileExists(FileName) then 462 535 LoadFromFile(FileName); 536 (* 537 TableCreateIfNotExists('Model'); 538 TableCreateIfNotExists('ModelField'); 539 TableCreateIfNotExists('DataType'); 540 TableCreateIfNotExists('Module'); 541 542 FieldCreateIfNotExists('ModelField', 'Name', DbManager.DataTypes.SearchByType(ftString)); 543 FieldCreateIfNotExists('ModelField', 'Caption', DbManager.DataTypes.SearchByType(ftString)); 544 FieldCreateIfNotExists('ModelField', 'Model', DbManager.DataTypes.SearchByType(ftString)); 545 FieldCreateIfNotExists('ModelField', 'DataType', DbManager.DataTypes.SearchByType(ftString)); 546 FieldCreateIfNotExists('Model', 'Name', DbManager.DataTypes.SearchByType(ftString)); 547 FieldCreateIfNotExists('Model', 'Caption', DbManager.DataTypes.SearchByType(ftString)); 548 FieldCreateIfNotExists('Model', 'Module', DbManager.DataTypes.SearchByType(ftString)); 549 FieldCreateIfNotExists('Model', 'System', DbManager.DataTypes.SearchByType(ftBoolean)); 550 FieldCreateIfNotExists('DataType', 'Name', DbManager.DataTypes.SearchByType(ftString)); 551 FieldCreateIfNotExists('DataType', 'Caption', DbManager.DataTypes.SearchByType(ftString)); 552 FieldCreateIfNotExists('DataType', 'FieldType', DbManager.DataTypes.SearchByType(ftInteger)); 553 FieldCreateIfNotExists('Module', 'Name', DbManager.DataTypes.SearchByType(ftString)); 554 FieldCreateIfNotExists('Module', 'Caption', DbManager.DataTypes.SearchByType(ftString)); 555 *) 463 556 end; 464 557 -
trunk/Forms/UFormFields.pas
r22 r23 91 91 if FormField.ShowModal = mrOk then begin 92 92 FormField.Save(NewField); 93 Fields.Table.DbClient.Query('INSERT INTO ModelField ( Name , TextBefore) VALUES ( ' +94 NewField.Name + ' , ' + NewField.TextBefore + ' )');93 Fields.Table.DbClient.Query('INSERT INTO ModelField ( Name , Caption , Model , DataType ) VALUES ( ' + 94 NewField.Name + ' , ' + NewField.TextBefore + ' , ' + TableName + ' , ' + NewField.DataType.Name + ' )'); 95 95 ReloadList; 96 96 end else NewField.Free; … … 160 160 I: Integer; 161 161 begin 162 Fields.Clear; 162 163 DbRows := TDbRows.Create; 163 164 Fields.Table.DbClient.Query('SELECT * FROM ModelField WHERE Model = ' + Fields.Table.Name, DbRows); 164 165 for I := 0 to DbRows.Count - 1 do begin 165 NewField := TField.Create; 166 NewField.Table := Fields.Table; 167 Fields.Add(NewField); 166 NewField := Fields.AddNew(DbRows[I].Values['Name'], Fields.Table.DbClient.DbManager.DataTypes.SearchByName(DbRows[I].Values['DataType'])); 167 NewField.TextBefore := DbRows[I].Values['Caption']; 168 168 end; 169 169 DbRows.Free; -
trunk/Forms/UFormTables.pas
r22 r23 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, 9 ActnList, Menus, UDatabase ;9 ActnList, Menus, UDatabase, SpecializedDictionary; 10 10 11 11 type … … 212 212 procedure TFormTables.FormCreate(Sender: TObject); 213 213 begin 214 FTables := TTables.Create (False);214 FTables := TTables.Create; 215 215 end; 216 216 … … 232 232 procedure TFormTables.ReloadList; 233 233 var 234 DbRows: TDbRows; 235 NewTable: TTable; 234 236 I: Integer; 235 237 begin 236 if Assigned(DbClient) then DbClient.LoadTables(Tables) 237 else Tables.Clear; 238 Tables.Clear; 239 if Assigned(DbClient) then begin 240 DbRows := TDbRows.Create; 241 DbClient.Query('SELECT * FROM Model', DbRows); 242 for I := 0 to DbRows.Count - 1 do begin 243 NewTable := Tables.AddNew(TDictionaryStringString(DbRows[I]).Values['Name']); 244 NewTable.Caption := TDictionaryStringString(DbRows[I]).Values['Caption']; 245 end; 246 DbRows.Free; 247 end; 248 238 249 for I := 0 to Tables.Count - 1 do 239 250 TTable(Tables[I]).LoadRecordsCount; -
trunk/Languages/MyData.cs.po
r22 r23 9 9 "MIME-Version: 1.0\n" 10 10 "Content-Transfer-Encoding: 8bit\n" 11 "X-Generator: Poedit 2.0. 6\n"11 "X-Generator: Poedit 2.0.4\n" 12 12 "Language: cs\n" 13 13 … … 65 65 #: tformconnect.buttonxmlbrowse.caption 66 66 msgid "Browse" 67 msgstr " "67 msgstr "Procházet" 68 68 69 69 #: tformconnect.caption 70 70 msgid "Connection parameters:" 71 msgstr " "71 msgstr "Parametry spojení:" 72 72 73 73 #: tformconnect.label1.caption … … 79 79 #: tformconnect.label2.caption 80 80 msgid "Database engine:" 81 msgstr " "81 msgstr "Databázový systém:" 82 82 83 83 #: tformconnect.label3.caption 84 84 msgid "Xml file:" 85 msgstr " "85 msgstr "Xml soubor:" 86 86 87 87 #: tformconnect.label4.caption 88 88 msgid "Host:" 89 msgstr " "89 msgstr "Hostitel:" 90 90 91 91 #: tformconnect.label5.caption 92 92 msgid "Port:" 93 msgstr " "93 msgstr "Port:" 94 94 95 95 #: tformconnect.tabsheetregistry.caption … … 170 170 #: tformfield.label2.caption 171 171 msgid "Text before:" 172 msgstr "Text za:"172 msgstr "Text před:" 173 173 174 174 #: tformfield.label3.caption 175 175 msgid "Text after:" 176 msgstr "Text p řed:"176 msgstr "Text po:" 177 177 178 178 #: tformfield.label4.caption … … 500 500 msgstr "Čas" 501 501 502 #: uenginexml.scolumnnotfoundintable 503 msgid "Column %s not found in table %s" 504 msgstr "Sloupec %d nenalezen v tabulce %s" 505 506 #: uenginexml.stablenotfound 507 msgid "Table %s not found" 508 msgstr "Tabulka %s nenalezena" 509 510 #: uenginexml.sunsupportedsqlcommand 511 msgid "Unsupported SQL command: %s" 512 msgstr "Nepodporovaný SQL příkaz: %s" 513 502 514 #: uenginexml.swrongfileformat 503 515 msgid "Wrong file format" … … 559 571 msgid "Database query error: \"%s\"" 560 572 msgstr "Chyba požadavku databáze: \"%s\"" 561 -
trunk/Languages/MyData.po
r22 r23 475 475 msgstr "" 476 476 477 #: uenginexml.scolumnnotfoundintable 478 msgid "Column %s not found in table %s" 479 msgstr "" 480 481 #: uenginexml.stablenotfound 482 msgid "Table %s not found" 483 msgstr "" 484 485 #: uenginexml.sunsupportedsqlcommand 486 msgid "Unsupported SQL command: %s" 487 msgstr "" 488 477 489 #: uenginexml.swrongfileformat 478 490 msgid "Wrong file format" -
trunk/MyData.lpr
r20 r23 11 11 UFormTable, UFormRecords, UFormRecord, UFormFields, UFormField, 12 12 TemplateGenerics, UFormMain, SysUtils, 13 UFormConnect, UFormDatabases, UFormPreferences , UDbClientRegistry;13 UFormConnect, UFormDatabases, UFormPreferences; 14 14 15 15 {$R *.res} -
trunk/UDatabase.pas
r22 r23 77 77 function SearchByName(Name: string): TField; 78 78 procedure Assign(Source: TFields); 79 function AddNew(Name: string; DataType: TDataType): TField; 79 80 end; 80 81 … … 96 97 procedure Assign(Source: TRecords); 97 98 function SearchByValue(Name, Value: string): TRecord; 99 function AddNew: TRecord; 98 100 end; 99 101 … … 120 122 DbClient: TDbClient; 121 123 function SearchByName(Name: string): TTable; 124 function AddNew(Name: string): TTable; 122 125 end; 123 126 … … 145 148 Name: string; 146 149 Params: TDbConnectParams; 150 DbManager: TDbManager; 147 151 destructor Destroy; override; 148 152 function GetClient: TDbClient; … … 189 193 procedure SetConnectProfile(AValue: TDbConnectProfile); virtual; 190 194 public 195 DbManager: TDbManager; 191 196 procedure Query(Text: string; DbRows: TDbRows = nil); virtual; 192 procedure LoadTables(Tables: TTables); virtual;193 197 constructor Create; virtual; 194 198 procedure Load; virtual; … … 350 354 351 355 ConnectProfile := TDbConnectProfile.Create; 356 ConnectProfile.DbManager := DbManager; 352 357 ConnectProfile.ClientType := ClientType; 353 358 ConnectProfile.Name := GetValue('Name', ''); … … 414 419 Result := ClientType.DatabaseClientClass.Create; 415 420 Result.ConnectProfile := Self; 421 Result.DbManager := DbManager; 416 422 end; 417 423 … … 428 434 end; 429 435 436 function TTables.AddNew(Name: string): TTable; 437 begin 438 Result := TTable.Create; 439 Result.DbClient := DbClient; 440 Result.Name := Name; 441 Add(Result); 442 end; 443 430 444 { TDbClient } 431 445 … … 443 457 procedure TDbClient.Query(Text: string; DbRows: TDbRows = nil); 444 458 begin 445 end;446 447 procedure TDbClient.LoadTables(Tables: TTables);448 begin449 Tables.Clear;450 459 end; 451 460 … … 598 607 end; 599 608 609 function TRecords.AddNew: TRecord; 610 begin 611 Result := TRecord.Create; 612 Result.Parent := Parent; 613 Result.InitValues; 614 Add(Result); 615 end; 616 600 617 { TFields } 601 618 … … 622 639 TField(Items[I]).Assign(TField(Source.Items[I])); 623 640 end; 641 end; 642 643 function TFields.AddNew(Name: string; DataType: TDataType): TField; 644 begin 645 Result := TField.Create; 646 Result.Table := Table; 647 Result.Name := Name; 648 Result.DataType := DataType; 649 Add(Result); 624 650 end; 625 651
Note:
See TracChangeset
for help on using the changeset viewer.