Repository navigation
Expand file tree
/
Copy pathfMain.pas
More file actions
139 lines (119 loc) · 3.75 KB
/
Copy pathfMain.pas
File metadata and controls
139 lines (119 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
unit fMain;
{$MODE DELPHI}
interface
uses
SynSQLite3Static, SysUtils, Classes, Forms, Controls, Graphics, Dialogs, Math,
UVisualGrid, SynCommons, mORMot, StdCtrls, mORMotSQLite3, SynDBSQLite3, SynDB;
type
TSampleData = class(TSQLRecord)
protected
fName: RawUTF8;
fFoo: Integer;
published
property name: RawUTF8 read fName write fName;
property foo: Integer read fFoo write fFoo;
end;
{ TForm1 }
TForm1 = class(TForm, IDataSource)
VisualGrid1: TVisualGrid;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FModel: TSQLModel;
FServer: TSQLRestServerDB;
FProp: TSQLDBConnectionProperties;
function FetchPage(constref AParams: TPageFetchParams; var ADataTable: TDataTable): TPageFetchResult;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
function TForm1.FetchPage(constref AParams: TPageFetchParams; var ADataTable: TDataTable): TPageFetchResult;
var
LColumnFilter: TColumnFilter;
LOrderBy: utf8string;
LModulo, LLimit, LRowCount: Integer;
LOffset: Int64;
i: integer;
begin
if Length(AParams.Filter) > 0 then
begin
LOrderBy := ' order by';
for LColumnFilter in AParams.Filter do
begin
LOrderBy := LOrderBy + ' ' + LColumnFilter.ColumnName;
case LColumnFilter.Sort of
sdNone: ;
sdAscending: LOrderBy := LOrderBy + ' asc';
sdDescending: LOrderBy := LOrderBy + ' desc';
end;
LOrderBy := LOrderBy + ',';
end;
SetLength(LOrderBy, Length(LOrderBy)-1);
end;
// rather bad approach, but for demo purposes is ok
// https://stackoverflow.com/questions/14468586/efficient-paging-in-sqlite-with-millions-of-records
Result.TotalDataCount := FServer.TableRowCount(TSampleData);
// APageIndex = -1 is acceptable as initial value
if AParams.PageIndex < -1 then
raise Exception.CreateFmt('Wrong value for APageNumber (%d)', [AParams.PageIndex]);
// Single mode uses the shared nonpositive-size contract. SQLite's LIMIT -1
// returns the complete ordered result without dividing by a page size.
if AParams.PageSize <= 0 then begin
Result.PageCount := Ord(Result.TotalDataCount > 0);
Result.PageIndex := Result.PageCount - 1;
LLimit := -1;
LOffset := 0;
LRowCount := Result.TotalDataCount;
end else begin
Result.PageCount := Result.TotalDataCount div AParams.PageSize;
LModulo := Result.TotalDataCount mod AParams.PageSize;
if LModulo > 0 then Inc(Result.PageCount);
Result.PageIndex := Min(Max(0,AParams.PageIndex),Result.PageCount-1);
LLimit := AParams.PageSize;
LOffset := Int64(Max(0,Result.PageIndex)) * AParams.PageSize;
LRowCount := Min(AParams.PageSize,Result.TotalDataCount-Integer(LOffset));
end;
with FProp.Execute('select * from SampleData' + LOrderBy +
' LIMIT ? OFFSET ?', [LLimit, LOffset]) do
begin
SetLength(ADataTable.Columns, ColumnCount);
for i := 0 to ColumnCount - 1 do
ADataTable.Columns[i] := ColumnName(i);
SetLength(ADataTable.Rows, LRowCount);
i := 0;
while Step do
begin
RowDocVariant(ADataTable.Rows[i]);
Inc(i);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
sd: TSampleData;
begin
FModel := TSQLModel.Create([TSampleData]);
FServer := TSQLRestServerDB.Create(FModel, ':memory:');
FServer.CreateMissingTables;
FProp := TSQLDBSQLite3ConnectionProperties.Create(FServer.DB);
with TAutoFree.One(sd, TSampleData.Create) do
for i := 0 to 10000 do
begin
sd.Name := 'name' + inttostr(i);
sd.Foo := i;
FServer.Add(sd, true);
end;
VisualGrid1.DataSource := Self;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FProp.Free;
FServer.Free;
FModel.Free;
end;
end.