Mercurial > hg > orthanc-databases
comparison Framework/MySQL/MySQLDatabase.cpp @ 22:1e9bad493475
prevent running unit tests on a non-existing db
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 11 Jul 2018 14:39:59 +0200 |
parents | 9e419261f1c9 |
children | b2ff1cd2907a |
comparison
equal
deleted
inserted
replaced
21:2e5d2c69d4f9 | 22:1e9bad493475 |
---|---|
262 LOG(ERROR) << "The MySQL database is locked by another instance of Orthanc"; | 262 LOG(ERROR) << "The MySQL database is locked by another instance of Orthanc"; |
263 Close(); | 263 Close(); |
264 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | 264 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
265 } | 265 } |
266 } | 266 } |
267 | |
268 | |
269 static void CheckAlphanumericString(const std::string& name) | |
270 { | |
271 for (size_t i = 0; i < name.length(); i++) | |
272 { | |
273 if (!isalnum(name[i])) | |
274 { | |
275 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
276 } | |
277 } | |
278 } | |
267 | 279 |
268 | 280 |
269 bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction, | 281 bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction, |
270 const std::string& name) | 282 const std::string& name) |
271 { | 283 { |
272 if (mysql_ == NULL) | 284 if (mysql_ == NULL) |
273 { | 285 { |
274 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | 286 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
275 } | 287 } |
276 | 288 |
277 for (size_t i = 0; i < name.length(); i++) | 289 CheckAlphanumericString(name); |
278 { | 290 |
279 if (!isalnum(name[i])) | |
280 { | |
281 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
282 } | |
283 } | |
284 | |
285 Query query("SELECT COUNT(*) FROM information_schema.TABLES WHERE " | 291 Query query("SELECT COUNT(*) FROM information_schema.TABLES WHERE " |
286 "(TABLE_SCHEMA = ${database}) AND (TABLE_NAME = ${table})", true); | 292 "(TABLE_SCHEMA = ${database}) AND (TABLE_NAME = ${table})", true); |
287 query.SetType("database", ValueType_Utf8String); | 293 query.SetType("database", ValueType_Utf8String); |
288 query.SetType("table", ValueType_Utf8String); | 294 query.SetType("table", ValueType_Utf8String); |
289 | 295 |
290 MySQLStatement statement(*this, query); | 296 MySQLStatement statement(*this, query); |
291 | 297 |
292 Dictionary args; | 298 Dictionary args; |
293 args.SetUtf8Value("database", parameters_.GetDatabase()); | 299 args.SetUtf8Value("database", parameters_.GetDatabase()); |
294 args.SetUtf8Value("table", name); | 300 args.SetUtf8Value("table", name); |
301 | |
302 std::auto_ptr<IResult> result(statement.Execute(transaction, args)); | |
303 return (!result->IsDone() && | |
304 result->GetFieldsCount() == 1 && | |
305 result->GetField(0).GetType() == ValueType_Integer64 && | |
306 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1); | |
307 } | |
308 | |
309 | |
310 bool MySQLDatabase::DoesDatabaseExist(MySQLTransaction& transaction, | |
311 const std::string& name) | |
312 { | |
313 if (mysql_ == NULL) | |
314 { | |
315 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
316 } | |
317 | |
318 CheckAlphanumericString(name); | |
319 | |
320 Query query("SELECT COUNT(*) FROM information_schema.SCHEMATA " | |
321 "WHERE SCHEMA_NAME = ${database}", true); | |
322 query.SetType("database", ValueType_Utf8String); | |
323 | |
324 MySQLStatement statement(*this, query); | |
325 | |
326 Dictionary args; | |
327 args.SetUtf8Value("database", name); | |
295 | 328 |
296 std::auto_ptr<IResult> result(statement.Execute(transaction, args)); | 329 std::auto_ptr<IResult> result(statement.Execute(transaction, args)); |
297 return (!result->IsDone() && | 330 return (!result->IsDone() && |
298 result->GetFieldsCount() == 1 && | 331 result->GetFieldsCount() == 1 && |
299 result->GetField(0).GetType() == ValueType_Integer64 && | 332 result->GetField(0).GetType() == ValueType_Integer64 && |