добавил часть модулей, нужно переделать БД на records
This commit is contained in:
+239
-135
@@ -7,13 +7,12 @@ uses
|
||||
FMX.Grid, FireDAC.Stan.Def, FireDAC.Stan.Intf, FireDAC.Stan.Option,
|
||||
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Pool,
|
||||
FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef,
|
||||
FireDAC.Stan.ExprFuncs, FireDAC.Phys.SQLiteWrapper.Stat, FireDAC.VCLUI.Wait,
|
||||
FireDAC.Stan.ExprFuncs, FireDAC.Phys.SQLiteWrapper.Stat, FireDAC.FMXUI.Wait,
|
||||
FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
|
||||
FireDAC.FMXUI.Wait, FireDAC.Comp.UI, uRecords, System.Generics.Collections;
|
||||
FireDAC.Comp.UI, uRecords, System.Generics.Collections, System.Rtti, System.TypInfo;
|
||||
|
||||
type
|
||||
TSettingsDatabase = class
|
||||
|
||||
private
|
||||
FConnection: TFDConnection;
|
||||
procedure InitializeDatabase;
|
||||
@@ -23,32 +22,27 @@ type
|
||||
function GetColumnsList(Grid: TStringGrid): string;
|
||||
function GetValuesPlaceholders(Grid: TStringGrid): string;
|
||||
function CheckTableExists(const TableName: string): Boolean;
|
||||
procedure EnsureTableForRecord(const TableName: string; RecordTypeInfo: PTypeInfo);
|
||||
// function GetFieldDefinition(Field: TRttiField): string;
|
||||
function GetSQLType(Field: TRttiField): string;
|
||||
function TableHasColumn(const TableName, ColumnName: string): Boolean;
|
||||
public
|
||||
FChannel: string;
|
||||
|
||||
constructor Create(const DatabasePath: string);
|
||||
destructor Destroy; override;
|
||||
|
||||
// Ìåòîäû äëÿ ðàáîòû ñ íàñòðîéêàìè
|
||||
function ReadSetting(const Name: string; Default: string = ''): string;
|
||||
procedure WriteSetting(const Name, Value: string);
|
||||
function getLoginData(): TLogin;
|
||||
|
||||
// Ìåòîäû äëÿ ðàáîòû ñ TStringGrid
|
||||
function getLoginData: TLogin;
|
||||
procedure SaveGridToTable(const TableName: string; Grid: TStringGrid);
|
||||
procedure LoadGridFromTable(const TableName: string; Grid: TStringGrid);
|
||||
|
||||
// Ìåòîäû äëÿ ðàáîòû ñ Users
|
||||
// procedure SaveUsers(const Users: array of User);
|
||||
procedure LoadUsers(var users: tlist<tuser>);
|
||||
|
||||
// Ìåòîäû äëÿ ðàáîòû ñ Ãðóïïîâûìè îòâåòàìè
|
||||
procedure LoadUsers(var users: TList<TUser>);
|
||||
procedure addGroupResponse(Name, Respons: string);
|
||||
procedure getGroupResponse(aName: string; const lbResponse: Tstrings);
|
||||
procedure getGroupName(const lbName: Tstrings);
|
||||
procedure getGroupResponse(aName: string; const lbResponse: TStrings);
|
||||
procedure getGroupName(const lbName: TStrings);
|
||||
procedure delGroupName(aName: string);
|
||||
procedure delGroupResponse(aName, aResponse: string);
|
||||
|
||||
procedure SaveRecordArray<T>(const TableName: string; const Items: array of T);
|
||||
procedure LoadRecordArray<T>(const TableName: string; var Items: TArray<T>);
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -67,8 +61,7 @@ begin
|
||||
on E: Exception do
|
||||
begin
|
||||
FreeAndNil(FConnection);
|
||||
raise Exception.Create('Îøèáêà ïðè ïîäêëþ÷åíèè ê áàçå äàííûõ: ' +
|
||||
E.Message);
|
||||
raise Exception.Create('Îøèáêà ïðè ïîäêëþ÷åíèè ê áàçå äàííûõ: ' + E.Message);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
@@ -80,7 +73,8 @@ begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
Query.SQL.Text := 'delete FROM GroupResponse where Name = "' + aName + '"';
|
||||
Query.SQL.Text := 'DELETE FROM GroupResponse WHERE Name = :name';
|
||||
Query.ParamByName('name').AsString := aName;
|
||||
Query.ExecSQL;
|
||||
finally
|
||||
Query.Free;
|
||||
@@ -94,10 +88,9 @@ begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
Query.SQL.Text := 'delete FROM GroupResponse where Name = :name' +
|
||||
' and Response = :response';
|
||||
Query.Params.ParamByName('name').AsString := aName;
|
||||
Query.Params.ParamByName('response').AsString := aResponse;
|
||||
Query.SQL.Text := 'DELETE FROM GroupResponse WHERE Name = :name AND Response = :response';
|
||||
Query.ParamByName('name').AsString := aName;
|
||||
Query.ParamByName('response').AsString := aResponse;
|
||||
Query.ExecSQL;
|
||||
finally
|
||||
Query.Free;
|
||||
@@ -117,16 +110,14 @@ var
|
||||
FDQuery: TFDQuery;
|
||||
FieldExists: Boolean;
|
||||
begin
|
||||
// Ñîçäàåì òàáëèöó, åñëè îíà íå ñóùåñòâóåò
|
||||
FConnection.ExecSQL('CREATE TABLE IF NOT EXISTS params (' +
|
||||
' name TEXT PRIMARY KEY,' + ' value TEXT' + ');');
|
||||
// Ñîçäàåì òàáëèöó äëÿ ïîëüçîâàòåëåé, åñëè îíà íå ñóùåñòâóåò
|
||||
|
||||
FConnection.ExecSQL('CREATE TABLE IF NOT EXISTS users (' +
|
||||
' id TEXT PRIMARY KEY,' + ' login TEXT,' + ' DisplayName TEXT,' +
|
||||
' created_at DATETIME,' + ' follow_at DATETIME,' + ' isVip TEXT,' +
|
||||
' isModer TEXT,' + ' isO TEXT,' + ' streamer TEXT' +
|
||||
' isModer TEXT,' + ' isO TEXT,' + ' streamer TEXT' + ');');
|
||||
|
||||
');');
|
||||
FDQuery := TFDQuery.Create(nil);
|
||||
try
|
||||
FDQuery.Connection := FConnection;
|
||||
@@ -144,71 +135,19 @@ begin
|
||||
end;
|
||||
FDQuery.Close;
|
||||
if not FieldExists then
|
||||
begin
|
||||
FConnection.ExecSQL('ALTER TABLE users ADD COLUMN streamer TEXT');
|
||||
end;
|
||||
finally
|
||||
FDQuery.Free;
|
||||
end;
|
||||
|
||||
FConnection.ExecSQL
|
||||
('CREATE TABLE IF NOT EXISTS GroupResponse (ID INTEGER PRIMARY KEY, Name TEXT, Response TEXT);');
|
||||
|
||||
FConnection.ExecSQL('CREATE TABLE IF NOT EXISTS GroupResponse (ID INTEGER PRIMARY KEY, Name TEXT, Response TEXT);');
|
||||
end;
|
||||
|
||||
{ procedure TSettingsDatabase.SaveUsers(const Users: array of User);
|
||||
var
|
||||
Query: TFDQuery;
|
||||
UserItem: User;
|
||||
s: string;
|
||||
begin
|
||||
if Length(Users) < 1 then
|
||||
exit;
|
||||
// Âñòàâëÿåì äàííûå èç ìàññèâà ïîëüçîâàòåëåé â òàáëèöó
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
for UserItem in Users do
|
||||
begin
|
||||
Query.SQL.Text :=
|
||||
'INSERT OR REPLACE INTO users (id, login, DisplayName, created_at, follow_at, isVip, isModer, isO, streamer) '
|
||||
+ 'VALUES (:id, :login, :DisplayName, :created_at, :follow_at, :isVip, :isModer, :isO, :streamer);';
|
||||
Query.ParamByName('id').AsString := UserItem.id;
|
||||
Query.ParamByName('login').AsString := UserItem.login;
|
||||
Query.ParamByName('DisplayName').AsString := UserItem.DisplayName;
|
||||
Query.ParamByName('created_at').AsDateTime := UserItem.created_at;
|
||||
Query.ParamByName('follow_at').AsDateTime := UserItem.follow_at;
|
||||
Query.ParamByName('streamer').AsString := FChannel;
|
||||
|
||||
if UserItem.isVip then
|
||||
s := 'True'
|
||||
else
|
||||
s := 'False';
|
||||
Query.ParamByName('isVip').AsString := s;
|
||||
if UserItem.isModer then
|
||||
s := 'True'
|
||||
else
|
||||
s := 'False';
|
||||
Query.ParamByName('isModer').AsString := s;
|
||||
if UserItem.isO then
|
||||
s := 'True'
|
||||
else
|
||||
s := 'False';
|
||||
Query.ParamByName('isO').AsString := s;
|
||||
Query.ExecSQL;
|
||||
end;
|
||||
finally
|
||||
Query.Free;
|
||||
end;
|
||||
end; }
|
||||
|
||||
procedure TSettingsDatabase.LoadUsers(var users: tlist<tuser>);
|
||||
procedure TSettingsDatabase.LoadUsers(var users: TList<TUser>);
|
||||
var
|
||||
Query: TFDQuery;
|
||||
UserItem: tuser;
|
||||
|
||||
UserItem: TUser;
|
||||
begin
|
||||
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
@@ -230,7 +169,6 @@ begin
|
||||
users.Add(UserItem);
|
||||
Query.Next;
|
||||
end;
|
||||
|
||||
finally
|
||||
Query.Free;
|
||||
end;
|
||||
@@ -262,14 +200,13 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.getGroupName(const lbName: Tstrings);
|
||||
procedure TSettingsDatabase.getGroupName(const lbName: TStrings);
|
||||
var
|
||||
Query: TFDQuery;
|
||||
begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
|
||||
lbName.Clear;
|
||||
Query.SQL.Text := 'SELECT DISTINCT Name FROM GroupResponse';
|
||||
Query.Open;
|
||||
@@ -283,18 +220,16 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.getGroupResponse(aName: string;
|
||||
const lbResponse: Tstrings);
|
||||
procedure TSettingsDatabase.getGroupResponse(aName: string; const lbResponse: TStrings);
|
||||
var
|
||||
Query: TFDQuery;
|
||||
begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
|
||||
lbResponse.Clear;
|
||||
Query.SQL.Text := 'SELECT * FROM GroupResponse WHERE name=:TableName';
|
||||
Query.ParamByName('TableName').AsString := aName;
|
||||
Query.SQL.Text := 'SELECT Response FROM GroupResponse WHERE Name = :name';
|
||||
Query.ParamByName('name').AsString := aName;
|
||||
Query.Open;
|
||||
while not Query.EOF do
|
||||
begin
|
||||
@@ -304,7 +239,6 @@ begin
|
||||
finally
|
||||
Query.Free;
|
||||
end;
|
||||
|
||||
end;
|
||||
|
||||
function TSettingsDatabase.getLoginData: TLogin;
|
||||
@@ -346,9 +280,9 @@ begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
Query.SQL.Text :=
|
||||
Format('INSERT INTO GroupResponse (Name, Response) VALUES (''%s'', ''%s'')',
|
||||
[Name, Respons]);
|
||||
Query.SQL.Text := 'INSERT INTO GroupResponse (Name, Response) VALUES (:name, :response)';
|
||||
Query.ParamByName('name').AsString := Name;
|
||||
Query.ParamByName('response').AsString := Respons;
|
||||
Query.ExecSQL;
|
||||
finally
|
||||
Query.Free;
|
||||
@@ -363,8 +297,7 @@ begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
Query.SQL.Text :=
|
||||
'SELECT COUNT(*) FROM sqlite_master WHERE type=''table'' AND name=:TableName';
|
||||
Query.SQL.Text := 'SELECT COUNT(*) FROM sqlite_master WHERE type=''table'' AND name=:TableName';
|
||||
Query.ParamByName('TableName').AsString := TableName;
|
||||
Query.Open;
|
||||
Result := (Query.Fields[0].AsInteger > 0);
|
||||
@@ -373,40 +306,27 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.LoadGridFromTable(const TableName: string;
|
||||
Grid: TStringGrid);
|
||||
procedure TSettingsDatabase.LoadGridFromTable(const TableName: string; Grid: TStringGrid);
|
||||
var
|
||||
Query: TFDQuery;
|
||||
Col, Row: Integer;
|
||||
begin
|
||||
if not CheckTableExists(TableName) then Exit;
|
||||
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
|
||||
// Ïðîâåðèòü íàëè÷èå òàáëèöû
|
||||
if not CheckTableExists(TableName) then
|
||||
begin
|
||||
// Òàáëèöà íå ñóùåñòâóåò, âûõîäèì èç ïðîöåäóðû
|
||||
exit;
|
||||
end;
|
||||
|
||||
Query.SQL.Text := 'SELECT * FROM ' + TableName;
|
||||
Query.Open;
|
||||
|
||||
// Î÷èùàåì Grid
|
||||
Grid.RowCount := 0;
|
||||
|
||||
// Çàïîëíÿåì Grid äàííûìè èç òàáëèöû
|
||||
while not Query.EOF do
|
||||
begin
|
||||
Row := Grid.RowCount;
|
||||
Grid.RowCount := Grid.RowCount + 1;
|
||||
|
||||
for Col := 0 to Grid.ColumnCount - 1 do
|
||||
begin
|
||||
Grid.Cells[Col, Row] := Query.FieldByName('col' + IntToStr(Col)
|
||||
).AsString;
|
||||
end;
|
||||
Grid.Cells[Col, Row] := Query.FieldByName('col' + IntToStr(Col)).AsString;
|
||||
|
||||
Query.Next;
|
||||
end;
|
||||
@@ -415,33 +335,22 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.SaveGridToTable(const TableName: string;
|
||||
Grid: TStringGrid);
|
||||
procedure TSettingsDatabase.SaveGridToTable(const TableName: string; Grid: TStringGrid);
|
||||
var
|
||||
Query: TFDQuery;
|
||||
Col, Row: Integer;
|
||||
begin
|
||||
// Óäàëÿåì ñòàðóþ òàáëèöó, åñëè îíà ñóùåñòâóåò
|
||||
FConnection.ExecSQL('DROP TABLE IF EXISTS ' + TableName);
|
||||
FConnection.ExecSQL('CREATE TABLE ' + TableName + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, ' + GetColumnsDefinition(Grid) + ');');
|
||||
|
||||
// Ñîçäàåì íîâóþ òàáëèöó
|
||||
FConnection.ExecSQL('CREATE TABLE ' + TableName + ' (' +
|
||||
' id INTEGER PRIMARY KEY AUTOINCREMENT,' + ' ' +
|
||||
GetColumnsDefinition(Grid) + ');');
|
||||
|
||||
// Âñòàâëÿåì äàííûå èç Grid â òàáëèöó
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
for Row := 0 to Grid.RowCount - 1 do
|
||||
begin
|
||||
Query.SQL.Text := 'INSERT INTO ' + TableName + ' (' + GetColumnsList(Grid)
|
||||
+ ') VALUES (' + GetValuesPlaceholders(Grid) + ')';
|
||||
Query.SQL.Text := 'INSERT INTO ' + TableName + ' (' + GetColumnsList(Grid) + ') VALUES (' + GetValuesPlaceholders(Grid) + ')';
|
||||
for Col := 0 to Grid.ColumnCount - 1 do
|
||||
begin
|
||||
Query.ParamByName('col' + IntToStr(Col)).AsString :=
|
||||
Grid.Cells[Col, Row];
|
||||
end;
|
||||
Query.ParamByName('col' + IntToStr(Col)).AsString := Grid.Cells[Col, Row];
|
||||
Query.ExecSQL;
|
||||
end;
|
||||
finally
|
||||
@@ -470,13 +379,10 @@ end;
|
||||
|
||||
procedure TSettingsDatabase.SetSetting(const Name, Value: string);
|
||||
begin
|
||||
FConnection.ExecSQL
|
||||
('INSERT OR REPLACE INTO params (name, value) VALUES (:name, :value)',
|
||||
[Name, Value]);
|
||||
FConnection.ExecSQL('INSERT OR REPLACE INTO params (name, value) VALUES (:name, :value)', [Name, Value]);
|
||||
end;
|
||||
|
||||
function TSettingsDatabase.ReadSetting(const Name: string;
|
||||
Default: string = ''): string;
|
||||
function TSettingsDatabase.ReadSetting(const Name: string; Default: string = ''): string;
|
||||
begin
|
||||
Result := GetSetting(Name);
|
||||
if Result = '' then
|
||||
@@ -488,4 +394,202 @@ begin
|
||||
SetSetting(Name, Value);
|
||||
end;
|
||||
|
||||
function TSettingsDatabase.TableHasColumn(const TableName, ColumnName: string): Boolean;
|
||||
var
|
||||
Query: TFDQuery;
|
||||
begin
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
Query.SQL.Text := Format('PRAGMA table_info(%s)', [TableName]);
|
||||
Query.Open;
|
||||
Result := False;
|
||||
while not Query.EOF do
|
||||
begin
|
||||
if SameText(Query.FieldByName('name').AsString, ColumnName) then
|
||||
Exit(True);
|
||||
Query.Next;
|
||||
end;
|
||||
finally
|
||||
Query.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TSettingsDatabase.GetSQLType(Field: TRttiField): string;
|
||||
begin
|
||||
case Field.FieldType.TypeKind of
|
||||
tkInteger, tkInt64:
|
||||
Result := 'INTEGER';
|
||||
tkFloat:
|
||||
Result := 'REAL';
|
||||
tkUString, tkString, tkWString, tkLString:
|
||||
Result := 'TEXT';
|
||||
tkEnumeration:
|
||||
if Field.FieldType = TypeInfo(Boolean) then
|
||||
Result := 'BOOLEAN'
|
||||
else
|
||||
Result := 'INTEGER';
|
||||
else
|
||||
raise Exception.CreateFmt('Unsupported type for field %s', [Field.Name]);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.EnsureTableForRecord(const TableName: string; RecordTypeInfo: PTypeInfo);
|
||||
var
|
||||
Context: TRttiContext;
|
||||
RttiType: TRttiType;
|
||||
Field: TRttiField;
|
||||
FieldDefs: string;
|
||||
Query: TFDQuery;
|
||||
begin
|
||||
if not CheckTableExists(TableName) then
|
||||
begin
|
||||
Context := TRttiContext.Create;
|
||||
try
|
||||
RttiType := Context.GetType(RecordTypeInfo);
|
||||
FieldDefs := '';
|
||||
for Field in RttiType.GetFields do
|
||||
begin
|
||||
if FieldDefs <> '' then
|
||||
FieldDefs := FieldDefs + ', ';
|
||||
FieldDefs := FieldDefs + Field.Name + ' ' + GetSQLType(Field);
|
||||
end;
|
||||
FConnection.ExecSQL(Format('CREATE TABLE %s (%s)', [TableName, FieldDefs]));
|
||||
finally
|
||||
Context.Free;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
Context := TRttiContext.Create;
|
||||
try
|
||||
RttiType := Context.GetType(RecordTypeInfo);
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
for Field in RttiType.GetFields do
|
||||
begin
|
||||
if not TableHasColumn(TableName, Field.Name) then
|
||||
begin
|
||||
Query.SQL.Text := Format('ALTER TABLE %s ADD COLUMN %s %s', [TableName, Field.Name, GetSQLType(Field)]);
|
||||
Query.ExecSQL;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
Query.Free;
|
||||
end;
|
||||
finally
|
||||
Context.Free;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.SaveRecordArray<T>(const TableName: string; const Items: array of T);
|
||||
var
|
||||
Context: TRttiContext;
|
||||
RttiType: TRttiType;
|
||||
Fields: TArray<TRttiField>;
|
||||
Query: TFDQuery;
|
||||
Rec: T;
|
||||
Field: TRttiField;
|
||||
FieldNames, Placeholders: string;
|
||||
i: Integer;
|
||||
begin
|
||||
if Length(Items) = 0 then Exit;
|
||||
|
||||
EnsureTableForRecord(TableName, TypeInfo(T));
|
||||
FConnection.ExecSQL(Format('DELETE FROM %s', [TableName]));
|
||||
|
||||
Context := TRttiContext.Create;
|
||||
try
|
||||
RttiType := Context.GetType(TypeInfo(T));
|
||||
Fields := RttiType.GetFields;
|
||||
|
||||
Query := TFDQuery.Create(nil);
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
|
||||
FieldNames := '';
|
||||
Placeholders := '';
|
||||
for i := 0 to High(Fields) do
|
||||
begin
|
||||
if i > 0 then
|
||||
begin
|
||||
FieldNames := FieldNames + ', ';
|
||||
Placeholders := Placeholders + ', ';
|
||||
end;
|
||||
FieldNames := FieldNames + Fields[i].Name;
|
||||
Placeholders := Placeholders + ':p_' + Fields[i].Name;
|
||||
end;
|
||||
|
||||
Query.SQL.Text := 'INSERT INTO '+TableName+' ('+FieldNames+') VALUES ('+Placeholders+')';
|
||||
|
||||
|
||||
for Rec in Items do
|
||||
begin
|
||||
Query.Params.Clear;
|
||||
for Field in Fields do
|
||||
Query.ParamByName('p_'+Field.Name).Value := Field.GetValue(@Rec).AsVariant;
|
||||
Query.ExecSQL;
|
||||
end;
|
||||
finally
|
||||
Query.Free;
|
||||
end;
|
||||
finally
|
||||
Context.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TSettingsDatabase.LoadRecordArray<T>(const TableName: string; var Items: TArray<T>);
|
||||
var
|
||||
Context: TRttiContext;
|
||||
RttiType: TRttiType;
|
||||
Fields: TArray<TRttiField>;
|
||||
Query: TFDQuery;
|
||||
Rec: T;
|
||||
RecList: TList<T>;
|
||||
Field: TRttiField;
|
||||
begin
|
||||
if not CheckTableExists(TableName) then
|
||||
begin
|
||||
SetLength(Items, 0);
|
||||
Exit;
|
||||
end;
|
||||
|
||||
EnsureTableForRecord(TableName, TypeInfo(T));
|
||||
|
||||
Context := TRttiContext.Create;
|
||||
try
|
||||
RttiType := Context.GetType(TypeInfo(T));
|
||||
Fields := RttiType.GetFields;
|
||||
|
||||
Query := TFDQuery.Create(nil);
|
||||
RecList := TList<T>.Create;
|
||||
try
|
||||
Query.Connection := FConnection;
|
||||
Query.SQL.Text := Format('SELECT * FROM %s', [TableName]);
|
||||
Query.Open;
|
||||
|
||||
while not Query.EOF do
|
||||
begin
|
||||
Rec := Default(T);
|
||||
for Field in Fields do
|
||||
begin
|
||||
if Query.FindField(Field.Name) <> nil then
|
||||
Field.SetValue(@Rec, TValue.FromVariant(Query.FieldByName(Field.Name).Value));
|
||||
end;
|
||||
RecList.Add(Rec);
|
||||
Query.Next;
|
||||
end;
|
||||
|
||||
Items := RecList.ToArray;
|
||||
finally
|
||||
Query.Free;
|
||||
RecList.Free;
|
||||
end;
|
||||
finally
|
||||
Context.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user