annotate PostgreSQL/Plugins/PostgreSQLStorageArea.cpp @ 106:b559af8fe6e0

Remove "ASSERT" in SQL for compatibility with older releases of PostgreSQL
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Jan 2019 15:35:52 +0100
parents 714c5d2bee76
children cc3dc759c989
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
67
714c5d2bee76 preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
14
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "PostgreSQLStorageArea.h"
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h"
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <Plugins/Samples/Common/OrthancPluginCppWrapper.h>
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <Core/Logging.h>
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 namespace OrthancDatabases
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 IDatabase* PostgreSQLStorageArea::OpenInternal()
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 std::auto_ptr<PostgreSQLDatabase> db(new PostgreSQLDatabase(parameters_));
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 db->Open();
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 if (parameters_.HasLock())
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 db->AdvisoryLock(43 /* some arbitrary constant */);
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 if (clearAll_)
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 db->ClearAll();
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 PostgreSQLTransaction t(*db);
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 if (!db->DoesTableExist("StorageArea"))
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 db->Execute("CREATE TABLE IF NOT EXISTS StorageArea("
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 "uuid VARCHAR NOT NULL PRIMARY KEY,"
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 "content OID NOT NULL,"
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 "type INTEGER NOT NULL)");
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 // Automatically remove the large objects associated with the table
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 db->Execute("CREATE OR REPLACE RULE StorageAreaDelete AS ON DELETE "
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 "TO StorageArea DO SELECT lo_unlink(old.content);");
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 t.Commit();
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 return db.release();
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 PostgreSQLStorageArea::PostgreSQLStorageArea(const PostgreSQLParameters& parameters) :
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 StorageBackend(new Factory(*this)),
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 parameters_(parameters),
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 clearAll_(false)
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
9774802fd05f PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }