ctkDICOM.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QSqlDatabase>
  17. #include <QSqlError>
  18. #include <QSqlQuery>
  19. #include <QFile>
  20. #include <QStringList>
  21. // ctkDICOM includes
  22. #include "ctkDICOM.h"
  23. // STD includes
  24. #include <iostream>
  25. //----------------------------------------------------------------------------
  26. class ctkDICOMPrivate: public ctkPrivate<ctkDICOM>
  27. {
  28. public:
  29. ctkDICOMPrivate();
  30. bool executeScript(const QString& script);
  31. QSqlDatabase Database;
  32. QString DatabaseFileName;
  33. QString LastError;
  34. };
  35. //----------------------------------------------------------------------------
  36. // ctkDICOMPrivate methods
  37. //------------------------------------------------------------------------------
  38. ctkDICOMPrivate::ctkDICOMPrivate()
  39. {
  40. }
  41. //----------------------------------------------------------------------------
  42. // ctkDICOMWidget methods
  43. //------------------------------------------------------------------------------
  44. ctkDICOM::ctkDICOM(QObject* _parent): Superclass(_parent)
  45. {
  46. CTK_INIT_PRIVATE(ctkDICOM);
  47. }
  48. //----------------------------------------------------------------------------
  49. ctkDICOM::~ctkDICOM()
  50. {
  51. }
  52. //----------------------------------------------------------------------------
  53. bool ctkDICOM::openDatabase(const QString& databaseFileName)
  54. {
  55. CTK_D(ctkDICOM);
  56. d->Database = QSqlDatabase::addDatabase("QSQLITE","DICOM-DB");
  57. d->Database.setDatabaseName(databaseFileName);
  58. if ( ! (d->Database.open()) )
  59. {
  60. d->LastError = d->Database.lastError().text();
  61. return false;
  62. }
  63. if ( d->Database.tables().empty() )
  64. {
  65. initializeDatabase();
  66. }
  67. return true;
  68. }
  69. //------------------------------------------------------------------------------
  70. const QString& ctkDICOM::GetLastError() const {
  71. CTK_D(const ctkDICOM);
  72. return d->LastError;
  73. }
  74. //------------------------------------------------------------------------------
  75. const QSqlDatabase& ctkDICOM::database() const {
  76. CTK_D(const ctkDICOM);
  77. return d->Database;
  78. }
  79. //------------------------------------------------------------------------------
  80. bool ctkDICOMPrivate::executeScript(const QString& script) {
  81. QFile scriptFile(script);
  82. scriptFile.open(QIODevice::ReadOnly);
  83. if ( !scriptFile.isOpen() )
  84. {
  85. qDebug() << "Script file " << script << " could not be opened!\n";
  86. return false;
  87. }
  88. QString sqlCommands( QTextStream(&scriptFile).readAll() );
  89. sqlCommands.replace( '\n', ' ' );
  90. sqlCommands.replace("; ", ";\n");
  91. QStringList sqlCommandsLines = sqlCommands.split('\n');
  92. QSqlQuery query(Database);
  93. for (QStringList::iterator it = sqlCommandsLines.begin(); it != sqlCommandsLines.end()-1; ++it)
  94. {
  95. if (! (*it).startsWith("--") )
  96. {
  97. query.exec(*it);
  98. if (query.lastError().type())
  99. {
  100. qDebug() << "There was an error during execution of the statement: " << (*it);
  101. qDebug() << "Error message: " << query.lastError().text();
  102. return false;
  103. }
  104. }
  105. }
  106. return true;
  107. }
  108. //------------------------------------------------------------------------------
  109. bool ctkDICOM::initializeDatabase(const char* sqlFileName)
  110. {
  111. CTK_D(ctkDICOM);
  112. return d->executeScript(sqlFileName);
  113. }
  114. //------------------------------------------------------------------------------
  115. void ctkDICOM::closeDatabase()
  116. {
  117. CTK_D(ctkDICOM);
  118. d->Database.close();
  119. }