comparison PostgreSQL/Plugins/PostgreSQLStorageArea.cpp @ 14:9774802fd05f

PostgreSQLStorageArea working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Jul 2018 20:28:27 +0200
parents
children 9e419261f1c9
comparison
equal deleted inserted replaced
13:927264a0c137 14:9774802fd05f
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "PostgreSQLStorageArea.h"
23
24 #include "../../Framework/Common/FileValue.h"
25 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h"
26
27 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h>
28 #include <Core/Logging.h>
29
30
31 namespace OrthancDatabases
32 {
33 IDatabase* PostgreSQLStorageArea::OpenInternal()
34 {
35 std::auto_ptr<PostgreSQLDatabase> db(new PostgreSQLDatabase(parameters_));
36
37 db->Open();
38
39 if (parameters_.HasLock())
40 {
41 db->AdvisoryLock(43 /* some arbitrary constant */);
42 }
43
44 if (clearAll_)
45 {
46 db->ClearAll();
47 }
48
49 {
50 PostgreSQLTransaction t(*db);
51
52 if (!db->DoesTableExist("StorageArea"))
53 {
54 db->Execute("CREATE TABLE IF NOT EXISTS StorageArea("
55 "uuid VARCHAR NOT NULL PRIMARY KEY,"
56 "content OID NOT NULL,"
57 "type INTEGER NOT NULL)");
58
59 // Automatically remove the large objects associated with the table
60 db->Execute("CREATE OR REPLACE RULE StorageAreaDelete AS ON DELETE "
61 "TO StorageArea DO SELECT lo_unlink(old.content);");
62 }
63
64 t.Commit();
65 }
66
67 return db.release();
68 }
69
70
71 PostgreSQLStorageArea::PostgreSQLStorageArea(const PostgreSQLParameters& parameters) :
72 StorageBackend(new Factory(*this)),
73 parameters_(parameters),
74 clearAll_(false)
75 {
76 }
77
78
79 void PostgreSQLStorageArea::Create(DatabaseManager::Transaction& transaction,
80 const std::string& uuid,
81 const void* content,
82 size_t size,
83 OrthancPluginContentType type)
84 {
85 DatabaseManager::CachedStatement statement(
86 STATEMENT_FROM_HERE, GetManager(),
87 "INSERT INTO StorageArea VALUES (${uuid}, ${content}, ${type})");
88
89 statement.SetParameterType("uuid", ValueType_Utf8String);
90 statement.SetParameterType("content", ValueType_File);
91 statement.SetParameterType("type", ValueType_Integer64);
92
93 Dictionary args;
94 args.SetUtf8Value("uuid", uuid);
95 args.SetFileValue("content", content, size);
96 args.SetIntegerValue("type", type);
97
98 statement.Execute(args);
99 }
100
101
102 void PostgreSQLStorageArea::Read(void*& content,
103 size_t& size,
104 DatabaseManager::Transaction& transaction,
105 const std::string& uuid,
106 OrthancPluginContentType type)
107 {
108 DatabaseManager::CachedStatement statement(
109 STATEMENT_FROM_HERE, GetManager(),
110 "SELECT content FROM StorageArea WHERE uuid=${uuid} AND type=${type}");
111
112 statement.SetParameterType("uuid", ValueType_Utf8String);
113 statement.SetParameterType("type", ValueType_Integer64);
114
115 Dictionary args;
116 args.SetUtf8Value("uuid", uuid);
117 args.SetIntegerValue("type", type);
118
119 statement.Execute(args);
120
121 if (statement.IsDone())
122 {
123 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
124 }
125 else if (statement.GetResultFieldsCount() != 1 ||
126 statement.GetResultField(0).GetType() != ValueType_File)
127 {
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
129 }
130 else
131 {
132 const FileValue& value = dynamic_cast<const FileValue&>(statement.GetResultField(0));
133 ReadFromString(content, size, value.GetContent());
134 }
135 }
136
137
138 void PostgreSQLStorageArea::Remove(DatabaseManager::Transaction& transaction,
139 const std::string& uuid,
140 OrthancPluginContentType type)
141 {
142 DatabaseManager::CachedStatement statement(
143 STATEMENT_FROM_HERE, GetManager(),
144 "DELETE FROM StorageArea WHERE uuid=${uuid} AND type=${type}");
145
146 statement.SetParameterType("uuid", ValueType_Utf8String);
147 statement.SetParameterType("type", ValueType_Integer64);
148
149 Dictionary args;
150 args.SetUtf8Value("uuid", uuid);
151 args.SetIntegerValue("type", type);
152
153 statement.Execute(args);
154 }
155 }