annotate Framework/PostgreSQL/PostgreSQLDatabase.cpp @ 319:baaa265d84f4

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 20 Jul 2021 08:14:24 +0200
parents b8ba198a8df3
children 16aac0287485
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
193
3236894320d6 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
107
5765cc5fd268 reverted fix for OS X that breaks other targets
Sebastien Jodogne <s.jodogne@orthanc-labs.com>
parents: 105
diff changeset
22 #include "PostgreSQLIncludes.h" // Must be the first
105
eefbcee7bd78 fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 103
diff changeset
23 #include "PostgreSQLDatabase.h"
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
25 #include "../Common/ImplicitTransaction.h"
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
26 #include "../Common/RetryDatabaseFactory.h"
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "PostgreSQLResult.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "PostgreSQLStatement.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include "PostgreSQLTransaction.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
152
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
31 #include <Logging.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
32 #include <OrthancException.h>
266
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
33 #include <Toolbox.h>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <boost/lexical_cast.hpp>
134
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
36 #include <boost/thread.hpp>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace OrthancDatabases
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 void PostgreSQLDatabase::ThrowException(bool log)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 if (log)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 LOG(ERROR) << "PostgreSQL error: "
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 << PQerrorMessage(reinterpret_cast<PGconn*>(pg_));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 if (PQstatus(reinterpret_cast<PGconn*>(pg_)) == CONNECTION_OK)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 void PostgreSQLDatabase::Close()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 if (pg_ != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 LOG(INFO) << "Closing connection to PostgreSQL";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 PQfinish(reinterpret_cast<PGconn*>(pg_));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 pg_ = NULL;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
46
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
70
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
71 PostgreSQLDatabase::~PostgreSQLDatabase()
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
72 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
73 try
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
74 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
75 Close();
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
76 }
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
77 catch (Orthanc::OrthancException&)
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
78 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
79 // Ignore possible exceptions due to connection loss
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
80 }
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
81 }
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 29
diff changeset
82
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 void PostgreSQLDatabase::Open()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if (pg_ != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 // Already connected
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 return;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 std::string s;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 parameters_.Format(s);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 pg_ = PQconnectdb(s.c_str());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if (pg_ == NULL ||
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 PQstatus(reinterpret_cast<PGconn*>(pg_)) != CONNECTION_OK)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 std::string message;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 if (pg_)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 message = PQerrorMessage(reinterpret_cast<PGconn*>(pg_));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 PQfinish(reinterpret_cast<PGconn*>(pg_));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 pg_ = NULL;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 LOG(ERROR) << "PostgreSQL error: " << message;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
12
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
112 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
134
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
115 bool PostgreSQLDatabase::RunAdvisoryLockStatement(const std::string& statement)
12
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
116 {
216
fbb52129158a TransactionType given to PostgreSQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
117 PostgreSQLTransaction transaction(*this, TransactionType_ReadWrite);
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
134
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
119 Query query(statement, false);
29
2fb9cd42af14 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
120 PostgreSQLStatement s(*this, query);
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
12
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
122 PostgreSQLResult result(s);
134
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
123
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
124 bool success = (!result.IsDone() &&
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
125 result.GetBoolean(0));
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
126
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
127 transaction.Commit();
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
128
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
129 return success;
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
130 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
131
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
132
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
133 bool PostgreSQLDatabase::AcquireAdvisoryLock(int32_t lock)
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
134 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
135 return RunAdvisoryLockStatement(
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
136 "select pg_try_advisory_lock(" +
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
137 boost::lexical_cast<std::string>(lock) + ")");
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
138 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
139
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
140
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
141 bool PostgreSQLDatabase::ReleaseAdvisoryLock(int32_t lock)
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
142 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
143 return RunAdvisoryLockStatement(
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
144 "select pg_advisory_unlock(" +
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
145 boost::lexical_cast<std::string>(lock) + ")");
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
146 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
147
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
148
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
149 void PostgreSQLDatabase::AdvisoryLock(int32_t lock)
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
150 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
151 if (!AcquireAdvisoryLock(lock))
12
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
152 {
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
153 LOG(ERROR) << "The PostgreSQL database is locked by another instance of Orthanc";
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
41543239072d transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
155 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
159 void PostgreSQLDatabase::ExecuteMultiLines(const std::string& sql)
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 LOG(TRACE) << "PostgreSQL: " << sql;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 Open();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 PGresult* result = PQexec(reinterpret_cast<PGconn*>(pg_), sql.c_str());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 if (result == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 ThrowException(true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 bool ok = (PQresultStatus(result) == PGRES_COMMAND_OK ||
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 PQresultStatus(result) == PGRES_TUPLES_OK);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 if (ok)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 PQclear(result);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 std::string message = PQresultErrorMessage(result);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 PQclear(result);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 LOG(ERROR) << "PostgreSQL error: " << message;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 ThrowException(false);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
266
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
188 bool PostgreSQLDatabase::DoesTableExist(const std::string& name)
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
266
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
190 std::string lower;
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
191 Orthanc::Toolbox::ToLowerCase(lower, name);
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 // http://stackoverflow.com/a/24089729/881731
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 PostgreSQLStatement statement(*this,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 "SELECT 1 FROM pg_catalog.pg_class c "
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 "JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 "WHERE n.nspname = 'public' AND c.relkind='r' "
214
ab96698c73a3 removed useless information about read-only in ITransaction and IPrecompiledStatement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
199 "AND c.relname=$1");
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 statement.DeclareInputString(0);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 statement.BindString(0, lower);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 PostgreSQLResult result(statement);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 return !result.IsDone();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
266
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
209 bool PostgreSQLDatabase::DoesColumnExist(const std::string& tableName,
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
210 const std::string& columnName)
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
211 {
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
212 std::string lowerTable, lowerColumn;
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
213 Orthanc::Toolbox::ToLowerCase(lowerTable, tableName);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
214 Orthanc::Toolbox::ToLowerCase(lowerColumn, columnName);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
215
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
216 PostgreSQLStatement statement(*this,
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
217 "SELECT 1 FROM information_schema.columns "
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
218 "WHERE table_schema=$1 AND table_name=$2 AND column_name=$3");
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
219
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
220 statement.DeclareInputString(0);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
221 statement.DeclareInputString(1);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
222 statement.DeclareInputString(2);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
223
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
224 statement.BindString(0, "public" /* schema */);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
225 statement.BindString(1, lowerTable);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
226 statement.BindString(2, lowerColumn);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
227
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
228 PostgreSQLResult result(statement);
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
229 return !result.IsDone();
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
230 }
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
231
cc7af42d4f23 Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
232
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 void PostgreSQLDatabase::ClearAll()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 {
216
fbb52129158a TransactionType given to PostgreSQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
235 PostgreSQLTransaction transaction(*this, TransactionType_ReadWrite);
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 // Remove all the large objects
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
238 ExecuteMultiLines("SELECT lo_unlink(loid) FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) as loids;");
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 // http://stackoverflow.com/a/21247009/881731
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
241 ExecuteMultiLines("DROP SCHEMA public CASCADE;");
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
242 ExecuteMultiLines("CREATE SCHEMA public;");
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
243 ExecuteMultiLines("GRANT ALL ON SCHEMA public TO postgres;");
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
244 ExecuteMultiLines("GRANT ALL ON SCHEMA public TO public;");
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
245 ExecuteMultiLines("COMMENT ON SCHEMA public IS 'standard public schema';");
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 transaction.Commit();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 IPrecompiledStatement* PostgreSQLDatabase::Compile(const Query& query)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 return new PostgreSQLStatement(*this, query);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
257 namespace
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 {
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
259 class PostgreSQLImplicitTransaction : public ImplicitTransaction
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
260 {
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
261 private:
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
262 PostgreSQLDatabase& db_;
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
263
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
264 protected:
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
265 virtual IResult* ExecuteInternal(IPrecompiledStatement& statement,
280
b8ba198a8df3 adding missing ORTHANC_OVERRIDE
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 266
diff changeset
266 const Dictionary& parameters) ORTHANC_OVERRIDE
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
267 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
268 return dynamic_cast<PostgreSQLStatement&>(statement).Execute(*this, parameters);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
269 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
270
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
271 virtual void ExecuteWithoutResultInternal(IPrecompiledStatement& statement,
280
b8ba198a8df3 adding missing ORTHANC_OVERRIDE
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 266
diff changeset
272 const Dictionary& parameters) ORTHANC_OVERRIDE
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
273 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
274 dynamic_cast<PostgreSQLStatement&>(statement).ExecuteWithoutResult(*this, parameters);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
275 }
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
276
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
277 public:
252
33fa478c119a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
278 explicit PostgreSQLImplicitTransaction(PostgreSQLDatabase& db) :
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
279 db_(db)
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
280 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
281 }
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
282
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
283 virtual bool DoesTableExist(const std::string& name) ORTHANC_OVERRIDE
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
284 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
285 return db_.DoesTableExist(name.c_str());
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
286 }
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
287
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
288 virtual bool DoesTriggerExist(const std::string& name) ORTHANC_OVERRIDE
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
289 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
290 return false;
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
291 }
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
292
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
293 virtual void ExecuteMultiLines(const std::string& query) ORTHANC_OVERRIDE
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
294 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
295 db_.ExecuteMultiLines(query);
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
296 }
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
297 };
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
298 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
299
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
300
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
301 ITransaction* PostgreSQLDatabase::CreateTransaction(TransactionType type)
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
302 {
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
303 switch (type)
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
304 {
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
305 case TransactionType_Implicit:
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
306 return new PostgreSQLImplicitTransaction(*this);
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
307
216
fbb52129158a TransactionType given to PostgreSQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
308 case TransactionType_ReadWrite:
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
309 case TransactionType_ReadOnly:
216
fbb52129158a TransactionType given to PostgreSQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
310 return new PostgreSQLTransaction(*this, type);
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
311
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
312 default:
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 214
diff changeset
313 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
314 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 }
134
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
316
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
317
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
318 PostgreSQLDatabase::TransientAdvisoryLock::TransientAdvisoryLock(
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
319 PostgreSQLDatabase& database,
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
320 int32_t lock) :
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
321 database_(database),
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
322 lock_(lock)
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
323 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
324 bool locked = true;
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
325
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
326 for (unsigned int i = 0; i < 10; i++)
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
327 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
328 if (database_.AcquireAdvisoryLock(lock_))
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
329 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
330 locked = false;
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
331 break;
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
332 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
333 else
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
334 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
335 boost::this_thread::sleep(boost::posix_time::milliseconds(500));
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
336 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
337 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
338
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
339 if (locked)
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
340 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
341 LOG(ERROR) << "Cannot acquire a transient advisory lock";
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
342 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
343 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
344 }
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
345
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
346
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
347 PostgreSQLDatabase::TransientAdvisoryLock::~TransientAdvisoryLock()
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
348 {
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
349 database_.ReleaseAdvisoryLock(lock_);
cc3dc759c989 Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 107
diff changeset
350 }
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
351
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
352
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
353 class PostgreSQLDatabase::Factory : public RetryDatabaseFactory
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
354 {
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
355 private:
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
356 PostgreSQLParameters parameters_;
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
357
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
358 protected:
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
359 virtual IDatabase* TryOpen()
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
360 {
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
361 std::unique_ptr<PostgreSQLDatabase> db(new PostgreSQLDatabase(parameters_));
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
362 db->Open();
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
363 return db.release();
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
364 }
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
365
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
366 public:
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
367 explicit Factory(const PostgreSQLParameters& parameters) :
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
368 RetryDatabaseFactory(parameters.GetMaxConnectionRetries(),
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
369 parameters.GetConnectionRetryInterval()),
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
370 parameters_(parameters)
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
371 {
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
372 }
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
373 };
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
374
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
375
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
376 IDatabaseFactory* PostgreSQLDatabase::CreateDatabaseFactory(const PostgreSQLParameters& parameters)
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
377 {
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
378 return new Factory(parameters);
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
379 }
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
380
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
381
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
382 PostgreSQLDatabase* PostgreSQLDatabase::CreateDatabaseConnection(const PostgreSQLParameters& parameters)
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
383 {
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
384 Factory factory(parameters);
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
385 return dynamic_cast<PostgreSQLDatabase*>(factory.Open());
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 216
diff changeset
386 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 }