Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLParameters.cpp @ 66:e78d7077ee1c db-changes
preparing for db-changes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 21 Dec 2018 17:19:17 +0100 |
parents | 89a114f36c42 |
children | 714c5d2bee76 |
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 | |
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 "MySQLParameters.h" | |
23 | |
61 | 24 #include "MySQLDatabase.h" |
25 | |
0 | 26 #include <Core/Logging.h> |
27 #include <Core/OrthancException.h> | |
28 | |
29 namespace OrthancDatabases | |
30 { | |
31 void MySQLParameters::Reset() | |
32 { | |
33 host_ = "localhost"; | |
34 username_.clear(); | |
35 password_.clear(); | |
36 database_.clear(); | |
37 port_ = 3306; | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
38 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
39 #if defined(_WIN32) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
40 unixSocket_.clear(); |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
41 #else |
0 | 42 unixSocket_ = "/var/run/mysqld/mysqld.sock"; |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
43 #endif |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
44 |
0 | 45 lock_ = true; |
46 } | |
47 | |
48 | |
49 MySQLParameters::MySQLParameters() | |
50 { | |
51 Reset(); | |
52 } | |
53 | |
54 | |
55 MySQLParameters::MySQLParameters(const OrthancPlugins::OrthancConfiguration& configuration) | |
56 { | |
57 Reset(); | |
58 | |
59 std::string s; | |
60 if (configuration.LookupStringValue(s, "Host")) | |
61 { | |
62 SetHost(s); | |
63 } | |
64 | |
65 if (configuration.LookupStringValue(s, "Username")) | |
66 { | |
67 SetUsername(s); | |
68 } | |
69 | |
70 if (configuration.LookupStringValue(s, "Password")) | |
71 { | |
72 SetPassword(s); | |
73 } | |
74 | |
75 if (configuration.LookupStringValue(s, "Database")) | |
76 { | |
77 SetDatabase(s); | |
78 } | |
79 | |
80 unsigned int port; | |
81 if (configuration.LookupUnsignedIntegerValue(port, "Port")) | |
82 { | |
83 SetPort(port); | |
84 } | |
85 | |
86 if (configuration.LookupStringValue(s, "UnixSocket")) | |
87 { | |
88 SetUnixSocket(s); | |
89 } | |
90 | |
91 lock_ = configuration.GetBooleanValue("Lock", true); // Use locking by default | |
92 } | |
93 | |
94 | |
95 void MySQLParameters::SetHost(const std::string& host) | |
96 { | |
97 host_ = host; | |
98 } | |
99 | |
100 | |
101 void MySQLParameters::SetUsername(const std::string& username) | |
102 { | |
103 username_ = username; | |
104 } | |
105 | |
106 | |
107 void MySQLParameters::SetPassword(const std::string& password) | |
108 { | |
109 password_ = password; | |
110 } | |
111 | |
112 | |
113 void MySQLParameters::SetDatabase(const std::string& database) | |
114 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
115 if (database.empty()) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
116 { |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
117 LOG(ERROR) << "MySQL: Empty database name"; |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
118 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
119 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
120 |
61 | 121 if (!MySQLDatabase::IsValidDatabaseIdentifier(database)) |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
122 { |
61 | 123 LOG(ERROR) << "MySQL: Only alphanumeric characters are allowed in a " |
124 << "database name: \"" << database << "\""; | |
125 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
126 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
127 |
0 | 128 database_ = database; |
129 } | |
130 | |
131 | |
132 void MySQLParameters::SetPort(unsigned int port) | |
133 { | |
134 if (port >= 65535) | |
135 { | |
136 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
137 } | |
138 else | |
139 { | |
140 port_ = port; | |
141 } | |
142 } | |
143 | |
144 | |
145 void MySQLParameters::SetUnixSocket(const std::string& socket) | |
146 { | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
147 #if defined(_WIN32) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
148 if (!socket.empty()) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
149 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
150 LOG(WARNING) << "MySQL: Setting an UNIX socket on Windows has no effect"; |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
151 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
152 #endif |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
153 |
0 | 154 unixSocket_ = socket; |
155 } | |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
156 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
157 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
158 void MySQLParameters::Format(Json::Value& target) const |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
159 { |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
160 target = Json::objectValue; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
161 target["Host"] = host_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
162 target["Username"] = username_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
163 target["Password"] = password_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
164 target["Database"] = database_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
165 target["Port"] = port_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
166 target["UnixSocket"] = unixSocket_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
167 target["Lock"] = lock_; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
168 } |
0 | 169 } |