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
300
301
302
303
304
|
{
This file is part of the Free Component Library (FCL)
Copyright (c) 2012 by the Free Pascal development team
SQLDB-based index database
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit SQLDBIndexDB;
{$mode objfpc}{$H+}
interface
uses
SysUtils, fpIndexer, sqldb, db;
// SQLDB Specific, cache query objects
type
TCachedQueryType = (cqtInsertWord, cqtGetWordID, cqtInsertFile, cqtGetFileID,
cqtInsertLanguage, cqtGetLanguageID, cqtInsertMatch);
// Interbase specific
const
{$note @MvC, TIndexTable is defined as: itWords, itLanguages, itFiles, itMatches, below order seems to be wrong}
DefaultGeneratorNames: array[TIndexTable] of string = ('GEN_WORDS', 'GEN_MATCHES', 'GEN_LANGUAGES', 'GEN_FILES');
type
{ TSQLDBIndexDB }
TSQLDBIndexDB = class(TSQLIndexDB)
private
// SQLDB specific
db: TSQLConnection;
FLastURLID: int64;
FLastURL: string;
FLastLanguageID: int64;
FLastLanguage: string;
FLastWordID: int64;
FLastWord: string;
FQueries: array [TCachedQueryType] of TSQLQuery;
protected
// SQLDB Specific statements
procedure Execute(const sql: string; ignoreErrors: boolean = True); override;
function GetLanguageID(const ALanguage: string): int64;
function GetWordID(const AWord: string): int64;
function GetURLID(const URL: string; ATimeStamp: TDateTime; ALanguageID: int64; DoCreate: boolean = True): int64; override;
function CreateQuery(const ASQL: string): TSQLQuery;
function CreateCachedQuery(QueryType: TCachedQueryType; const ASQL: string): TSQLQuery;
// Connection specific, need to be overridden
function GetConnection: TSQLConnection; virtual; abstract;
procedure InsertMatch(AWordID, aFileID, aLanguageID: int64; const ASearchData: TSearchWordData); virtual; abstract;
function InsertWord(const AWord: string): int64; virtual; abstract;
function InsertURL(const URL: string; ATimeStamp: TDateTime; ALanguageID: int64): int64; virtual; abstract;
function InsertLanguage(const ALanguage: string): int64; virtual; abstract;
public
destructor Destroy; override;
procedure Connect; override;
procedure Disconnect; override;
procedure CreateDB; override;
procedure BeginTrans; override;
procedure CommitTrans; override;
procedure CompactDB; override;
procedure AddSearchData(ASearchData: TSearchWordData); override;
procedure FindSearchData(SearchWord: TWordParser; FPSearch: TFPSearch; SearchOptions: TSearchOptions); override;
procedure DeleteWordsFromFile(URL: string); override;
end;
implementation
{ TSQLDBIndexDB }
function TSQLDBIndexDB.GetLanguageID(const ALanguage: string): int64;
var
Q: TSQLQuery;
begin
if SameFileName(FLastLanguage, ALanguage) then
Result := FLastLanguageID
else
begin
Q := CreateCachedQuery(cqtGetLanguageID, GetLanguageSQL);
Q.ParamByName(GetFieldName(ifLanguagesName)).AsString := ALanguage;
Q.Open;
try
if (Q.EOF and Q.BOF) then
Result := InsertLanguage(ALanguage)
else
Result := Q.FieldByName(GetFieldName(ifLanguagesID)).AsLargeInt;
FLastLanguageID := Result;
FLastLanguage := ALanguage;
finally
Q.Close;
end;
end;
end;
function TSQLDBIndexDB.GetWordID(const AWord: string): int64;
var
Q: TSQLQuery;
begin
if (FLastWord = AWord) then
Result := FLastWordID
else
begin
Q := CreateCachedQuery(cqtGetWordID, GetWordSQL);
Q.ParamByName(GetFieldName(ifWordsWord)).AsString := AWord;
Q.Open;
try
if (Q.EOF and Q.BOF) then
Result := InsertWord(AWord)
else
Result := Q.FieldByName(GetFieldName(ifWordsID)).AsLargeInt;
FLastWordID := Result;
FLastWord := AWord;
finally
Q.Close;
end;
end;
end;
function TSQLDBIndexDB.CreateQuery(const ASQL: string): TSQLQuery;
begin
Result := TSQLQuery.Create(Self);
Result.Database := Self.db;
Result.Transaction := Self.db.Transaction;
Result.SQL.Text := ASQL;
//Writeln('SQL :',ASQL);
end;
function TSQLDBIndexDB.GetURLID(const URL: string; ATimeStamp: TDateTime; ALanguageID: int64; DoCreate: boolean = True): int64;
var
Q: TSQLQuery;
begin
if SameFileName(FLastURL, URL) then
Result := FLastURLID
else
begin
Q := CreateCachedQuery(cqtGetFileID, GetSearchFileSQL);
If Length(URL)>255 then
Writeln('URL Length : ',Length(URL),' : ',URL);
Q.ParamByName(GetFieldName(ifFilesURL)).AsString := URL;
Q.Open;
try
if (Q.EOF and Q.BOF) then
begin
if DoCreate then
Result := InsertURL(URL, ATimeStamp, ALanguageID)
else
Result := -1;
end
else
Result := Q.FieldByName(GetFieldName(ifFilesID)).AsLargeInt;
FLastURLID := Result;
FLastURL := URL;
finally
Q.Close;
end;
end;
end;
function TSQLDBIndexDB.CreateCachedQuery(QueryType: TCachedQueryType; const ASQL: string): TSQLQuery;
begin
if FQueries[QueryType] = nil then
begin
FQueries[QueryType] := CreateQuery(ASQL);
FQueries[QueryType].Prepare;
end;
Result := FQueries[QueryType];
end;
procedure TSQLDBIndexDB.AddSearchData(ASearchData: TSearchWordData);
var
WID, FID, LID: int64;
begin
//check if the SearchWord already is in the list
LID := GetLanguageID(ASearchData.Language);
FID := GetURLID(ASearchData.URL, ASearchData.FileDate, LID);
WID := GetWordID(ASearchData.SearchWord);
InsertMatch(WID, FID, LID, ASearchData);
end;
procedure TSQLDBIndexDB.FindSearchData(SearchWord: TWordParser; FPSearch: TFPSearch; SearchOptions: TSearchOptions);
var
Q: TSQLQuery;
FN, FP, FD, FW, FC: TField;
Res: TSearchWordData;
S,WW : String;
I,L : Integer;
begin
Q := CreateQuery(GetMatchSQL(SearchOptions,SearchWord,True));
try
Writeln(Q.SQL.Text);
WW := getFieldName(ifWordsWord);
for i := 0 to SearchWord.Count - 1 do
If SearchWord.Token[i].TokenType=wtWord then
begin
S:=SearchWord.Token[i].Value;
if (Length(S)>0) and (S[1]='''') then
Delete(S,1,1);
L:=Length(S);
if (l>0) and (S[l]='''') then
Delete(S,l,1);
if (soContains in Searchoptions) then
S:='%'+S+'%';
Q.ParamByName(WW+IntToStr(i)).AsString:=S;
end;
Q.Open;
FN := Q.FieldByName(GetFieldName(ifFilesURL));
FD := Q.FieldByName(GetFieldName(ifFilesTimeStamp));
FC := Q.FieldByName(GetFieldName(ifMatchesContext));
FP := Q.FieldByName(GetFieldName(ifMatchesPosition));
FW := Q.FieldByName(GetFieldName(ifWordsWord));
while not Q.EOF do
begin
Res.FileDate := FD.AsDateTime;
Res.URL := FN.AsString;
Res.SearchWord := FW.AsString;
Res.Position := FP.AsInteger;
Res.Context:=FC.aSString;
FPSearch.AddResult(Q.RecNo, Res);
Q.Next;
end;
finally
Q.Free;
end;
end;
procedure TSQLDBIndexDB.DeleteWordsFromFile(URL: string);
begin
inherited DeleteWordsFromFile(URL);
FLastURL := '';
end;
procedure TSQLDBIndexDB.Execute(const sql: string; ignoreErrors: boolean = True);
begin
if SQL = '' then
exit;
try
DB.ExecuteDirect(sql);
except
if not IgnoreErrors then
raise;
end;
end;
procedure TSQLDBIndexDB.Connect;
begin
if (DB = nil) then
db := GetConnection;
if DB.Transaction = nil then
DB.Transaction := TSQLTransaction.Create(db);
DB.Connected := True;
end;
procedure TSQLDBIndexDB.Disconnect;
Var
T : TCachedQueryType;
begin
For T:=Low(TCachedQueryType) to High(TCachedQueryType) do
FreeAndNil(FQueries[T]);
FreeAndNil(DB);
end;
procedure TSQLDBIndexDB.CreateDB;
begin
if DB = nil then
DB := GetConnection;
DB.CreateDB;
Connect;
CreateIndexerTables;
end;
destructor TSQLDBIndexDB.Destroy;
begin
Disconnect;
inherited Destroy;
end;
procedure TSQLDBIndexDB.BeginTrans;
begin
DB.Transaction.StartTransaction;
end;
procedure TSQLDBIndexDB.CommitTrans;
begin
DB.Transaction.Commit;
end;
procedure TSQLDBIndexDB.CompactDB;
begin
//not yet implemented
end;
end.
|