Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLDatabase.h @ 568:77c8544bbd7d find-refactoring
merge
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Mon, 23 Sep 2024 16:06:53 +0200 |
parents | 54d518dcd74a |
children |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
507
54d518dcd74a
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
497
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
54d518dcd74a
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
497
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 8 * |
9 * This program is free software: you can redistribute it and/or | |
10 * modify it under the terms of the GNU Affero General Public License | |
11 * as published by the Free Software Foundation, either version 3 of | |
12 * the License, or (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * Affero General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU Affero General Public License | |
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #pragma once | |
25 | |
26 #if ORTHANC_ENABLE_POSTGRESQL != 1 | |
27 # error PostgreSQL support must be enabled to use this file | |
28 #endif | |
29 | |
30 #include "PostgreSQLParameters.h" | |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
31 #include "../Common/IDatabaseFactory.h" |
0 | 32 |
33 namespace OrthancDatabases | |
34 { | |
35 class PostgreSQLDatabase : public IDatabase | |
36 { | |
37 private: | |
38 friend class PostgreSQLStatement; | |
39 friend class PostgreSQLLargeObject; | |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
40 friend class PostgreSQLTransaction; |
0 | 41 |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
42 class Factory; |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
43 |
0 | 44 PostgreSQLParameters parameters_; |
45 void* pg_; /* Object of type "PGconn*" */ | |
46 | |
47 void ThrowException(bool log); | |
48 | |
49 void Close(); | |
50 | |
134
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
51 bool RunAdvisoryLockStatement(const std::string& statement); |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
52 |
0 | 53 public: |
186 | 54 explicit PostgreSQLDatabase(const PostgreSQLParameters& parameters) : |
55 parameters_(parameters), | |
56 pg_(NULL) | |
0 | 57 { |
58 } | |
59 | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
60 ~PostgreSQLDatabase(); |
0 | 61 |
62 void Open(); | |
63 | |
429
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
418
diff
changeset
|
64 bool IsVerboseEnabled() const |
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
418
diff
changeset
|
65 { |
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
418
diff
changeset
|
66 return parameters_.IsVerboseEnabled(); |
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
418
diff
changeset
|
67 } |
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
418
diff
changeset
|
68 |
134
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
69 bool AcquireAdvisoryLock(int32_t lock); |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
70 |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
71 bool ReleaseAdvisoryLock(int32_t lock); |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
72 |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
73 void AdvisoryLock(int32_t lock); |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
74 |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
75 void ExecuteMultiLines(const std::string& sql); |
0 | 76 |
266
cc7af42d4f23
Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
77 bool DoesTableExist(const std::string& name); |
cc7af42d4f23
Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
78 |
cc7af42d4f23
Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
79 bool DoesColumnExist(const std::string& tableName, |
cc7af42d4f23
Store revisions for metadata and attachments in PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
80 const std::string& columnName); |
0 | 81 |
82 void ClearAll(); // Only for unit tests! | |
83 | |
186 | 84 virtual Dialect GetDialect() const ORTHANC_OVERRIDE |
0 | 85 { |
86 return Dialect_PostgreSQL; | |
87 } | |
88 | |
186 | 89 virtual IPrecompiledStatement* Compile(const Query& query) ORTHANC_OVERRIDE; |
0 | 90 |
215
b40b30075c51
added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
91 virtual ITransaction* CreateTransaction(TransactionType type) ORTHANC_OVERRIDE; |
134
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
92 |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
93 class TransientAdvisoryLock |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
94 { |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
95 private: |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
96 PostgreSQLDatabase& database_; |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
97 int32_t lock_; |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
98 |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
99 public: |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
100 TransientAdvisoryLock(PostgreSQLDatabase& database, |
418
a7f0f27fe33c
wip: advisory lock around CreateInstance: not ok see WO-139
Alain Mazy <am@osimis.io>
parents:
417
diff
changeset
|
101 int32_t lock, |
a7f0f27fe33c
wip: advisory lock around CreateInstance: not ok see WO-139
Alain Mazy <am@osimis.io>
parents:
417
diff
changeset
|
102 unsigned int retries = 10, |
a7f0f27fe33c
wip: advisory lock around CreateInstance: not ok see WO-139
Alain Mazy <am@osimis.io>
parents:
417
diff
changeset
|
103 unsigned int retryInterval = 500); |
134
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
104 |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
105 ~TransientAdvisoryLock(); |
cc3dc759c989
Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
106 }; |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
107 |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
108 static IDatabaseFactory* CreateDatabaseFactory(const PostgreSQLParameters& parameters); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
109 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
110 static PostgreSQLDatabase* CreateDatabaseConnection(const PostgreSQLParameters& parameters); |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
111 |
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
112 protected: |
497 | 113 const std::string GetReadWriteTransactionStatement() const |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
114 { |
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
115 return parameters_.GetReadWriteTransactionStatement(); |
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
116 } |
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
117 |
497 | 118 const std::string GetReadOnlyTransactionStatement() const |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
119 { |
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
120 return parameters_.GetReadOnlyTransactionStatement(); |
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
121 } |
0 | 122 }; |
123 } |