Changeset 23 for trunk/DbEngines/UEngineXML.pas
- Timestamp:
- Mar 26, 2018, 12:40:10 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.