-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathDBTableRepository.cs
More file actions
219 lines (188 loc) · 8.04 KB
/
DBTableRepository.cs
File metadata and controls
219 lines (188 loc) · 8.04 KB
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
/*
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is "DBTableRepository.java". Description:
"Implements TableRepository by looking up values from the default HL7
normative database"
The Initial Developer of the Original Code is University Health Network. Copyright (C)
2001. All Rights Reserved.
Contributor(s): ______________________________________.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License (the "GPL"), in which case the provisions of the GPL are
applicable instead of those above. If you wish to allow use of your version of this
file only under the terms of the GPL and not to allow others to use your version
of this file under the MPL, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by the GPL License.
If you do not delete the provisions above, a recipient may use your version of
this file under either the MPL or the GPL.
*/
namespace NHapi.SourceGeneration
{
using System;
using System.Collections;
using System.Data.Common;
using System.Text;
using NHapi.Base.Log;
/// <summary>
/// Implements TableRepository by looking up values from the default HL7
/// normative database. Values are cached after they are looked up once.
/// </summary>
/// <author>Bryan Tripp (bryan_tripp@sourceforge.net).</author>
public class DBTableRepository : TableRepository
{
private readonly Hashtable tables;
private readonly int bufferSize = 3000; // max # of tables or values that can be cached at a time
private int[] tableList;
static DBTableRepository()
{
_ = HapiLogFactory.GetHapiLog(typeof(DBTableRepository));
}
/// <summary>
/// Table repository.
/// </summary>
protected internal DBTableRepository()
{
tableList = null;
tables = new Hashtable();
}
/// <summary> Returns a list of HL7 lookup tables that are defined in the normative database. </summary>
public override int[] Tables
{
get
{
if (tableList == null)
{
try
{
var conn = NormativeDatabase.Instance.Connection;
var stmt = TransactionManager.Manager.CreateStatement(conn);
DbCommand temp_OleDbCommand;
temp_OleDbCommand = stmt;
temp_OleDbCommand.CommandText = "select distinct table_id from TableValues";
var rs = temp_OleDbCommand.ExecuteReader();
var roomyList = new int[bufferSize];
var c = 0;
while (rs.Read())
{
roomyList[c++] = rs.GetInt32(1 - 1);
}
stmt.Dispose();
NormativeDatabase.Instance.ReturnConnection(conn);
tableList = new int[c];
Array.Copy(roomyList, 0, tableList, 0, c);
}
catch (DbException sqle)
{
throw new LookupException("Can't get table list from database: " + sqle.Message);
}
}
return tableList;
}
}
/// <summary> Returns true if the given value exists in the given table.</summary>
public override bool CheckValue(int table, string value_Renamed)
{
var exists = false;
var values = GetValues(table);
var c = 0;
while (c < values.Length && !exists)
{
if (value_Renamed.Equals(values[c++]))
{
exists = true;
}
}
return exists;
}
/// <summary> Returns a list of the values for the given table in the normative database. </summary>
public override string[] GetValues(int table)
{
var key = table;
// see if the value list exists in the cache
var o = tables[key];
string[] values;
if (o != null)
{
values = (string[])o;
}
else
{
// not cached yet ...
int c;
var roomyValues = new string[bufferSize];
try
{
var conn = NormativeDatabase.Instance.Connection;
var stmt = TransactionManager.Manager.CreateStatement(conn);
var sql = new StringBuilder("select table_value from TableValues where table_id = ");
sql.Append(table);
DbCommand temp_OleDbCommand;
temp_OleDbCommand = stmt;
temp_OleDbCommand.CommandText = sql.ToString();
var rs = temp_OleDbCommand.ExecuteReader();
c = 0;
while (rs.Read())
{
roomyValues[c++] = Convert.ToString(rs[1 - 1]);
}
stmt.Dispose();
NormativeDatabase.Instance.ReturnConnection(conn);
}
catch (DbException sqle)
{
throw new LookupException("Couldn't look up values for table " + table + ": " + sqle.Message);
}
if (c == 0)
{
throw new UndefinedTableException("No values found for table " + table);
}
values = new string[c];
Array.Copy(roomyValues, 0, values, 0, c);
tables[key] = values;
}
return values;
}
/// <summary> Returns the description matching the table and value given. As currently implemented
/// this method performs a database call each time - caching should probably be added,
/// although this method will probably not be used very often.
/// </summary>
public override string GetDescription(int table, string value_Renamed)
{
var sql = new StringBuilder("select Description from TableValues where table_id = ");
sql.Append(table);
sql.Append(" and table_value = '");
sql.Append(value_Renamed);
sql.Append("'");
string description;
try
{
var conn = NormativeDatabase.Instance.Connection;
var stmt = TransactionManager.Manager.CreateStatement(conn);
DbCommand temp_OleDbCommand;
temp_OleDbCommand = stmt;
temp_OleDbCommand.CommandText = sql.ToString();
var rs = temp_OleDbCommand.ExecuteReader();
if (rs.Read())
{
description = Convert.ToString(rs[1 - 1]);
}
else
{
throw new UnknownValueException("The value " + value_Renamed + " could not be found in the table " + table +
" - SQL: " + sql.ToString());
}
stmt.Dispose();
NormativeDatabase.Instance.ReturnConnection(conn);
}
catch (DbException e)
{
throw new LookupException("Can't find value " + value_Renamed + " in table " + table, e);
}
return description;
}
}
}