@@ -259,6 +259,86 @@ void phongo_server_init(zval *return_value, mongoc_client_t *client, int server_
259259}
260260/* }}} */
261261
262+ bool phongo_query_init (php_phongo_query_t * query , zval * filter , zval * options TSRMLS_DC ) /* {{{ */
263+ {
264+ zval * zquery = NULL ;
265+
266+ if (filter && !(Z_TYPE_P (filter ) == IS_ARRAY || Z_TYPE_P (filter ) == IS_OBJECT )) {
267+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected filter to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (filter )));
268+ return false;
269+ }
270+
271+ MAKE_STD_ZVAL (zquery );
272+ array_init (zquery );
273+
274+ if (options ) {
275+ /* TODO: Ensure batchSize, limit, and skip are 32-bit */
276+ query -> batch_size = php_array_fetchc_long (options , "batchSize" );
277+ query -> limit = php_array_fetchc_long (options , "limit" );
278+ query -> skip = php_array_fetchc_long (options , "skip" );
279+
280+ query -> flags = 0 ;
281+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("tailable" )) ? MONGOC_QUERY_TAILABLE_CURSOR : 0 ;
282+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("slaveOk" )) ? MONGOC_QUERY_SLAVE_OK : 0 ;
283+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("oplogReplay" )) ? MONGOC_QUERY_OPLOG_REPLAY : 0 ;
284+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("noCursorTimeout" )) ? MONGOC_QUERY_NO_CURSOR_TIMEOUT : 0 ;
285+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("awaitData" )) ? MONGOC_QUERY_AWAIT_DATA : 0 ;
286+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("exhaust" )) ? MONGOC_QUERY_EXHAUST : 0 ;
287+ query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("partial" )) ? MONGOC_QUERY_PARTIAL : 0 ;
288+
289+
290+ if (php_array_existsc (options , "modifiers" )) {
291+ zval * modifiers = php_array_fetchc (options , "modifiers" );
292+
293+ if (modifiers && !(Z_TYPE_P (modifiers ) == IS_ARRAY || Z_TYPE_P (modifiers ) == IS_OBJECT )) {
294+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected modifiers to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (modifiers )));
295+ zval_ptr_dtor (& zquery );
296+ return false;
297+ }
298+
299+ convert_to_array_ex (& modifiers );
300+ zend_hash_merge (HASH_OF (zquery ), HASH_OF (modifiers ), (void (* )(void * ))zval_add_ref , NULL , sizeof (zval * ), 1 );
301+ }
302+
303+ if (php_array_existsc (options , "projection" )) {
304+ zval * projection = php_array_fetchc (options , "projection" );
305+
306+ if (projection && !(Z_TYPE_P (projection ) == IS_ARRAY || Z_TYPE_P (projection ) == IS_OBJECT )) {
307+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected projection to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (projection )));
308+ zval_ptr_dtor (& zquery );
309+ return false;
310+ }
311+
312+ convert_to_array_ex (& projection );
313+ query -> selector = bson_new ();
314+ zval_to_bson (projection , PHONGO_BSON_NONE , query -> selector , NULL TSRMLS_CC );
315+ }
316+
317+ if (php_array_existsc (options , "sort" )) {
318+ zval * sort = php_array_fetchc (options , "sort" );
319+
320+ if (sort && !(Z_TYPE_P (sort ) == IS_ARRAY || Z_TYPE_P (sort ) == IS_OBJECT )) {
321+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected sort to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (sort )));
322+ zval_ptr_dtor (& zquery );
323+ return false;
324+ }
325+
326+ convert_to_array_ex (& sort );
327+ Z_ADDREF_P (sort );
328+ add_assoc_zval_ex (zquery , ZEND_STRS ("$orderby" ), sort );
329+ }
330+ }
331+
332+ Z_ADDREF_P (filter );
333+ add_assoc_zval_ex (zquery , ZEND_STRS ("$query" ), filter );
334+
335+ query -> query = bson_new ();
336+ zval_to_bson (zquery , PHONGO_BSON_NONE , query -> query , NULL TSRMLS_CC );
337+ zval_ptr_dtor (& zquery );
338+
339+ return true;
340+ } /* }}} */
341+
262342zend_bool phongo_writeconcernerror_init (zval * return_value , bson_t * bson TSRMLS_DC ) /* {{{ */
263343{
264344 bson_iter_t iter ;
@@ -363,7 +443,6 @@ php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, mongoc_wri
363443 return writeresult ;
364444
365445} /* }}} */
366-
367446/* }}} */
368447
369448/* {{{ CRUD */
@@ -1056,6 +1135,7 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
10561135
10571136/* }}} */
10581137
1138+ /* {{{ mongoc types from from_zval */
10591139const mongoc_write_concern_t * phongo_write_concern_from_zval (zval * zwrite_concern TSRMLS_DC ) /* {{{ */
10601140{
10611141 if (zwrite_concern ) {
@@ -1088,87 +1168,9 @@ const php_phongo_query_t* phongo_query_from_zval(zval *zquery TSRMLS_DC) /* {{{
10881168
10891169 return intern ;
10901170} /* }}} */
1171+ /* }}} */
10911172
1092- bool phongo_query_init (php_phongo_query_t * query , zval * filter , zval * options TSRMLS_DC ) /* {{{ */
1093- {
1094- zval * zquery = NULL ;
1095-
1096- if (filter && !(Z_TYPE_P (filter ) == IS_ARRAY || Z_TYPE_P (filter ) == IS_OBJECT )) {
1097- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected filter to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (filter )));
1098- return false;
1099- }
1100-
1101- MAKE_STD_ZVAL (zquery );
1102- array_init (zquery );
1103-
1104- if (options ) {
1105- /* TODO: Ensure batchSize, limit, and skip are 32-bit */
1106- query -> batch_size = php_array_fetchc_long (options , "batchSize" );
1107- query -> limit = php_array_fetchc_long (options , "limit" );
1108- query -> skip = php_array_fetchc_long (options , "skip" );
1109-
1110- query -> flags = 0 ;
1111- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("tailable" )) ? MONGOC_QUERY_TAILABLE_CURSOR : 0 ;
1112- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("slaveOk" )) ? MONGOC_QUERY_SLAVE_OK : 0 ;
1113- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("oplogReplay" )) ? MONGOC_QUERY_OPLOG_REPLAY : 0 ;
1114- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("noCursorTimeout" )) ? MONGOC_QUERY_NO_CURSOR_TIMEOUT : 0 ;
1115- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("awaitData" )) ? MONGOC_QUERY_AWAIT_DATA : 0 ;
1116- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("exhaust" )) ? MONGOC_QUERY_EXHAUST : 0 ;
1117- query -> flags |= php_array_fetchl_bool (options , ZEND_STRS ("partial" )) ? MONGOC_QUERY_PARTIAL : 0 ;
1118-
1119-
1120- if (php_array_existsc (options , "modifiers" )) {
1121- zval * modifiers = php_array_fetchc (options , "modifiers" );
1122-
1123- if (modifiers && !(Z_TYPE_P (modifiers ) == IS_ARRAY || Z_TYPE_P (modifiers ) == IS_OBJECT )) {
1124- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected modifiers to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (modifiers )));
1125- zval_ptr_dtor (& zquery );
1126- return false;
1127- }
1128-
1129- convert_to_array_ex (& modifiers );
1130- zend_hash_merge (HASH_OF (zquery ), HASH_OF (modifiers ), (void (* )(void * ))zval_add_ref , NULL , sizeof (zval * ), 1 );
1131- }
1132-
1133- if (php_array_existsc (options , "projection" )) {
1134- zval * projection = php_array_fetchc (options , "projection" );
1135-
1136- if (projection && !(Z_TYPE_P (projection ) == IS_ARRAY || Z_TYPE_P (projection ) == IS_OBJECT )) {
1137- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected projection to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (projection )));
1138- zval_ptr_dtor (& zquery );
1139- return false;
1140- }
1141-
1142- convert_to_array_ex (& projection );
1143- query -> selector = bson_new ();
1144- zval_to_bson (projection , PHONGO_BSON_NONE , query -> selector , NULL TSRMLS_CC );
1145- }
1146-
1147- if (php_array_existsc (options , "sort" )) {
1148- zval * sort = php_array_fetchc (options , "sort" );
1149-
1150- if (sort && !(Z_TYPE_P (sort ) == IS_ARRAY || Z_TYPE_P (sort ) == IS_OBJECT )) {
1151- phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected sort to be array or object, %s given" , zend_get_type_by_const (Z_TYPE_P (sort )));
1152- zval_ptr_dtor (& zquery );
1153- return false;
1154- }
1155-
1156- convert_to_array_ex (& sort );
1157- Z_ADDREF_P (sort );
1158- add_assoc_zval_ex (zquery , ZEND_STRS ("$orderby" ), sort );
1159- }
1160- }
1161-
1162- Z_ADDREF_P (filter );
1163- add_assoc_zval_ex (zquery , ZEND_STRS ("$query" ), filter );
1164-
1165- query -> query = bson_new ();
1166- zval_to_bson (zquery , PHONGO_BSON_NONE , query -> query , NULL TSRMLS_CC );
1167- zval_ptr_dtor (& zquery );
1168-
1169- return true;
1170- } /* }}} */
1171-
1173+ /* {{{ phongo zval from mongoc types */
11721174void php_phongo_cursor_id_new_from_id (zval * object , int64_t cursorid TSRMLS_DC ) /* {{{ */
11731175{
11741176 php_phongo_cursorid_t * intern ;
@@ -1189,7 +1191,6 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
11891191 bson_oid_to_string (oid , intern -> oid );
11901192} /* }}} */
11911193
1192-
11931194void php_phongo_read_preference_to_zval (zval * retval , const mongoc_read_prefs_t * read_prefs ) /* {{{ */
11941195{
11951196
@@ -1228,7 +1229,6 @@ void php_phongo_write_concern_to_zval(zval *retval, const mongoc_write_concern_t
12281229 add_assoc_bool_ex (retval , ZEND_STRS ("journal" ), mongoc_write_concern_get_journal (write_concern ));
12291230} /* }}} */
12301231
1231-
12321232void php_phongo_cursor_to_zval (zval * retval , php_phongo_cursor_t * cursor ) /* {{{ */
12331233{
12341234
@@ -1299,6 +1299,7 @@ void php_phongo_cursor_to_zval(zval *retval, php_phongo_cursor_t *cursor) /* {{{
12991299 add_assoc_long_ex (retval , ZEND_STRS ("server_id" ), cursor -> server_id );
13001300
13011301} /* }}} */
1302+ /* }}} */
13021303
13031304
13041305mongoc_client_t * php_phongo_make_mongo_client (const char * uri , zval * driverOptions TSRMLS_DC ) /* {{{ */
@@ -1474,7 +1475,7 @@ void php_phongo_new_regex_from_regex_and_options(zval *object, const char *patte
14741475 intern -> flags = estrndup (flags , intern -> flags_len );
14751476} /* }}} */
14761477
1477- bool php_phongo_writeresult_get_write_errors (php_phongo_writeresult_t * writeresult , bson_error_t * error )
1478+ bool php_phongo_writeresult_get_write_errors (php_phongo_writeresult_t * writeresult , bson_error_t * error ) /* {{{ */
14781479{
14791480 const char * err = NULL ;
14801481 uint32_t code = 0 ;
@@ -1498,8 +1499,8 @@ bool php_phongo_writeresult_get_write_errors(php_phongo_writeresult_t *writeresu
14981499 return true;
14991500 }
15001501 return false;
1501- }
1502- bool php_phongo_writeresult_get_writeconcern_error (php_phongo_writeresult_t * writeresult , bson_error_t * error )
1502+ } /* }}} */
1503+ bool php_phongo_writeresult_get_writeconcern_error (php_phongo_writeresult_t * writeresult , bson_error_t * error ) /* {{{ */
15031504{
15041505 const char * err = NULL ;
15051506 uint32_t code = 0 ;
@@ -1519,31 +1520,31 @@ bool php_phongo_writeresult_get_writeconcern_error(php_phongo_writeresult_t *wri
15191520 }
15201521
15211522 return false;
1522- }
1523- zval * php_phongo_throw_write_errors (php_phongo_writeresult_t * wr TSRMLS_DC )
1523+ } /* }}} */
1524+ zval * php_phongo_throw_write_errors (php_phongo_writeresult_t * wr TSRMLS_DC ) /* {{{ */
15241525{
15251526 bson_error_t error ;
15261527
15271528 if (php_phongo_writeresult_get_write_errors (wr , & error )) {
15281529 return phongo_throw_exception (PHONGO_ERROR_WRITE_SINGLE_FAILED TSRMLS_CC , "%s" , error .message );
15291530 }
15301531 return NULL ;
1531- }
1532- zval * php_phongo_throw_write_concern_error (php_phongo_writeresult_t * wr TSRMLS_DC )
1532+ } /* }}} */
1533+ zval * php_phongo_throw_write_concern_error (php_phongo_writeresult_t * wr TSRMLS_DC ) /* {{{ */
15331534{
15341535 bson_error_t error ;
15351536
15361537 if (php_phongo_writeresult_get_writeconcern_error (wr , & error )) {
15371538 return phongo_throw_exception (PHONGO_ERROR_WRITECONCERN_FAILED TSRMLS_CC , "%s" , error .message );
15381539 }
15391540 return NULL ;
1540- }
1541- php_phongo_writeresult_t * php_phongo_writeresult_get_from_bulkwriteexception (zval * ex TSRMLS_DC )
1541+ } /* }}} */
1542+ php_phongo_writeresult_t * php_phongo_writeresult_get_from_bulkwriteexception (zval * ex TSRMLS_DC ) /* {{{ */
15421543{
15431544 zval * wr = zend_read_property (php_phongo_bulkwriteexception_ce , ex , ZEND_STRL ("writeResult" ), 0 TSRMLS_CC );
15441545
15451546 return (php_phongo_writeresult_t * )zend_object_store_get_object (wr TSRMLS_CC );
1546- }
1547+ } /* }}} */
15471548
15481549static void php_phongo_cursor_free_current (php_phongo_cursor_t * cursor ) /* {{{ */
15491550{
@@ -1553,15 +1554,15 @@ static void php_phongo_cursor_free_current(php_phongo_cursor_t *cursor) /* {{{ *
15531554 }
15541555} /* }}} */
15551556
1556- void php_phongo_cursor_free (php_phongo_cursor_t * cursor )
1557+ void php_phongo_cursor_free (php_phongo_cursor_t * cursor ) /* {{{ */
15571558{
15581559 if (cursor -> cursor ) {
15591560 mongoc_cursor_destroy (cursor -> cursor );
15601561 cursor -> cursor = NULL ;
15611562 }
15621563
15631564 php_phongo_cursor_free_current (cursor );
1564- }
1565+ } /* }}} */
15651566
15661567/* {{{ Iterator */
15671568static void php_phongo_cursor_iterator_dtor (zend_object_iterator * iter TSRMLS_DC ) /* {{{ */
0 commit comments