Skip to content

Commit 10ad008

Browse files
author
Grahame Grieve
committed
finish rewriting LOINC module
1 parent 805c625 commit 10ad008

File tree

4 files changed

+357
-817
lines changed

4 files changed

+357
-817
lines changed

library/ftx/ftx_loinc_importer.pas

+23-47
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,7 @@
3939
ftx_loinc_services, ftx_service;
4040

4141

42-
const
43-
KEY_INCREMENT = 10;
44-
4542
type
46-
47-
{ TKeySet }
48-
49-
TKeySet = class (TFslObject)
50-
private
51-
FKeys : TKeyArray;
52-
FCount : integer;
53-
public
54-
procedure addKey(key : integer);
55-
procedure getKeys(b : TFslStringBuilder);
56-
end;
5743

5844
{ TCodeInformation }
5945
TCodeInformation = class (TFslObject)
@@ -110,7 +96,7 @@ TLoincImporter = class (TFslObject)
11096
FPropValues : TDictionary<String, Integer>;
11197

11298
// working items
113-
conn, dbCodes, dbRels, dbDesc, dbProps: TFDBConnection;
99+
conn, dbCodes, dbRels, dbDesc, dbProps, dbText: TFDBConnection;
114100
codes : TCodeMap;
115101
codeList : TFslList<TCodeInformation>;
116102
rels, langs, statii, dTypes, props : TKeyMap;
@@ -225,6 +211,7 @@ procedure TLoincImporter.ImportLOINC;
225211
dbRels := db.GetConnection('relationships');
226212
dbDesc := db.GetConnection('descriptions');
227213
dbProps := db.GetConnection('properties');
214+
dbText := db.GetConnection('text');
228215
try
229216
CreateTables(1);
230217

@@ -236,6 +223,8 @@ procedure TLoincImporter.ImportLOINC;
236223
dbDesc.prepare;
237224
dbProps.sql := 'Insert into Properties (PropertyKey, PropertyTypeKey, CodeKey, PropertyValueKey) values (:pk, :ptk, :ck, :v)';
238225
dbProps.prepare;
226+
dbText.sql := 'Insert into TextIndex (CodeKey, Type, Lang, Text) values (:ck, :tk, :lk, :t)';
227+
dbText.prepare;
239228

240229
ProcessLanguageVariants(2, st);
241230
FStepCount := 12 + st.count;
@@ -255,11 +244,13 @@ procedure TLoincImporter.ImportLOINC;
255244
dbRels.terminate;
256245
dbDesc.terminate;
257246
dbProps.terminate;
247+
dbText.terminate;
258248

259249
dbCodes.Release;
260250
dbRels.Release;
261251
dbDesc.terminate;
262252
dbProps.terminate;
253+
dbText.terminate;
263254
conn.Release;
264255
except
265256
on e : Exception do
@@ -430,6 +421,9 @@ procedure TLoincImporter.CreateTables(step: integer);
430421
'PRIMARY KEY (`AncestorKey`, `DescendentKey`))';
431422
conn.ExecSQL(sql);
432423

424+
sql := 'CREATE VIRTUAL TABLE TextIndex USING fts5(codekey UNINDEXED, type UNINDEXED, lang UNINDEXED, text)';
425+
conn.ExecSQL(sql);
426+
433427
addEntry('Types', 'TypeKey', 'Code', nil, 'Code', 1);
434428
addEntry('Types', 'TypeKey', 'Code', nil, 'Part', 2);
435429
addEntry('Types', 'TypeKey', 'Code', nil, 'AnswerList', 3);
@@ -509,7 +503,8 @@ procedure TLoincImporter.CreateTables(step: integer);
509503
addEntry('PropertyTypes', 'PropertyTypeKey', 'Description', props, 'AskAtOrderEntry', 6);
510504
addEntry('PropertyTypes', 'PropertyTypeKey', 'Description', props, 'UNITSREQUIRED', 7);
511505
addEntry('PropertyTypes', 'PropertyTypeKey', 'Description', props, 'CLASS', 8);
512-
506+
addEntry('PropertyTypes', 'PropertyTypeKey', 'Description', props, 'Copyright', 9);
507+
513508
conn.ExecSQL('Insert into Languages (LanguageKey, Code, Description) values (1, ''en-US'', ''English (United States)'')');
514509
langs.addKey('en-US', 1);
515510
end;
@@ -528,6 +523,12 @@ procedure TLoincImporter.ProcessDescription(codeKey, languageKey, descriptionTyp
528523
dbDesc.BindInteger('tk', descriptionType);
529524
dbDesc.BindString('v', value);
530525
dbDesc.Execute;
526+
527+
dbText.BindInteger('ck', codeKey);
528+
dbText.BindInteger('tk', descriptionType);
529+
dbDesc.BindInteger('lk', languageKey);
530+
dbText.BindString('t', value);
531+
dbText.Execute;
531532
end;
532533
end;
533534

@@ -729,6 +730,7 @@ procedure TLoincImporter.ProcessCodeItems(items: TStringArray);
729730
ProcessProperty(ck, props.getKey('PanelType'), items[34]);
730731
ProcessProperty(ck, props.getKey('AskAtOrderEntry'), items[35]);
731732
ProcessProperty(ck, props.getKey('UNITSREQUIRED'), items[18]);
733+
ProcessProperty(ck, props.getKey('Copyright'), items[23]);
732734

733735

734736
ProcessDescription(ck, 1, dTypes.getKey('LONG_COMMON_NAME'), d);
@@ -1090,7 +1092,9 @@ procedure TLoincImporter.ProcessHierarchyItems(items : TStringArray);
10901092
dbCodes.execute;
10911093
ciC := codes.addCode(c, ck, codeList);
10921094
end;
1093-
if items[2] <> '' then
1095+
if items[2] = '' then
1096+
conn.ExecSQL('Insert into Config (ConfigKey, Value) values (3, '''+items[3]+''')')
1097+
else
10941098
begin
10951099
ciP := codes.GetCode(items[2]);
10961100

@@ -1148,12 +1152,12 @@ procedure TLoincImporter.StoreClosureTable(step: integer);
11481152
for ci in codeList do
11491153
begin
11501154
inc(count);
1151-
if count mod 1000 = 0 then
1155+
if count mod 10 = 0 then
11521156
Progress(step, count / codeList.count, 'Storing Closure Table: '+pct(count, codeList.count));
11531157
if (ci.FChildren <> nil) then
11541158
begin
11551159
conn.BindInteger('a', ci.FKey);
1156-
for k in ci.FChildren.FKeys do
1160+
for k in ci.FChildren.Keys do
11571161
begin
11581162
conn.BindInteger('d', k);
11591163
conn.Execute;
@@ -1185,34 +1189,6 @@ function TKeyMap.getKey(code: String): integer;
11851189
raise EFslException.create(code+' not found in '+FName+' Table');
11861190
end;
11871191

1188-
{ TKeySet }
1189-
1190-
procedure TKeySet.addKey(key: integer);
1191-
var
1192-
i : integer;
1193-
begin
1194-
for i := low(FKeys) to High(FKeys) do
1195-
if FKeys[i] = key then
1196-
exit;
1197-
if (FCount = length(FKeys)) then
1198-
SetLength(FKeys, length(FKeys) + KEY_INCREMENT);
1199-
FKeys[FCount] := key;
1200-
inc(FCount);
1201-
end;
1202-
1203-
procedure TKeySet.getKeys(b : TFslStringBuilder);
1204-
var
1205-
i : integer;
1206-
begin
1207-
b.clear;
1208-
for i := 0 to FCount - 1 do
1209-
begin
1210-
if i > 0 then
1211-
b.append(',');
1212-
b.append(inttostr(FKeys[i]));
1213-
end;
1214-
end;
1215-
12161192
{ TCodeInformation }
12171193

12181194
constructor TCodeInformation.create;

0 commit comments

Comments
 (0)