/** Test compound where clause, using the AND operator. */ void tst_QDjangoWhere::andWhere() { QDjangoWhere testQuery; const QDjangoWhere queryId("id", QDjangoWhere::Equals, 1); const QDjangoWhere queryUsername("username", QDjangoWhere::Equals, "foo"); testQuery = queryId && queryUsername; CHECKWHERE(testQuery, QLatin1String("id = ? AND username = ?"), QVariantList() << 1 << "foo"); // and with "all" queryset testQuery = QDjangoWhere() && queryId; CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); testQuery = queryId && QDjangoWhere(); CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); // and with "none" queryset testQuery = !QDjangoWhere() && queryId; QCOMPARE(testQuery.isNone(), true); CHECKWHERE(testQuery, QLatin1String("1 != 0"), QVariantList()); testQuery = queryId && !QDjangoWhere(); QCOMPARE(testQuery.isNone(), true); CHECKWHERE(testQuery, QLatin1String("1 != 0"), QVariantList()); // negation testQuery = !(queryId && queryUsername); CHECKWHERE(testQuery, QLatin1String("NOT (id = ? AND username = ?)"), QVariantList() << 1 << "foo"); }
/** Test compound where clause, using the OR operator. */ void tst_QDjangoWhere::orWhere() { QDjangoWhere testQuery; const QDjangoWhere queryId("id", QDjangoWhere::Equals, 1); const QDjangoWhere queryUsername("username", QDjangoWhere::Equals, "foo"); testQuery = queryId || queryUsername; CHECKWHERE(testQuery, QLatin1String("id = ? OR username = ?"), QVariantList() << 1 << "foo"); // or with "all" queryset testQuery = QDjangoWhere() || queryId; QCOMPARE(testQuery.isAll(), true); CHECKWHERE(testQuery, QString(), QVariantList()); testQuery = queryId || QDjangoWhere(); QCOMPARE(testQuery.isAll(), true); CHECKWHERE(testQuery, QString(), QVariantList()); // or with "none" queryset testQuery = !QDjangoWhere() || queryId; CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); testQuery = queryId || !QDjangoWhere(); CHECKWHERE(testQuery, QLatin1String("id = ?"), QVariantList() << 1); // negation testQuery = !(queryId || queryUsername); CHECKWHERE(testQuery, QLatin1String("NOT (id = ? OR username = ?)"), QVariantList() << 1 << "foo"); }
/** Combines the current QDjangoWhere with the \a other QDjangoWhere using * a logical OR. * * \param other */ QDjangoWhere QDjangoWhere::operator||(const QDjangoWhere &other) const { if (isAll() || other.isNone()) return *this; else if (isNone() || other.isAll()) return other; QDjangoWhere result; result.m_combine = OrCombine; result.m_children << *this << other; return result; }
/** Test empty where clause. */ void tst_QDjangoWhere::emptyWhere() { // construct empty where clause QDjangoWhere testQuery; QCOMPARE(testQuery.isAll(), true); QCOMPARE(testQuery.isNone(), false); CHECKWHERE(testQuery, QString(), QVariantList()); // negate the where clause testQuery = !QDjangoWhere(); QCOMPARE(testQuery.isAll(), false); QCOMPARE(testQuery.isNone(), true); CHECKWHERE(testQuery, QLatin1String("1 != 0"), QVariantList()); }
void tst_QDjangoWhere::toString() { QDjangoWhere testQuery; const QDjangoWhere queryId("id", QDjangoWhere::Equals, 1); const QDjangoWhere queryUsername("username", QDjangoWhere::Equals, "foo"); testQuery = queryId; QCOMPARE(testQuery.toString(), QLatin1String("QDjangoWhere(key=\"id\", operation=\"Equals\", value=\"1\", negate=false)")); testQuery = queryId || queryUsername; QCOMPARE(testQuery.toString(), QLatin1String("QDjangoWhere(key=\"id\", operation=\"Equals\", value=\"1\", negate=false) || QDjangoWhere(key=\"username\", operation=\"Equals\", value=\"foo\", negate=false)")); testQuery = queryId && queryUsername; QCOMPARE(testQuery.toString(), QLatin1String("QDjangoWhere(key=\"id\", operation=\"Equals\", value=\"1\", negate=false) && QDjangoWhere(key=\"username\", operation=\"Equals\", value=\"foo\", negate=false)")); }
/** Combines the current QDjangoWhere with the \a other QDjangoWhere using * a logical OR. * * \param other */ QDjangoWhere QDjangoWhere::operator||(const QDjangoWhere &other) const { if (isAll() || other.isNone()) return *this; else if (isNone() || other.isAll()) return other; if (d->combine == QDjangoWherePrivate::OrCombine) { QDjangoWhere result = *this; result.d->children << other; return result; } QDjangoWhere result; result.d->combine = QDjangoWherePrivate::OrCombine; result.d->children << *this << other; return result; }
static QScriptValue whereToString(QScriptContext *context, QScriptEngine *engine) { QDjangoWhere q = engine->fromScriptValue<QDjangoWhere>(context->thisObject()); return engine->toScriptValue(QLatin1String("Q(") + q.sql(QDjango::database()) + QLatin1String(")")); }