comparison Framework/Plugins/IndexBackend.cpp @ 262:b0c65094b299

adding support for revisions in attachments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 20 Apr 2021 14:46:42 +0200
parents 34e2b93a7ac1
children 29d2b76516f6
comparison
equal deleted inserted replaced
261:34e2b93a7ac1 262:b0c65094b299
56 56
57 return s; 57 return s;
58 } 58 }
59 59
60 60
61 int64_t IndexBackend::ReadInteger64(const DatabaseManager::StatementBase& statement, 61 static int64_t ReadInteger64(const DatabaseManager::StatementBase& statement,
62 size_t field) 62 size_t field)
63 { 63 {
64 if (statement.IsDone()) 64 if (statement.IsDone())
65 { 65 {
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); 66 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
67 } 67 }
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
79 } 79 }
80 } 80 }
81 81
82 82
83 int32_t IndexBackend::ReadInteger32(const DatabaseManager::StatementBase& statement, 83 static int32_t ReadInteger32(const DatabaseManager::StatementBase& statement,
84 size_t field) 84 size_t field)
85 { 85 {
86 if (statement.IsDone()) 86 if (statement.IsDone())
87 { 87 {
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); 88 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
89 } 89 }
100 return static_cast<int32_t>(value); 100 return static_cast<int32_t>(value);
101 } 101 }
102 } 102 }
103 103
104 104
105 std::string IndexBackend::ReadString(const DatabaseManager::StatementBase& statement, 105 static std::string ReadString(const DatabaseManager::StatementBase& statement,
106 size_t field) 106 size_t field)
107 { 107 {
108 const IValue& value = statement.GetResultField(field); 108 const IValue& value = statement.GetResultField(field);
109 109
110 switch (value.GetType()) 110 switch (value.GetType())
111 { 111 {
121 } 121 }
122 } 122 }
123 123
124 124
125 template <typename T> 125 template <typename T>
126 void IndexBackend::ReadListOfIntegers(std::list<T>& target, 126 static void ReadListOfIntegers(std::list<T>& target,
127 DatabaseManager::CachedStatement& statement, 127 DatabaseManager::CachedStatement& statement,
128 const Dictionary& args) 128 const Dictionary& args)
129 { 129 {
130 statement.Execute(args); 130 statement.Execute(args);
131 131
132 target.clear(); 132 target.clear();
133 133
147 } 147 }
148 } 148 }
149 } 149 }
150 150
151 151
152 void IndexBackend::ReadListOfStrings(std::list<std::string>& target, 152 static void ReadListOfStrings(std::list<std::string>& target,
153 DatabaseManager::CachedStatement& statement, 153 DatabaseManager::CachedStatement& statement,
154 const Dictionary& args) 154 const Dictionary& args)
155 { 155 {
156 statement.Execute(args); 156 statement.Execute(args);
157 157
158 target.clear(); 158 target.clear();
159 159
347 { 347 {
348 return outputFactory_->CreateOutput(); 348 return outputFactory_->CreateOutput();
349 } 349 }
350 } 350 }
351 351
352 352
353 void IndexBackend::AddAttachment(DatabaseManager& manager, 353 static void ExecuteAddAttachment(DatabaseManager::CachedStatement& statement,
354 Dictionary& args,
354 int64_t id, 355 int64_t id,
355 const OrthancPluginAttachment& attachment, 356 const OrthancPluginAttachment& attachment)
356 int64_t revision) 357 {
357 {
358 // TODO - REVISIONS
359
360 DatabaseManager::CachedStatement statement(
361 STATEMENT_FROM_HERE, manager,
362 "INSERT INTO AttachedFiles VALUES(${id}, ${type}, ${uuid}, "
363 "${compressed}, ${uncompressed}, ${compression}, ${hash}, ${hash-compressed})");
364
365 statement.SetParameterType("id", ValueType_Integer64); 358 statement.SetParameterType("id", ValueType_Integer64);
366 statement.SetParameterType("type", ValueType_Integer64); 359 statement.SetParameterType("type", ValueType_Integer64);
367 statement.SetParameterType("uuid", ValueType_Utf8String); 360 statement.SetParameterType("uuid", ValueType_Utf8String);
368 statement.SetParameterType("compressed", ValueType_Integer64); 361 statement.SetParameterType("compressed", ValueType_Integer64);
369 statement.SetParameterType("uncompressed", ValueType_Integer64); 362 statement.SetParameterType("uncompressed", ValueType_Integer64);
370 statement.SetParameterType("compression", ValueType_Integer64); 363 statement.SetParameterType("compression", ValueType_Integer64);
371 statement.SetParameterType("hash", ValueType_Utf8String); 364 statement.SetParameterType("hash", ValueType_Utf8String);
372 statement.SetParameterType("hash-compressed", ValueType_Utf8String); 365 statement.SetParameterType("hash-compressed", ValueType_Utf8String);
373 366
374 Dictionary args;
375 args.SetIntegerValue("id", id); 367 args.SetIntegerValue("id", id);
376 args.SetIntegerValue("type", attachment.contentType); 368 args.SetIntegerValue("type", attachment.contentType);
377 args.SetUtf8Value("uuid", attachment.uuid); 369 args.SetUtf8Value("uuid", attachment.uuid);
378 args.SetIntegerValue("compressed", attachment.compressedSize); 370 args.SetIntegerValue("compressed", attachment.compressedSize);
379 args.SetIntegerValue("uncompressed", attachment.uncompressedSize); 371 args.SetIntegerValue("uncompressed", attachment.uncompressedSize);
380 args.SetIntegerValue("compression", attachment.compressionType); 372 args.SetIntegerValue("compression", attachment.compressionType);
381 args.SetUtf8Value("hash", attachment.uncompressedHash); 373 args.SetUtf8Value("hash", attachment.uncompressedHash);
382 args.SetUtf8Value("hash-compressed", attachment.compressedHash); 374 args.SetUtf8Value("hash-compressed", attachment.compressedHash);
383 375
384 statement.Execute(args); 376 statement.Execute(args);
377 }
378
379
380 void IndexBackend::AddAttachment(DatabaseManager& manager,
381 int64_t id,
382 const OrthancPluginAttachment& attachment,
383 int64_t revision)
384 {
385 if (HasRevisionsSupport())
386 {
387 DatabaseManager::CachedStatement statement(
388 STATEMENT_FROM_HERE, manager,
389 "INSERT INTO AttachedFiles VALUES(${id}, ${type}, ${uuid}, ${compressed}, "
390 "${uncompressed}, ${compression}, ${hash}, ${hash-compressed}, ${revision})");
391
392 Dictionary args;
393
394 statement.SetParameterType("revision", ValueType_Integer64);
395 args.SetIntegerValue("revision", revision);
396
397 ExecuteAddAttachment(statement, args, id, attachment);
398 }
399 else
400 {
401 DatabaseManager::CachedStatement statement(
402 STATEMENT_FROM_HERE, manager,
403 "INSERT INTO AttachedFiles VALUES(${id}, ${type}, ${uuid}, ${compressed}, "
404 "${uncompressed}, ${compression}, ${hash}, ${hash-compressed})");
405
406 Dictionary args;
407 ExecuteAddAttachment(statement, args, id, attachment);
408 }
385 } 409 }
386 410
387 411
388 void IndexBackend::AttachChild(DatabaseManager& manager, 412 void IndexBackend::AttachChild(DatabaseManager& manager,
389 int64_t parent, 413 int64_t parent,
1024 args.SetUtf8Value("date", resource.date); 1048 args.SetUtf8Value("date", resource.date);
1025 1049
1026 statement.Execute(args); 1050 statement.Execute(args);
1027 } 1051 }
1028 1052
1029 1053
1030 /* Use GetOutput().AnswerAttachment() */ 1054 static bool ExecuteLookupAttachment(DatabaseManager::CachedStatement& statement,
1031 bool IndexBackend::LookupAttachment(IDatabaseBackendOutput& output, 1055 IDatabaseBackendOutput& output,
1032 int64_t& revision /*out*/,
1033 DatabaseManager& manager,
1034 int64_t id, 1056 int64_t id,
1035 int32_t contentType) 1057 int32_t contentType)
1036 { 1058 {
1037 DatabaseManager::CachedStatement statement(
1038 STATEMENT_FROM_HERE, manager,
1039 "SELECT uuid, uncompressedSize, compressionType, compressedSize, "
1040 "uncompressedHash, compressedHash FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
1041
1042 statement.SetReadOnly(true); 1059 statement.SetReadOnly(true);
1043 statement.SetParameterType("id", ValueType_Integer64); 1060 statement.SetParameterType("id", ValueType_Integer64);
1044 statement.SetParameterType("type", ValueType_Integer64); 1061 statement.SetParameterType("type", ValueType_Integer64);
1045 1062
1046 Dictionary args; 1063 Dictionary args;
1060 ReadInteger64(statement, 1), 1077 ReadInteger64(statement, 1),
1061 ReadString(statement, 4), 1078 ReadString(statement, 4),
1062 ReadInteger32(statement, 2), 1079 ReadInteger32(statement, 2),
1063 ReadInteger64(statement, 3), 1080 ReadInteger64(statement, 3),
1064 ReadString(statement, 5)); 1081 ReadString(statement, 5));
1065
1066 revision = 0; // TODO - REVISIONS
1067
1068 return true; 1082 return true;
1083 }
1084 }
1085
1086
1087
1088 /* Use GetOutput().AnswerAttachment() */
1089 bool IndexBackend::LookupAttachment(IDatabaseBackendOutput& output,
1090 int64_t& revision /*out*/,
1091 DatabaseManager& manager,
1092 int64_t id,
1093 int32_t contentType)
1094 {
1095 if (HasRevisionsSupport())
1096 {
1097 DatabaseManager::CachedStatement statement(
1098 STATEMENT_FROM_HERE, manager,
1099 "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedHash, "
1100 "compressedHash, revision FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
1101
1102 if (ExecuteLookupAttachment(statement, output, id, contentType))
1103 {
1104 revision = ReadInteger64(statement, 6);
1105 return true;
1106 }
1107 else
1108 {
1109 return false;
1110 }
1111 }
1112 else
1113 {
1114 DatabaseManager::CachedStatement statement(
1115 STATEMENT_FROM_HERE, manager,
1116 "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedHash, "
1117 "compressedHash FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
1118
1119 revision = 0;
1120
1121 return ExecuteLookupAttachment(statement, output, id, contentType);
1069 } 1122 }
1070 } 1123 }
1071 1124
1072 1125
1073 static bool ReadGlobalProperty(std::string& target, 1126 static bool ReadGlobalProperty(std::string& target,