comparison OrthancServer/Database/SQLiteDatabaseWrapper.cpp @ 3116:0fa7181ac4e5 db-changes

conrt
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 12 Jan 2019 11:08:53 +0100
parents 2e1711f80f74
children f86ebf971a72
comparison
equal deleted inserted replaced
3115:2c108461d409 3116:0fa7181ac4e5
327 { 327 {
328 db_.OpenInMemory(); 328 db_.OpenInMemory();
329 } 329 }
330 330
331 331
332 int SQLiteDatabaseWrapper::GetGlobalIntegerProperty(GlobalProperty property,
333 unsigned int defaultValue)
334 {
335 std::string tmp;
336
337 if (!LookupGlobalProperty(tmp, GlobalProperty_DatabasePatchLevel))
338 {
339 return defaultValue;
340 }
341 else
342 {
343 try
344 {
345 return boost::lexical_cast<int>(tmp);
346 }
347 catch (boost::bad_lexical_cast&)
348 {
349 throw OrthancException(ErrorCode_ParameterOutOfRange,
350 "Global property " + boost::lexical_cast<std::string>(property) +
351 " should be an integer, but found: " + tmp);
352 }
353 }
354 }
355
356
332 void SQLiteDatabaseWrapper::Open() 357 void SQLiteDatabaseWrapper::Open()
333 { 358 {
334 db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); 359 db_.Execute("PRAGMA ENCODING=\"UTF-8\";");
335 360
336 // Performance tuning of SQLite with PRAGMAs 361 // Performance tuning of SQLite with PRAGMAs
342 //db_.Execute("PRAGMA TEMP_STORE=memory"); 367 //db_.Execute("PRAGMA TEMP_STORE=memory");
343 368
344 // Make "LIKE" case-sensitive in SQLite 369 // Make "LIKE" case-sensitive in SQLite
345 db_.Execute("PRAGMA case_sensitive_like = true;"); 370 db_.Execute("PRAGMA case_sensitive_like = true;");
346 371
347 if (!db_.DoesTableExist("GlobalProperties")) 372 {
348 { 373 SQLite::Transaction t(db_);
349 LOG(INFO) << "Creating the database"; 374 t.Begin();
350 std::string query; 375
351 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); 376 if (!db_.DoesTableExist("GlobalProperties"))
352 db_.Execute(query); 377 {
353 } 378 LOG(INFO) << "Creating the database";
354
355 // Check the version of the database
356 std::string tmp;
357 if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion))
358 {
359 tmp = "Unknown";
360 }
361
362 bool ok = false;
363 try
364 {
365 LOG(INFO) << "Version of the Orthanc database: " << tmp;
366 version_ = boost::lexical_cast<unsigned int>(tmp);
367 ok = true;
368 }
369 catch (boost::bad_lexical_cast&)
370 {
371 }
372
373 if (!ok)
374 {
375 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion,
376 "Incompatible version of the Orthanc database: " + tmp);
377 }
378
379 // New in Orthanc 1.5.1
380 if (version_ == 6)
381 {
382 if (!LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) ||
383 tmp != "1")
384 {
385 LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments";
386 std::string query; 379 std::string query;
387 EmbeddedResources::GetFileResource(query, EmbeddedResources::INSTALL_TRACK_ATTACHMENTS_SIZE); 380 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE);
388 db_.Execute(query); 381 db_.Execute(query);
389 } 382 }
383
384 // Check the version of the database
385 std::string tmp;
386 if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion))
387 {
388 tmp = "Unknown";
389 }
390
391 bool ok = false;
392 try
393 {
394 LOG(INFO) << "Version of the Orthanc database: " << tmp;
395 version_ = boost::lexical_cast<unsigned int>(tmp);
396 ok = true;
397 }
398 catch (boost::bad_lexical_cast&)
399 {
400 }
401
402 if (!ok)
403 {
404 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion,
405 "Incompatible version of the Orthanc database: " + tmp);
406 }
407
408 // New in Orthanc 1.5.1
409 if (version_ == 6)
410 {
411 if (!LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) ||
412 tmp != "1")
413 {
414 LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments";
415 std::string query;
416 EmbeddedResources::GetFileResource(query, EmbeddedResources::INSTALL_TRACK_ATTACHMENTS_SIZE);
417 db_.Execute(query);
418 }
419 }
420
421 /*if (GetGlobalIntegerProperty(GlobalProperty_DatabasePatchLevel, 0) <= 0)
422 {
423 SetGlobalProperty(GlobalProperty_DatabasePatchLevel, "1");
424 }*/
425
426 t.Commit();
390 } 427 }
391 428
392 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; 429 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor;
393 db_.Register(signalRemainingAncestor_); 430 db_.Register(signalRemainingAncestor_);
394 } 431 }