Changeset 20 for trunk/DbEngines


Ignore:
Timestamp:
Mar 23, 2018, 1:59:25 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Database classes reorganized. Now TDbConnectProfile is class which holds information about connection to database.
  • Modified: TDbManager is top most class for managing other database classes.
  • Modified: TDbConnectParams class contains client specific parameters for connect profile.
Location:
trunk/DbEngines
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineMySQL.pas

    r15 r20  
    1212  { TDatabaseMySQL }
    1313
    14   TDatabaseMySQL = class(TDatabaseClient)
     14  TDatabaseMySQL = class(TDbClient)
    1515  private
    1616    procedure LoadFields(Table: TTable);
    17     procedure LoadTables;
     17  protected
     18    procedure LoadTables(Tables: TTables); override;
    1819  public
    1920    SqlDatabase: TSqlDatabase;
     
    2526  end;
    2627
     28  { TDbConnectParamsMySQL }
     29
     30  TDbConnectParamsMySQL = class(TDbConnectParams)
     31  protected
     32    function GetConnectionString: string; override;
     33    procedure SetConnectionString(AValue: string); override;
     34  public
     35    Host: string;
     36    Port: Word;
     37  end;
     38
     39
    2740implementation
     41
     42{ TDbConnectParamsMySQL }
     43
     44function TDbConnectParamsMySQL.GetConnectionString: string;
     45begin
     46  Result := 'mysql://' + Host + ':' + IntToStr(Port);
     47end;
     48
     49procedure TDbConnectParamsMySQL.SetConnectionString(AValue: string);
     50var
     51  URL: TURL;
     52begin
     53  URL := TURL.Create;
     54  try
     55    URL.AsString := AValue;
     56    if (URL.Scheme <> 'mysql') and (AValue <> '') then
     57      raise Exception.Create('Wrong connection string. Required mysql scheme.');
     58    Host := URL.Host.AsString;
     59    Port := URL.Port;
     60  finally
     61    URL.Free;
     62  end;
     63end;
    2864
    2965{ TDatabaseMySQL }
     
    5187      if DbRows2.Count > 0 then begin
    5288        TypeName := TDictionaryStringString(DbRows2[0]).Values['Name'];
    53         NewField.DataType := Table.Database.Engine.DataTypes.FindByName(TypeName);
     89        NewField.DataType := Table.DbClient.ClientType.DataTypes.FindByName(TypeName);
    5490        if not Assigned(NewField.DataType) then
    55           NewField.DataType := Table.Database.Engine.DataTypes.FindByType(ftString);
     91          NewField.DataType := Table.DbClient.ClientType.DataTypes.FindByType(ftString);
    5692      end else begin
    5793        // Use string as default
    58         NewField.DataType := Table.Database.Engine.DataTypes.FindByType(ftString);
     94        NewField.DataType := Table.DbClient.ClientType.DataTypes.FindByType(ftString);
    5995      end;
    6096      Table.Fields.Add(NewField);
     
    66102end;
    67103
    68 procedure TDatabaseMySQL.LoadTables;
     104procedure TDatabaseMySQL.LoadTables(Tables: TTables);
    69105var
    70106  DbRows: TDbRows;
     
    78114      NewTable := TTable.Create;
    79115      NewTable.Id := StrToInt(TDictionaryStringString(DbRows[I]).Values['Id']);
    80       NewTable.Database := Database;
     116      NewTable.DbClient := Self;
    81117      NewTable.Name := TDictionaryStringString(DbRows[I]).Values['Name'];
    82118      NewTable.Caption := TDictionaryStringString(DbRows[I]).Values['Title'];
    83119      LoadFields(NewTable);
    84       Database.Tables.Add(NewTable);
     120      Tables.Add(NewTable);
    85121    end;
    86122  finally
     
    112148  URL := TURL.Create;
    113149  try
    114     URL.AsString := Database.ConnectionString;
     150    URL.AsString := ConnectProfile.Params.ConnectionString;
    115151    if URL.Scheme <> 'mysql' then
    116152      raise Exception.Create('Wrong connection string. Required mysql protocol.');
     
    124160      SqlDatabase.Database := Copy(URL.Path, 2, High(Integer));
    125161    SqlDatabase.Connect;
    126     LoadTables;
    127162  finally
    128163    URL.Free;
  • trunk/DbEngines/UEngineSQLite.pas

    r7 r20  
    99
    1010type
    11   TDatabaseSQLite = class(TDatabaseClient)
     11  TDatabaseSQLite = class(TDbClient)
    1212
    1313  end;
     14
     15  TDbConnectParamsSQLite = class(TDbConnectParams)
     16
     17  end;
     18
    1419
    1520implementation
  • trunk/DbEngines/UEngineXML.pas

    r17 r20  
    1212  { TDatabaseXML }
    1313
    14   TDatabaseXML = class(TDatabaseClient)
     14  TDatabaseXML = class(TDbClient)
    1515  private
    16     function GetFileName: string;
     16    FFileName: string;
     17    Tables: TTables;
    1718    procedure LoadNodeRecord(Row: TRecord; Node: TDOMNode);
    1819    procedure SaveNodeRecord(Row: TRecord; Node: TDOMNode);
     
    3031    procedure SaveToFile(FileName: string);
    3132    function GetNextPart(var Text: string): string;
     33  protected
    3234  public
    3335    procedure Query(DbRows: TDbRows; Text: string); override;
     36    procedure LoadTables(Tables: TTables); override;
    3437    procedure Load; override;
    3538    procedure Save; override;
    36     property FileName: string read GetFileName;
     39    constructor Create; override;
     40    destructor Destroy; override;
     41    property FileName: string read FFileName write FFileName;
     42  end;
     43
     44  { TDbConnectParamsXml }
     45
     46  TDbConnectParamsXml = class(TDbConnectParams)
     47  protected
     48    function GetConnectionString: string; override;
     49    procedure SetConnectionString(AValue: string); override;
     50  public
     51    FileName: string;
    3752  end;
    3853
     
    4661  SWrongFileFormat = 'Wrong file format';
    4762
    48 { TDatabaseXML }
    49 
    50 function TDatabaseXML.GetFileName: string;
     63{ TDbConnectParamsXml }
     64
     65function TDbConnectParamsXml.GetConnectionString: string;
     66begin
     67  Result := 'file:///' + FileName;
     68end;
     69
     70procedure TDbConnectParamsXml.SetConnectionString(AValue: string);
    5171var
    5272  URL: TURL;
     
    5474  URL := TURL.Create;
    5575  try
    56     URL.AsString := Database.ConnectionString;
    57     if URL.Scheme <> 'file' then
     76    URL.AsString := AValue;
     77    if (URL.Scheme <> 'file') and (AValue <> '') then
    5878      raise Exception.Create('Wrong connection string. Required file scheme.');
    59 
    60     Result := URL.Path
     79    FileName := Copy(URL.Path, 2, Length(URL.Path));
    6180  finally
    6281    URL.Free;
    6382  end;
    6483end;
     84
     85{ TDatabaseXML }
    6586
    6687procedure TDatabaseXML.LoadNodeRecord(Row: TRecord; Node: TDOMNode);
     
    137158begin
    138159  Field.Name := ReadString(Node, 'Name', '');
    139   Field.DataType := Field.Table.Database.Engine.DataTypes.FindByType(TFieldType(ReadInteger(Node, 'Type', 0)));
     160  Field.DataType := Field.Table.DbClient.ClientType.DataTypes.FindByType(TFieldType(ReadInteger(Node, 'Type', 0)));
    140161  Field.TextBefore := ReadString(Node, 'TextBefore', '');
    141162  Field.TextAfter := ReadString(Node, 'TextAfter', '');
     
    219240  while Assigned(Node2) and (Node2.NodeName = 'Table') do begin
    220241    NewTable := TTable.Create;
    221     NewTable.Database := Tables.Database;
     242    NewTable.DbClient := Tables.DbClient;
    222243    LoadNodeTable(NewTable, Node2);
    223244    Tables.Add(NewTable);
     
    252273      NewNode := FindNode('Tables');
    253274      if Assigned(NewNode) then
    254         LoadNodeTables(Database.Tables, NewNode);
     275        LoadNodeTables(Tables, NewNode);
    255276    end;
    256277  finally
     
    273294      NewNode := OwnerDocument.CreateElement('Tables');
    274295      AppendChild(NewNode);
    275       SaveNodeTables(Database.Tables, NewNode);
     296      SaveNodeTables(Tables, NewNode);
    276297    end;
    277298    ForceDirectories(ExtractFileDir(FileName));
     
    310331      TableName := GetNextPart(Text);
    311332    end else raise Exception.Create('No table specified with FROM');
    312     Table := Database.Tables.SearchByName(TableName);
     333    Table := Tables.SearchByName(TableName);
    313334    if Assigned(Table) then begin
    314335      DbRows.Count := 0;
     
    330351end;
    331352
     353procedure TDatabaseXML.LoadTables(Tables: TTables);
     354begin
     355  inherited;
     356
     357end;
     358
    332359procedure TDatabaseXML.Load;
    333360begin
     
    341368end;
    342369
     370constructor TDatabaseXML.Create;
     371begin
     372  Tables := TTables.Create;
     373  inherited Create;
     374end;
     375
     376destructor TDatabaseXML.Destroy;
     377begin
     378  Tables.Free;
     379  inherited Destroy;
     380end;
     381
    343382
    344383end.
Note: See TracChangeset for help on using the changeset viewer.