Ignore:
Timestamp:
Feb 16, 2015, 11:37:47 PM (10 years ago)
Author:
chronos
Message:
  • Added: Connection to MySQL host and loading list of available tables.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineMySQL.pas

    r7 r12  
    66
    77uses
    8   Classes, SysUtils, UDatabase;
     8  Classes, SysUtils, UDatabase, USqlDatabase, UHtmlClasses, SpecializedDictionary;
    99
    1010type
     11
     12  { TDatabaseMySQL }
     13
    1114  TDatabaseMySQL = class(TDatabaseClient)
    12 
     15  private
     16    procedure LoadTables;
     17  public
     18    SqlDatabase: TSqlDatabase;
     19    constructor Create; override;
     20    destructor Destroy; override;
     21    procedure Load; override;
     22    procedure Save; override;
    1323  end;
    1424
    1525implementation
    1626
     27{ TDatabaseMySQL }
     28
     29procedure TDatabaseMySQL.LoadTables;
     30var
     31  DbRows: TDbRows;
     32  NewTable: TTable;
     33  I: Integer;
     34begin
     35  DbRows := TDbRows.Create;
     36  try
     37    SqlDatabase.Query(DbRows, 'SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = "' + SqlDatabase.Database + '"');
     38    for I := 0 to DbRows.Count - 1 do begin
     39      NewTable := TTable.Create;
     40      NewTable.Database := Database;
     41      NewTable.Name := (TDictionaryStringString(DbRows[I])[0]).Value;
     42      NewTable.Caption := NewTable.Name;
     43      Database.Tables.Add(NewTable);
     44    end;
     45  finally
     46    DbRows.Free;
     47  end;
     48end;
     49
     50constructor TDatabaseMySQL.Create;
     51begin
     52  inherited Create;
     53  SqlDatabase := TSqlDatabase.Create(nil);
     54end;
     55
     56destructor TDatabaseMySQL.Destroy;
     57begin
     58  SqlDatabase.Free;
     59  inherited Destroy;
     60end;
     61
     62procedure TDatabaseMySQL.Load;
     63var
     64  URL: TURL;
     65begin
     66  URL := TURL.Create;
     67  try
     68    URL.AsString := Database.ConnectionString;
     69    if URL.Scheme <> 'mysql' then
     70      raise Exception.Create('Wrong connection string. Required mysql protocol.');
     71
     72    SqlDatabase.HostName := URL.Host.AsString;
     73    if URL.Port <> 0 then
     74      SqlDatabase.Port := URL.Port;
     75    SqlDatabase.UserName := URL.UserName;
     76    SqlDatabase.Password := URL.Password;
     77    if Copy(URL.Path, 1, 1) = '/' then
     78      SqlDatabase.Database := Copy(URL.Path, 2, High(Integer));
     79    SqlDatabase.Connect;
     80    LoadTables;
     81  finally
     82    URL.Free;
     83  end;
     84  inherited Load;
     85end;
     86
     87procedure TDatabaseMySQL.Save;
     88begin
     89  SqlDatabase.Disconnect;
     90  inherited Save;
     91end;
     92
    1793end.
    1894
Note: See TracChangeset for help on using the changeset viewer.