-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmSCM.pas
230 lines (200 loc) · 6.16 KB
/
dmSCM.pas
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
unit dmSCM;
interface
uses
System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.UI.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Phys, FireDAC.VCLUI.Wait, FireDAC.Phys.MSSQL,
FireDAC.Phys.MSSQLDef, System.Variants, DateUtils;
type
TSCM = class(TDataModule)
scmConnection: TFDConnection;
qrySwimClub: TFDQuery;
dsSwimClub: TDataSource;
qrySession: TFDQuery;
dsSession: TDataSource;
qryLBHeader: TFDQuery;
dsLBHeader: TDataSource;
qryGetStartOfSession: TFDQuery;
dsGetStartOfSession: TDataSource;
qryHeatStatus: TFDQuery;
dsHeatStatus: TDataSource;
qryGetSessionCount: TFDQuery;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
FIsActive: boolean;
public
{ Public declarations }
function GetStartOfSwimmingSeason(SwimClubID: integer): TDateTime;
function GetStartOfSession(SessionID: integer): TDateTime;
function GetSessionCount(SwimClubID: integer; SDate, EDate: TDateTime): Integer;
procedure ActivateTable();
// procedure SimpleLoadSettingString(ASection, AName: string; var AValue: string);
// procedure SimpleMakeTemporyFDConnection(Server, User, Password: string;
// OsAuthent: boolean);
// procedure SimpleSaveSettingString(ASection, AName, AValue: string);
property IsActive: boolean read FIsActive write FIsActive;
end;
const
SCMCONFIGFILENAME = 'SCMConfig.ini';
var
SCM: TSCM;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
uses
System.IOUtils, IniFiles;
procedure TSCM.ActivateTable;
begin
FIsActive := false;
// TODO: activate all the tables on the form ?....
if scmConnection.Connected then
begin
qryHeatStatus.Connection := scmConnection;
qryHeatStatus.Open;
qrySwimClub.Connection := scmConnection;
qrySwimClub.Open;
qrySession.Connection := scmConnection;
qrySession.Open;
qryLBHeader.Connection := scmConnection;
qryLBHeader.Open;
if qryHeatStatus.Active and qrySwimClub.Active and qrySession.Active and
qryLBHeader.Active then
FIsActive := true;
qryGetSessionCount.Connection := scmConnection;
qryGetStartOfSession.Connection := scmConnection;
end;
end;
procedure TSCM.DataModuleCreate(Sender: TObject);
begin
ActivateTable;
end;
function TSCM.GetSessionCount(SwimClubID: integer; SDate,
EDate: TDateTime): Integer;
begin
result := 0;
if qryGetSessionCount.Active then
qryGetSessionCount.Close;
qryGetSessionCount.ParamByName('SWIMCLUBID').AsInteger := SwimClubID;
qryGetSessionCount.ParamByName('SDATE').AsDateTime := SDate;
qryGetSessionCount.ParamByName('EDATE').AsDateTime := EDate;
qryGetSessionCount.Prepare;
qryGetSessionCount.Open;
if qryGetSessionCount.Active then
begin
if not qryGetSessionCount.IsEmpty then
begin
result := qryGetSessionCount.FieldByName('SessionCount').AsInteger;
end;
end;
end;
function TSCM.GetStartOfSession(SessionID: integer): TDateTime;
begin
result := Date;
if qryGetStartOfSession.Active then
qryGetStartOfSession.Close;
qryGetStartOfSession.ParamByName('SESSIONID').AsInteger := SessionID;
qryGetStartOfSession.Prepare;
qryGetStartOfSession.Open;
if qryGetStartOfSession.Active then
begin
if not qryGetStartOfSession.IsEmpty then
begin
result := qryGetStartOfSession.FieldByName('SessionStart').AsDateTime;
end;
end;
end;
function TSCM.GetStartOfSwimmingSeason(SwimClubID: integer): TDateTime;
var
dt: TDateTime;
begin
result := Date();
if SwimClubID > 0 then
begin
if qrySwimClub.Active then
qrySwimClub.Close;
qrySwimClub.ParamByName('SWIMCLUBID').AsInteger := SwimClubID;
qrySwimClub.Prepare;
qrySwimClub.Open;
if qrySwimClub.Active then
begin
if not qrySwimClub.IsEmpty then
begin
dt := qrySwimClub.FieldByName('StartOfSwimSeason').AsDateTime;
// If ANow and AThen are two and a half years apart,
// calling WithinPastYears with AYears set to 2 returns True.
if WithinPastYears(result, dt, 1) then
result := dt;
end;
end;
end;
end;
{$REGION SIMPLE TEMPORY CONNECTION AND INIFILES CONFIGURATION}
{
procedure TSCM.SimpleLoadSettingString(ASection, AName: string; var AValue: string);
var
ini: TIniFile;
begin
ini := TIniFile.Create(TPath.GetDocumentsPath + PathDelim +
SCMCONFIGFILENAME);
try
AValue := ini.ReadString(ASection, Aname, '');
finally
ini.Free;
end;
end;
procedure TSCM.SimpleMakeTemporyFDConnection(Server, User, Password: string;
OsAuthent: boolean);
var
AValue, ASection, AName: string;
begin
if (scmConnection.Connected) then
begin
scmConnection.Close();
end;
scmConnection.Params.Add('Server=' + Server);
scmConnection.Params.Add('DriverID=MSSQL');
scmConnection.Params.Add('Database=SwimClubMeet');
scmConnection.Params.Add('User_name=' + User);
scmConnection.Params.Add('Password=' + Password);
if (OsAuthent) then
AValue := 'Yes'
else
AValue := 'No';
scmConnection.Params.Add('OSAuthent=' + AValue);
scmConnection.Params.Add('Mars=yes');
scmConnection.Params.Add('MetaDefSchema=dbo');
scmConnection.Params.Add('ExtendedMetadata=False');
scmConnection.Params.Add('ApplicationName=scmTimeKeeper');
scmConnection.Connected := true;
// ON SUCCESS - Save connection details.
if (scmConnection.Connected) then
begin
ASection := 'MSSQL_SwimClubMeet';
AName := 'Server';
SimpleSaveSettingString(ASection, AName, Server);
AName := 'User';
SimpleSaveSettingString(ASection, AName, User);
AName := 'Password';
SimpleSaveSettingString(ASection, AName, Password);
AName := 'OSAuthent';
SimpleSaveSettingString(ASection, AName, AValue);
end
end;
procedure TSCM.SimpleSaveSettingString(ASection, AName, AValue: string);
var
ini: TIniFile;
begin
ini := TIniFile.Create(TPath.GetDocumentsPath + PathDelim +
SCMCONFIGFILENAME);
try
ini.WriteString(ASection, AName, AValue);
finally
ini.Free;
end;
end;
}
{$REGION end}
end.