Changeset 21


Ignore:
Timestamp:
Mar 23, 2018, 3:06:47 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Allow to create and remove tables for XML backend.
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineMySQL.pas

    r20 r21  
    1919  public
    2020    SqlDatabase: TSqlDatabase;
    21     procedure Query(DbRows: TDbRows; Text: string); override;
     21    procedure Query(Text: string; DbRows: TDbRows = nil); override;
    2222    constructor Create; override;
    2323    destructor Destroy; override;
     
    8787      if DbRows2.Count > 0 then begin
    8888        TypeName := TDictionaryStringString(DbRows2[0]).Values['Name'];
    89         NewField.DataType := Table.DbClient.ClientType.DataTypes.FindByName(TypeName);
     89        NewField.DataType := Table.DbClient.ClientType.DataTypes.SearchByName(TypeName);
    9090        if not Assigned(NewField.DataType) then
    91           NewField.DataType := Table.DbClient.ClientType.DataTypes.FindByType(ftString);
     91          NewField.DataType := Table.DbClient.ClientType.DataTypes.SearchByType(ftString);
    9292      end else begin
    9393        // Use string as default
    94         NewField.DataType := Table.DbClient.ClientType.DataTypes.FindByType(ftString);
     94        NewField.DataType := Table.DbClient.ClientType.DataTypes.SearchByType(ftString);
    9595      end;
    9696      Table.Fields.Add(NewField);
     
    125125end;
    126126
    127 procedure TDatabaseMySQL.Query(DbRows: TDbRows; Text: string);
     127procedure TDatabaseMySQL.Query(Text: string; DbRows: TDbRows = nil);
    128128begin
    129129  SqlDatabase.Query(DbRows, Text);
  • trunk/DbEngines/UEngineXML.pas

    r20 r21  
    3030    procedure LoadFromFile(FileName: string);
    3131    procedure SaveToFile(FileName: string);
    32     function GetNextPart(var Text: string): string;
     32    function GetNextPart(var Text: string; Separator: string = ' '): string;
    3333  protected
    3434  public
    35     procedure Query(DbRows: TDbRows; Text: string); override;
     35    procedure Query(Text: string; DbRows: TDbRows = nil); override;
    3636    procedure LoadTables(Tables: TTables); override;
    3737    procedure Load; override;
     
    158158begin
    159159  Field.Name := ReadString(Node, 'Name', '');
    160   Field.DataType := Field.Table.DbClient.ClientType.DataTypes.FindByType(TFieldType(ReadInteger(Node, 'Type', 0)));
     160  Field.DataType := Field.Table.DbClient.ClientType.DataTypes.SearchByType(TFieldType(ReadInteger(Node, 'Type', 0)));
    161161  Field.TextBefore := ReadString(Node, 'TextBefore', '');
    162162  Field.TextAfter := ReadString(Node, 'TextAfter', '');
     
    296296      SaveNodeTables(Tables, NewNode);
    297297    end;
    298     ForceDirectories(ExtractFileDir(FileName));
     298    if Pos(DirectorySeparator, FileName) > 0 then
     299      ForceDirectories(ExtractFileDir(FileName));
    299300    WriteXMLFile(Doc, FileName);
    300301  finally
     
    303304end;
    304305
    305 function TDatabaseXML.GetNextPart(var Text: string): string;
    306 begin
    307   if Pos(' ', Text) > 0 then begin
    308     Result := Trim(Copy(Text, 1, Pos(' ', Text) - 1));
    309     Delete(Text, 1, Pos(' ', Text));
     306function TDatabaseXML.GetNextPart(var Text: string; Separator: string = ' '): string;
     307begin
     308  if Pos(Separator, Text) > 0 then begin
     309    Result := Trim(Copy(Text, 1, Pos(Separator, Text) - Length(Separator)));
     310    Delete(Text, 1, Pos(Separator, Text));
    310311  end else begin
    311312    Result := Text;
     
    314315end;
    315316
    316 procedure TDatabaseXML.Query(DbRows: TDbRows; Text: string);
     317procedure TDatabaseXML.Query(Text: string; DbRows: TDbRows = nil);
    317318var
    318319  Command: string;
     
    348349      end else raise Exception.Create('Unsupported columns ' + Columns + ' specification');
    349350    end else raise Exception.Create('Table ' + TableName + ' not found.');
    350   end else raise Exception.Create('Unsupported SQL command ' + Command);
     351  end else
     352  if Command = 'CREATE' then begin
     353    Command := GetNextPart(Text);
     354    if Command = 'TABLE' then begin
     355      TableName := GetNextPart(Text);
     356      Table := TTable.Create;
     357      Table.Name := TableName;
     358      Table.DbClient := Self;
     359      Tables.Add(Table);;
     360    end else raise Exception.Create('TABLE keyword expected');
     361  end else
     362  if Command = 'DROP' then begin
     363    Command := GetNextPart(Text);
     364    if Command = 'TABLE' then begin
     365      TableName := GetNextPart(Text);
     366      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');
     370  end else
     371    raise Exception.Create('Unsupported SQL command ' + Command);
    351372end;
    352373
    353374procedure TDatabaseXML.LoadTables(Tables: TTables);
    354375begin
    355   inherited;
    356 
     376  Tables.Assign(Self.Tables);
    357377end;
    358378
    359379procedure TDatabaseXML.Load;
    360380begin
     381  FileName := TDbConnectParamsXml(ConnectProfile.Params).FileName;
    361382  if FileExists(FileName) then
    362383    LoadFromFile(FileName);
  • trunk/Forms/UFormTables.lfm

    r20 r21  
    1010  OnActivate = FormActivate
    1111  OnClose = FormClose
     12  OnCreate = FormCreate
     13  OnDestroy = FormDestroy
    1214  OnShow = FormShow
    1315  LCLVersion = '1.8.0.6'
  • trunk/Forms/UFormTables.pas

    r20 r21  
    4040    procedure FormActivate(Sender: TObject);
    4141    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
     42    procedure FormCreate(Sender: TObject);
     43    procedure FormDestroy(Sender: TObject);
    4244    procedure FormShow(Sender: TObject);
    4345    procedure ListView1Data(Sender: TObject; Item: TListItem);
     
    4547      Selected: Boolean);
    4648  private
     49    FDbClient: TDbClient;
    4750    FTables: TTables;
    48     procedure SetTables(AValue: TTables);
     51    procedure SetDbClient(AValue: TDbClient);
    4952  public
    50     property Tables: TTables read FTables write SetTables;
     53    property DbClient: TDbClient read FDbClient write SetDbClient;
     54    property Tables: TTables read FTables;
    5155    procedure UpdateInterface;
    5256    procedure ReloadList;
     
    8791end;
    8892
    89 procedure TFormTables.SetTables(AValue: TTables);
    90 begin
    91   if FTables = AValue then Exit;
    92   FTables := AValue;
     93procedure TFormTables.SetDbClient(AValue: TDbClient);
     94begin
     95  if FDbClient = AValue then Exit;
     96  FDbClient := AValue;
     97  FTables.DbClient := AValue;
    9398  ReloadList;
    9499end;
     
    114119  if FormTable.ShowModal = mrOk then begin
    115120    FormTable.Save(NewTable);
    116     Tables.Add(NewTable);
     121    Tables.DbClient.Query('CREATE TABLE ' + NewTable.Name + ' (ID INTEGER)');
    117122    ReloadList;
    118123  end else NewTable.Free;
     
    135140    if MessageDlg(SRemoveTable, Format(SRemoveTableConfirm, [TTable(ListView1.Selected.Data).Name]),
    136141    mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
    137       Tables.Remove(ListView1.Selected.Data);
     142      Tables.DbClient.Query('DROP TABLE ' + TTable(ListView1.Selected.Data).Name);
    138143      ReloadList;
    139144    end;
     
    201206procedure TFormTables.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    202207begin
    203   //Tables.DbClient.Save;
     208end;
     209
     210procedure TFormTables.FormCreate(Sender: TObject);
     211begin
     212  FTables := TTables.Create;
     213end;
     214
     215procedure TFormTables.FormDestroy(Sender: TObject);
     216begin
     217  FTables.Free;
    204218end;
    205219
     
    208222  I: Integer;
    209223begin
     224  ReloadList;
    210225  for I := 0 to ToolBar1.ButtonCount - 1 do
    211226    ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
     
    217232  I: Integer;
    218233begin
     234  if Assigned(DbClient) then DbClient.LoadTables(Tables)
     235    else Tables.Clear;
    219236  for I := 0 to Tables.Count - 1 do
    220237    TTable(Tables[I]).LoadRecordsCount;
  • trunk/UCore.pas

    r20 r21  
    8787
    8888procedure TCore.ADatabaseConnectExecute(Sender: TObject);
    89 var
    90   NewClient: TDbClient;
    9189begin
    9290  if FormDatabases.ShowModal = mrOk then begin
    9391    DbClient := nil;
    94     NewClient := FormDatabases.SelectedProfile.ClientType.DatabaseClientClass.Create;
    95     NewClient.ConnectProfile := FormDatabases.SelectedProfile;
    96     DbClient := NewClient;
     92    DbClient := FormDatabases.SelectedProfile.GetClient;
    9793  end;
    9894end;
     
    134130    DbClient.Load;
    135131    Preferences.LastDatabaseName := FDbClient.ConnectProfile.Name;
    136     if not Assigned(FormTables.Tables) then
    137       FormTables.Tables := TTables.Create;
    138     DbClient.LoadTables(FormTables.Tables);
     132    FormTables.DbClient := DbClient;
    139133  end;
    140134  UpdateInterface;
     
    186180    UpdateInterface;
    187181    if Preferences.RememberDatabase then begin
    188       ConnectProfile := DbManager.ConnectProfiles.FindByName(Preferences.LastDatabaseName);
    189       if Assigned(ConnectProfile) then
    190         DbClient := ConnectProfile.ClientType.DatabaseClientClass.Create;
     182      ConnectProfile := DbManager.ConnectProfiles.SearchByName(Preferences.LastDatabaseName);
     183      if Assigned(ConnectProfile) then begin
     184        DbClient := ConnectProfile.GetClient;
     185      end;
    191186    end else ADatabaseConnect.Execute;
    192187  end;
     
    196191begin
    197192  SaveConfig;
     193  DbClient := nil;
    198194end;
    199195
  • trunk/UDatabase.pas

    r20 r21  
    143143    Name: string;
    144144    Params: TDbConnectParams;
     145    destructor Destroy; override;
     146    function GetClient: TDbClient;
    145147    property ClientType: TDbClientType read FClientType write SetClientType;
    146148  end;
     
    152154    procedure LoadFromRegistry(Context: TRegistryContext);
    153155    procedure SaveToRegistry(Context: TRegistryContext);
    154     function FindByName(Name: string): TDbConnectProfile;
     156    function SearchByName(Name: string): TDbConnectProfile;
    155157  end;
    156158
     
    170172    function RegisterType(Id: Integer; Name, Title: string;
    171173      FieldType: TFieldType; FieldTypeClass: TFieldTypeSpecificClass): TDataType;
    172     function FindByType(FieldType: TFieldType): TDataType;
    173     function FindByName(Name: string): TDataType;
     174    function SearchByType(FieldType: TFieldType): TDataType;
     175    function SearchByName(Name: string): TDataType;
    174176  end;
    175177
     
    183185    procedure SetConnectProfile(AValue: TDbConnectProfile); virtual;
    184186  public
    185     procedure Query(DbRows: TDbRows; Text: string); virtual;
     187    procedure Query(Text: string; DbRows: TDbRows = nil); virtual;
    186188    procedure LoadTables(Tables: TTables); virtual;
    187189    constructor Create; virtual;
     
    373375end;
    374376
    375 function TDbConnectProfiles.FindByName(Name: string): TDbConnectProfile;
     377function TDbConnectProfiles.SearchByName(Name: string): TDbConnectProfile;
    376378var
    377379  I: Integer;
     
    389391  if FClientType = AValue then Exit;
    390392  if Assigned(FClientType) then begin
    391     Params.Free;
     393    FreeAndNil(Params);
    392394  end;
    393395  FClientType := AValue;
     
    395397    Params := FClientType.ConnectParmasClass.Create;
    396398  end;
     399end;
     400
     401destructor TDbConnectProfile.Destroy;
     402begin
     403  ClientType := nil;
     404  if Assigned(Params) then Params.Free;
     405  inherited Destroy;
     406end;
     407
     408function TDbConnectProfile.GetClient: TDbClient;
     409begin
     410  Result := ClientType.DatabaseClientClass.Create;
     411  Result.ConnectProfile := Self;
    397412end;
    398413
     
    422437end;
    423438
    424 procedure TDbClient.Query(DbRows: TDbRows; Text: string);
     439procedure TDbClient.Query(Text: string; DbRows: TDbRows = nil);
    425440begin
    426441end;
     
    482497end;
    483498
    484 function TDataTypes.FindByType(FieldType: TFieldType): TDataType;
     499function TDataTypes.SearchByType(FieldType: TFieldType): TDataType;
    485500var
    486501  I: Integer;
     
    492507end;
    493508
    494 function TDataTypes.FindByName(Name: string): TDataType;
     509function TDataTypes.SearchByName(Name: string): TDataType;
    495510var
    496511  I: Integer;
     
    510525  DataTypes.Clear;
    511526  for I := Low(TFieldType) to High(TFieldType) do
    512     if I in Types then DataTypes.Add(ADataTypes.FindByType(I));
     527    if I in Types then DataTypes.Add(ADataTypes.SearchByType(I));
    513528end;
    514529
     
    698713  Records.Clear;
    699714  DbRows := TDbRows.Create;
    700   DbClient.Query(DbRows, 'SELECT * FROM ' + Name);
     715  DbClient.Query('SELECT * FROM ' + Name, DbRows);
    701716  for I := 0 to DbRows.Count - 1 do begin
    702717    NewRecord := TRecord.Create;
     
    723738  Records.Clear;
    724739  DbRows := TDbRows.Create;
    725   DbClient.Query(DbRows, 'SELECT COUNT(*) FROM ' + Name);
     740  DbClient.Query('SELECT COUNT(*) FROM ' + Name, DbRows);
    726741  if DbRows.Count = 1 then begin
    727742    RecordsCount := StrToInt(TDictionaryStringString(DbRows[0]).Items[0].Value);
Note: See TracChangeset for help on using the changeset viewer.