Ignore:
Timestamp:
Mar 26, 2018, 12:40:10 AM (6 years ago)
Author:
chronos
Message:
  • Modified: More changes to implement SQL over XML file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineXML.pas

    r22 r23  
    3333    function CheckNext(var Source: string; Text: string): Boolean;
    3434    function GetNextPart(var Text: string; Separator: string = ' '): string;
     35    function TableCreateIfNotExists(Name: string): TTable;
     36    function FieldCreateIfNotExists(TableName, FieldName: string; DataType: TDataType): TField;
    3537  protected
    3638  public
    3739    procedure Query(Text: string; DbRows: TDbRows = nil); override;
    38     procedure LoadTables(Tables: TTables); override;
    3940    procedure Load; override;
    4041    procedure Save; override;
     
    6263resourcestring
    6364  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';
    6468
    6569{ TDbConnectParamsXml }
     
    336340    Result := Text;
    337341    Text := '';
     342  end;
     343end;
     344
     345function TDatabaseXML.TableCreateIfNotExists(Name: string): TTable;
     346begin
     347  Result := Tables.SearchByName(Name);
     348  if not Assigned(Result) then begin
     349    Tables.AddNew(Name);
     350  end;
     351end;
     352
     353function TDatabaseXML.FieldCreateIfNotExists(TableName, FieldName: string; DataType: TDataType): TField;
     354var
     355  Table: TTable;
     356begin
     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);
    338362  end;
    339363end;
     
    359383  Command := GetNextPart(Text);
    360384  if Command = 'SELECT' then begin
     385    DbRows.Count := 0;
    361386    Columns := GetNextPart(Text);
    362387    Expect(Text, 'FROM');
    363388    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
    367397      if Columns = '*' then begin
    368         for I := 0 to Table.Records.Count - 1 do begin
     398        for I := 0 to Tables.Count - 1 do begin
    369399          NewRecord := TDictionaryStringString.Create;
    370           for F := 0 to Table.Fields.Count - 1 do
    371             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);
    372402          DbRows.Add(NewRecord);
    373403        end;
     
    375405      if Columns = 'COUNT(*)' then begin
    376406        NewRecord := TDictionaryStringString.Create;
    377         NewRecord.Add('COUNT(*)', IntToStr(Table.Records.Count));
     407        NewRecord.Add('COUNT(*)', IntToStr(Tables.Count));
    378408        DbRows.Add(NewRecord);
    379409      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;
    381450  end else
    382451  if Command = 'INSERT' then begin
     
    401470    Expect(Text, ')');
    402471    if TableName = 'Model' then begin
    403       Table := TTable.Create;
    404       Table.Name := InsertValues.Values['Name'];
     472      Table := Tables.AddNew(InsertValues.Values['Name']);
    405473      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']]));
    408482    end else begin
    409483      Table := Tables.SearchByName(TableName);
    410484      if Assigned(Table) then begin
    411         Row := TRecord.Create;
    412         Row.Parent := Table;
    413         Row.InitValues;
     485        Row := Table.Records.AddNew;
    414486        for ValueIndex := 0 to InsertValues.Count - 1 do begin
    415487          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]));
    418493        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]));
    421495    end;
    422496    InsertValues.Free;
     
    436510      if Assigned(Table) then begin
    437511        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);
    439517    end else begin
    440518      Table := Tables.SearchByName(TableName);
     
    444522          Table.Records.Remove(Row);
    445523        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]));
    447525    end;
    448526  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]));
    455528end;
    456529
     
    461534  if FileExists(FileName) then
    462535    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  *)
    463556end;
    464557
Note: See TracChangeset for help on using the changeset viewer.