Skip to content
Snippets Groups Projects
Commit fbcfff6f authored by Stéphane Albert's avatar Stéphane Albert
Browse files

ENH: Initialized empty $/mvd2/db.sqlite database file.

parent 10411453
No related branches found
No related tags found
No related merge requests found
......@@ -42,7 +42,7 @@ set( Common_Core_HEADERS_MOC
mvdBackgroundTask.h
# mvdCore.h
mvdDatabaseConnection.h
mvdDatabaseError.h
# mvdDatabaseError.h
mvdDatabaseModel.h
mvdDatasetDescriptor.h
mvdDatasetModel.h
......
......@@ -25,6 +25,7 @@
//
// Qt includes (sorted by alphabetic order)
//// Must be included before system/custom includes.
#include <QtSql>
//
// System includes (sorted by alphabetic order)
......@@ -37,6 +38,8 @@
//
// Monteverdi includes (sorted by alphabetic order)
#include "Core/mvdDatabaseError.h"
#include "Core/mvdI18nCoreApplication.h"
namespace mvd
{
......@@ -77,6 +80,23 @@ DatabaseConnection
{
}
/*******************************************************************************/
void
DatabaseConnection
::Foo()
{
QSqlDatabase db( QSqlDatabase::addDatabase( "QSQLITE", "mvd2" ) );
QString filename(
I18nCoreApplication::ConstInstance()->GetCacheDir().filePath( "db.sqlite" )
);
db.setDatabaseName( filename );
if( !db.open() )
throw DatabaseError( db.lastError() );
}
/*******************************************************************************/
/* SLOTS */
/*******************************************************************************/
......
......@@ -89,6 +89,10 @@ public:
/** \brief Destructor. */
virtual ~DatabaseConnection();
/**
*/
static void Foo();
/*-[ PUBLIC SLOTS SECTION ]------------------------------------------------*/
//
......
......@@ -28,6 +28,7 @@
//
// System includes (sorted by alphabetic order)
#include <sstream>
//
// ITK includes (sorted by alphabetic order)
......@@ -37,6 +38,7 @@
//
// Monteverdi includes (sorted by alphabetic order)
#include "Core/mvdAlgorithm.h"
namespace mvd
{
......@@ -59,21 +61,92 @@ namespace
/*****************************************************************************/
/* STATIC IMPLEMENTATION SECTION */
/*****************************************************************************/
std::string
DatabaseError
::whatString( const QSqlError& sqlError, const QString& msg )
{
std::stringstream strStream( std::ios_base::out );
switch( sqlError.type() )
{
case QSqlError::NoError:
strStream <<
ToStdString(
QCoreApplication::translate(
"mvd::DatabaseError",
"No database error"
)
);
break;
case QSqlError::ConnectionError:
strStream <<
ToStdString(
QCoreApplication::translate(
"mvd::DatabaseError",
"Database connection error"
)
);
break;
case QSqlError::StatementError:
strStream <<
ToStdString(
QCoreApplication::translate(
"mvd::DatabaseError",
"SQL statement syntax error" )
);
break;
case QSqlError::TransactionError:
strStream <<
ToStdString(
QCoreApplication::translate(
"mvd::DatabaseError",
"Database transaction failed error" )
);
break;
case QSqlError::UnknownError:
strStream <<
ToStdString(
QCoreApplication::translate(
"mvd::DatabaseError",
"Unknown database error" )
);
break;
default:
assert( false && "Unexpected QSqlError::ErrorType." );
break;
}
strStream <<
" " << sqlError.number() <<
": " << ToStdString( sqlError.text() );
if( !msg.isEmpty() )
strStream << std::endl << ToStdString( msg );
return strStream.str();
}
/*****************************************************************************/
/* CLASS IMPLEMENTATION SECTION */
/*******************************************************************************/
DatabaseError
::DatabaseError( QObject* parent ) :
QObject( parent )
::DatabaseError( const QSqlError& sqlError ) :
std::runtime_error( whatString( sqlError ) ),
m_SqlError( sqlError )
{
}
/*******************************************************************************/
DatabaseError
::~DatabaseError()
throw()
{
}
......
......@@ -31,10 +31,11 @@
//
// Qt includes (sorted by alphabetic order)
//// Must be included before system/custom includes.
#include <QtCore>
#include <QtSql>
//
// System includes (sorted by alphabetic order)
#include <stdexcept>
//
// ITK includes (sorted by alphabetic order)
......@@ -70,13 +71,9 @@ namespace mvd
* \brief WIP.
*/
class Monteverdi2_EXPORT DatabaseError :
public QObject
public std::runtime_error
{
/*-[ QOBJECT SECTION ]-----------------------------------------------------*/
Q_OBJECT;
/*-[ PUBLIC SECTION ]------------------------------------------------------*/
//
......@@ -84,22 +81,10 @@ class Monteverdi2_EXPORT DatabaseError :
public:
/** \brief Constructor. */
DatabaseError( QObject* parent =NULL );
DatabaseError( const QSqlError& sqlError );
/** \brief Destructor. */
virtual ~DatabaseError();
/*-[ PUBLIC SLOTS SECTION ]------------------------------------------------*/
//
// Public SLOTS.
public slots:
/*-[ SIGNALS SECTION ]-----------------------------------------------------*/
//
// Signals.
signals:
virtual ~DatabaseError() throw();
/*-[ PROTECTED SECTION ]---------------------------------------------------*/
......@@ -116,17 +101,13 @@ protected:
//
// Private methods.
private:
static std::string whatString( const QSqlError& sqlError,
const QString& msg =QString() );
//
// Private attributes.
private:
/*-[ PRIVATE SLOTS SECTION ]-----------------------------------------------*/
//
// Slots.
private slots:
QSqlError m_SqlError;
};
} // end namespace 'mvd'.
......
......@@ -35,8 +35,8 @@
//
// Monteverdi includes (sorted by alphabetic order)
#include "Core/mvdDatabaseConnection.h"
#include "Core/mvdDatasetModel.h"
//
#include "Gui/mvdAboutDialog.h"
#include "Gui/mvdI18nApplication.h"
......@@ -248,7 +248,12 @@ I18nMainWindow
dir.setPath( path );
}
I18nApplication::Instance()->MakeCacheDir( dir.path() );
bool isNew = I18nApplication::Instance()->MakeCacheDir( dir.path() );
//
// Setup initial empty database
if( isNew )
DatabaseConnection::Foo();
}
/*****************************************************************************/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment