// static
Query* MultiFieldQueryParser::parse(const TCHAR* query, const TCHAR** _fields, const uint8_t* flags, Analyzer* analyzer) {
	BooleanQuery* bQuery = _CLNEW BooleanQuery();
	for (size_t i = 0; _fields[i]!=NULL; i++) {
	  //TODO: this is really confusing... why not refactor _fields and flags to use a array object.
	  //flags can be NULL since NULL == 0...
		/*if (flags[i] == NULL) {
			_CLLDELETE(bQuery);
			_CLTHROWA(CL_ERR_IllegalArgument, "_fields.length != flags.length");
		}*/
		QueryParser* qp = _CLNEW QueryParser(_fields[i], analyzer);
		Query* q = qp->parse(query);
		if (q!=NULL && // q never null, just being defensive
			(!(q->instanceOf(BooleanQuery::getClassName())) || ((BooleanQuery*)q)->getClauseCount()>0)) {
				bQuery->add(q, true, (BooleanClause::Occur)flags[i]);
		} else
			_CLLDELETE(q);
		_CLLDELETE(qp);
	}
	return bQuery;
}
//static
Query* MultiFieldQueryParser::parse(const TCHAR** _queries, const TCHAR** _fields, Analyzer* analyzer)
{
	BooleanQuery* bQuery = _CLNEW BooleanQuery();
	for (size_t i = 0; _fields[i]!=NULL; i++)
	{
		if (_queries[i] == NULL) {
			_CLLDELETE(bQuery);
			_CLTHROWA(CL_ERR_IllegalArgument, "_queries.length != _fields.length");
		}
		// TODO: Reuse qp instead of creating it over and over again
		QueryParser* qp = _CLNEW QueryParser(_fields[i], analyzer);
		Query* q = qp->parse(_queries[i]);
		if (q!=NULL && // q never null, just being defensive
			(!(q->instanceOf(BooleanQuery::getClassName()) || ((BooleanQuery*)q)->getClauseCount() > 0))) {
				bQuery->add(q, true, BooleanClause::SHOULD);
		} else
			_CLLDELETE(q);
		_CLLDELETE(qp);
	}
	return bQuery;
}
std::unique_ptr<QueryTree> unpackFromQuery(const Query& query)
{
	QueryParser parser;
	return parser.parse(query);
}