ctkDICOM.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QDebug>
  12. #include <QSqlDatabase>
  13. #include <QSqlError>
  14. #include <QSqlQuery>
  15. #include <QFile>
  16. #include <QStringList>
  17. // ctkDICOM includes
  18. #include "ctkDICOM.h"
  19. // STD includes
  20. #include <iostream>
  21. //----------------------------------------------------------------------------
  22. class ctkDICOMPrivate: public ctkPrivate<ctkDICOM>
  23. {
  24. public:
  25. ctkDICOMPrivate();
  26. bool executeScript(const QString& script);
  27. QSqlDatabase Database;
  28. QString DatabaseFileName;
  29. QString LastError;
  30. };
  31. //----------------------------------------------------------------------------
  32. // ctkDICOMPrivate methods
  33. //------------------------------------------------------------------------------
  34. ctkDICOMPrivate::ctkDICOMPrivate()
  35. {
  36. }
  37. //----------------------------------------------------------------------------
  38. // ctkDICOMWidget methods
  39. //------------------------------------------------------------------------------
  40. ctkDICOM::ctkDICOM(QObject* _parent): Superclass(_parent)
  41. {
  42. CTK_INIT_PRIVATE(ctkDICOM);
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkDICOM::~ctkDICOM()
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. bool ctkDICOM::openDatabase(const QString& databaseFileName)
  50. {
  51. CTK_D(ctkDICOM);
  52. d->Database = QSqlDatabase::addDatabase("QSQLITE","DICOM-DB");
  53. d->Database.setDatabaseName(databaseFileName);
  54. if ( ! (d->Database.open()) )
  55. {
  56. d->LastError = d->Database.lastError().text();
  57. return false;
  58. }
  59. if ( d->Database.tables().empty() )
  60. {
  61. initializeDatabase();
  62. }
  63. return true;
  64. }
  65. //------------------------------------------------------------------------------
  66. const QString& ctkDICOM::GetLastError() const {
  67. CTK_D(const ctkDICOM);
  68. return d->LastError;
  69. }
  70. //------------------------------------------------------------------------------
  71. const QSqlDatabase& ctkDICOM::database() const {
  72. CTK_D(const ctkDICOM);
  73. return d->Database;
  74. }
  75. //------------------------------------------------------------------------------
  76. bool ctkDICOMPrivate::executeScript(const QString& script) {
  77. QFile scriptFile(script);
  78. scriptFile.open(QIODevice::ReadOnly);
  79. if ( !scriptFile.isOpen() )
  80. {
  81. qDebug() << "Script file " << script << " could not be opened!\n";
  82. return false;
  83. }
  84. QString sqlCommands( QTextStream(&scriptFile).readAll() );
  85. sqlCommands.replace( '\n', ' ' );
  86. sqlCommands.replace("; ", ";\n");
  87. QStringList sqlCommandsLines = sqlCommands.split('\n');
  88. QSqlQuery query(Database);
  89. for (QStringList::iterator it = sqlCommandsLines.begin(); it != sqlCommandsLines.end()-1; ++it)
  90. {
  91. if (! (*it).startsWith("--") )
  92. {
  93. query.exec(*it);
  94. if (query.lastError().type())
  95. {
  96. qDebug() << "There was an error during execution of the statement: " << (*it);
  97. qDebug() << "Error message: " << query.lastError().text();
  98. return false;
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. //------------------------------------------------------------------------------
  105. bool ctkDICOM::initializeDatabase(const char* sqlFileName)
  106. {
  107. CTK_D(ctkDICOM);
  108. return d->executeScript(sqlFileName);
  109. }
  110. //------------------------------------------------------------------------------
  111. void ctkDICOM::closeDatabase()
  112. {
  113. CTK_D(ctkDICOM);
  114. d->Database.close();
  115. }