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