-
Notifications
You must be signed in to change notification settings - Fork 10
/
unitreference.pas
191 lines (161 loc) · 4.95 KB
/
unitreference.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
unit UnitReference;
interface
uses
Classes, Fgl, SysUtils, UnitModule, UnitBible, UnitUtils, UnitLib;
type
TReferenceArray = array of TVerse;
TReferenceAlias = record
xreferences, book, chapter, verse, xbook, xchapter, xfromverse, xtoverse, votes : string;
end;
TReference = class(TModule)
private
z : TReferenceAlias;
function GetData(Verse: TVerse): TReferenceArray;
public
constructor Create(filePath: string);
end;
TReferences = class(TFPGList<TReference>)
private
procedure Load;
function ReferenceByLanguage(language: string): TReference;
public
constructor Create;
function GetData(Verse: TVerse; language: string; out info: string): TReferenceArray;
function DeleteItem(Item: TReference): boolean;
destructor Destroy; override;
end;
implementation
const
unboundAlias : TReferenceAlias = (
xreferences : 'xreferences';
book : 'book';
chapter : 'chapter';
verse : 'verse';
xbook : 'xbook';
xchapter : 'xchapter';
xfromverse : 'xfromverse';
xtoverse : 'xtoverse';
votes : 'votes';
);
mybibleAlias : TReferenceAlias = (
xreferences : 'cross_references';
book : 'book';
chapter : 'chapter';
verse : 'verse';
// toverse : 'verse_end';
xbook : 'book_to';
xchapter : 'chapter_to';
xfromverse : 'verse_to_start';
xtoverse : 'verse_to_end';
votes : 'votes';
);
//========================================================================================
// TReference
//========================================================================================
constructor TReference.Create(filePath: string);
begin
inherited Create(filePath);
z := unboundAlias;
if format = mybible then z := mybibleAlias;
if connected and not TableExists(z.xreferences ) then connected := false;
end;
function TReference.GetData(Verse: TVerse): TReferenceArray;
var
V : TVerse;
v_from, v_to : string;
id, toverse, votes, 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.xreferences +
' WHERE ' + z.book + ' = ' + ToStr(id) +
' AND ' + z.chapter + ' = ' + ToStr(Verse.chapter) +
' AND (' + z.verse + ' BETWEEN ' + v_from + ' AND ' + v_to + ') ';
Query.Open;
Query.Last;
SetLength(Result, Query.RecordCount);
Query.First;
count := 0;
while not Query.Eof do
try
v.Init;
try v.book := Query.FieldByName(z.xbook ).AsInteger; except end;
try v.chapter := Query.FieldByName(z.xchapter ).AsInteger; except end;
try v.number := Query.FieldByName(z.xfromverse).AsInteger; except end;
try toverse := Query.FieldByName(z.xtoverse ).AsInteger; except end;
try votes := Query.FieldByName(z.votes ).AsInteger; except end;
if votes <= 1 then continue;
v.book := DecodeID(v.book);
if toverse = 0 then v.count := 1
else v.count := toverse - v.number + 1;
Result[count] := v;
count += 1;
finally
Query.Next;
end;
SetLength(Result, count);
except
//
end;
finally
Query.Close;
end;
end;
//=================================================================================================
// TReferences
//=================================================================================================
constructor TReferences.Create;
begin
inherited;
Load;
end;
procedure TReferences.Load;
var
Reference : TReference;
f : string;
begin
for f in DatabaseList do
if f.Contains('.xrefs.') then
begin
Reference := TReference.Create(f);
if Reference.connected then Add(Reference) else Reference.Free;
end;
end;
function TReferences.ReferenceByLanguage(language: string): TReference;
var
Reference : TReference;
begin
Result := nil;
for Reference in Self do
if Reference.language = language then Exit(Reference);
end;
function TReferences.GetData(Verse: TVerse; language: string; out info: string): TReferenceArray;
var
Reference : TReference;
begin
info := '';
Reference := ReferenceByLanguage(language);
if Reference = nil then Reference := ReferenceByLanguage('en');
if Reference = nil then Exit([]);
Result := Reference.GetData(Verse);
info := Reference.info;
end;
function TReferences.DeleteItem(Item: TReference): boolean;
begin
if not Item.Delete then Exit(false);
Item.Free;
Delete(IndexOf(Item));
Exit(true);
end;
destructor TReferences.Destroy;
var
Reference : TReference;
begin
for Reference in Self do Reference.Free;
inherited Destroy;
end;
end.