Skip to content

Commit 4582eda

Browse files
committed
Adding RTL.DB Units
1 parent 69af7f9 commit 4582eda

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

RTL/RTL.DB.FireDAC.pas

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
unit RTL.DB.FireDAC;
2+
3+
interface
4+
5+
uses
6+
RTL.DB,
7+
Data.DB;
8+
9+
type
10+
TFDMemTableDataSetCloner = class(TInterfacedObject, IDataSetCloner)
11+
public
12+
function Copy(ASource: TDataSet): TDataSet;
13+
function CopySelected(const ASelectedRecords: TArray<TBookmark>; ASource: TDataSet): TDataSet;
14+
end;
15+
16+
implementation
17+
18+
uses
19+
FireDAC.Comp.DataSet,
20+
FireDAC.Comp.Client;
21+
22+
function TFDMemTableDataSetCloner.Copy(ASource: TDataSet): TDataSet;
23+
var
24+
ACopy: TFDMemTable;
25+
begin
26+
ACopy := TFDMemTable.Create(NIL);
27+
try
28+
ACopy.CopyDataSet(ASource, [coStructure, coAppend, coRestart]);
29+
finally
30+
Result := ACopy;
31+
end;
32+
end;
33+
34+
function TFDMemTableDataSetCloner.CopySelected(const ASelectedRecords: TArray<TBookmark>; ASource: TDataSet): TDataSet;
35+
var
36+
ACopy: TFDMemTable;
37+
I: Integer;
38+
begin
39+
ACopy := TFDMemTable.Create(NIL);
40+
try
41+
ACopy.CopyDataSet(ASource, [coStructure, coRestart]);
42+
ASource.DisableControls;
43+
try
44+
for I := Low(ASelectedRecords) to High(ASelectedRecords) do
45+
begin
46+
ASource.GotoBookmark(ASelectedRecords[I]);
47+
ACopy.Append;
48+
ACopy.CopyFields(ASource);
49+
ACopy.Post;
50+
end;
51+
finally
52+
ASource.EnableControls;
53+
end;
54+
finally
55+
Result := ACopy;
56+
end;
57+
end;
58+
59+
end.

RTL/RTL.DB.pas

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
unit RTL.DB;
2+
3+
interface
4+
5+
uses
6+
Data.DB;
7+
8+
type
9+
/// <summary>
10+
/// Interface que copia un DataSet
11+
/// </summary>
12+
IDataSetCloner = interface
13+
['{D61E44DC-3DDF-4CEF-B390-8F470B6017FB}']
14+
/// <summary>
15+
/// Retorna una copia del DataSet enviado en el parametro ASource
16+
/// </summary>
17+
function Copy(ASource: TDataSet): TDataSet;
18+
19+
/// <summary>
20+
/// Retorna una copia del DataSet enviado en el parametro ASource; solo los record que estan en ASelectedRecords
21+
/// </summary>
22+
function CopySelected(const ASelectedRecords: TArray<TBookmark>; ASource: TDataSet): TDataSet;
23+
end;
24+
25+
implementation
26+
27+
end.

RTL/RTL.Excel.pas

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
unit RTL.Excel;
2+
3+
interface
4+
5+
uses
6+
RTL.DB,
7+
Spring.Collections,
8+
System.Classes,
9+
Data.DB,
10+
Data.Win.ADODB;
11+
12+
type
13+
IExcelFile = interface
14+
['{0CAB1799-A7A1-41E1-8BC9-DFA8D256CE39}']
15+
function GetOnFileOpen: TNotifyEvent;
16+
function GetSheets: IEnumerable<string>;
17+
function GetFileName: string;
18+
19+
procedure SetFileName(const Value: string);
20+
procedure SetOnFileOpen(const Value: TNotifyEvent);
21+
22+
function SheetData(const SheetName: string; const ACloner: IDataSetCloner): TDataSet;
23+
24+
property FileName: string read GetFileName write SetFileName;
25+
property Sheets: IEnumerable<string> read GetSheets;
26+
27+
property OnFileOpen: TNotifyEvent read GetOnFileOpen write SetOnFileOpen;
28+
end;
29+
30+
TExcelFile = class(TInterfacedObject, IExcelFile)
31+
strict private
32+
FConnection: TADOConnection;
33+
FFileName: string;
34+
FOnFileOpen: TNotifyEvent;
35+
36+
function CreateQuery(AOwner: TComponent): TADOQuery;
37+
38+
procedure OpenFile(const AFileName: string);
39+
procedure CloseFile;
40+
41+
function GetOnFileOpen: TNotifyEvent;
42+
function GetSheets: IEnumerable<string>;
43+
function GetFileName: string;
44+
45+
procedure SetFileName(const Value: string);
46+
procedure SetOnFileOpen(const Value: TNotifyEvent);
47+
strict protected
48+
function GetConnectionString: string; virtual;
49+
public
50+
constructor Create; overload;
51+
constructor Create(const AFileName: string); overload;
52+
destructor Destroy; override;
53+
54+
function SheetData(const SheetName: string; const ACloner: IDataSetCloner): TDataSet;
55+
56+
property FileName: string read GetFileName write SetFileName;
57+
property Sheets: IEnumerable<string> read GetSheets;
58+
59+
property OnFileOpen: TNotifyEvent read GetOnFileOpen write SetOnFileOpen;
60+
end;
61+
62+
implementation
63+
64+
uses
65+
System.SysUtils;
66+
67+
constructor TExcelFile.Create;
68+
begin
69+
inherited Create;
70+
FFileName := EmptyStr;
71+
72+
FConnection := TADOConnection.Create(NIL);
73+
FConnection.Provider := 'Microsoft.Jet.OLEDB.4.0';
74+
FConnection.LoginPrompt := False;
75+
end;
76+
77+
constructor TExcelFile.Create(const AFileName: string);
78+
begin
79+
Create;
80+
Filename := AFileName;
81+
end;
82+
83+
destructor TExcelFile.Destroy;
84+
begin
85+
FConnection.Free;
86+
inherited Destroy;
87+
end;
88+
89+
function TExcelFile.CreateQuery(AOwner: TComponent): TADOQuery;
90+
begin
91+
Result := TADOQuery.Create(AOwner);
92+
Result.Connection := FConnection;
93+
end;
94+
95+
procedure TExcelFile.CloseFile;
96+
begin
97+
if FFileName.IsEmpty then
98+
Exit;
99+
100+
FFileName := EmptyStr;
101+
FConnection.Close;
102+
end;
103+
104+
function TExcelFile.GetSheets: IEnumerable<string>;
105+
var
106+
LSheets: IList<string>;
107+
LTables: TStrings;
108+
s: string;
109+
begin
110+
LSheets := TCollections.CreateList<string>;
111+
112+
if not FConnection.Connected then
113+
Exit(LSheets);
114+
115+
LTables := TStringList.Create;
116+
try
117+
FConnection.GetTableNames(LTables);
118+
119+
for s in LTables do
120+
LSheets.Add(s);
121+
finally
122+
LTables.Free;
123+
end;
124+
125+
Result := LSheets;
126+
end;
127+
128+
function TExcelFile.GetConnectionString: string;
129+
begin
130+
Result := 'Provider=Microsoft.JET.OLEDB.4.0;Data Source=%s;Extended Properties="Excel 8.0;HDR=No";';
131+
end;
132+
133+
function TExcelFile.GetFileName: string;
134+
begin
135+
Result := FFileName;
136+
end;
137+
138+
function TExcelFile.GetOnFileOpen: TNotifyEvent;
139+
begin
140+
Result := FOnFileOpen;
141+
end;
142+
143+
procedure TExcelFile.OpenFile(const AFileName: string);
144+
begin
145+
FFileName := EmptyStr;
146+
FConnection.Close;
147+
FConnection.ConnectionString := Format(GetConnectionString, [AFileName]);
148+
FConnection.Open;
149+
FFileName := AFileName;
150+
151+
if Assigned(FOnFileOpen) then
152+
FOnFileOpen(Self);
153+
end;
154+
155+
procedure TExcelFile.SetFileName(const Value: string);
156+
begin
157+
OpenFile(Value);
158+
end;
159+
160+
procedure TExcelFile.SetOnFileOpen(const Value: TNotifyEvent);
161+
begin
162+
FOnFileOpen := Value;
163+
end;
164+
165+
function TExcelFile.SheetData(const SheetName: string; const ACloner: IDataSetCloner): TDataSet;
166+
var
167+
LQuery: TADOQuery;
168+
begin
169+
if not Assigned(ACloner) then
170+
raise Exception.CreateFmt('%s.SheetData :: ACloner is not assigned', [ClassName]);
171+
172+
if SheetName.IsEmpty then
173+
raise Exception.CreateFmt('%s.SheetData :: SheetName is Empty', [ClassName]);
174+
175+
LQuery := CreateQuery(NIL);
176+
try
177+
LQuery.SQL.Text := Format(' SELECT * FROM [%s] ', [SheetName]);
178+
LQuery.Open;
179+
Result := ACloner.Copy(LQuery);
180+
finally
181+
LQuery.Free;
182+
end;
183+
end;
184+
185+
end.

0 commit comments

Comments
 (0)