comparison Framework/Plugins/IndexBackend.cpp @ 396:7b3acfa95bd8 db-protobuf

implementation of list/add/remove labels in postgresql
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 06 Apr 2023 19:00:29 +0200
parents d14e6ff04a5c
children 8dedfd982b83
comparison
equal deleted inserted replaced
395:a7a029043670 396:7b3acfa95bd8
2615 assert(result.instanceId != -1); 2615 assert(result.instanceId != -1);
2616 } 2616 }
2617 #endif 2617 #endif
2618 2618
2619 2619
2620 void IndexBackend::AddLabel(int64_t resource, 2620 void IndexBackend::AddLabel(DatabaseManager& manager,
2621 int64_t resource,
2621 const std::string& label) 2622 const std::string& label)
2622 { 2623 {
2623 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); 2624 std::unique_ptr<DatabaseManager::CachedStatement> statement;
2624 } 2625
2625 2626 switch (manager.GetDialect())
2626 2627 {
2627 void IndexBackend::RemoveLabel(int64_t resource, 2628 case Dialect_PostgreSQL:
2629 statement.reset(new DatabaseManager::CachedStatement(
2630 STATEMENT_FROM_HERE, manager,
2631 "INSERT INTO Labels VALUES(${id}, ${label}) ON CONFLICT DO NOTHING"));
2632 break;
2633
2634 default:
2635 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
2636 }
2637
2638 statement->SetParameterType("id", ValueType_Integer64);
2639 statement->SetParameterType("label", ValueType_Utf8String);
2640
2641 Dictionary args;
2642 args.SetIntegerValue("id", resource);
2643 args.SetUtf8Value("label", label);
2644
2645 statement->Execute(args);
2646 }
2647
2648
2649 void IndexBackend::RemoveLabel(DatabaseManager& manager,
2650 int64_t resource,
2628 const std::string& label) 2651 const std::string& label)
2629 { 2652 {
2630 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); 2653 DatabaseManager::CachedStatement statement(
2631 } 2654 STATEMENT_FROM_HERE, manager,
2632 2655 "DELETE FROM Labels WHERE id=${id} AND label=${label}");
2633 2656
2634 void IndexBackend::ListLabels(std::set<std::string>& target, 2657 statement.SetParameterType("id", ValueType_Integer64);
2658 statement.SetParameterType("label", ValueType_Utf8String);
2659
2660 Dictionary args;
2661 args.SetIntegerValue("id", resource);
2662 args.SetUtf8Value("label", label);
2663
2664 statement.Execute(args);
2665 }
2666
2667
2668 void IndexBackend::ListLabels(std::list<std::string>& target,
2669 DatabaseManager& manager,
2635 int64_t resource) 2670 int64_t resource)
2636 { 2671 {
2637 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); 2672 DatabaseManager::CachedStatement statement(
2673 STATEMENT_FROM_HERE, manager,
2674 "SELECT label FROM Labels WHERE id=${id}");
2675
2676 statement.SetReadOnly(true);
2677 statement.SetParameterType("id", ValueType_Integer64);
2678
2679 Dictionary args;
2680 args.SetIntegerValue("id", resource);
2681
2682 ReadListOfStrings(target, statement, args);
2638 } 2683 }
2639 2684
2640 2685
2641 void IndexBackend::Register(IndexBackend* backend, 2686 void IndexBackend::Register(IndexBackend* backend,
2642 size_t countConnections, 2687 size_t countConnections,
2689 target = boost::lexical_cast<int>(value); 2734 target = boost::lexical_cast<int>(value);
2690 return true; 2735 return true;
2691 } 2736 }
2692 catch (boost::bad_lexical_cast&) 2737 catch (boost::bad_lexical_cast&)
2693 { 2738 {
2694 LOG(ERROR) << "Corrupted PostgreSQL database"; 2739 LOG(ERROR) << "Corrupted database";
2695 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); 2740 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
2696 } 2741 }
2697 } 2742 }
2698 else 2743 else
2699 { 2744 {