From: Kevron Rees Date: Thu, 20 Nov 2014 04:39:34 +0000 (-0800) Subject: bluemonkey database module X-Git-Tag: 0.12.903~50 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6d37323f87e07ab767225d522c8e2161a969c633;p=profile%2Fivi%2Fautomotive-message-broker.git bluemonkey database module --- diff --git a/plugins/bluemonkey/CMakeLists.txt b/plugins/bluemonkey/CMakeLists.txt index 8edad58..6fdbae3 100644 --- a/plugins/bluemonkey/CMakeLists.txt +++ b/plugins/bluemonkey/CMakeLists.txt @@ -29,6 +29,15 @@ if(communi) install(TARGETS bluemonkeyIrcModule LIBRARY DESTINATION ${PLUGIN_INSTALL_PATH}) endif(communi) +find_package(Qt5Sql) + +if(Qt5Sql_FOUND) + message(STATUS "enabling database bluemonkey module") + add_library(bluemonkeyDbModule MODULE db.cpp) + set_target_properties(bluemonkeyDbModule PROPERTIES PREFIX "") + target_link_libraries(bluemonkeyDbModule ${link_libraries} ${QT_LIBRARIES} ${Qt5Sql_LIBRARIES}) + install(TARGETS bluemonkeyDbModule LIBRARY DESTINATION ${PLUGIN_INSTALL_PATH}) +endif() include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${communi_INCLUDE_DIRS} ${QT_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/plugins/common) diff --git a/plugins/bluemonkey/bluemonkey.cpp b/plugins/bluemonkey/bluemonkey.cpp index 632f26f..081ec31 100644 --- a/plugins/bluemonkey/bluemonkey.cpp +++ b/plugins/bluemonkey/bluemonkey.cpp @@ -102,6 +102,7 @@ BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* e, map con auth = new Authenticate(config, this); qmlRegisterType("", 1, 0, "QTimer"); + qmlRegisterType("", 1, 0, "QObject"); } @@ -275,6 +276,11 @@ QObject *BluemonkeySink::createTimer() return new QTimer(this); } +QObject *BluemonkeySink::createQObject() +{ + return new QObject(this); +} + void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTime end, QJSValue cbFunction) { double b = (double)begin.toMSecsSinceEpoch() / 1000.0; diff --git a/plugins/bluemonkey/bluemonkey.h b/plugins/bluemonkey/bluemonkey.h index 99cbf63..8617f7e 100644 --- a/plugins/bluemonkey/bluemonkey.h +++ b/plugins/bluemonkey/bluemonkey.h @@ -99,8 +99,6 @@ public: virtual void propertyChanged(AbstractPropertyType* value); virtual const std::string uuid() const; - QJSEngine* engine; - virtual int supportedOperations(); private: //source privates @@ -133,6 +131,7 @@ public Q_SLOTS: void log(QString str); QObject* createTimer(); + QObject* createQObject(); void getHistory(QStringList properties, QDateTime begin, QDateTime end, QJSValue cbFunction); @@ -149,6 +148,7 @@ public Q_SLOTS: void createCustomProperty(QString name, QJSValue defaultValue, int zone); private: + QJSEngine* engine; QStringList configsToLoad; Authenticate* auth; diff --git a/plugins/bluemonkey/config.js b/plugins/bluemonkey/config.js index 15cd0de..c4a6987 100644 --- a/plugins/bluemonkey/config.js +++ b/plugins/bluemonkey/config.js @@ -12,6 +12,7 @@ var Zone = { BackSide : 1 << 9 }; +bluemonkey.loadModule(""); bluemonkey.createCustomProperty("VehicleSpeed", 10); bluemonkey.createCustomProperty("EngineSpeed", 5000); diff --git a/plugins/bluemonkey/db.cpp b/plugins/bluemonkey/db.cpp new file mode 100644 index 0000000..07bb9e3 --- /dev/null +++ b/plugins/bluemonkey/db.cpp @@ -0,0 +1,90 @@ +#include "db.h" + +#include + +#include +#include +#include +#include + +extern "C" std::map create(std::map config, QObject* parent) +{ + std::map moduleInstances; + moduleInstances["database"] = new BluemonkeyDatabaseModule(parent); + return moduleInstances; +} + +bool Database::open(QString connectionName, QString filename) +{ + db = QSqlDatabase::addDatabase("QSQLITE", connectionName); + db.setDatabaseName(filename); + return db.open(); +} + +void Database::close() +{ + if(db.isOpen()) + { + db.close(); + } +} + +QObject *Database::exec(QString query) +{ + return new Query(db.exec(query), this); +} + +QString Database::lastError() +{ + return db.lastError().text(); +} + + +Query::Query(QSqlQuery q, QObject* parent) + :QObject(parent) +{ + query = q; +} + +Query::Query(QString connectionName) +{ + setConnectionName(connectionName); +} + +void Query::setConnectionName(QString connectionName) +{ + mConnectionName = connectionName; + db = QSqlDatabase::database(connectionName); + query = QSqlQuery(db); +} + +bool Query::exec(QString queryStr) +{ + query.setNumericalPrecisionPolicy(QSql::HighPrecision); + return query.exec(queryStr); +} + +QVariantList Query::results() +{ + QVariantList results; + + QSqlRecord r = query.record(); + + while(query.next()) + { + + QVariantMap prop; + ///iterate on the properties: + for(int i=0; i< r.count(); i++) + { + QString name = r.fieldName(i); + + QVariant val = query.value(i); + prop[name] = val; + } + + results << prop; + } + + return results; +} diff --git a/plugins/bluemonkey/db.h b/plugins/bluemonkey/db.h new file mode 100644 index 0000000..32716df --- /dev/null +++ b/plugins/bluemonkey/db.h @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include + +class Database; +class Query; + +class Database: public QObject +{ + Q_OBJECT +public: + Database(QObject* parent = nullptr):QObject(parent){ } + ~Database() { close(); } + + QSqlDatabase database() { return db; } + +public Q_SLOTS: + + bool open(QString connectionName, QString filename); + void close(); + + QObject* exec(QString query); + + QString lastError(); + +private: + QSqlDatabase db; +}; + +class Query: public QObject +{ + Q_OBJECT +public: + Query(QObject* parent = nullptr):QObject(parent){} + Query(QSqlQuery q, QObject* parent = nullptr); + Q_INVOKABLE Query(QString connectionName); +public Q_SLOTS: + void setConnectionName(QString connectionName); + + bool exec(QString queryStr); + + QVariantList results(); + +private: + QSqlQuery query; + QString mConnectionName; + QSqlDatabase db; +}; + + +class BluemonkeyDatabaseModule : public QObject +{ + Q_OBJECT +public: + BluemonkeyDatabaseModule(QObject* parent = nullptr): QObject(parent) { } + +public Q_SLOTS: + QObject* createNewDatabase() + { + return new Database(this); + } + + QObject* createNewQuery() + { + return new Query(this); + } +};