Ejemplo n.º 1
0
		void testBSONSelect() {
			cout << "\ntestBSONSelect()" << endl;

			char* selectsimple;
			BSONObj* obj;
			BSONObj* expected;
			BSONObj* result;

			obj = BSONParser::parse("{ name: 'John', age: 35, one: { data: 1 }, children: [ { name: 'Joshua', age: 15}, { name: 'Mary', age: 30}] }");
			selectsimple = "$\"name\", $\"one.data\"";
			result = obj->select(const_cast<const char*>(selectsimple));
			expected = BSONParser::parse("{ name: 'John', one: { data: 1 }}");
			TEST_ASSERT(*result == *expected);
			delete expected;
			delete result;

			selectsimple = "*";
			result = obj->select(const_cast<const char*>(selectsimple));
			expected = new BSONObj(*obj);
			TEST_ASSERT(*result == *expected);

			delete obj;
		}
Ejemplo n.º 2
0
BSONObj* BSONObj::select(const char* sel) const {
	std::set<std::string> columns = bson_splitSelect(sel);
	bool include_all = (strcmp(sel, "*") == 0);

	BSONObj* result = new BSONObj();

	for (std::map<std::string, BSONContent* >::const_iterator i = this->_elements.begin(); i != this->_elements.end(); i++) {
		std::string key = i->first;
		if (include_all || (columns.find(key) != columns.end())) {
			BSONContent* origContent = i->second;

			switch (origContent->type()) {
				case BSON_TYPE:  
					{
						BSONContentBSON* bbson = (BSONContentBSON*)origContent;
						BSONObj* inner = (BSONObj*)*bbson;

						if (!include_all) {
							char* subselect = bson_subselect(sel, key.c_str());
							BSONObj* innerSubSelect = inner->select(subselect);
							result->add(key, *innerSubSelect);
							delete innerSubSelect;
						} else {
							result->add(key, *inner);
						}
						break;
					}
				case BSONARRAY_TYPE: 
					{
						BSONContentBSONArray* bbsonarray = (BSONContentBSONArray*)origContent;
						BSONArrayObj* innerArray = (BSONArrayObj*)*bbsonarray;
						if (!include_all) {
							char* subselect = bson_subselect(sel, key.c_str());
							BSONArrayObj* innerSubArray = innerArray->select(subselect);
							result->add(key, *innerSubArray);
							delete innerSubArray;
						} else {
							result->add(key, *innerArray);
						}
						break;
					}
				case BOOL_TYPE: 
					{
						BSONContentBoolean* bb = (BSONContentBoolean*)origContent;
						bool val = *bb;
						result->add(key, val);
						break;
					}
				case INT_TYPE: 
					{
						BSONContentInt* bint = (BSONContentInt*)origContent;
						__int32 val = *bint;
						result->add(key, val);
						break;
					}
				case LONG_TYPE:
					{
						BSONContentLong* blong = (BSONContentLong*)origContent;
						__int64 val = *blong;
						result->add(key, val);
						break;
					}
				case DOUBLE_TYPE:
					{
						BSONContentDouble* bdouble = (BSONContentDouble*)origContent;
						double val = *bdouble;
						result->add(key, val);
						break;
					}
				case PTRCHAR_TYPE:
				case STRING_TYPE:
					{
						BSONContentString* bstring = (BSONContentString*)origContent;
						djondb::string str = *bstring;
						const char* val = str.c_str();
						__int32 len = str.length();
						result->add(key, const_cast<char*>(val), len);
						break;
					}
			}
		}
	}
	return result;
}