Mercurial > hg > orthanc-databases
comparison PostgreSQL/Plugins/StoragePlugin.cpp @ 12:41543239072d
transactions for storage area
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Jul 2018 18:34:56 +0200 |
parents | 17bce6a07b2b |
children | 9774802fd05f |
comparison
equal
deleted
inserted
replaced
11:0217486720b3 | 12:41543239072d |
---|---|
19 **/ | 19 **/ |
20 | 20 |
21 | 21 |
22 #include "../../Framework/Plugins/StorageBackend.h" | 22 #include "../../Framework/Plugins/StorageBackend.h" |
23 | 23 |
24 #include "../../Framework/Common/FileValue.h" | |
25 #include "../../Framework/PostgreSQL/PostgreSQLDatabase.h" | |
26 #include "../../Framework/PostgreSQL/PostgreSQLLargeObject.h" | |
27 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h" | |
28 | |
24 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h> | 29 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h> |
25 #include <Core/Logging.h> | 30 #include <Core/Logging.h> |
31 | |
32 | |
33 namespace OrthancDatabases | |
34 { | |
35 class PostgreSQLStorageArea : public StorageBackend | |
36 { | |
37 private: | |
38 class Factory : public IDatabaseFactory | |
39 { | |
40 private: | |
41 PostgreSQLStorageArea& that_; | |
42 | |
43 public: | |
44 Factory(PostgreSQLStorageArea& that) : | |
45 that_(that) | |
46 { | |
47 } | |
48 | |
49 virtual Dialect GetDialect() const | |
50 { | |
51 return Dialect_PostgreSQL; | |
52 } | |
53 | |
54 virtual IDatabase* Open() | |
55 { | |
56 return that_.OpenInternal(); | |
57 } | |
58 }; | |
59 | |
60 OrthancPluginContext* context_; | |
61 PostgreSQLParameters parameters_; | |
62 bool clearAll_; | |
63 | |
64 IDatabase* OpenInternal() | |
65 { | |
66 std::auto_ptr<PostgreSQLDatabase> db(new PostgreSQLDatabase(parameters_)); | |
67 | |
68 db->Open(); | |
69 | |
70 if (parameters_.HasLock()) | |
71 { | |
72 db->AdvisoryLock(43 /* some arbitrary constant */); | |
73 } | |
74 | |
75 if (clearAll_) | |
76 { | |
77 db->ClearAll(); | |
78 } | |
79 | |
80 { | |
81 PostgreSQLTransaction t(*db); | |
82 | |
83 if (!db->DoesTableExist("StorageArea")) | |
84 { | |
85 db->Execute("CREATE TABLE IF NOT EXISTS StorageArea(" | |
86 "uuid VARCHAR NOT NULL PRIMARY KEY," | |
87 "content OID NOT NULL," | |
88 "type INTEGER NOT NULL)"); | |
89 | |
90 // Automatically remove the large objects associated with the table | |
91 db->Execute("CREATE OR REPLACE RULE StorageAreaDelete AS ON DELETE " | |
92 "TO StorageArea DO SELECT lo_unlink(old.content);"); | |
93 } | |
94 | |
95 t.Commit(); | |
96 } | |
97 | |
98 return db.release(); | |
99 } | |
100 | |
101 public: | |
102 PostgreSQLStorageArea(const PostgreSQLParameters& parameters) : | |
103 StorageBackend(new Factory(*this)), | |
104 parameters_(parameters), | |
105 clearAll_(false) | |
106 { | |
107 } | |
108 | |
109 void SetClearAll(bool clear) | |
110 { | |
111 clearAll_ = clear; | |
112 } | |
113 | |
114 | |
115 virtual void Create(DatabaseManager::Transaction& transaction, | |
116 const std::string& uuid, | |
117 const void* content, | |
118 size_t size, | |
119 OrthancPluginContentType type) | |
120 { | |
121 std::auto_ptr<FileValue> file(new FileValue(content, size)); | |
122 | |
123 { | |
124 DatabaseManager::CachedStatement statement( | |
125 STATEMENT_FROM_HERE, GetManager(), | |
126 "INSERT INTO StorageArea VALUES (${uuid}, ${content}, ${type})"); | |
127 | |
128 statement.SetParameterType("uuid", ValueType_Utf8String); | |
129 statement.SetParameterType("content", ValueType_File); | |
130 statement.SetParameterType("type", ValueType_Integer64); | |
131 | |
132 Dictionary args; | |
133 args.SetUtf8Value("uuid", uuid); | |
134 args.SetValue("content", file.release()); | |
135 args.SetIntegerValue("type", type); | |
136 | |
137 statement.Execute(args); | |
138 } | |
139 } | |
140 | |
141 | |
142 virtual void Read(void*& content, | |
143 size_t& size, | |
144 DatabaseManager::Transaction& transaction, | |
145 const std::string& uuid, | |
146 OrthancPluginContentType type) | |
147 { | |
148 } | |
149 | |
150 | |
151 virtual void Remove(DatabaseManager::Transaction& transaction, | |
152 const std::string& uuid, | |
153 OrthancPluginContentType type) | |
154 { | |
155 DatabaseManager::CachedStatement statement( | |
156 STATEMENT_FROM_HERE, GetManager(), | |
157 "DELETE FROM StorageArea WHERE uuid=${uuid} AND type=${type}"); | |
158 | |
159 statement.SetParameterType("uuid", ValueType_Utf8String); | |
160 statement.SetParameterType("type", ValueType_Integer64); | |
161 | |
162 Dictionary args; | |
163 args.SetUtf8Value("uuid", uuid); | |
164 args.SetIntegerValue("type", type); | |
165 | |
166 statement.Execute(args); | |
167 } | |
168 }; | |
169 } | |
26 | 170 |
27 | 171 |
28 static bool DisplayPerformanceWarning() | 172 static bool DisplayPerformanceWarning() |
29 { | 173 { |
30 (void) DisplayPerformanceWarning; // Disable warning about unused function | 174 (void) DisplayPerformanceWarning; // Disable warning about unused function |
77 return 0; | 221 return 0; |
78 } | 222 } |
79 | 223 |
80 try | 224 try |
81 { | 225 { |
82 // TODO | 226 OrthancDatabases::PostgreSQLParameters parameters(postgresql); |
83 //OrthancDatabases::StorageBackend::Register(); | 227 OrthancDatabases::StorageBackend::Register |
228 (context, new OrthancDatabases::PostgreSQLStorageArea(parameters)); | |
84 } | 229 } |
85 catch (Orthanc::OrthancException& e) | 230 catch (Orthanc::OrthancException& e) |
86 { | 231 { |
87 LOG(ERROR) << e.What(); | 232 LOG(ERROR) << e.What(); |
88 return -1; | 233 return -1; |