Skip to content

Commit 8664c2d

Browse files
committed
Implement object compare handler for WriteConcern
1 parent 034974e commit 8664c2d

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,70 @@ static HashTable* phongo_writeconcern_get_properties(zend_object* object)
401401
return phongo_writeconcern_get_properties_hash(object, false, false, false);
402402
}
403403

404+
/**
405+
* Note: only equality comparison is supported for objects.
406+
* Inequality will always return -1, making sorting arbitrary.
407+
*/
408+
static int phongo_writeconcern_compare_objects(zval* o1, zval* o2)
409+
{
410+
ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
411+
412+
phongo_writeconcern_t* intern1;
413+
phongo_writeconcern_t* intern2;
414+
415+
const mongoc_write_concern_t* wc1 = NULL;
416+
const mongoc_write_concern_t* wc2 = NULL;
417+
418+
intern1 = Z_WRITECONCERN_OBJ_P(o1);
419+
intern2 = Z_WRITECONCERN_OBJ_P(o2);
420+
421+
if (intern1 && intern1->write_concern) {
422+
wc1 = intern1->write_concern;
423+
}
424+
425+
if (intern2 && intern2->write_concern) {
426+
wc2 = intern2->write_concern;
427+
}
428+
429+
if (wc1 == wc2) {
430+
return 0;
431+
}
432+
433+
if (wc1 == NULL || wc2 == NULL) {
434+
return -1;
435+
}
436+
437+
if (mongoc_write_concern_get_w(wc1) != mongoc_write_concern_get_w(wc2)) {
438+
return -1;
439+
}
440+
441+
const char* wtag1 = NULL;
442+
const char* wtag2 = NULL;
443+
444+
wtag1 = mongoc_write_concern_get_wtag(wc1);
445+
wtag2 = mongoc_write_concern_get_wtag(wc2);
446+
447+
if (wtag1 == wtag2) {
448+
} else if (wtag1 == NULL || wtag2 == NULL) {
449+
return -1;
450+
} else if (strcmp(wtag1, wtag2) != 0) {
451+
return -1;
452+
}
453+
454+
if (mongoc_write_concern_get_journal(wc1) != mongoc_write_concern_get_journal(wc2)) {
455+
return -1;
456+
}
457+
458+
return 0;
459+
}
460+
404461
void phongo_writeconcern_init_ce(INIT_FUNC_ARGS)
405462
{
406463
phongo_writeconcern_ce = register_class_MongoDB_Driver_WriteConcern(phongo_serializable_ce);
407464
phongo_writeconcern_ce->create_object = phongo_writeconcern_create_object;
408465

409466
memcpy(&phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
467+
phongo_handler_writeconcern.compare = phongo_writeconcern_compare_objects;
410468
phongo_handler_writeconcern.get_debug_info = phongo_writeconcern_get_debug_info;
411469
phongo_handler_writeconcern.get_properties = phongo_writeconcern_get_properties;
412470
phongo_handler_writeconcern.free_obj = phongo_writeconcern_free_object;

src/phongo_compat.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@
9999
#define PHONGO_RETVAL_SMART_STR(val) RETVAL_STRINGL(ZSTR_VAL((val).s), ZSTR_LEN((val).s));
100100
#define ZVAL_STATIC_INIT \
101101
{ \
102-
{ \
103-
0 \
104-
} \
102+
{ 0 } \
105103
}
106104

107105
#define ADD_NEXT_INDEX_INT64_OBJ(_zv, _value) \
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern equality comparison
3+
--FILE--
4+
<?php
5+
6+
var_dump(new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY) == new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY));
7+
var_dump(new MongoDB\Driver\WriteConcern("foo") == new MongoDB\Driver\WriteConcern("foo"));
8+
var_dump(new MongoDB\Driver\WriteConcern(1, 0, 1) == new MongoDB\Driver\WriteConcern(1, 0, 1));
9+
10+
// False for anything else
11+
var_dump(new MongoDB\Driver\WriteConcern(1) == new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY));
12+
var_dump(new MongoDB\Driver\WriteConcern("foo") == new MongoDB\Driver\WriteConcern("bar"));
13+
var_dump(new MongoDB\Driver\WriteConcern("foo", 0, 1) == new MongoDB\Driver\WriteConcern("foo", 0, 0));
14+
var_dump(new MongoDB\Driver\WriteConcern(1, 0, 1) == new MongoDB\Driver\WriteConcern(1, 0, 0));
15+
16+
// Object comparison fallback if one value is not a WriteConcern
17+
var_dump(new MongoDB\Driver\WriteConcern(1) > new MongoDB\BSON\Int64('8589934592'));
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
bool(true)
24+
bool(true)
25+
bool(true)
26+
bool(false)
27+
bool(false)
28+
bool(false)
29+
bool(false)
30+
bool(false)
31+
===DONE===

0 commit comments

Comments
 (0)