Mercurial > hg > orthanc-databases
comparison Framework/MySQL/MySQLDatabase.cpp @ 16:9e419261f1c9
mysql storage area working
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Jul 2018 10:10:35 +0200 |
parents | 5a97c68a7a51 |
children | 1e9bad493475 |
comparison
equal
deleted
inserted
replaced
15:dfc7002add9c | 16:9e419261f1c9 |
---|---|
143 { | 143 { |
144 LOG(ERROR) << "Cannot set the character set to UTF8"; | 144 LOG(ERROR) << "Cannot set the character set to UTF8"; |
145 Close(); | 145 Close(); |
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | 146 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
147 } | 147 } |
148 | 148 } |
149 if (parameters_.HasLock()) | 149 |
150 { | 150 |
151 try | 151 namespace |
152 { | 152 { |
153 Query query("SELECT GET_LOCK('Lock', 0);", false); | 153 class ResultWrapper : public boost::noncopyable |
154 MySQLStatement statement(*this, query); | 154 { |
155 | 155 private: |
156 MySQLTransaction t(*this); | 156 MYSQL_RES *result_; |
157 Dictionary args; | 157 |
158 | 158 public: |
159 std::auto_ptr<IResult> result(t.Execute(statement, args)); | 159 ResultWrapper(MySQLDatabase& mysql, |
160 | 160 const std::string& sql) : |
161 if (result->IsDone() || | 161 result_(NULL) |
162 result->GetField(0).GetType() != ValueType_Integer64 || | 162 { |
163 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() != 1) | 163 if (mysql_real_query(mysql.GetObject(), sql.c_str(), sql.size())) |
164 { | 164 { |
165 mysql.LogError(); | |
165 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | 166 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
166 } | 167 } |
167 | 168 |
168 t.Commit(); | 169 result_ = mysql_use_result(mysql.GetObject()); |
169 } | 170 if (result_ == NULL) |
170 catch (Orthanc::OrthancException&) | 171 { |
171 { | 172 mysql.LogError(); |
172 LOG(ERROR) << "The MySQL database is locked by another instance of Orthanc"; | 173 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
173 Close(); | 174 } |
175 } | |
176 | |
177 ~ResultWrapper() | |
178 { | |
179 if (result_ != NULL) | |
180 { | |
181 mysql_free_result(result_); | |
182 result_ = NULL; | |
183 } | |
184 } | |
185 | |
186 MYSQL_RES *GetObject() | |
187 { | |
188 return result_; | |
189 } | |
190 }; | |
191 } | |
192 | |
193 | |
194 bool MySQLDatabase::LookupGlobalStringVariable(std::string& value, | |
195 const std::string& variable) | |
196 { | |
197 ResultWrapper result(*this, "SELECT @@global." + variable); | |
198 | |
199 MYSQL_ROW row = mysql_fetch_row(result.GetObject()); | |
200 if (mysql_errno(mysql_) == 0 && | |
201 row && | |
202 row[0]) | |
203 { | |
204 value = std::string(row[0]); | |
205 return true; | |
206 } | |
207 else | |
208 { | |
209 return false; | |
210 } | |
211 } | |
212 | |
213 | |
214 bool MySQLDatabase::LookupGlobalIntegerVariable(int64_t& value, | |
215 const std::string& variable) | |
216 { | |
217 std::string s; | |
218 | |
219 if (LookupGlobalStringVariable(s, variable)) | |
220 { | |
221 try | |
222 { | |
223 value = boost::lexical_cast<int64_t>(s); | |
224 return true; | |
225 } | |
226 catch (boost::bad_lexical_cast&) | |
227 { | |
174 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | 228 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
175 } | 229 } |
176 } | 230 } |
177 } | 231 else |
178 | 232 { |
179 | 233 return false; |
234 } | |
235 } | |
236 | |
237 | |
238 void MySQLDatabase::AdvisoryLock(int32_t lock) | |
239 { | |
240 try | |
241 { | |
242 Query query("SELECT GET_LOCK('Lock" + | |
243 boost::lexical_cast<std::string>(lock) + "', 0);", false); | |
244 MySQLStatement statement(*this, query); | |
245 | |
246 MySQLTransaction t(*this); | |
247 Dictionary args; | |
248 | |
249 std::auto_ptr<IResult> result(t.Execute(statement, args)); | |
250 | |
251 if (result->IsDone() || | |
252 result->GetField(0).GetType() != ValueType_Integer64 || | |
253 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() != 1) | |
254 { | |
255 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
256 } | |
257 | |
258 t.Commit(); | |
259 } | |
260 catch (Orthanc::OrthancException&) | |
261 { | |
262 LOG(ERROR) << "The MySQL database is locked by another instance of Orthanc"; | |
263 Close(); | |
264 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
265 } | |
266 } | |
267 | |
268 | |
180 bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction, | 269 bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction, |
181 const std::string& name) | 270 const std::string& name) |
182 { | 271 { |
183 if (mysql_ == NULL) | 272 if (mysql_ == NULL) |
184 { | 273 { |
210 result->GetField(0).GetType() == ValueType_Integer64 && | 299 result->GetField(0).GetType() == ValueType_Integer64 && |
211 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1); | 300 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1); |
212 } | 301 } |
213 | 302 |
214 | 303 |
215 void MySQLDatabase::Execute(const std::string& sql) | 304 void MySQLDatabase::Execute(const std::string& sql, |
305 bool arobaseSeparator) | |
216 { | 306 { |
217 if (mysql_ == NULL) | 307 if (mysql_ == NULL) |
218 { | 308 { |
219 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | 309 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
220 } | 310 } |
229 { | 319 { |
230 std::string s = Orthanc::Toolbox::StripSpaces(commands[i]); | 320 std::string s = Orthanc::Toolbox::StripSpaces(commands[i]); |
231 | 321 |
232 if (!s.empty()) | 322 if (!s.empty()) |
233 { | 323 { |
234 // Replace the escape character "@" by a semicolon | 324 if (arobaseSeparator) |
235 std::replace(s.begin(), s.end(), '@', ';'); | 325 { |
326 // Replace the escape character "@" by a semicolon | |
327 std::replace(s.begin(), s.end(), '@', ';'); | |
328 } | |
236 | 329 |
237 LOG(TRACE) << "MySQL: " << s; | 330 LOG(TRACE) << "MySQL: " << s; |
238 CheckErrorCode(mysql_query(mysql_, s.c_str())); | 331 CheckErrorCode(mysql_query(mysql_, s.c_str())); |
239 } | 332 } |
240 } | 333 } |