| 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 "MazDBQuery.h" |
| 19 | #include <QDebug> |
| 20 | #include <QRect> |
| 21 | #include <QRectF> |
| 22 | #include <functional> |
| 23 | #include "QJsEngineShim.h" |
| 24 | |
| 25 | QueryBase::QueryBase(QObject *parent) : QObject(parent), |
| 26 | m_matchRule(Equals) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | QString QueryBase::prop() const |
| 31 | { |
| 32 | return m_property; |
| 33 | } |
| 34 | |
| 35 | QVariant QueryBase::value() const |
| 36 | { |
| 37 | return m_value; |
| 38 | } |
| 39 | |
| 40 | QueryBase::MatchTypes QueryBase::matchType() const |
| 41 | { |
| 42 | return m_matchRule; |
| 43 | } |
| 44 | |
| 45 | bool QueryBase::hasMatch(const QVariant &value) |
| 46 | { |
| 47 | if (m_comparitor.isCallable()) { |
| 48 | QJSEngine *engine = qjsEngine(this); |
| 49 | QJSValueList list; |
| 50 | list << engine->toScriptValue<QVariant>(value); |
| 51 | return m_comparitor.call(list).toBool(); |
| 52 | } |
| 53 | |
| 54 | if (m_value.isNull()) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool matches = false; |
| 59 | QStringList supported; |
| 60 | switch(value.type()) { |
| 61 | case QVariant::Invalid: |
| 62 | matches = false; |
| 63 | break; |
| 64 | case QVariant::Uuid: |
| 65 | case QVariant::Url: |
| 66 | case QVariant::String: |
| 67 | case QVariant::ByteArray: |
| 68 | case QVariant::LongLong: |
| 69 | case QVariant::ULongLong: |
| 70 | case QVariant::Int: |
| 71 | case QVariant::UInt: |
| 72 | case QVariant::Bool: |
| 73 | case QVariant::Double: |
| 74 | case QVariant::Char: |
| 75 | case QVariant::KeySequence: |
| 76 | case QMetaType::QDate: |
| 77 | case QMetaType::QTime: |
| 78 | case QMetaType::QDateTime:{ |
| 79 | matches = compare(value); |
| 80 | break; |
| 81 | } |
| 82 | case QVariant::Rect: { |
| 83 | supported << "x" << "y" << "width" << "height"; |
| 84 | if (!m_property.isEmpty() && supported.contains(m_property)) { |
| 85 | QRect r = qvariant_cast<QRect>(value); |
| 86 | if (m_property == "x") { |
| 87 | matches = compare(r.x()); |
| 88 | } else if (m_property == "y") { |
| 89 | matches = compare(r.y()); |
| 90 | } else if (m_property == "height") { |
| 91 | matches = compare(r.height()); |
| 92 | } else if (m_property == "width") { |
| 93 | matches = compare(r.width()); |
| 94 | } |
| 95 | } else { |
| 96 | matches = compare(value); |
| 97 | } |
| 98 | break; |
| 99 | } |
| 100 | case QVariant::RectF: { |
| 101 | supported << "x" << "y" << "width" << "height"; |
| 102 | if (!m_property.isEmpty() && supported.contains(m_property)) { |
| 103 | QRectF r = qvariant_cast<QRectF>(value); |
| 104 | if (m_property == "x") { |
| 105 | matches = compare(r.x()); |
| 106 | } else if (m_property == "y") { |
| 107 | matches = compare(r.y()); |
| 108 | } else if (m_property == "height") { |
| 109 | matches = compare(r.height()); |
| 110 | } else if (m_property == "width") { |
| 111 | matches = compare(r.width()); |
| 112 | } |
| 113 | } else { |
| 114 | matches = compare(value); |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | case QVariant::Size: { |
| 119 | supported << "width" << "height"; |
| 120 | if (!m_property.isEmpty() && supported.contains(m_property)) { |
| 121 | QSize s = qvariant_cast<QSize>(value); |
| 122 | if (m_property == "height") { |
| 123 | matches = compare(s.height()); |
| 124 | } else if (m_property == "width") { |
| 125 | matches = compare(s.width()); |
| 126 | } |
| 127 | } else { |
| 128 | matches = compare(value); |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | case QVariant::SizeF: { |
| 133 | supported << "width" << "height"; |
| 134 | if (!m_property.isEmpty() && supported.contains(m_property)) { |
| 135 | QSizeF s = qvariant_cast<QSizeF>(value); |
| 136 | if (m_property == "height") { |
| 137 | matches = compare(s.height()); |
| 138 | } else if (m_property == "width") { |
| 139 | matches = compare(s.width()); |
| 140 | } |
| 141 | } else { |
| 142 | matches = compare(value); |
| 143 | } |
| 144 | break; |
| 145 | } |
| 146 | case QVariant::Point: { |
| 147 | supported << "x" << "y"; |
| 148 | if (!m_property.isEmpty() && supported.contains(m_property)) { |
| 149 | QPoint p = qvariant_cast<QPoint>(value); |
| 150 | if (m_property == "x") { |
| 151 | matches = compare(p.x()); |
| 152 | } else if (m_property == "y") { |
| 153 | matches = compare(p.y()); |
| 154 | } |
| 155 | } else { |
| 156 | matches = compare(value); |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | case QVariant::PointF: { |
| 161 | supported << "x" << "y"; |
| 162 | if (!m_property.isEmpty() && supported.contains(m_property)) { |
| 163 | QPointF p = qvariant_cast<QPointF>(value); |
| 164 | if (m_property == "x") { |
| 165 | matches = compare(p.x()); |
| 166 | } else if (m_property == "y") { |
| 167 | matches = compare(p.y()); |
| 168 | } |
| 169 | } else { |
| 170 | matches = compare(value); |
| 171 | } |
| 172 | break; |
| 173 | } |
| 174 | case QMetaType::QVariantHash: |
| 175 | case QMetaType::QVariantMap: { |
| 176 | if (!m_property.isEmpty()) { |
| 177 | QVariantMap map = value.toMap(); |
| 178 | if (map.contains(m_property)) { |
| 179 | matches = compare(map.value(m_property)); |
| 180 | } else if (m_property == "*") { |
| 181 | for (auto key : map.keys()) { |
| 182 | matches = compare(map.value(key)); |
| 183 | if (matches) { |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | } else { |
| 188 | QStringList path = m_property.split('.', QString::SplitBehavior::SkipEmptyParts); |
| 189 | QVariant val = value; |
| 190 | for (const QString &key : path) { |
| 191 | QVariantMap m = val.toMap(); |
| 192 | if (m.isEmpty() || (val = m.value(key)).isNull()) { |
| 193 | return false; |
| 194 | } |
| 195 | matches = compare(val); |
| 196 | } |
| 197 | } |
| 198 | } else { |
| 199 | qWarning() << "No property declared for QVariantMap/Hash"; |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | default: { |
| 206 | #ifndef QT_NO_DATASTREAM |
| 207 | #else |
| 208 | Q_ASSERT(!"MazDB: Cannot save custom types without QDataStream support"); |
| 209 | #endif |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | return matches; |
| 214 | } |
| 215 | |
| 216 | QJSValue QueryBase::comparitor() const |
| 217 | { |
| 218 | return m_comparitor; |
| 219 | } |
| 220 | |
| 221 | void QueryBase::setProp(QString property) |
| 222 | { |
| 223 | if (m_property == property) |
| 224 | return; |
| 225 | |
| 226 | m_property = property; |
| 227 | emit queryChanged(); |
| 228 | } |
| 229 | |
| 230 | void QueryBase::setValue(QVariant value) |
| 231 | { |
| 232 | if (m_value == value) |
| 233 | return; |
| 234 | |
| 235 | m_value = value; |
| 236 | emit queryChanged(); |
| 237 | } |
| 238 | |
| 239 | void QueryBase::setMatchType(QueryBase::MatchTypes matchRule) |
| 240 | { |
| 241 | if (m_matchRule == matchRule) |
| 242 | return; |
| 243 | |
| 244 | m_matchRule = matchRule; |
| 245 | emit queryChanged(); |
| 246 | } |
| 247 | |
| 248 | void QueryBase::setComparitor(QJSValue comparitor) |
| 249 | { |
| 250 | m_comparitor = comparitor; |
| 251 | emit queryChanged(); |
| 252 | } |
| 253 | |
| 254 | bool QueryBase::compare(const QVariant &value) |
| 255 | { |
| 256 | switch(m_matchRule) { |
| 257 | case Equals: |
| 258 | return value == m_value; |
| 259 | case NotEquals: |
| 260 | return value != m_value; |
| 261 | case LessThan: |
| 262 | return value < m_value; |
| 263 | case LessThanOrEqual: |
| 264 | return value <= m_value; |
| 265 | case GreaterThan: |
| 266 | return value > m_value; |
| 267 | case GreaterThanOrEqual: |
| 268 | return value >= m_value; |
| 269 | default: |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | MazDBQuery::MazDBQuery(QObject *parent) : QObject(parent) |
| 275 | { |
| 276 | } |
| 277 | |
| 278 | Range *MazDBQuery::range() |
| 279 | { |
| 280 | return &m_range; |
| 281 | } |
| 282 | |
| 283 | int MazDBQuery::limit() const |
| 284 | { |
| 285 | return m_limit; |
| 286 | } |
| 287 | |
| 288 | QQmlListProperty<QueryBase> MazDBQuery::query() |
| 289 | { |
| 290 | return QQmlListProperty<QueryBase>(this, m_queries); |
| 291 | } |
| 292 | |
| 293 | bool MazDBQuery::runQuery(MazDB *db, MazCallBack callback) |
| 294 | { |
| 295 | QStringList keys; |
| 296 | |
| 297 | if (m_range.hasRange()) { |
| 298 | MazCallBack func = [&keys](QString key, QVariant value) { |
| 299 | Q_UNUSED(value); |
| 300 | keys << key; |
| 301 | return true; |
| 302 | }; |
| 303 | db->rangeStream(func, m_range.start(), m_range.end()); |
| 304 | } else { |
| 305 | MazKeyCallBack func = [&keys](QString key) { |
| 306 | keys << key; |
| 307 | return true; |
| 308 | }; |
| 309 | db->keyStream(func); |
| 310 | } |
| 311 | |
| 312 | if (keys.isEmpty()) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | auto runWHEREorORqueries = [&](const QStringList &keys) -> QStringList { |
| 317 | QStringList r; |
| 318 | WhereQuery *whereQuery = getWhereQuery(); |
| 319 | OrQuery *orQuery = getOrQuery(); |
| 320 | if (whereQuery) { |
| 321 | for (const QString &key : keys) { |
| 322 | QVariant val = db->get(key); |
| 323 | if (whereQuery->hasMatch(val)) { |
| 324 | r << key; |
| 325 | } else if (orQuery && orQuery->hasMatch(val)) { |
| 326 | r << key; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return r; |
| 331 | }; |
| 332 | |
| 333 | auto runANDqueries = [&](const QStringList &keys) -> QStringList { |
| 334 | QStringList r = keys; |
| 335 | QList<AndQuery *> andQueries = getAndQueries(); |
| 336 | if (!andQueries.isEmpty()) { |
| 337 | for (AndQuery *andQuery : andQueries) { |
| 338 | QStringList tmpKeys = r; |
| 339 | for (const QString &key : tmpKeys) { |
| 340 | QVariant val = db->get(key); |
| 341 | if (!andQuery->hasMatch(val)) { |
| 342 | r.removeAll(key); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | return r; |
| 348 | }; |
| 349 | |
| 350 | QStringList results = runWHEREorORqueries(keys); |
| 351 | if (!results.isEmpty()) { |
| 352 | results = runANDqueries(results); |
| 353 | } |
| 354 | |
| 355 | for (const QString &result : results) { |
| 356 | callback(result, db->get(result)); |
| 357 | } |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | void MazDBQuery::setLimit(int limit) |
| 363 | { |
| 364 | if (m_limit == limit) |
| 365 | return; |
| 366 | |
| 367 | m_limit = limit; |
| 368 | emit limitChanged(limit); |
| 369 | } |
| 370 | |
| 371 | WhereQuery *MazDBQuery::getWhereQuery() |
| 372 | { |
| 373 | for (QueryBase *query : m_queries) { |
| 374 | if (auto where = qobject_cast<WhereQuery *>(query)) { |
| 375 | return where; |
| 376 | } |
| 377 | } |
| 378 | qWarning() << "No WHERE clause!"; |
| 379 | return Q_NULLPTR; |
| 380 | } |
| 381 | |
| 382 | OrQuery *MazDBQuery::getOrQuery() |
| 383 | { |
| 384 | for (QueryBase *query : m_queries) { |
| 385 | if (auto orQuery = qobject_cast<OrQuery*>(query)) { |
| 386 | return orQuery; |
| 387 | } |
| 388 | } |
| 389 | return Q_NULLPTR; |
| 390 | } |
| 391 | |
| 392 | QList<AndQuery *> MazDBQuery::getAndQueries() |
| 393 | { |
| 394 | QList<AndQuery *> r; |
| 395 | for (QueryBase *query : m_queries) { |
| 396 | if (auto andQuery = qobject_cast<AndQuery*>(query)) { |
| 397 | r << andQuery; |
| 398 | } |
| 399 | } |
| 400 | return r; |
| 401 | } |
| 402 | |
| 403 | Range::Range(QObject *parent) : QObject(parent) |
| 404 | { |
| 405 | } |
| 406 | |
| 407 | bool Range::hasRange() const |
| 408 | { |
| 409 | return (!m_start.isEmpty() && !m_end.isEmpty()); |
| 410 | } |
| 411 | |
| 412 | QString Range::start() const |
| 413 | { |
| 414 | return m_start; |
| 415 | } |
| 416 | |
| 417 | QString Range::end() const |
| 418 | { |
| 419 | return m_end; |
| 420 | } |
| 421 | |
| 422 | void Range::setStart(QString start) |
| 423 | { |
| 424 | if (m_start == start) |
| 425 | return; |
| 426 | |
| 427 | m_start = start; |
| 428 | emit startChanged(start); |
| 429 | } |
| 430 | |
| 431 | void Range::setEnd(QString end) |
| 432 | { |
| 433 | if (m_end == end) |
| 434 | return; |
| 435 | |
| 436 | m_end = end; |
| 437 | emit endChanged(end); |
| 438 | } |