| 1 | /* Copyright (C) 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "MazDBQueryEngine.h" |
| 19 | #include <QDebug> |
| 20 | #include "QJsEngineShim.h" |
| 21 | |
| 22 | MazDBQueryEngine::MazDBQueryEngine(QObject *parent) : QObject(parent), |
| 23 | m_db(Q_NULLPTR), m_query(Q_NULLPTR) |
| 24 | { |
| 25 | m_db = new MazDB(this); |
| 26 | connect(m_db, &MazDB::filenameChanged, this, &MazDBQueryEngine::sourceChanged); |
| 27 | } |
| 28 | |
| 29 | MazDBQuery *MazDBQueryEngine::query() const |
| 30 | { |
| 31 | return m_query; |
| 32 | } |
| 33 | |
| 34 | QString MazDBQueryEngine::source() const |
| 35 | { |
| 36 | return m_db->filename(); |
| 37 | } |
| 38 | |
| 39 | bool MazDBQueryEngine::queryStream(MazCallBack callback) |
| 40 | { |
| 41 | return m_query->runQuery(m_db, callback); |
| 42 | } |
| 43 | |
| 44 | bool MazDBQueryEngine::queryStream(QJSValue callback) |
| 45 | { |
| 46 | if (!callback.isCallable()) |
| 47 | return false; |
| 48 | |
| 49 | MazCallBack func = [this, &callback](QString key, QVariant value) { |
| 50 | QJSEngine *engine = qjsEngine(this); |
| 51 | QJSValueList list; |
| 52 | list << QJSValue(key); |
| 53 | list << engine->toScriptValue<QVariant>(value); |
| 54 | bool result = callback.call(list).toBool(); |
| 55 | return result; |
| 56 | }; |
| 57 | return queryStream(func); |
| 58 | } |
| 59 | |
| 60 | void MazDBQueryEngine::setSource(const QString &src) |
| 61 | { |
| 62 | if (source() != src) { |
| 63 | m_db->setFilename(src); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void MazDBQueryEngine::setQuery(MazDBQuery *query) |
| 68 | { |
| 69 | if (m_query == query) |
| 70 | return; |
| 71 | |
| 72 | m_query = query; |
| 73 | emit queryChanged(query); |
| 74 | } |
| 75 |