comparison PostgreSQL/Plugins/PostgreSQLIndex.cpp @ 71:d40c5fecd160 db-changes

new extension implemented for PostgreSQL: CreateInstance
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Jan 2019 13:51:52 +0100
parents 714c5d2bee76
children 8dd29af7c844
comparison
equal deleted inserted replaced
70:e6c13ddd26d9 71:d40c5fecd160
33 33
34 namespace Orthanc 34 namespace Orthanc
35 { 35 {
36 // Some aliases for internal properties 36 // Some aliases for internal properties
37 static const GlobalProperty GlobalProperty_HasTrigramIndex = GlobalProperty_DatabaseInternal0; 37 static const GlobalProperty GlobalProperty_HasTrigramIndex = GlobalProperty_DatabaseInternal0;
38 static const GlobalProperty GlobalProperty_HasCreateInstance = GlobalProperty_DatabaseInternal1;
38 } 39 }
39 40
40 41
41 namespace OrthancDatabases 42 namespace OrthancDatabases
42 { 43 {
124 125
125 { 126 {
126 PostgreSQLTransaction t(*db); 127 PostgreSQLTransaction t(*db);
127 128
128 int hasTrigram = 0; 129 int hasTrigram = 0;
129 if (!LookupGlobalIntegerProperty(hasTrigram, *db, t, Orthanc::GlobalProperty_HasTrigramIndex) || 130 if (!LookupGlobalIntegerProperty(hasTrigram, *db, t,
131 Orthanc::GlobalProperty_HasTrigramIndex) ||
130 hasTrigram != 1) 132 hasTrigram != 1)
131 { 133 {
132 /** 134 /**
133 * Apply fix for performance issue (speed up wildcard search 135 * Apply fix for performance issue (speed up wildcard search
134 * by using GIN trigrams). This implements the patch suggested 136 * by using GIN trigrams). This implements the patch suggested
162 << "PostgreSQL server, e.g. on Debian: sudo apt install postgresql-contrib"; 164 << "PostgreSQL server, e.g. on Debian: sudo apt install postgresql-contrib";
163 } 165 }
164 } 166 }
165 } 167 }
166 168
169 {
170 PostgreSQLTransaction t(*db);
171
172 int hasCreateInstance = 0;
173 if (!LookupGlobalIntegerProperty(hasCreateInstance, *db, t,
174 Orthanc::GlobalProperty_HasCreateInstance) ||
175 hasCreateInstance != 1)
176 {
177 LOG(INFO) << "Installing the CreateInstance extension";
178
179 std::string query;
180 Orthanc::EmbeddedResources::GetFileResource
181 (query, Orthanc::EmbeddedResources::POSTGRESQL_CREATE_INSTANCE);
182 db->Execute(query);
183
184 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasCreateInstance, 1);
185
186 t.Commit();
187 }
188 }
189
167 return db.release(); 190 return db.release();
168 } 191 }
169 192
170 193
171 PostgreSQLIndex::PostgreSQLIndex(const PostgreSQLParameters& parameters) : 194 PostgreSQLIndex::PostgreSQLIndex(const PostgreSQLParameters& parameters) :
193 216
194 statement.Execute(args); 217 statement.Execute(args);
195 218
196 return ReadInteger64(statement, 0); 219 return ReadInteger64(statement, 0);
197 } 220 }
221
222
223 void PostgreSQLIndex::CreateInstance(OrthancPluginCreateInstanceResult& result,
224 const char* hashPatient,
225 const char* hashStudy,
226 const char* hashSeries,
227 const char* hashInstance)
228 {
229 DatabaseManager::CachedStatement statement(
230 STATEMENT_FROM_HERE, GetManager(),
231 "SELECT * FROM CreateInstance(${patient}, ${study}, ${series}, ${instance})");
232
233 statement.SetParameterType("patient", ValueType_Utf8String);
234 statement.SetParameterType("study", ValueType_Utf8String);
235 statement.SetParameterType("series", ValueType_Utf8String);
236 statement.SetParameterType("instance", ValueType_Utf8String);
237
238 Dictionary args;
239 args.SetUtf8Value("patient", hashPatient);
240 args.SetUtf8Value("study", hashStudy);
241 args.SetUtf8Value("series", hashSeries);
242 args.SetUtf8Value("instance", hashInstance);
243
244 statement.Execute(args);
245
246 if (statement.IsDone() ||
247 statement.GetResultFieldsCount() != 8)
248 {
249 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
250 }
251
252 for (size_t i = 0; i < 8; i++)
253 {
254 statement.SetResultFieldType(i, ValueType_Integer64);
255 }
256
257 result.isNewInstance = (ReadInteger64(statement, 3) == 1);
258 result.instanceId = ReadInteger64(statement, 7);
259
260 if (result.isNewInstance)
261 {
262 result.isNewPatient = (ReadInteger64(statement, 0) == 1);
263 result.isNewStudy = (ReadInteger64(statement, 1) == 1);
264 result.isNewSeries = (ReadInteger64(statement, 2) == 1);
265 result.patientId = ReadInteger64(statement, 4);
266 result.studyId = ReadInteger64(statement, 5);
267 result.seriesId = ReadInteger64(statement, 6);
268 }
269 }
198 } 270 }