-
Notifications
You must be signed in to change notification settings - Fork 5
/
theworddictionary.pas
299 lines (254 loc) · 8.46 KB
/
theworddictionary.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
unit TheWordDictionary;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StrUtils, sqlite3conn, sqldb, PCRE, gmap, gutil;
type
{ TTheWordDictionary }
TTheWordDictionary = class
private
FAbbrev: string;
FAbout: string;
FAuthor: string;
FConnection: TSQLite3Connection;
FDescription: string;
FEnglishDescription: string;
FID: string;
FTitleEnglish: string;
FTopicID: integer;
FInsertConfig: TSQLQuery;
FInsertTopic: TSQLQuery;
FInsertContent: TSQLQuery;
FLanguage: string;
FMorph: boolean;
FStrong: boolean;
FTitle: string;
FVersionDate: string;
FVersionMajor: string;
FVersionMinor: string;
function CreateQuery(statement: string): TSQLQuery;
procedure SetAbbrev(AValue: string);
procedure SetAbout(AValue: string);
procedure SetAuthor(AValue: string);
procedure SetDescription(AValue: string);
procedure SetEnglishDescription(AValue: string);
procedure SetLanguage(AValue: string);
procedure SetStrong(AValue: boolean);
procedure SetID(AValue: string);
procedure SetMorph(AValue: boolean);
procedure SetProperty(name: string; value: string);
procedure SetTitle(AValue: string);
procedure SetTitleEnglish(AValue: string);
procedure SetVersionDate(AValue: string);
procedure SetVersionMajor(AValue: string);
procedure SetVersionMinor(AValue: string);
function CreateTopic(topic: string; lemma: string): integer;
procedure CreateContent(topic_id: integer; content: string);
function GetCurrentTopicID: integer;
function GetNextTopicID: integer;
public
constructor Create(filename: string; strongs: boolean);
destructor Destroy; override;
procedure AddEntry(topic: string; lemma: string; content: string);
procedure Commit;
property Abbrev: string read FAbbrev write SetAbbrev;
property Title: string read FTitle write SetTitle;
property TitleEnglish: string read FTitleEnglish write SetTitleEnglish;
property Description: string read FDescription write SetDescription;
property EnglishDescription: string read FEnglishDescription write SetEnglishDescription;
property About: string read FAbout write SetAbout;
property ID: string read FID write SetID;
property Strong: boolean read FStrong write SetStrong;
property Morph: boolean read FMorph write SetMorph;
property VersionMajor: string read FVersionMajor write SetVersionMajor;
property VersionMinor: string read FVersionMinor write SetVersionMinor;
property VersionDate: string read FVersionDate write SetVersionDate;
property Author: string read FAuthor write SetAuthor;
property Language: string read FLanguage write SetLanguage;
end;
resourcestring
SCreateTheWordDictionaryError = 'Unable to create new TheWord module file';
implementation
{ TTheWordDictionary }
function TTheWordDictionary.CreateQuery(statement: string): TSQLQuery;
begin
result := TSQLQuery.Create(nil);
result.DataBase := FConnection;
result.SQL.Text := statement;
result.Prepare;
end;
procedure TTheWordDictionary.SetAbbrev(AValue: string);
begin
if FAbbrev=AValue then Exit;
FAbbrev:=AValue;
SetProperty('abbrev', AValue);
end;
procedure TTheWordDictionary.SetAbout(AValue: string);
begin
if FAbout=AValue then Exit;
FAbout:=AValue;
SetProperty('about', AValue);
end;
procedure TTheWordDictionary.SetAuthor(AValue: string);
begin
if FAuthor=AValue then Exit;
FAuthor:=AValue;
SetProperty('author', AValue);
end;
procedure TTheWordDictionary.SetDescription(AValue: string);
begin
if FDescription=AValue then Exit;
FDescription:=AValue;
SetProperty('description', AValue);
end;
procedure TTheWordDictionary.SetEnglishDescription(AValue: string);
begin
if FEnglishDescription=AValue then Exit;
FEnglishDescription:=AValue;
SetProperty('description.english', AValue);
end;
procedure TTheWordDictionary.SetLanguage(AValue: string);
begin
if FLanguage=AValue then Exit;
FLanguage:=AValue;
SetProperty('lang', AValue);
end;
procedure TTheWordDictionary.SetStrong(AValue: boolean);
begin
if FStrong=AValue then Exit;
FStrong:=AValue;
SetProperty('strong', IfThen(AValue, '1', '0'));
end;
procedure TTheWordDictionary.SetID(AValue: string);
begin
if FID=AValue then Exit;
FID:=AValue;
SetProperty('id', AValue);
end;
procedure TTheWordDictionary.SetMorph(AValue: boolean);
begin
if FMorph=AValue then Exit;
FMorph:=AValue;
SetProperty('morph', IfThen(AValue, '1', '0'));
end;
procedure TTheWordDictionary.SetProperty(name: string; value: string);
begin
FInsertConfig.Params.ParamByName('name').AsString := name;
FInsertConfig.Params.ParamByName('value').AsString := value;
FInsertConfig.ExecSQL;
end;
procedure TTheWordDictionary.SetTitle(AValue: string);
begin
if FTitle=AValue then Exit;
FTitle:=AValue;
SetProperty('title', AValue);
end;
procedure TTheWordDictionary.SetTitleEnglish(AValue: string);
begin
if FTitleEnglish=AValue then Exit;
FTitleEnglish:=AValue;
SetProperty('title.english', AValue);
end;
procedure TTheWordDictionary.SetVersionDate(AValue: string);
begin
if FVersionDate=AValue then Exit;
FVersionDate:=AValue;
SetProperty('version.date', AValue);
end;
procedure TTheWordDictionary.SetVersionMajor(AValue: string);
begin
if FVersionMajor=AValue then Exit;
FVersionMajor:=AValue;
SetProperty('version.major', AValue);
end;
procedure TTheWordDictionary.SetVersionMinor(AValue: string);
begin
if FVersionMinor=AValue then Exit;
FVersionMinor:=AValue;
SetProperty('version.minor', AValue);
end;
function TTheWordDictionary.CreateTopic(topic: string; lemma: string): integer;
begin
FInsertTopic.Params.ParamByName('id').AsInteger := GetNextTopicID;
FInsertTopic.Params.ParamByName('subject').AsString := topic;
FInsertTopic.Params.ParamByName('order').AsInteger := GetCurrentTopicID;
FInsertTopic.Params.ParamByName('lemma').AsString := lemma;
FInsertTopic.ExecSQL;
result := GetCurrentTopicID;
end;
procedure TTheWordDictionary.CreateContent(topic_id: integer; content: string);
begin
FInsertContent.Params.ParamByName('topic_id').AsInteger := topic_id;
FInsertContent.Params.ParamByName('data').AsString := content;
FInsertContent.ExecSQL;
end;
function TTheWordDictionary.GetCurrentTopicID: integer;
begin
result := FTopicID;
end;
function TTheWordDictionary.GetNextTopicID: integer;
begin
inc(FTopicID);
result := FTopicID;
end;
constructor TTheWordDictionary.Create(filename: string; strongs: boolean);
var
existed: boolean;
begin
existed := FileExists(filename);
FConnection := TSQLite3Connection.Create(nil);
FConnection.DatabaseName := filename;
FConnection.Transaction := TSQLTransaction.Create(nil);
FConnection.StartTransaction;
try
FConnection.Open;
FConnection.Transaction.Active := true;
if existed then
begin
FConnection.ExecuteDirect('DELETE FROM config');
FConnection.ExecuteDirect('DELETE FROM content');
FConnection.ExecuteDirect('DELETE FROM topics');
FConnection.ExecuteDirect('DROP TABLE IF EXISTS topics_wordindex');
end
else
begin
FConnection.ExecuteDirect('CREATE TABLE config(name text, value text)');
FConnection.ExecuteDirect('CREATE TABLE content(topic_id integer primary key, data text, data2 blob)');
FConnection.ExecuteDirect('CREATE TABLE topics(id integer primary key, pid integer default 0, subject text, rel_order, content_type text, strong_orig_word text)');
FConnection.ExecuteDirect('CREATE INDEX idx_topics_subject on topics(subject)');
end;
FInsertConfig := CreateQuery('INSERT INTO config VALUES (:name, :value)');
FInsertContent := CreateQuery('INSERT INTO content VALUES (:topic_id, :data, NULL)');
FInsertTopic := CreateQuery('INSERT INTO topics VALUES (:id, 0, :subject, :order, NULL, :lemma)');
SetProperty('keywords', 'Strongs');
SetProperty('type', '1');
SetProperty('content.type', 'rtf');
SetProperty('schema.version', '1');
SetProperty('strong', '1');
except
raise Exception.Create(SCreateTheWordDictionaryError);
end;
FTopicID := 0;
end;
destructor TTheWordDictionary.Destroy;
begin
inherited Destroy;
FInsertConfig.Free;
FInsertContent.Free;
FInsertTopic.Free;
FConnection.Transaction.Free;
FConnection.Close;
FConnection.Free;
end;
procedure TTheWordDictionary.AddEntry(topic: string; lemma: string;
content: string);
begin
if content.IsEmpty then
exit;
CreateContent(CreateTopic(topic, lemma), content);
end;
procedure TTheWordDictionary.Commit;
begin
FConnection.Transaction.Commit;
end;
end.