예제 #1
0
bool MojoCompareMatcher::Match(const MojObject& response)
{
	LOG_AM_TRACE("Entering function %s", __FUNCTION__);

	if (response.contains(m_key)) {
		MojObject value;
		MojString valueString;
		response.get(m_key, value);
		value.stringValue(valueString);
		if (value != m_value) {
			MojString oldValueString;
			m_value.stringValue(oldValueString);

			LOG_AM_DEBUG("Compare Matcher: Comparison key \"%s\" value changed from \"%s\" to \"%s\".  Firing.", 
				m_key.data(),
				oldValueString.data(), valueString.data());

			return true;
		} else {
			LOG_AM_DEBUG("Compare Matcher: Comparison key \"%s\" value \"%s\" unchanged.",
				m_key.data(), valueString.data());
		}
	} else {
		LOG_AM_DEBUG("Compare Matcher: Comparison key (%s) not present.",
			m_key.data());
	}

	return false;
}
void MojoNewWhereMatcher::ValidateOp(const MojObject& op,
	const MojObject& val) const
{
	if (op.type() != MojObject::TypeString) {
		throw std::runtime_error("Operation must be specified as a string "
			"property");
	}

	MojString opStr;
	MojErr err = op.stringValue(opStr);
	if (err) {
		throw std::runtime_error("Failed to convert operation to "
			"string value");
	}

	if ((opStr != "<") && (opStr != "<=") && (opStr != "=") &&
		(opStr != ">=") && (opStr != ">") && (opStr != "!=") &&
		(opStr != "where")) {
		throw std::runtime_error("Operation must be one of '<', '<=', "
			"'=', '>=', '>', '!=', and 'where'");
	}

	if (opStr == "where") {
		ValidateClauses(val);
	}
}
MojoNewWhereMatcher::MatchResult MojoNewWhereMatcher::CheckProperty(
	const MojObject& key, const MojObject& response, const MojObject& op,
	const MojObject& val, MatchMode mode) const
{
	if (key.type() == MojObject::TypeString) {
		MojString keyStr;
		MojErr err = key.stringValue(keyStr);
		if (err) {
			throw std::runtime_error("Failed to convert property lookup key "
				"to string");
		}

		MojObject propVal;
		bool found = response.get(keyStr.data(), propVal);
		if (!found) {
			return NoProperty;
		}

		return CheckMatch(propVal, op, val);

	} else if (key.type() == MojObject::TypeArray) {
		return CheckProperty(key, key.arrayBegin(), response, op, val, mode);
	} else {
		throw std::runtime_error("Key specified was neither a string or "
			"array of strings");
	}
}
예제 #4
0
bool MojoCompareMatcher::Match(const MojObject& response)
{
	MojLogTrace(s_log);

	if (response.contains(m_key)) {
		MojObject value;
		MojString valueString;
		response.get(m_key, value);
		value.stringValue(valueString);
		if (value != m_value) {
			MojString oldValueString;
			m_value.stringValue(oldValueString);

			MojLogInfo(s_log, _T("Compare Matcher: Comparison key \"%s\" "
				"value changed from \"%s\" to \"%s\".  Firing."), m_key.data(),
				oldValueString.data(), valueString.data());

			return true;
		} else {
			MojLogInfo(s_log, _T("Compare Matcher: Comparison key \"%s\" "
				"value \"%s\" unchanged."), m_key.data(), valueString.data());
		}
	} else {
		MojLogInfo(s_log, _T("Compare Matcher: Comparison key (%s) not "
			"present."), m_key.data());
	}

	return false;
}
예제 #5
0
MojErr MojObjectTest::emptyTest(MojObject& obj)
{
    MojObject obj2;
    MojString str1;
    MojString str2;

    bool found = false;

    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());

    MojTestAssert(obj.begin() == obj.end());
    MojTestAssert(!obj.contains(_T("hello")));
    MojTestAssert(!obj.get(_T("hi"), obj2));
    MojErr err = obj.del(_T("hello"), found);
    MojTestErrCheck(err);

    obj2.assign(obj);
    MojTestAssert(obj2.type() == obj.type());
    MojTestAssert(obj2 == obj);
    MojTestAssert(!(obj2 != obj));

    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    err = obj2.stringValue(str2);
    MojTestErrCheck(err);
    MojTestAssert(str1 == str2);

    return MojErrNone;
}
예제 #6
0
MojErr MojDbPropExtractor::handleVal(const MojObject& val, KeySet& valsOut, MojSize idx) const
{
    LOG_TRACE("Entering function %s", __FUNCTION__);

	MojAssert(idx < m_prop.size());

	MojErr err = MojErrNone;
	if (idx == m_prop.size() - 1) {
		// if we're at the end of the prop path, use this object as the value
		if (m_tokenizer.get() && val.type() == MojObject::TypeString) {
			MojString text;
			err = val.stringValue(text);
			MojErrCheck(err);
			if (m_tokenizer.get()) {
				err = m_tokenizer->tokenize(text, m_collator.get(), valsOut);
				MojErrCheck(err);
			}
		} else {
			MojDbKey key;
			err = key.assign(val, m_collator.get());
			MojErrCheck(err);
			err = valsOut.put(key);
			MojErrCheck(err);
		}
	} else {
		// otherwise, keep recursing
		err = valsImpl(val, valsOut, idx + 1);
		MojErrCheck(err);
	}
	return MojErrNone;
}
MojoObjectString::MojoObjectString(const MojObject& obj)
{
	MojErr err = obj.stringValue(m_str);

	/* Intentially ignore err here.  This is for debug output, let's not
	 * make the problem worse... */
	(void)err;
}
예제 #8
0
파일: MojDb.cpp 프로젝트: KyleMaas/db8
MojErr MojDb::getLocale(MojString& valOut, MojDbReq& req)
{
	MojObject curLocale;
	MojErr err = getState(LocaleKey, curLocale, req);
	MojErrCheck(err);
	err = curLocale.stringValue(valOut);
	MojErrCheck(err);

	return MojErrNone;
}
예제 #9
0
MojErr MojDbQuery::Page::fromObject(const MojObject& obj)
{
	MojString str;
	MojErr err = obj.stringValue(str);
	MojErrCheck(err);
	err = str.base64Decode(m_key.byteVec());
	MojErrCheck(err);

	return MojErrNone;
}
예제 #10
0
bool MojoWhereMatcher::GetObjectValue(const MojObject& key,
	const MojObject& response, MojObject& value) const
{
	if (key.type() == MojObject::TypeString) {
		MojString keyStr;
		MojErr err = key.stringValue(keyStr);
		if (err) {
			throw std::runtime_error("Error attempting to retrieve key name "
				"getting object value");
		}

		if (response.contains(keyStr)) {
			return response.get(keyStr, value);
		} else {
			return false;
		}
	} else if (key.type() == MojObject::TypeArray) {
		MojObject cur = response;
		for (MojObject::ConstArrayIterator iter = key.arrayBegin();
			iter != key.arrayEnd(); ++iter) {
			const MojObject& keyObj = *iter; 

			if (keyObj.type() != MojObject::TypeString) {
				throw std::runtime_error("Non-string found in property "
					"specifiction array");
			}

			MojString keyStr;
			MojErr err = keyObj.stringValue(keyStr);
			if (err) {
				throw std::runtime_error("Failed to access lookup key");
			}

			if (cur.contains(keyStr)) {
				MojObject tmp;
				if (!cur.get(keyStr, tmp)) {
					return false;
				} else {
					cur = tmp;
				}
			} else {
				return false;
			}
		}

		value = cur;
		return true;
	} else {
		throw std::runtime_error("Property specification must be the "
			"property name or array of property names");
	}
}
예제 #11
0
MojErr MojDbLunaServiceApp::configure(const MojObject& conf)
{
    LOG_TRACE("Entering function %s", __FUNCTION__);

    MojErr err = Base::configure(conf);
    MojErrCheck(err);

    m_conf = conf;

    conf.get(MojDbStorageEngine::engineFactory()->name(), m_engineConf);
    err = m_env->configure(m_engineConf);
    MojErrCheck(err);
    err = m_mainService.db().configure(conf);
    MojErrCheck(err);

    MojObject dbConf;
    err = conf.getRequired("db", dbConf);
    MojErrCheck(err);

    MojObject dbPath;
    err = dbConf.getRequired("path", dbPath);
    MojErrCheck(err);
    err = dbPath.stringValue(m_dbDir);
    MojErrCheck(err);

    MojObject serviceName;
    err = dbConf.getRequired("service_name", serviceName);
    MojErrCheck(err);
    err = serviceName.stringValue(m_serviceName);
    MojErrCheck(err);

    err = m_mainService.configure(dbConf);
    MojErrCheck(err);

    return MojErrNone;
}
예제 #12
0
MojErr MojDbQueryPlan::pushSearch(MojDbKeyBuilder& lowerBuilder, MojDbKeyBuilder& upperBuilder,
		const MojObject& val, MojDbTextCollator* collator)
{
	// get text
	MojString text;
	MojErr err = val.stringValue(text);
	MojErrCheck(err);
	MojDbKeyBuilder::KeySet toks;

	// tokenize
	MojRefCountedPtr<MojDbTextTokenizer> tokenizer(new MojDbTextTokenizer);
	MojAllocCheck(tokenizer.get());
	err = tokenizer->init(m_locale);
	MojErrCheck(err);
	err = tokenizer->tokenize(text, collator, toks);
	MojErrCheck(err);

	// remove prefixes
	MojDbKeyBuilder::KeySet::Iterator i;
	err = toks.begin(i);
	MojErrCheck(err);
	MojDbKeyBuilder::KeySet::ConstIterator prev = toks.end();
	while (i != toks.end()) {
		if (prev != toks.end() && prev->stringPrefixOf(*i)) {
			bool found = false;
			err = toks.del(*prev, found);
			MojErrCheck(err);
			MojAssert(found);
		}
		prev = i;
		++i;
	}

	// push toks
	err = lowerBuilder.push(toks);
	MojErrCheck(err);
	err = upperBuilder.push(toks);
	MojErrCheck(err);
	m_groupCount = (MojUInt32) toks.size();

	return MojErrNone;
}
예제 #13
0
MojoNewWhereMatcher::MatchResult MojoNewWhereMatcher::CheckMatch(
	const MojObject& rhs, const MojObject& op, const MojObject& val) const
{
	MojString opStr;
	MojErr err = op.stringValue(opStr);
	if (err) {
		throw std::runtime_error("Failed to convert operation to string "
			"value");
	}

	bool result;

	if (opStr == "<") {
		result = (rhs < val);
	} else if (opStr == "<=") {
		result = (rhs <= val);
	} else if (opStr == "=") {
		result = (rhs == val);
	} else if (opStr == "!=") {
		result = (rhs != val);
	} else if (opStr == ">=") {
		result = (rhs >= val);
	} else if (opStr == ">") {
		result = (rhs > val);
	} else if (opStr == "where") {
		result = CheckClause(val, rhs, AndMode);
	} else {
		throw std::runtime_error("Unknown comparison operator in where "
			"clause");
	}

	if (result) {
		return Matched;
	} else {
		return NotMatched;
	}
}
int ConnectionManagerProxy::ConfidenceToInt(const MojObject& confidenceObj) const
{
	if (confidenceObj.type() != MojObject::TypeString) {
		LOG_WARNING(MSGID_NON_STRING_TYPE_NW_CONFIDENCE, 0, "Network confidence must be specified as a string");
		return ConnectionConfidenceUnknown;
	}

	MojString confidenceStr;
	MojErr err = confidenceObj.stringValue(confidenceStr);
	if (err != MojErrNone) {
		LOG_WARNING(MSGID_GET_NW_CONFIDENCE_LEVEL_FAIL, 0, "Failed to retreive network confidence level as string");
		return ConnectionConfidenceUnknown;
	}

	for (int i = 0; i < ConnectionConfidenceMax; ++i) {
		if (confidenceStr == ConnectionConfidenceNames[i]) {
			return i;
		}
	}

	LOG_DEBUG("Unknown connection confidence string: \"%s\"", confidenceStr.data() );

	return ConnectionConfidenceUnknown;
}
예제 #15
0
MojErr MojDbPerfUpdateTest::updateObjsViaMerge(MojDb& db, const MojChar* kindId, MojErr (MojDbPerfTest::*createFn) (MojObject&, MojUInt64))
{
	// register all the kinds
	MojTime time;
	MojErr err = putKinds(db, time);
	MojTestErrCheck(err);

	// put objects using createFn
	MojObject objs;
	err = putObjs(db, kindId, numInsertForPut, createFn, objs);
	MojTestErrCheck(err);

	MojObject midObj;
	bool found = objs.at(numInsertForPut/2, midObj);
	MojTestAssert(found);

	MojTime objTime;
	err = mergeObj(db, midObj, objTime);
	MojTestErrCheck(err);
	MojUInt64 mergeTime = objTime.microsecs();
	err = MojPrintF("\n -------------------- \n");
	MojTestErrCheck(err);
	err = MojPrintF("   merging single object - index %llu - of kind %s %llu times took: %llu microsecs\n", numInsertForPut/2, kindId, numMergeIterations, mergeTime);
	MojTestErrCheck(err);
	err = MojPrintF("   time per merge: %llu microsecs", (mergeTime) / (numMergeIterations));
	MojTestErrCheck(err);
	err = MojPrintF("\n\n");
	MojTestErrCheck(err);

	MojString buf;
	err = buf.format("Merge single object - index %llu - %llu times,%s,%llu,%llu,%llu,\n", numInsertForPut/2, numMergeIterations, kindId, mergeTime, mergeTime/numMergeIterations, mergeTime/(1*numMergeIterations));
	MojTestErrCheck(err);
	err = fileWrite(file, buf);
	MojTestErrCheck(err);

	MojTime batchTime;
	MojObject::ArrayIterator beginArr;
	err = objs.arrayBegin(beginArr);
	MojErrCheck(err);
	err = batchMergeObj(db, beginArr, beginArr + (numInsertForPut / 10), batchTime);
	MojTestErrCheck(err);
	mergeTime = batchTime.microsecs();
	err = MojPrintF("   merging batch - %llu objects - of kind %s %llu times took: %llu microsecs\n", numInsertForPut/10, kindId, numBatchMergeIterations, mergeTime);
	MojTestErrCheck(err);
	err = MojPrintF("   time per batch merge: %llu microsecs\n", (mergeTime) / (numBatchMergeIterations));
	MojTestErrCheck(err);
	err = MojPrintF("   time per object: %llu microsecs", (mergeTime) / (numInsertForPut/10 * numBatchMergeIterations));
	MojTestErrCheck(err);
	err = MojPrintF("\n\n");
	MojTestErrCheck(err);

	err = buf.format("Batch merge %llu objects %llu times,%s,%llu,%llu,%llu,\n", numInsertForPut/10, numBatchMergeIterations, kindId, mergeTime, mergeTime/numBatchMergeIterations, mergeTime/(numInsertForPut/10*numBatchMergeIterations));
	MojTestErrCheck(err);
	err = fileWrite(file, buf);
	MojTestErrCheck(err);

	MojTime mergeQueryTime;
	MojTestErrCheck(err);
	MojDbQuery query;
	err = query.from(kindId);
	MojTestErrCheck(err);
	query.limit(numInsertForPut/5);
	query.desc(true);

	MojObject queryObj;
	err = query.toObject(queryObj);
	MojTestErrCheck(err);
	MojString queryStr;
	err = queryObj.stringValue(queryStr);
	MojTestErrCheck(err);

	MojObject props;
	err = props.putString(_T("newKey"), _T("here's a new value"));
	MojTestErrCheck(err);
	MojUInt32 count;
	err = queryMergeObj(db, query, props, count, mergeQueryTime);
	MojTestErrCheck(err);
	mergeTime = mergeQueryTime.microsecs();
	err = MojPrintF("   merging with query - %d objects - of kind %s %llu times took: %llu microsecs\n", count, kindId, numBatchMergeIterations, mergeTime);
	MojTestErrCheck(err);
	err = MojPrintF("   time per merge: %llu microsecs\n", (mergeTime) / (numBatchMergeIterations));
	MojTestErrCheck(err);
	err = MojPrintF("   time per object: %llu microsecs", (mergeTime) / (count * numBatchMergeIterations));
	MojTestErrCheck(err);
	err = MojPrintF("\n\n");
	MojTestErrCheck(err);

	err = buf.format("Merge with query %s,,,,,\nMerge with query - %d objects - %llu times,%s,%llu,%llu,%llu,\n", queryStr.data(), count, numBatchMergeIterations, kindId, mergeTime, mergeTime/numBatchMergeIterations, mergeTime/(count*numBatchMergeIterations));
	MojTestErrCheck(err);
	err = fileWrite(file, buf);
	MojTestErrCheck(err);

	return MojErrNone;
}
예제 #16
0
파일: MojDb.cpp 프로젝트: KyleMaas/db8
MojErr MojDb::delKind(const MojObject& id, bool& foundOut, MojUInt32 flags, MojDbReqRef req)
{
	MojLogTrace(s_log);
	foundOut = false;

	MojErr err = beginReq(req, true);
	MojErrCheck(err);

	// del kind obj
	MojString idStr;
	err = id.stringValue(idStr);
	MojErrCheck(err);

	MojDbKind *pk = NULL;

	// If Kinds has sub-kinds, we give an error
	err = m_kindEngine.getKind(idStr.data(), pk);
	MojErrCheck(err);

	if (pk->nsubkinds() > 0) {
		MojLogWarning(s_log, _T("delKind_error: %s has %d subkinds \n"), idStr.data(), pk->nsubkinds());
		MojErrThrow(MojErrDbKindHasSubKinds);
	}

	//MojLogInfo(s_log, _T("delKind: %s \n"), idStr.data());
	MojLogWarning(s_log, _T("delKind: %s \n"), idStr.data());

	err = m_kindEngine.checkOwnerPermission(idStr, req);
	MojErrCheck(err);

	MojString dbId;
	err = formatKindId(idStr, dbId);
	MojErrCheck(err);
	MojObject deleted;
	bool found = false;
	MojDbAdminGuard adminGuard(req);
	err = delImpl(dbId, found, deleted, req, flags);
	MojErrCheck(err);

	if (found) {
		// del objects
		MojDbQuery query;
		err = query.from(idStr);
		MojErrCheck(err);
		err = query.includeDeleted(true);
		MojErrCheck(err);
		MojUInt32 count;
		req->fixmode(true); 
		err = delImpl(query, count, req, flags | FlagPurge);
		MojErrCheck(err);

		// del associated permissions
		query.clear();
		err = query.from(MojDbKindEngine::PermissionId);
		MojErrCheck(err);
		err = query.where(MojDbServiceDefs::ObjectKey, MojDbQuery::OpEq, idStr);
		MojErrCheck(err);
		req->fixmode(true);
		err = delImpl(query, count, req, flags);
		MojErrCheck(err);

		// del kind
		MojErr errAcc = m_kindEngine.delKind(idStr, req);
		MojErrAccumulate(err, errAcc);
	}
	err = commitKind(idStr, req, err);
	MojErrCheck(err);
	foundOut = found;

	return MojErrNone;
}
예제 #17
0
MojErr MojObjectTest::typeTest()
{
    MojObject obj;
    MojObject obj2;
    MojString str1;
    MojString str2;
    MojHashMap<MojObject, MojObject> map;

    // null
    MojTestAssert(obj.type() == MojObject::TypeUndefined);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    MojErr err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("null"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    MojTestAssert(obj == obj2);
    MojTestAssert(obj.compare(obj2) == 0);
    err = obj.coerce(MojObject::TypeNull);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeNull);
    err = obj.coerce(MojObject::TypeString);
    MojTestErrCheck(err);
    MojTestAssert(obj == str1);
    err = obj.coerce(MojObject::TypeBool);
    MojTestErrCheck(err);
    MojTestAssert(obj == true);
    err = obj.coerce(MojObject::TypeInt);
    MojTestErrCheck(err);
    MojTestAssert(obj == 1);
    err = obj.coerce(MojObject::TypeDecimal);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeDecimal && obj == MojDecimal(1, 0));
    err = obj.coerce(MojObject::TypeObject);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeObject);
    err = obj.coerce(MojObject::TypeArray);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeArray);

    // object
    err = obj.put(_T("hello"), 5);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeObject);
    MojTestAssert(obj.size() == 1);
    MojTestAssert(!obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("{\"hello\":5}"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    obj.clear(MojObject::TypeObject);
    MojTestAssert(obj.type() == MojObject::TypeObject);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("{}"));
    // array
    for (int i = 0; i < 1000; ++i) {
        err = obj.push(i);
        MojTestErrCheck(err);
    }
    MojTestAssert(obj.type() == MojObject::TypeArray);
    MojTestAssert(obj.size() == 1000);
    MojTestAssert(!obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    for (int i = 0; i < 1000; ++i) {
        MojTestAssert(obj.at(i, obj2));
        MojTestAssert(obj2 == i);
    }
    MojTestAssert(!obj.at(1000, obj2));
    err = obj.setAt(1001, 1001);
    MojTestErrCheck(err);
    MojTestAssert(obj.size() == 1002);
    MojTestAssert(obj.at(1000, obj2));
    MojTestAssert(obj2.type() == MojObject::TypeUndefined);
    obj.clear(MojObject::TypeArray);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("[]"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    // string
    err = str1.assign(_T("yo"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.type() == MojObject::TypeString);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str2);
    MojTestErrCheck(err);
    MojTestAssert(str1 == str2);
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    obj.clear(MojObject::TypeString);
    MojTestAssert(obj.boolValue() == false);
    err = str1.assign(_T("12345"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 12345);
    MojTestAssert(obj.decimalValue() == MojDecimal(12345, 0));
    err = str1.assign(_T("-67890"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == -67890);
    MojTestAssert(obj.decimalValue() == MojDecimal(-67890, 0));
    err = str1.assign(_T("12345000000000"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 12345000000000LL);
    err = str1.assign(_T("12345.6789"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 12345);
    MojTestAssert(obj.decimalValue() == MojDecimal(12345, 678900));
    err = str1.assign(_T("1.0e3"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 1);
    MojTestAssert(obj.decimalValue() == MojDecimal(1000, 0));
    err = str1.assign(_T("45hello"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 45);
    MojTestAssert(obj.decimalValue() == MojDecimal(45, 0));
    // bool
    obj = true;
    MojTestAssert(obj.type() == MojObject::TypeBool);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 1);
    MojTestAssert(obj.decimalValue() == MojDecimal(1, 0));
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("true"));
    obj.clear(MojObject::TypeBool);
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("false"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    // MojDecimal
    obj = MojDecimal(3, 140000);
    MojTestAssert(obj.type() == MojObject::TypeDecimal);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 3);
    MojTestAssert(obj.decimalValue() == MojDecimal(3.14));
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("3.14"));
    obj.clear(MojObject::TypeDecimal);
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("0.0"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    // MojDecimal
    obj = -987654321;
    MojTestAssert(obj.type() == MojObject::TypeInt);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == -987654321);
    MojTestAssert(obj.decimalValue() == MojDecimal(-987654321, 0));
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("-987654321"));
    obj.clear(MojObject::TypeInt);
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("0"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));

    return MojErrNone;
}
예제 #18
0
MojErr MojDbQuery::fromObject(const MojObject& obj)
{
	// TODO: validate against query schema

	bool found;
	MojErr err;
	MojObject array;
	MojString str;

	// distinct
	found = false;
	err = obj.get(DistinctKey, str, found);
	MojErrCheck(err);
	if (found) {
		err = distinct(str);
		MojErrCheck(err);
		// if "distinct" is set, force "distinct" column into "select".
		err = select(str);
		MojErrCheck(err);
		// order
		err = order(str);
		MojErrCheck(err);
	} else {
		// select
		if (obj.get(SelectKey, array)) {
			if(array.empty()) {
				MojErrThrowMsg(MojErrDbInvalidQuery, _T("db: select clause but no selected properties"));
			}
			MojObject prop;
			MojSize i = 0;
			while (array.at(i++, prop)) {
				MojErr err = prop.stringValue(str);
				MojErrCheck(err);
				err = select(str);
				MojErrCheck(err);
			}
		}
		// order
		found = false;
		err = obj.get(OrderByKey, str, found);
		MojErrCheck(err);
		if (found) {
			err = order(str);
			MojErrCheck(err);
		}
	}
	// from
	err = obj.getRequired(FromKey, str);
	MojErrCheck(err);
	err = from(str);
	MojErrCheck(err);
	// where
	if (obj.get(WhereKey, array)) {
		err = addClauses(m_whereClauses, array);
		MojErrCheck(err);
	}
	// filter
	if (obj.get(FilterKey, array)) {
		err = addClauses(m_filterClauses, array);
		MojErrCheck(err);
	}
	// desc
	bool descVal;
	if (obj.get(DescKey, descVal)) {
		desc(descVal);
	}
	// limit
	MojInt64 lim;
	if (obj.get(LimitKey, lim)) {
		if (lim < 0)
			MojErrThrowMsg(MojErrDbInvalidQuery, _T("db: negative query limit"));
	} else {
		lim = LimitDefault;
	}
	limit((MojUInt32) lim);
	// page
	MojObject pageObj;
	if (obj.get(PageKey, pageObj)) {
		Page pagec;
		err = pagec.fromObject(pageObj);
		MojErrCheck(err);
		page(pagec);
	}
	bool incDel = false;
	if (obj.get(IncludeDeletedKey, incDel) && incDel) {
		err = includeDeleted();
		MojErrCheck(err);
	}
	return MojErrNone;
}