Changeset 13 for trunk/DbEngines


Ignore:
Timestamp:
Mar 17, 2015, 12:09:11 AM (10 years ago)
Author:
chronos
Message:
  • Modified: Now records are loaded from SQL database using SQL query. The query is parsed also by XML database client to load records from XML file.
  • Added: Packages Network and Synapse.
Location:
trunk/DbEngines
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DbEngines/UEngineMySQL.pas

    r12 r13  
    1414  TDatabaseMySQL = class(TDatabaseClient)
    1515  private
     16    procedure LoadFields(Table: TTable);
    1617    procedure LoadTables;
    1718  public
    1819    SqlDatabase: TSqlDatabase;
     20    procedure Query(DbRows: TDbRows; Text: string); override;
    1921    constructor Create; override;
    2022    destructor Destroy; override;
     
    2729{ TDatabaseMySQL }
    2830
     31procedure TDatabaseMySQL.LoadFields(Table: TTable);
     32var
     33  DbRows: TDbRows;
     34  NewField: TField;
     35  I: Integer;
     36  DataType: Integer;
     37begin
     38  DbRows := TDbRows.Create;
     39  try
     40    SqlDatabase.Query(DbRows, 'SELECT * FROM `ModelField` WHERE `Model` = ' + IntToStr(Table.Id) + '');
     41    for I := 0 to DbRows.Count - 1 do begin
     42      NewField := TField.Create;
     43      NewField.Table := Table;
     44      NewField.Name := TDictionaryStringString(DbRows[I]).Values['Name'];
     45      NewField.TextBefore := TDictionaryStringString(DbRows[I]).Values['Title'];
     46      DataType := StrToInt(TDictionaryStringString(DbRows[I]).Values['DataType']);
     47      NewField.DataType := Table.Database.Engine.DataTypes.FindByType(TFieldType(DataType));
     48      if not Assigned(NewField.DataType) then
     49        NewField.DataType := Table.Database.Engine.DataTypes.FindByType(ftString);
     50      Table.Fields.Add(NewField);
     51    end;
     52  finally
     53    DbRows.Free;
     54  end;
     55end;
     56
    2957procedure TDatabaseMySQL.LoadTables;
    3058var
    3159  DbRows: TDbRows;
     60  DbRows2: TDbRows;
    3261  NewTable: TTable;
    3362  I: Integer;
     
    3564  DbRows := TDbRows.Create;
    3665  try
    37     SqlDatabase.Query(DbRows, 'SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = "' + SqlDatabase.Database + '"');
     66    SqlDatabase.Query(DbRows, 'SELECT `Id`,`Name`,`Title` FROM `Model`');
    3867    for I := 0 to DbRows.Count - 1 do begin
    3968      NewTable := TTable.Create;
     69      NewTable.Id := StrToInt(TDictionaryStringString(DbRows[I]).Values['Id']);
    4070      NewTable.Database := Database;
    41       NewTable.Name := (TDictionaryStringString(DbRows[I])[0]).Value;
    42       NewTable.Caption := NewTable.Name;
     71      NewTable.Name := TDictionaryStringString(DbRows[I]).Values['Name'];
     72      NewTable.Caption := TDictionaryStringString(DbRows[I]).Values['Title'];
     73      LoadFields(NewTable);
     74      DbRows2 := TDbRows.Create;
     75      try
     76        SqlDatabase.Query(DbRows2, 'SELECT COUNT(*) FROM `' + NewTable.Name + '`');
     77        if DbRows2.Count = 1 then
     78          NewTable.RecordsCount := StrToInt(DbRows2[0].Values['COUNT(*)']);
     79      finally
     80        DbRows2.Free;
     81      end;
    4382      Database.Tables.Add(NewTable);
    4483    end;
     
    4685    DbRows.Free;
    4786  end;
     87end;
     88
     89procedure TDatabaseMySQL.Query(DbRows: TDbRows; Text: string);
     90begin
     91  SqlDatabase.Query(DbRows, Text);
    4892end;
    4993
  • trunk/DbEngines/UEngineXML.pas

    r12 r13  
    77uses
    88  Classes, SysUtils, DOM, XMLRead, XMLWrite, UDatabase, UXMLUtils, FileUtil,
    9   UHtmlClasses;
     9  UHtmlClasses, USqlDatabase, SpecializedDictionary;
    1010
    1111type
     
    2929    procedure LoadFromFile(FileName: string);
    3030    procedure SaveToFile(FileName: string);
     31    function GetNextPart(var Text: string): string;
    3132  public
     33    procedure Query(DbRows: TDbRows; Text: string); override;
    3234    procedure Load; override;
    3335    procedure Save; override;
     
    280282end;
    281283
     284function TDatabaseXML.GetNextPart(var Text: string): string;
     285begin
     286  if Pos(' ', Text) > 0 then begin
     287    Result := Trim(Copy(Text, 1, Pos(' ', Text) - 1));
     288    Delete(Text, 1, Pos(' ', Text));
     289  end else begin
     290    Result := Text;
     291    Text := '';
     292  end;
     293end;
     294
     295procedure TDatabaseXML.Query(DbRows: TDbRows; Text: string);
     296var
     297  Command: string;
     298  Columns: string;
     299  TableName: string;
     300  Table: TTable;
     301  NewRecord: TDictionaryStringString;
     302  I: Integer;
     303  F: Integer;
     304begin
     305  Command := GetNextPart(Text);
     306  if Command = 'SELECT' then begin
     307    Columns := GetNextPart(Text);
     308    Command := GetNextPart(Text);
     309    if Command = 'FROM' then begin
     310      TableName := GetNextPart(Text);
     311    end;
     312    Table := Database.Tables.SearchByName(TableName);
     313    if Assigned(Table) then begin
     314      DbRows.Count := 0;
     315      if Columns = '*' then begin
     316        for I := 0 to Table.Records.Count - 1 do begin
     317          NewRecord := TDictionaryStringString.Create;
     318          for F := 0 to Table.Fields.Count - 1 do
     319            NewRecord.Add(TField(Table.Fields[F]).Name, TValue(TRecord(Table.Records[I]).Values[I]).GetString);
     320          DbRows.Add(NewRecord);
     321        end;
     322      end else
     323      if Columns = 'COUNT(*)' then begin
     324        NewRecord := TDictionaryStringString.Create;
     325        NewRecord.Add('COUNT(*)', IntToStr(Table.Records.Count));
     326        DbRows.Add(NewRecord);
     327      end else raise Exception.Create('Unsupported columns ' + Columns + ' specification');
     328    end else raise Exception.Create('Table ' + TableName + ' not found.');
     329  end else raise Exception.Create('Unsupported SQL command ' + Command);
     330end;
     331
    282332procedure TDatabaseXML.Load;
    283333begin
Note: See TracChangeset for help on using the changeset viewer.