Ignore:
Timestamp:
Mar 24, 2018, 12:33:48 AM (6 years ago)
Author:
chronos
Message:
  • Modified: Tables creation/deletion using SQL syntax for XML backend.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineXML.pas

    r21 r22  
    3030    procedure LoadFromFile(FileName: string);
    3131    procedure SaveToFile(FileName: string);
     32    procedure Expect(var Source: string; Text: string);
     33    function CheckNext(var Source: string; Text: string): Boolean;
    3234    function GetNextPart(var Text: string; Separator: string = ' '): string;
    3335  protected
     
    304306end;
    305307
     308procedure TDatabaseXML.Expect(var Source: string; Text: string);
     309var
     310  Found: string;
     311begin
     312  Found := GetNextPart(Source);
     313  if Found <> Text then
     314    raise Exception.Create('Expected ' + Text + ' but ' + Found + ' found.');
     315end;
     316
     317function TDatabaseXML.CheckNext(var Source: string; Text: string): Boolean;
     318var
     319  Found: string;
     320  SourceCopy: string;
     321begin
     322  SourceCopy := Source;
     323  Found := GetNextPart(SourceCopy);
     324  if Found = Text then begin
     325    Source := SourceCopy;
     326    Result := True;
     327  end else Result := False;
     328end;
     329
    306330function TDatabaseXML.GetNextPart(var Text: string; Separator: string = ' '): string;
    307331begin
     
    324348  I: Integer;
    325349  F: Integer;
     350  Row: TRecord;
     351  WhereName: string;
     352  WhereValue: string;
     353  InsertValues: TStringList;
     354  Field: TField;
     355  FieldIndex: Integer;
     356  NewValue: TValue;
     357  ValueIndex: Integer;
    326358begin
    327359  Command := GetNextPart(Text);
    328360  if Command = 'SELECT' then begin
    329361    Columns := GetNextPart(Text);
    330     Command := GetNextPart(Text);
    331     if Command = 'FROM' then begin
    332       TableName := GetNextPart(Text);
    333     end else raise Exception.Create('No table specified with FROM');
     362    Expect(Text, 'FROM');
     363    TableName := GetNextPart(Text);
    334364    Table := Tables.SearchByName(TableName);
    335365    if Assigned(Table) then begin
     
    350380    end else raise Exception.Create('Table ' + TableName + ' not found.');
    351381  end else
    352   if Command = 'CREATE' then begin
    353     Command := GetNextPart(Text);
    354     if Command = 'TABLE' then begin
    355       TableName := GetNextPart(Text);
     382  if Command = 'INSERT' then begin
     383    InsertValues := TStringList.Create;
     384    Expect(Text, 'INTO');
     385    TableName := GetNextPart(Text);
     386    Expect(Text, '(');
     387    InsertValues.Add(GetNextPart(Text) + InsertValues.NameValueSeparator);
     388    while CheckNext(Text, ',') do begin
     389      InsertValues.Add(GetNextPart(Text) + InsertValues.NameValueSeparator);
     390    end;
     391    Expect(Text, ')');
     392    Expect(Text, 'VALUES');
     393    Expect(Text, '(');
     394    ValueIndex := 0;
     395    InsertValues.ValueFromIndex[ValueIndex] := GetNextPart(Text);
     396    Inc(ValueIndex);
     397    while CheckNext(Text, ',') do begin
     398      InsertValues.ValueFromIndex[ValueIndex] := GetNextPart(Text);
     399      Inc(ValueIndex);
     400    end;
     401    Expect(Text, ')');
     402    if TableName = 'Model' then begin
    356403      Table := TTable.Create;
    357       Table.Name := TableName;
     404      Table.Name := InsertValues.Values['Name'];
     405      Table.Caption := InsertValues.Values['Caption'];
    358406      Table.DbClient := Self;
    359       Tables.Add(Table);;
    360     end else raise Exception.Create('TABLE keyword expected');
     407      Tables.Add(Table);
     408    end else begin
     409      Table := Tables.SearchByName(TableName);
     410      if Assigned(Table) then begin
     411        Row := TRecord.Create;
     412        Row.Parent := Table;
     413        Row.InitValues;
     414        for ValueIndex := 0 to InsertValues.Count - 1 do begin
     415          Field := Table.Fields.SearchByName(InsertValues.Names[ValueIndex]);
     416          FieldIndex := Table.Fields.IndexOf(Field);
     417          TValue(Row.Values[FieldIndex]).SetString(InsertValues.ValueFromIndex[ValueIndex]);
     418        end;
     419        Table.Records.Add(Row);
     420      end else raise Exception.Create(Format('Table %s not found', [TableName]));
     421    end;
     422    InsertValues.Free;
    361423  end else
    362   if Command = 'DROP' then begin
    363     Command := GetNextPart(Text);
    364     if Command = 'TABLE' then begin
    365       TableName := GetNextPart(Text);
     424  if Command = 'DELETE' then begin
     425    Expect(Text, 'FROM');
     426    TableName := GetNextPart(Text);
     427    if CheckNext(Text, 'WHERE') then begin
     428      WhereName := GetNextPart(Text);
     429      Command := GetNextPart(Text);
     430      if Command = '=' then begin
     431        WhereValue := GetNextPart(Text);
     432      end else raise Exception.Create('Expression error');
     433    end;
     434    if TableName = 'Model' then begin
     435      Table := Tables.SearchByName(WhereValue);
     436      if Assigned(Table) then begin
     437        Tables.Remove(Table);
     438      end else raise Exception.Create(Format('Table %s not found', [whereValue]));
     439    end else begin
    366440      Table := Tables.SearchByName(TableName);
    367       if Assigned(Table) then Tables.Remove(Table)
    368       else raise Exception.Create('Table ' + TableName + ' not found');
    369     end else raise Exception.Create('TABLE keyword expected');
     441      if Assigned(Table) then begin
     442        Row := Table.Records.SearchByValue(WhereName, WhereValue);
     443        if Assigned(Row) then begin
     444          Table.Records.Remove(Row);
     445        end else raise Exception.Create('Row not found');
     446      end else raise Exception.Create(Format('Table %s not found', [TableName]));
     447    end;
    370448  end else
    371449    raise Exception.Create('Unsupported SQL command ' + Command);
     
    380458begin
    381459  FileName := TDbConnectParamsXml(ConnectProfile.Params).FileName;
     460  Tables.DbClient := Self;
    382461  if FileExists(FileName) then
    383462    LoadFromFile(FileName);
Note: See TracChangeset for help on using the changeset viewer.