Skip to content

Commit 2990a0d

Browse files
committed
Implement object compare handler for WriteConcern
1 parent 034974e commit 2990a0d

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/MongoDB/WriteConcern.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,67 @@ 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 (mongoc_write_concern_get_w(wc1) != mongoc_write_concern_get_w(wc2)) {
430+
return -1;
431+
}
432+
433+
const char* wtag1 = NULL;
434+
const char* wtag2 = NULL;
435+
436+
wtag1 = mongoc_write_concern_get_wtag(wc1);
437+
wtag2 = mongoc_write_concern_get_wtag(wc2);
438+
439+
if (wtag1 == wtag2) {
440+
return 0;
441+
}
442+
443+
if (wtag1 == NULL || wtag2 == NULL) {
444+
return -1;
445+
}
446+
447+
if (strcmp(wtag1, wtag2) != 0) {
448+
return -1;
449+
}
450+
451+
if (mongoc_write_concern_get_journal(wc1) != mongoc_write_concern_get_journal(wc2)) {
452+
return -1;
453+
}
454+
455+
return 0;
456+
}
457+
404458
void phongo_writeconcern_init_ce(INIT_FUNC_ARGS)
405459
{
406460
phongo_writeconcern_ce = register_class_MongoDB_Driver_WriteConcern(phongo_serializable_ce);
407461
phongo_writeconcern_ce->create_object = phongo_writeconcern_create_object;
408462

409463
memcpy(&phongo_handler_writeconcern, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
464+
phongo_handler_writeconcern.compare = phongo_writeconcern_compare_objects;
410465
phongo_handler_writeconcern.get_debug_info = phongo_writeconcern_get_debug_info;
411466
phongo_handler_writeconcern.get_properties = phongo_writeconcern_get_properties;
412467
phongo_handler_writeconcern.free_obj = phongo_writeconcern_free_object;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
15+
// Object comparison fallback if one value is not a WriteConcern
16+
var_dump(new MongoDB\Driver\WriteConcern(1) > new MongoDB\BSON\Int64('8589934592'));
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
bool(true)
23+
bool(true)
24+
bool(true)
25+
bool(false)
26+
bool(false)
27+
bool(false)
28+
bool(false)
29+
===DONE===

0 commit comments

Comments
 (0)