comparison OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp @ 5080:d7274e43ea7c attach-custom-data

allow plugins to store a customData in the Attachments table to e.g. store custom paths without requiring an external DB
author Alain Mazy <am@osimis.io>
date Thu, 08 Sep 2022 17:42:08 +0200
parents e6f26be401fa
children 9770d537880d
comparison
equal deleted inserted replaced
5079:4366b4c41441 5080:d7274e43ea7c
322 virtual void AddAttachment(int64_t id, 322 virtual void AddAttachment(int64_t id,
323 const FileInfo& attachment, 323 const FileInfo& attachment,
324 int64_t revision) ORTHANC_OVERRIDE 324 int64_t revision) ORTHANC_OVERRIDE
325 { 325 {
326 // TODO - REVISIONS 326 // TODO - REVISIONS
327 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); 327 SQLite::Statement s(db_, SQLITE_FROM_HERE,
328 "INSERT INTO AttachedFiles (id, fileType, uuid, compressedSize, uncompressedSize, compressionType, uncompressedMD5, compressedMD5, customData) "
329 "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)");
328 s.BindInt64(0, id); 330 s.BindInt64(0, id);
329 s.BindInt(1, attachment.GetContentType()); 331 s.BindInt(1, attachment.GetContentType());
330 s.BindString(2, attachment.GetUuid()); 332 s.BindString(2, attachment.GetUuid());
331 s.BindInt64(3, attachment.GetCompressedSize()); 333 s.BindInt64(3, attachment.GetCompressedSize());
332 s.BindInt64(4, attachment.GetUncompressedSize()); 334 s.BindInt64(4, attachment.GetUncompressedSize());
333 s.BindInt(5, attachment.GetCompressionType()); 335 s.BindInt(5, attachment.GetCompressionType());
334 s.BindString(6, attachment.GetUncompressedMD5()); 336 s.BindString(6, attachment.GetUncompressedMD5());
335 s.BindString(7, attachment.GetCompressedMD5()); 337 s.BindString(7, attachment.GetCompressedMD5());
338 s.BindString(8, attachment.GetCustomData());
336 s.Run(); 339 s.Run();
337 } 340 }
338
339 341
340 virtual void ApplyLookupResources(std::list<std::string>& resourcesId, 342 virtual void ApplyLookupResources(std::list<std::string>& resourcesId,
341 std::list<std::string>* instancesId, 343 std::list<std::string>* instancesId,
342 const std::vector<DatabaseConstraint>& lookup, 344 const std::vector<DatabaseConstraint>& lookup,
343 ResourceType queryLevel, 345 ResourceType queryLevel,
799 int64_t id, 801 int64_t id,
800 FileContentType contentType) ORTHANC_OVERRIDE 802 FileContentType contentType) ORTHANC_OVERRIDE
801 { 803 {
802 SQLite::Statement s(db_, SQLITE_FROM_HERE, 804 SQLite::Statement s(db_, SQLITE_FROM_HERE,
803 "SELECT uuid, uncompressedSize, compressionType, compressedSize, " 805 "SELECT uuid, uncompressedSize, compressionType, compressedSize, "
804 "uncompressedMD5, compressedMD5 FROM AttachedFiles WHERE id=? AND fileType=?"); 806 "uncompressedMD5, compressedMD5, customData FROM AttachedFiles WHERE id=? AND fileType=?");
805 s.BindInt64(0, id); 807 s.BindInt64(0, id);
806 s.BindInt(1, contentType); 808 s.BindInt(1, contentType);
807 809
808 if (!s.Step()) 810 if (!s.Step())
809 { 811 {
815 contentType, 817 contentType,
816 s.ColumnInt64(1), 818 s.ColumnInt64(1),
817 s.ColumnString(4), 819 s.ColumnString(4),
818 static_cast<CompressionType>(s.ColumnInt(2)), 820 static_cast<CompressionType>(s.ColumnInt(2)),
819 s.ColumnInt64(3), 821 s.ColumnInt64(3),
820 s.ColumnString(5)); 822 s.ColumnString(5),
823 s.ColumnString(6));
821 revision = 0; // TODO - REVISIONS 824 revision = 0; // TODO - REVISIONS
822 return true; 825 return true;
823 } 826 }
824 } 827 }
825 828
1096 return "SignalFileDeleted"; 1099 return "SignalFileDeleted";
1097 } 1100 }
1098 1101
1099 virtual unsigned int GetCardinality() const ORTHANC_OVERRIDE 1102 virtual unsigned int GetCardinality() const ORTHANC_OVERRIDE
1100 { 1103 {
1101 return 7; 1104 return 8;
1102 } 1105 }
1103 1106
1104 virtual void Compute(SQLite::FunctionContext& context) ORTHANC_OVERRIDE 1107 virtual void Compute(SQLite::FunctionContext& context) ORTHANC_OVERRIDE
1105 { 1108 {
1106 if (sqlite_.activeTransaction_ != NULL) 1109 if (sqlite_.activeTransaction_ != NULL)
1107 { 1110 {
1108 std::string uncompressedMD5, compressedMD5; 1111 std::string uncompressedMD5, compressedMD5, customData;
1109 1112
1110 if (!context.IsNullValue(5)) 1113 if (!context.IsNullValue(5))
1111 { 1114 {
1112 uncompressedMD5 = context.GetStringValue(5); 1115 uncompressedMD5 = context.GetStringValue(5);
1113 } 1116 }
1114 1117
1115 if (!context.IsNullValue(6)) 1118 if (!context.IsNullValue(6))
1116 { 1119 {
1117 compressedMD5 = context.GetStringValue(6); 1120 compressedMD5 = context.GetStringValue(6);
1121 }
1122
1123 if (!context.IsNullValue(7))
1124 {
1125 customData = context.GetStringValue(7);
1118 } 1126 }
1119 1127
1120 FileInfo info(context.GetStringValue(0), 1128 FileInfo info(context.GetStringValue(0),
1121 static_cast<FileContentType>(context.GetIntValue(1)), 1129 static_cast<FileContentType>(context.GetIntValue(1)),
1122 static_cast<uint64_t>(context.GetInt64Value(2)), 1130 static_cast<uint64_t>(context.GetInt64Value(2)),
1123 uncompressedMD5, 1131 uncompressedMD5,
1124 static_cast<CompressionType>(context.GetIntValue(3)), 1132 static_cast<CompressionType>(context.GetIntValue(3)),
1125 static_cast<uint64_t>(context.GetInt64Value(4)), 1133 static_cast<uint64_t>(context.GetInt64Value(4)),
1126 compressedMD5); 1134 compressedMD5,
1135 customData);
1127 1136
1128 sqlite_.activeTransaction_->GetListener().SignalAttachmentDeleted(info); 1137 sqlite_.activeTransaction_->GetListener().SignalAttachmentDeleted(info);
1129 } 1138 }
1130 } 1139 }
1131 }; 1140 };
1349 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion, 1358 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion,
1350 "Incompatible version of the Orthanc database: " + tmp); 1359 "Incompatible version of the Orthanc database: " + tmp);
1351 } 1360 }
1352 1361
1353 // New in Orthanc 1.5.1 1362 // New in Orthanc 1.5.1
1354 if (version_ == 6) 1363 if (version_ >= 6)
1355 { 1364 {
1356 if (!transaction->LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast, true /* unused in SQLite */) || 1365 if (!transaction->LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast, true /* unused in SQLite */) ||
1357 tmp != "1") 1366 tmp != "1")
1358 { 1367 {
1359 LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments"; 1368 LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments";
1391 void SQLiteDatabaseWrapper::Upgrade(unsigned int targetVersion, 1400 void SQLiteDatabaseWrapper::Upgrade(unsigned int targetVersion,
1392 IStorageArea& storageArea) 1401 IStorageArea& storageArea)
1393 { 1402 {
1394 boost::mutex::scoped_lock lock(mutex_); 1403 boost::mutex::scoped_lock lock(mutex_);
1395 1404
1396 if (targetVersion != 6) 1405 if (targetVersion != 7)
1397 { 1406 {
1398 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); 1407 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
1399 } 1408 }
1400 1409
1401 // This version of Orthanc is only compatible with versions 3, 4, 1410 // This version of Orthanc is only compatible with versions 3, 4,
1402 // 5 and 6 of the DB schema 1411 // 5 and 6 of the DB schema
1403 if (version_ != 3 && 1412 if (version_ != 3 &&
1404 version_ != 4 && 1413 version_ != 4 &&
1405 version_ != 5 && 1414 version_ != 5 &&
1406 version_ != 6) 1415 version_ != 6 &&
1416 version_ != 7)
1407 { 1417 {
1408 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); 1418 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion);
1409 } 1419 }
1410 1420
1411 if (version_ == 3) 1421 if (version_ == 3)
1442 transaction->Commit(0); 1452 transaction->Commit(0);
1443 } 1453 }
1444 1454
1445 version_ = 6; 1455 version_ = 6;
1446 } 1456 }
1457
1458 if (version_ == 6)
1459 {
1460 LOG(WARNING) << "Upgrading database version from 6 to 7";
1461 ExecuteUpgradeScript(db_, ServerResources::UPGRADE_DATABASE_6_TO_7);
1462 version_ = 7;
1463 }
1464
1447 } 1465 }
1448 1466
1449 1467
1450 IDatabaseWrapper::ITransaction* SQLiteDatabaseWrapper::StartTransaction(TransactionType type, 1468 IDatabaseWrapper::ITransaction* SQLiteDatabaseWrapper::StartTransaction(TransactionType type,
1451 IDatabaseListener& listener) 1469 IDatabaseListener& listener)