Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit e2871b4

Browse files
committed
HHVM-32: Refactored executeQuery into its own Utils function
1 parent df81025 commit e2871b4

File tree

3 files changed

+84
-69
lines changed

3 files changed

+84
-69
lines changed

src/MongoDB/Driver/Manager.cpp

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -219,77 +219,14 @@ Object HHVM_METHOD(MongoDBDriverManager, executeInsert, const String &ns, const
219219

220220
Object HHVM_METHOD(MongoDBDriverManager, executeQuery, const String &ns, const Object &query, const Variant &readPreference)
221221
{
222-
static Class* c_result;
223-
bson_t *bson_query = NULL, *bson_fields = NULL;
224-
const bson_t *doc;
225222
MongoDBDriverManagerData* manager_data = Native::data<MongoDBDriverManagerData>(this_);
226-
char *dbname;
227-
char *collname;
228-
mongoc_collection_t *collection;
229-
mongoc_cursor_t *cursor;
230223

231-
uint32_t skip, limit, batch_size;
232-
mongoc_query_flags_t flags;
233-
234-
/* Prepare */
235-
if (!MongoDriver::Utils::splitNamespace(ns, &dbname, &collname)) {
236-
throw Object(SystemLib::AllocInvalidArgumentExceptionObject("Invalid namespace: " + ns));
237-
return NULL;
238-
}
239-
240-
/* Get query properties */
241-
auto zquery = query->o_get(String("query"), false, s_MongoDriverQuery_className);
242-
243-
if (zquery.getType() == KindOfArray) {
244-
const Array& aquery = zquery.toArray();
245-
246-
skip = aquery[String("skip")].toInt32();
247-
limit = aquery[String("limit")].toInt32();
248-
batch_size = aquery[String("batch_size")].toInt32();
249-
flags = (mongoc_query_flags_t) aquery[String("flags")].toInt32();
250-
251-
VariantToBsonConverter converter(aquery[String("query")], HIPPO_BSON_NO_FLAGS);
252-
bson_query = bson_new();
253-
converter.convert(bson_query);
254-
255-
if (aquery.exists(String("fields"))) {
256-
VariantToBsonConverter converter(aquery[String("fields")], HIPPO_BSON_NO_FLAGS);
257-
bson_fields = bson_new();
258-
converter.convert(bson_fields);
259-
}
260-
}
261-
262-
/* Run query and get cursor */
263-
collection = mongoc_client_get_collection(manager_data->m_client, dbname, collname);
264-
cursor = mongoc_collection_find(collection, flags, skip, limit, batch_size, bson_query, bson_fields, NULL /*read_preference*/);
265-
mongoc_collection_destroy(collection);
266-
267-
/* Check for errors */
268-
if (!mongoc_cursor_next(cursor, &doc)) {
269-
bson_error_t error;
270-
271-
/* Could simply be no docs, which is not an error */
272-
if (mongoc_cursor_error(cursor, &error)) {
273-
mongoc_cursor_destroy(cursor);
274-
throw MongoDriver::Utils::throwExceptionFromBsonError(&error);
275-
276-
return NULL;
277-
}
278-
}
279-
280-
/* Prepare result */
281-
c_result = Unit::lookupClass(s_MongoDriverCursor_className.get());
282-
assert(c_result);
283-
ObjectData* obj = ObjectData::newInstance(c_result);
284-
285-
MongoDBDriverCursorData* cursor_data = Native::data<MongoDBDriverCursorData>(obj);
286-
287-
cursor_data->cursor = cursor;
288-
cursor_data->m_server_id = mongoc_cursor_get_hint(cursor);
289-
cursor_data->is_command_cursor = false;
290-
cursor_data->first_batch = doc ? bson_copy(doc) : NULL;
291-
292-
return obj;
224+
return MongoDriver::Utils::doExecuteQuery(
225+
ns,
226+
manager_data->m_client,
227+
query,
228+
NULL
229+
);
293230
}
294231

295232
Object HHVM_METHOD(MongoDBDriverManager, executeUpdate, const String &ns, const Variant &query, const Variant &newObj, const Variant &updateOptions, const Variant &writeConcern)

utils.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
#include "hphp/runtime/vm/native-data.h"
2121

2222
#include "utils.h"
23+
#include "mongodb.h"
2324

2425
#include "src/MongoDB/Driver/Cursor.h"
26+
#include "src/MongoDB/Driver/Query.h"
2527

2628
namespace MongoDriver
2729
{
@@ -222,4 +224,79 @@ HPHP::Object Utils::doExecuteCommand(const char *db, mongoc_client_t *client, bs
222224
return obj;
223225
}
224226

227+
HPHP::Object Utils::doExecuteQuery(const HPHP::String ns, mongoc_client_t *client, HPHP::Object query, mongoc_read_prefs_t *read_pref)
228+
{
229+
static HPHP::Class* c_result;
230+
bson_t *bson_query = NULL, *bson_fields = NULL;
231+
const bson_t *doc;
232+
mongoc_collection_t *collection;
233+
mongoc_cursor_t *cursor;
234+
235+
uint32_t skip, limit, batch_size;
236+
mongoc_query_flags_t flags;
237+
char *dbname;
238+
char *collname;
239+
240+
/* Prepare */
241+
if (!MongoDriver::Utils::splitNamespace(ns, &dbname, &collname)) {
242+
throw HPHP::Object(HPHP::SystemLib::AllocInvalidArgumentExceptionObject("Invalid namespace: " + ns));
243+
return NULL;
244+
}
245+
246+
/* Get query properties */
247+
auto zquery = query->o_get(HPHP::String("query"), false, HPHP::s_MongoDriverQuery_className);
248+
249+
if (zquery.getType() == HPHP::KindOfArray) {
250+
const HPHP::Array& aquery = zquery.toArray();
251+
252+
skip = aquery[HPHP::String("skip")].toInt32();
253+
limit = aquery[HPHP::String("limit")].toInt32();
254+
batch_size = aquery[HPHP::String("batch_size")].toInt32();
255+
flags = (mongoc_query_flags_t) aquery[HPHP::String("flags")].toInt32();
256+
257+
HPHP::VariantToBsonConverter converter(aquery[HPHP::String("query")], HIPPO_BSON_NO_FLAGS);
258+
bson_query = bson_new();
259+
converter.convert(bson_query);
260+
261+
if (aquery.exists(HPHP::String("fields"))) {
262+
HPHP::VariantToBsonConverter converter(aquery[HPHP::String("fields")], HIPPO_BSON_NO_FLAGS);
263+
bson_fields = bson_new();
264+
converter.convert(bson_fields);
265+
}
266+
}
267+
268+
/* Run query and get cursor */
269+
collection = mongoc_client_get_collection(client, dbname, collname);
270+
cursor = mongoc_collection_find(collection, flags, skip, limit, batch_size, bson_query, bson_fields, NULL /*read_preference*/);
271+
mongoc_collection_destroy(collection);
272+
273+
/* Check for errors */
274+
if (!mongoc_cursor_next(cursor, &doc)) {
275+
bson_error_t error;
276+
277+
/* Could simply be no docs, which is not an error */
278+
if (mongoc_cursor_error(cursor, &error)) {
279+
mongoc_cursor_destroy(cursor);
280+
throw MongoDriver::Utils::throwExceptionFromBsonError(&error);
281+
282+
return NULL;
283+
}
284+
}
285+
286+
/* Prepare result */
287+
c_result = HPHP::Unit::lookupClass(HPHP::s_MongoDriverCursor_className.get());
288+
assert(c_result);
289+
HPHP::ObjectData* obj = HPHP::ObjectData::newInstance(c_result);
290+
291+
HPHP::MongoDBDriverCursorData* cursor_data = HPHP::Native::data<HPHP::MongoDBDriverCursorData>(obj);
292+
293+
cursor_data->cursor = cursor;
294+
cursor_data->m_server_id = mongoc_cursor_get_hint(cursor);
295+
cursor_data->is_command_cursor = false;
296+
cursor_data->first_batch = doc ? bson_copy(doc) : NULL;
297+
298+
return obj;
299+
}
300+
301+
225302
}

utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Utils
4747
static HPHP::Object throwExceptionFromBsonError(bson_error_t *error);
4848

4949
static HPHP::Object doExecuteCommand(const char *db, mongoc_client_t *client, bson_t *command, mongoc_read_prefs_t *read_pref);
50+
static HPHP::Object doExecuteQuery(const HPHP::String ns, mongoc_client_t *client, HPHP::Object query, mongoc_read_prefs_t *read_pref);
5051
};
5152

5253
}

0 commit comments

Comments
 (0)