comparison PostgreSQL/Plugins/StoragePlugin.cpp @ 16:9e419261f1c9

mysql storage area working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 Jul 2018 10:10:35 +0200
parents 9774802fd05f
children 17f849b2af34
comparison
equal deleted inserted replaced
15:dfc7002add9c 16:9e419261f1c9
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 21
22 #include "../../Framework/Plugins/StorageBackend.h" 22 #include "../../Framework/Plugins/StorageBackend.h"
23 #include "PostgreSQLStorageArea.h"
23 24
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
29 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h>
30 #include <Core/Logging.h> 25 #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 DatabaseManager::CachedStatement statement(
149 STATEMENT_FROM_HERE, GetManager(),
150 "SELECT content FROM StorageArea WHERE uuid=$1 AND type=$2");
151
152 statement.SetParameterType("uuid", ValueType_Utf8String);
153 statement.SetParameterType("type", ValueType_Integer64);
154
155 Dictionary args;
156 args.SetUtf8Value("uuid", uuid);
157 args.SetIntegerValue("type", type);
158
159 statement.Execute(args);
160
161 if (statement.IsDone())
162 {
163 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
164 }
165 else if (statement.GetResultFieldsCount() != 1 ||
166 statement.GetResultField(0).GetType() != ValueType_File)
167 {
168 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
169 }
170 else
171 {
172 const FileValue& value = dynamic_cast<const FileValue&>(statement.GetResultField(0));
173 ReadFromString(content, size, value.GetContent());
174 }
175 }
176
177
178 virtual void Remove(DatabaseManager::Transaction& transaction,
179 const std::string& uuid,
180 OrthancPluginContentType type)
181 {
182 DatabaseManager::CachedStatement statement(
183 STATEMENT_FROM_HERE, GetManager(),
184 "DELETE FROM StorageArea WHERE uuid=${uuid} AND type=${type}");
185
186 statement.SetParameterType("uuid", ValueType_Utf8String);
187 statement.SetParameterType("type", ValueType_Integer64);
188
189 Dictionary args;
190 args.SetUtf8Value("uuid", uuid);
191 args.SetIntegerValue("type", type);
192
193 statement.Execute(args);
194 }
195 };
196 }
197 26
198 27
199 static bool DisplayPerformanceWarning() 28 static bool DisplayPerformanceWarning()
200 { 29 {
201 (void) DisplayPerformanceWarning; // Disable warning about unused function 30 (void) DisplayPerformanceWarning; // Disable warning about unused function