persistent storage feature implementation in progress.
{
dnGlobal()->setMessage1("Initialization failed");
}
+ return;
+ }
+
+ std::string dataStorePath = containerRoot.append("/data.db");
+
+ succeeded = mContainer->setDataStore(dataStorePath.c_str());
+ if (!succeeded)
+ {
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2("Failed to the setup data store");
+ }
+ else
+ {
+ dnGlobal()->setMessage1("Initialization failed");
+ }
+ return;
}
mValid = true;
virtual void replyWithStatusCode(int code, const char* message) = 0;
virtual void replyWithFile(const char* filePath) = 0;
- virtual ~DNHTTPServerImpl() {};
+ virtual ~DNHTTPServerImpl() {}
};
#endif
--- /dev/null
+// Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//
+// Created by tkawata on 3/24/2012.
+//
+#include "DNStorage.h"
+
+#include "DNStorageImpl.h"
+
+DNStorage::DNStorage(const char *storagePath)
+{
+ impl = DNStorageImpl::create(storagePath);
+}
+
+DNStorage::~DNStorage()
+{
+ if (impl)
+ {
+ impl->flush();
+ delete impl;
+ impl = 0;
+ }
+}
+
+bool DNStorage::setValue(const char *path, const char *key, float value)
+{
+ bool r = 0;
+ if (impl)
+ {
+ r = impl->setValue(path,key,value);
+ }
+ return r;
+}
+
+float DNStorage::getValue(const char *path, const char *key)
+{
+ float r = 0;
+ if (impl)
+ {
+ r = impl->getValue(path,key);
+ }
+ return r;
+}
+
+int DNStorage::insertToDataSet(const char *path, const char *key, float v, float x, float y, float z)
+{
+ int r = 0;
+ if (impl)
+ {
+ r = impl->insertToDataSet(path,key,v, x,y,z);
+ }
+ return r;
+}
+
+const DNStorageDataSet* DNStorage::queryDataSet(const char *path, const char *key, const DNStorageDataSetQuery *query) const
+{
+ const DNStorageDataSet *r = 0;
+ if (impl)
+ {
+ r = impl->queryDataSet(path,key,query);
+ }
+ return r;
+}
+
+bool DNStorage::updateDataSet(const char *path, const char *key, int index, float v, float x, float y, float z)
+{
+ bool r = false;
+ if (impl)
+ {
+ r = impl->updateDataSet(path,key,index,v,x,y,z);
+ }
+ return r;
+}
+
+bool DNStorage::deleteFromDataSet(const char *path, const char *key, int index)
+{
+ bool r = false;
+ if (impl)
+ {
+ r = impl->deleteFromDataSet(path,key,index);
+ }
+ return r;
+}
+
+bool DNStorage::flush()
+{
+ bool r = false;
+ if (impl)
+ r = impl->flush();
+
+ return r;
+}
+
+bool DNStorage::isValid()
+{
+ bool r = false;
+ if (impl)
+ r = impl->isValid();
+
+ return r;
+}
--- /dev/null
+// Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//
+// Created by tkawata on 3/24/2012.
+//
+#ifndef DNSTORAGE_H
+#define DNSTORAGE_H
+
+class DNStorageImpl;
+
+
+class DNStorageDataSetData
+{
+public:
+ int index;
+ float v1;
+ float v2;
+ float v3;
+};
+
+class DNStorageDataSetQuery
+{
+public:
+ enum SORT {
+ SORT_NONE,
+ SORT_ACENT_X,
+ SORT_ACENT_Y,
+ SORT_ACENT_Z,
+ SORT_DECENT_X,
+ SORT_DECENT_Y,
+ SORT_DECENT_Z
+ };
+
+ DNStorageDataSetQuery() : isSetXMin(false),isSetXMax(false),isSetYMin(false),isSetYMax(false),isSetZMin(false),isSetZMax(false) {}
+ virtual ~DNStorageDataSetQuery() {}
+ void setXMin(float min) { xMin = min;isSetXMin = true; }
+ void setXMax(float max) { xMax = max;isSetXMax = true; }
+ void setYMin(float min) { yMin = min;isSetYMin = true; }
+ void setYMax(float max) { yMax = max;isSetYMax = true; }
+ void setZMin(float min) { zMin = min;isSetZMin = true; }
+ void setZMax(float max) { zMax = max;isSetZMax = true; }
+ void setMaxDataCount(int count) { maxDataCount = count; }
+ void setSort(SORT s1 = SORT_NONE, SORT s2 = SORT_NONE, SORT s3 = SORT_NONE) { sort1 = s1; sort2 = s2; sort3 = s3; }
+
+ float xMin;
+ bool isSetXMin;
+ float xMax;
+ bool isSetXMax;
+ float yMin;
+ bool isSetYMin;
+ float yMax;
+ bool isSetYMax;
+ float zMin;
+ bool isSetZMin;
+ float zMax;
+ bool isSetZMax;
+ int maxDataCount;
+ bool isSetMaxDataCount;
+ SORT sort1;
+ SORT sort2;
+ SORT sort3;
+};
+
+
+class DNStorageDataSet
+{
+public:
+ int count;
+ DNStorageDataSetData *data;
+};
+
+class DNStorage
+{
+public:
+ DNStorage(const char *storagePath);
+ ~DNStorage();
+
+ //methods for simple key-value storage
+ bool setValue(const char *path, const char *key, float value);
+ float getValue(const char *path, const char *key);
+
+ //methods for dataset storage
+ int insertToDataSet(const char *path, const char *key, float v, float x, float y, float z);
+ const DNStorageDataSet *queryDataSet(const char *path, const char *key, const DNStorageDataSetQuery *query) const;
+ bool updateDataSet(const char *path, const char *key, int index, float v, float x, float y, float z);
+ bool deleteFromDataSet(const char *path, const char *key, int index);
+
+ bool flush();
+
+ bool isValid();
+
+private:
+ DNStorageImpl *impl;
+};
+
+#endif // DNSTORAGE_H
--- /dev/null
+// Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//
+// Created by tkawata on 3/24/2012.
+//
+#ifndef DNSTORAGEIMPL_H
+#define DNSTORAGEIMPL_H
+
+class DNStorageDataSet;
+class DNStorageDataSetQuery;
+
+class DNStorageImpl
+{
+public:
+ static DNStorageImpl* create(const char *storagePath);
+
+ virtual ~DNStorageImpl() {}
+ virtual bool setValue(const char *path, const char *key, float value) = 0;
+ virtual float getValue(const char *path, const char *key) = 0;
+
+ virtual int insertToDataSet(const char *path, const char *key, float v, float x, float y, float z) = 0;
+ virtual const DNStorageDataSet *queryDataSet(const char *path, const char *key, const DNStorageDataSetQuery *query) const = 0;
+ virtual bool updateDataSet(const char *path, const char *key, int index, float v, float x, float y, float z) = 0;
+ virtual bool deleteFromDataSet(const char *path, const char *key, int index) = 0;
+
+ virtual bool isValid() = 0;
+
+ virtual bool flush() = 0;
+};
+
+#endif // DNSOTRAGEIMPL_H
void MainWindow::vprintf(const char *fmt, va_list ap)
{
QString msg = QString().vsprintf(fmt,ap);
+#ifdef DEBUG
qDebug() << msg << endl;
+#endif
mConsoleLock.lock();
if (mHasPendingConsoleMessage)
mPendingConsoleMessage.append("\n");
#include "TKContainer.h"
#include "TKCell.h"
-
+#include "DNStorage.h"
TKContainer::~TKContainer()
{
}
mCells.clear();
mInterfaceCells.clear();
+
+ if (mStorage)
+ {
+ delete mStorage;
+ }
+}
+
+bool TKContainer::setDataStore(std::string storagePath)
+{
+ mStorage = new DNStorage(storagePath.c_str());
+ return mStorage->isValid();
}
TKCell* TKContainer::getCell(std::string theFQNName)
class TKCell;
class TKCellCode;
+class DNStorage;
typedef std::map<std::string, TKCell*> TKCellMap;
typedef std::map<std::string, TKCellCode*> TKCellCodeMap;
const TKCellCodeMap* getCellCodes() { return &mCellCodes; }
void setContentPath(std::string contentPath) { mContentPath = contentPath; }
std::string getContentPath() { return mContentPath; }
+ bool setDataStore(std::string storagePath);
TKCell* getCell(std::string theFQNName);
TKCell* getInterfaceCell(std::string theFQNName);
TKCellCode* getCellCode(std::string theCellCodeName);
inline virtual float getValue(std::string key) = 0;
protected:
- TKContainer() {}
+ TKContainer() : mStorage(NULL){}
TKCellMap mCells;
TKCellMap mInterfaceCells;
TKCellCodeMap mCellCodes;
std::string mContentPath;
-
+ DNStorage *mStorage;
};
#endif
--- /dev/null
+// Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//
+// Created by tkawata on 3/24/2012.
+//
+#include "qtdnstorageimpl.h"
+
+#include "DNStorageImpl.h"
+
+#include <QtSql>
+
+#include <DNGlobal.h>
+
+//static
+DNStorageImpl* DNStorageImpl::create(const char *storagePath)
+{
+ return new QtDNStorageImpl(storagePath);
+}
+
+QtDNStorageImpl::~QtDNStorageImpl()
+{
+ if (mDB.isValid() && mDB.isOpen())
+ {
+ mDB.close();
+ mDB.removeDatabase(mDB.connectionName());
+ }
+}
+
+QtDNStorageImpl::QtDNStorageImpl(const char *storagePath)
+{
+ mStoragePath = QString::fromLocal8Bit(storagePath);
+ mIsValid = initDB();
+}
+
+bool QtDNStorageImpl::setValue(const char *path, const char *key, float value)
+{
+ if (!mIsValid)
+ return false;
+
+ QString qPath(path);
+ QString qKey(key);
+ mPropertiesCountQuery.addBindValue(qPath);
+ mPropertiesCountQuery.addBindValue(qKey);
+ if (!mPropertiesCountQuery.exec())
+ {
+ std::string message = "SQL error while processing cell ";
+ message.append(path);
+ message.append("\n");
+ message.append(mPropertiesCountQuery.lastError().text().toStdString());
+ return false;
+ }
+ int cnt = mPropertiesCountQuery.boundValue(1).toInt();
+ if (cnt == 0)
+ {
+ mPropertiesInsertQuery.addBindValue(qPath);
+ mPropertiesInsertQuery.addBindValue(qKey);
+ mPropertiesInsertQuery.addBindValue(value);
+ if (!mPropertiesInsertQuery.exec())
+ {
+ std::string message = "SQL error while processing cell ";
+ message.append(path);
+ message.append("\n");
+ message.append(mPropertiesCountQuery.lastError().text().toStdString());
+ return false;
+ }
+ }
+ else
+ {
+ mPropertiesUpdateQuery.addBindValue(value);
+ mPropertiesUpdateQuery.addBindValue(qPath);
+ mPropertiesUpdateQuery.addBindValue(qKey);
+ if (!mPropertiesUpdateQuery.exec())
+ {
+ std::string message = "SQL error while processing cell ";
+ message.append(path);
+ message.append("\n");
+ message.append(mPropertiesCountQuery.lastError().text().toStdString());
+ return false;
+ }
+ }
+
+ return true;
+}
+
+float QtDNStorageImpl::getValue(const char *path, const char *key)
+{
+ if (!mIsValid)
+ return 0;
+
+ mPropertiesGetQuery.prepare("SELECT value FROM properties WHERE cell=? and key=?");
+
+ QString qPath(path);
+ QString qKey(key);
+ mPropertiesGetQuery.addBindValue(qPath);
+ mPropertiesGetQuery.addBindValue(qKey);
+ if (!mPropertiesGetQuery.exec())
+ {
+ std::string message = "SQL error while processing cell ";
+ message.append(path);
+ message.append("\n");
+ message.append(mPropertiesCountQuery.lastError().text().toStdString());
+ return false;
+ }
+ if (mPropertiesGetQuery.size() > 0)
+ {
+ return mPropertiesGetQuery.boundValue(1).toFloat();
+ }
+ else
+ return 0.0;
+}
+
+int QtDNStorageImpl::insertToDataSet(const char *path, const char *key, float v, float x, float y, float z)
+{
+ if (!mIsValid)
+ return 0;
+ //TODO
+ return 0;
+}
+
+const DNStorageDataSet* QtDNStorageImpl::queryDataSet(const char *path, const char *key, const DNStorageDataSetQuery *query) const
+{
+ if (!mIsValid)
+ return 0;
+ //TODO
+ return 0;
+}
+
+bool QtDNStorageImpl::updateDataSet(const char *path, const char *key, int index, float v, float x, float y, float z)
+{
+ if (!mIsValid)
+ return false;
+ //TODO
+ return false;
+}
+
+bool QtDNStorageImpl::deleteFromDataSet(const char *path, const char *key, int index)
+{
+ if (!mIsValid)
+ return false;
+ //TODO
+ return false;
+}
+
+bool QtDNStorageImpl::flush()
+{
+ if (!mIsValid || !mDB.isValid())
+ return false;
+
+ mDB.commit();
+
+ return true;
+}
+
+bool QtDNStorageImpl::initDB()
+{
+
+ mDB = QSqlDatabase::addDatabase("QSQLITE");
+ mDB.setDatabaseName(mStoragePath);
+
+ if (!mDB.open())
+ {
+ std::string message = "Failied to open storage file ";
+ message.append(mStoragePath.toStdString());
+ message.append("\n");
+ message.append(mDB.lastError().text().toStdString());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Persistent storage initialization error.");
+ dnGlobal()->setMessage2(message);
+ }
+ return false;
+ }
+
+ QStringList tables = mDB.tables();
+ if (!tables.contains("properties"))
+ {
+ QSqlQuery q;
+
+ if (!q.exec("CREATE TABLE properties(cell TEXT, prokey TEXT, value REAL, PRIMARY KEY (cell, prokey))"));
+ {
+ if (mDB.lastError().type() != QSqlError::NoError)
+ {
+ std::string message = "Failied to create data table (properties) in ";
+ message.append(mStoragePath.toStdString());
+ message.append("\n");
+ message.append(mDB.lastError().text().toStdString());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Persistent storage initialization error.");
+ dnGlobal()->setMessage2(message);
+ }
+ return false;
+ }
+ }
+ }
+
+ if (!tables.contains("dataset"))
+ {
+ QSqlQuery q;
+
+ if (!q.exec("CREATE TABLE dataset(cell TEXT, dskey TEXT, dsindex INTEGER, v REAL, x REAL, y REAL, z REAL, PRIMARY KEY (cell, dskey, dsindex))"));
+ {
+ if (mDB.lastError().type() != QSqlError::NoError)
+ {
+ std::string message = "Failied to create data table (dataset) in ";
+ message.append(mStoragePath.toStdString());
+ message.append("\n");
+ message.append(mDB.lastError().text().toStdString());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Persistent storage initialization error.");
+ dnGlobal()->setMessage2(message);
+ }
+ return false;
+ }
+ }
+ }
+
+ mPropertiesCountQuery.prepare("SELECT count(*) FROM properties WHERE cell=? and prokey=?");
+ mPropertiesInsertQuery.prepare("INSERT INTO properties (?,?,?)");
+ mPropertiesUpdateQuery.prepare("UPDATE SET value=? WHERE cell=? and prokey=?");
+ mPropertiesGetQuery.prepare("SELECT value FROM properties WHERE cell=? and prokey=?");
+
+ mDataSetInsetToDataSetQuery.prepare("INSERT INTO dataset (?,?,?, ?,?,?,?)");
+ mDataSetUpdateDataSetQuery.prepare("UPDATE SET v=?, x=?, y=? z=? WHERE cell=? and dskey=? and dsindex=?");
+ mDataSetDeleteFromDataSetQuery.prepare("DELETE FROM dataset WHERE cell=? and key=? and index=?");
+
+ return true;
+}
--- /dev/null
+// Copyright (c) 2012 Dennco Project
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//
+// Created by tkawata on 3/24/2012.
+//
+#ifndef QTDNSTORAGEIMPL_H
+#define QTDNSTORAGEIMPL_H
+
+#include "DNStorageImpl.h"
+
+#include <QString>
+#include <QtSql>
+
+class QtDNStorageImpl : public DNStorageImpl
+{
+public:
+ QtDNStorageImpl(const char *storagePath);
+ virtual ~QtDNStorageImpl();
+
+ virtual bool setValue(const char *path, const char *key, float value);
+ virtual float getValue(const char *path, const char *key);
+
+ virtual int insertToDataSet(const char *path, const char *key, float v, float x, float y, float z);
+ virtual const DNStorageDataSet *queryDataSet(const char *path, const char *key, const DNStorageDataSetQuery *query) const;
+ virtual bool updateDataSet(const char *path, const char *key, int index, float v, float x, float y, float z);
+ virtual bool deleteFromDataSet(const char *path, const char *key, int index);
+
+ virtual bool flush();
+
+ virtual bool isValid() { return mIsValid; }
+
+private:
+ bool initDB();
+ bool mIsValid;
+ QString mStoragePath;
+
+ QSqlQuery mPropertiesCountQuery;
+ QSqlQuery mPropertiesInsertQuery;
+ QSqlQuery mPropertiesUpdateQuery;
+ QSqlQuery mPropertiesGetQuery;
+
+ QSqlQuery mDataSetInsetToDataSetQuery;
+ QSqlQuery mDataSetUpdateDataSetQuery;
+ QSqlQuery mDataSetDeleteFromDataSetQuery;
+
+ QSqlDatabase mDB;
+
+};
+
+#endif // QTDNSTORAGEIMPL_H
#
#-------------------------------------------------
-QT += core gui webkit script xml
+QT += core gui webkit script xml sql
TARGET = QtDennco
TEMPLATE = app
Source/platform/qt/qtdnthreadimpl.cpp \
Source/QtDennco/dnwebinterface.cpp \
Source/QtScript/dnqsinputcell.cpp \
- Source/QtScript/dnqsoutputcell.cpp
+ Source/QtScript/dnqsoutputcell.cpp \
+ Source/platform/qt/qtdnstorageimpl.cpp \
+ Source/DNStorage.cpp
HEADERS += Source/QtDennco/mainwindow.h \
Source/TKUICell.h \
Source/platform/qt/qtdnthreadimpl.h \
Source/QtDennco/dnwebinterface.h \
Source/QtScript/dnqsinputcell.h \
- Source/QtScript/dnqsoutputcell.h
+ Source/QtScript/dnqsoutputcell.h \
+ Source/DNStorageImpl.h \
+ Source/DNStorage.h \
+ Source/platform/qt/qtdnstorageimpl.h
FORMS += Source/QtDennco/mainwindow.ui
Debug:DEFINES+=DEBUG