-
Notifications
You must be signed in to change notification settings - Fork 10
/
unitcommentary.pas
299 lines (264 loc) · 7.49 KB
/
unitcommentary.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
unit UnitCommentary;
{$modeswitch typehelpers}
interface
uses
Classes, Fgl, SysUtils, IniFiles, UnitModule, UnitBible, UnitUtils, UnitLib;
type
TCommentaryAlias = record
commentary, id, book, chapter, fromverse, toverse, marker, data : string;
end;
TCommentary = class(TModule)
private
z : TCommentaryAlias;
public
constructor Create(filePath: string);
function GetData(Verse: TVerse): TStringArray;
function GetMybibleFootnote(Verse: TVerse; marker: string): string;
function GetAll(footnotes: boolean = false): TStringArray;
end;
TCommentaries = class(TFPGList<TCommentary>)
private
procedure Load;
function FindCommentary(module: string): TCommentary;
procedure SavePrivates;
procedure ReadPrivates;
public
constructor Create;
function GetMybibleFootnote(module: string; Verse: TVerse; marker: string): string;
function GetAllFootnotes(module: string): TStringArray;
function FootnotesOnly: boolean;
function DeleteItem(Item: TCommentary): boolean;
procedure DeleteFootnotes(module: string);
destructor Destroy; override;
end;
implementation
const
unboundAlias : TCommentaryAlias = (
commentary : 'Commentary';
id : 'id';
book : 'book';
chapter : 'chapter';
fromverse : 'fromverse';
toverse : 'toverse';
marker : 'marker';
data : 'data';
);
mybibleAlias : TCommentaryAlias = (
commentary : 'commentaries';
id : 'id';
book : 'book_number';
chapter : 'chapter_number_from';
fromverse : 'verse_number_from';
// chapter : 'chapter_number_to';
toverse : 'verse_number_to';
marker : 'marker';
data : 'text';
);
//=================================================================================================
// TCommentary
//=================================================================================================
constructor TCommentary.Create(filePath: string);
begin
inherited Create(filePath);
z := unboundAlias;
if format = mybible then z := mybibleAlias;
if connected and not TableExists(z.commentary) then connected := false;
end;
function TCommentary.GetData(Verse: TVerse): TStringArray;
var
v_from, v_to : string;
line : string;
id : integer;
count : integer;
begin
Result := [];
id := EncodeID(Verse.book);
v_from := ToStr(Verse.number);
v_to := ToStr(Verse.number + Verse.count - 1);
try
try
Query.SQL.Text := 'SELECT * FROM ' + z.commentary +
' WHERE ' + z.book + ' = ' + ToStr(id) +
' AND ' + z.chapter + ' = ' + ToStr(Verse.chapter) +
' AND ((' + v_from + ' BETWEEN ' + z.fromverse + ' AND ' + z.toverse + ')' +
' OR (' + z.fromverse + ' BETWEEN ' + v_from + ' AND ' + v_to + ')) ';
Query.Open;
Query.Last;
SetLength(Result, Query.RecordCount);
Query.First;
count := 0;
while not Query.Eof do
try
line := Query.FieldByName(z.data).AsString;
if line.IsEmpty then Continue;
Result[count] := line;
count += 1;
finally
Query.Next;
end;
SetLength(Result, count);
except
//
end;
finally
Query.Close;
end;
end;
function TCommentary.GetMybibleFootnote(Verse: TVerse; marker: string): string;
begin
Result := '';
try
try
Query.SQL.Text := 'SELECT * FROM ' + z.commentary +
' WHERE ' + z.book + ' = ' + EncodeID(Verse.book).ToString +
' AND ' + z.chapter + ' = ' + ToStr(Verse.chapter) +
' AND ' + z.marker + ' ="' + marker + '" ';
Query.Open;
try Result := Query.FieldByName(z.data).AsString; except end;
except
//
end;
finally
Query.Close;
end;
end;
function TCommentary.GetAll(footnotes: boolean = false): TStringArray;
var
b : integer;
f, s : string;
begin
Result := [];
try
try
Query.SQL.Text := 'SELECT * FROM ' + z.commentary;
Query.Open;
f := iif(footnotes, z.marker, z.toverse);
while not Query.Eof do
try
try
b := Query.FieldByName(z.book).AsInteger;
s := DecodeID(b).ToString + #0;
s += Query.FieldByName(z.chapter ).AsString + #0;
s += Query.FieldByName(z.fromverse).AsString + #0;
s += Query.FieldByName(f ).AsString + #0;
s += Query.FieldByName(z.data ).AsString;
Result.Add(s);
except
//
end;
finally
Query.Next;
end;
except
//
end;
finally
Query.Close;
end;
end;
//=================================================================================================
// TCommentaries
//=================================================================================================
function Comparison(const Item1: TCommentary; const Item2: TCommentary): integer;
var
s1 : string = '';
s2 : string = '';
begin
if Item1.language = GetLanguageID then s1 := ' ';
if Item2.language = GetLanguageID then s2 := ' ';
Result := CompareText(s1 + Item1.Name, s2 + Item2.Name);
end;
constructor TCommentaries.Create;
begin
inherited;
Load;
Sort(Comparison);
ReadPrivates;
end;
procedure TCommentaries.Load;
var
Item : TCommentary;
f : string;
begin
for f in DatabaseList do
if f.Contains('.cmt.') or f.Contains('.commentaries.') then
begin
Item := TCommentary.Create(f);
if Item.connected then Add(Item) else Item.Free;
end;
end;
function TCommentaries.FindCommentary(module: string): TCommentary;
var
Commentary : TCommentary;
name : string;
begin
Result := nil;
name := ExtractOnlyName(module);
for Commentary in Self do
if Prefix(name, Commentary.filename) then Result := Commentary;
end;
function TCommentaries.GetMybibleFootnote(module: string; Verse: TVerse; marker: string): string;
var
Commentary : TCommentary;
begin
Result := '';
Commentary := FindCommentary(module);
if Commentary <> nil then Result := Commentary.GetMybibleFootnote(Verse, marker);
end;
function TCommentaries.GetAllFootnotes(module: string): TStringArray;
var
Commentary : TCommentary;
begin
Result := [];
Commentary := FindCommentary(module);
if Commentary <> nil then Result := Commentary.GetAll(true);
end;
function TCommentaries.FootnotesOnly: boolean;
var
Commentary : TCommentary;
begin
Result := True;
for Commentary in Self do
if not Commentary.footnotes then Result := False;
end;
function TCommentaries.DeleteItem(Item: TCommentary): boolean;
begin
if not Item.Delete then Exit(false);
Item.Free;
Delete(IndexOf(Item));
Exit(true);
end;
procedure TCommentaries.DeleteFootnotes(module: string);
var
Item : TCommentary;
begin
Item := FindCommentary(module);
if Item <> nil then DeleteItem(Item);
end;
procedure TCommentaries.SavePrivates;
var
IniFile : TIniFile;
Commentary : TCommentary;
begin
IniFile := TIniFile.Create(ConfigFile);
for Commentary in Self do Commentary.SavePrivate(IniFile);
IniFile.Free;
end;
procedure TCommentaries.ReadPrivates;
var
IniFile : TIniFile;
Commentary : TCommentary;
begin
IniFile := TIniFile.Create(ConfigFile);
for Commentary in Self do Commentary.ReadPrivate(IniFile);
IniFile.Free;
end;
destructor TCommentaries.Destroy;
var
Commentary : TCommentary;
begin
SavePrivates;
for Commentary in Self do Commentary.Free;
inherited Destroy;
end;
end.