ctkDICOM.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QDebug>
00023 #include <QSqlDatabase>
00024 #include <QSqlError>
00025 #include <QSqlQuery>
00026 #include <QFile>
00027 #include <QStringList>
00028 
00029 // ctkDICOM includes
00030 #include "ctkDICOM.h"
00031 
00032 // STD includes
00033 #include <iostream>
00034 #include <stdexcept>
00035 
00036 //----------------------------------------------------------------------------
00037 class ctkDICOMPrivate: public ctkPrivate<ctkDICOM>
00038 {
00039 public:
00040   ctkDICOMPrivate(); 
00041   bool executeScript(const QString& script);
00042 
00043   QSqlDatabase  Database;
00044   QString       DatabaseFileName;
00045   QString       LastError;
00046 };
00047 
00048 //----------------------------------------------------------------------------
00049 // ctkDICOMPrivate methods
00050 
00051 //------------------------------------------------------------------------------
00052 ctkDICOMPrivate::ctkDICOMPrivate()
00053 {
00054 }
00055 
00056 //----------------------------------------------------------------------------
00057 // ctkDICOMWidget methods
00058 
00059 //------------------------------------------------------------------------------
00060 ctkDICOM::ctkDICOM(QObject* _parent): Superclass(_parent)
00061 {
00062   CTK_INIT_PRIVATE(ctkDICOM);
00063 }
00064 
00065 //----------------------------------------------------------------------------
00066 ctkDICOM::~ctkDICOM()
00067 {
00068 }
00069 
00070 //----------------------------------------------------------------------------
00071 void ctkDICOM::openDatabase(const QString& databaseFileName)
00072 {
00073   CTK_D(ctkDICOM);
00074   d->Database = QSqlDatabase::addDatabase("QSQLITE","DICOM-DB");
00075   d->Database.setDatabaseName(databaseFileName);
00076   if ( ! (d->Database.open()) )
00077     {
00078     d->LastError = d->Database.lastError().text();
00079     throw std::runtime_error(qPrintable(d->LastError));
00080     }
00081   if ( d->Database.tables().empty() ) 
00082     {
00083     initializeDatabase();
00084     }
00085 }
00086 
00087 //------------------------------------------------------------------------------
00088 const QString& ctkDICOM::GetLastError() const {
00089   CTK_D(const ctkDICOM);
00090   return d->LastError; 
00091 }
00092 
00093 //------------------------------------------------------------------------------
00094 const QSqlDatabase& ctkDICOM::database() const {
00095   CTK_D(const ctkDICOM);
00096   return d->Database;
00097 }
00098 
00099 //------------------------------------------------------------------------------
00100 bool ctkDICOMPrivate::executeScript(const QString& script) {
00101   QFile scriptFile(script);
00102   scriptFile.open(QIODevice::ReadOnly);
00103   if  ( !scriptFile.isOpen() )
00104     {
00105     qDebug() << "Script file " << script << " could not be opened!\n";
00106     return false;
00107     }
00108 
00109   QString sqlCommands( QTextStream(&scriptFile).readAll() );
00110   sqlCommands.replace( '\n', ' ' );
00111   sqlCommands.replace("; ", ";\n");
00112 
00113   QStringList sqlCommandsLines = sqlCommands.split('\n');
00114 
00115   QSqlQuery query(Database);
00116 
00117   for (QStringList::iterator it = sqlCommandsLines.begin(); it != sqlCommandsLines.end()-1; ++it)
00118   {
00119     if (! (*it).startsWith("--") )
00120       {
00121       query.exec(*it);
00122       if (query.lastError().type())
00123         {
00124         qDebug() << "There was an error during execution of the statement: " << (*it);
00125         qDebug() << "Error message: " << query.lastError().text();
00126         return false;
00127         }
00128       }
00129   }
00130   return true;
00131 }
00132 
00133 //------------------------------------------------------------------------------
00134 bool ctkDICOM::initializeDatabase(const char* sqlFileName)
00135 {
00136   CTK_D(ctkDICOM);
00137   return d->executeScript(sqlFileName);
00138 }
00139 
00140 //------------------------------------------------------------------------------
00141 void ctkDICOM::closeDatabase()
00142 {
00143   CTK_D(ctkDICOM);
00144   d->Database.close();
00145 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1