Skip to content

Commit c64a088

Browse files
committed
PHPC-1491: Replace string read preferences with constants
1 parent 638c908 commit c64a088

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/MongoDB/ReadPreference.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434

3535
zend_class_entry* php_phongo_readpreference_ce;
3636

37+
#define PHONGO_READ_PRIMARY "primary"
38+
#define PHONGO_READ_PRIMARY_PREFERRED "primaryPreferred"
39+
#define PHONGO_READ_SECONDARY "secondary"
40+
#define PHONGO_READ_SECONDARY_PREFERRED "secondaryPreferred"
41+
#define PHONGO_READ_NEAREST "nearest"
42+
3743
/* Initialize the object from a HashTable and return whether it was successful.
3844
* An exception will be thrown on error. */
3945
static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t* intern, HashTable* props TSRMLS_DC) /* {{{ */
@@ -42,15 +48,15 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
4248
zval *mode, *tagSets, *maxStalenessSeconds;
4349

4450
if ((mode = zend_hash_str_find(props, "mode", sizeof("mode") - 1)) && Z_TYPE_P(mode) == IS_STRING) {
45-
if (strcasecmp(Z_STRVAL_P(mode), "primary") == 0) {
51+
if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY) == 0) {
4652
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
47-
} else if (strcasecmp(Z_STRVAL_P(mode), "primaryPreferred") == 0) {
53+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY_PREFERRED) == 0) {
4854
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
49-
} else if (strcasecmp(Z_STRVAL_P(mode), "secondary") == 0) {
55+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY) == 0) {
5056
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
51-
} else if (strcasecmp(Z_STRVAL_P(mode), "secondaryPreferred") == 0) {
57+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY_PREFERRED) == 0) {
5258
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
53-
} else if (strcasecmp(Z_STRVAL_P(mode), "nearest") == 0) {
59+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_NEAREST) == 0) {
5460
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
5561
} else {
5662
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires specific values for \"mode\" string field", ZSTR_VAL(php_phongo_readpreference_ce->name));
@@ -113,15 +119,15 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
113119
zval **mode, **tagSets, **maxStalenessSeconds;
114120

115121
if (zend_hash_find(props, "mode", sizeof("mode"), (void**) &mode) == SUCCESS && Z_TYPE_PP(mode) == IS_STRING) {
116-
if (strcasecmp(Z_STRVAL_PP(mode), "primary") == 0) {
122+
if (strcasecmp(Z_STRVAL_PP(mode), PHONGO_READ_PRIMARY) == 0) {
117123
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
118-
} else if (strcasecmp(Z_STRVAL_PP(mode), "primaryPreferred") == 0) {
124+
} else if (strcasecmp(Z_STRVAL_PP(mode), PHONGO_READ_PRIMARY_PREFERRED) == 0) {
119125
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
120-
} else if (strcasecmp(Z_STRVAL_PP(mode), "secondary") == 0) {
126+
} else if (strcasecmp(Z_STRVAL_PP(mode), PHONGO_READ_SECONDARY) == 0) {
121127
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
122-
} else if (strcasecmp(Z_STRVAL_PP(mode), "secondaryPreferred") == 0) {
128+
} else if (strcasecmp(Z_STRVAL_PP(mode), PHONGO_READ_SECONDARY_PREFERRED) == 0) {
123129
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
124-
} else if (strcasecmp(Z_STRVAL_PP(mode), "nearest") == 0) {
130+
} else if (strcasecmp(Z_STRVAL_PP(mode), PHONGO_READ_NEAREST) == 0) {
125131
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
126132
} else {
127133
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires specific values for \"mode\" string field", ZSTR_VAL(php_phongo_readpreference_ce->name));
@@ -225,15 +231,15 @@ static PHP_METHOD(ReadPreference, __construct)
225231
return;
226232
}
227233
} else if (Z_TYPE_P(mode) == IS_STRING) {
228-
if (strcasecmp(Z_STRVAL_P(mode), "primary") == 0) {
234+
if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY) == 0) {
229235
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
230-
} else if (strcasecmp(Z_STRVAL_P(mode), "primaryPreferred") == 0) {
236+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY_PREFERRED) == 0) {
231237
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
232-
} else if (strcasecmp(Z_STRVAL_P(mode), "secondary") == 0) {
238+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY) == 0) {
233239
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
234-
} else if (strcasecmp(Z_STRVAL_P(mode), "secondaryPreferred") == 0) {
240+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY_PREFERRED) == 0) {
235241
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
236-
} else if (strcasecmp(Z_STRVAL_P(mode), "nearest") == 0) {
242+
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_NEAREST) == 0) {
237243
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
238244
} else {
239245
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid mode: '%s'", Z_STRVAL_P(mode));
@@ -382,15 +388,15 @@ static const char* php_phongo_readpreference_get_mode_string(mongoc_read_mode_t
382388
{
383389
switch (mode) {
384390
case MONGOC_READ_PRIMARY:
385-
return "primary";
391+
return PHONGO_READ_PRIMARY;
386392
case MONGOC_READ_PRIMARY_PREFERRED:
387-
return "primaryPreferred";
393+
return PHONGO_READ_PRIMARY_PREFERRED;
388394
case MONGOC_READ_SECONDARY:
389-
return "secondary";
395+
return PHONGO_READ_SECONDARY;
390396
case MONGOC_READ_SECONDARY_PREFERRED:
391-
return "secondaryPreferred";
397+
return PHONGO_READ_SECONDARY_PREFERRED;
392398
case MONGOC_READ_NEAREST:
393-
return "nearest";
399+
return PHONGO_READ_NEAREST;
394400
default: /* Do nothing */
395401
break;
396402
}

0 commit comments

Comments
 (0)