Skip to content

Commit

Permalink
PHPC-2485: Remove WriteConcern and ReadPreference from execute method…
Browse files Browse the repository at this point in the history
… arginfo (#1783)

This functionality was deprecated by PHPC-2489 and can be removed for 2.0.
  • Loading branch information
jmikola authored Feb 5, 2025
1 parent 13c0d4f commit 4f7d08a
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 369 deletions.
44 changes: 10 additions & 34 deletions src/MongoDB/Manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeCommand)
size_t db_len;
zval* command;
zval* options = NULL;
bool free_options = false;
zval* zreadPreference = NULL;
zval* zsession = NULL;
uint32_t server_id = 0;
Expand All @@ -294,21 +293,19 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeCommand)

intern = Z_MANAGER_OBJ_P(getThis());

options = php_phongo_prep_legacy_option(options, "readPreference", &free_options);

if (!phongo_parse_session(options, intern->client, NULL, &zsession)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

if (!phongo_parse_read_preference(options, &zreadPreference)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

if (!php_phongo_manager_select_server(false, false, zreadPreference, zsession, intern->client, &server_id)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

/* If the Manager was created in a different process, reset the client so
Expand All @@ -317,11 +314,6 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeCommand)
PHONGO_RESET_CLIENT_IF_PID_DIFFERS(intern, intern);

phongo_execute_command(getThis(), PHONGO_COMMAND_RAW, db, command, options, server_id, return_value);

cleanup:
if (free_options) {
php_phongo_prep_legacy_option_free(options);
}
}

/* Execute a ReadCommand */
Expand Down Expand Up @@ -452,7 +444,6 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeQuery)
size_t namespace_len;
zval* query;
zval* options = NULL;
bool free_options = false;
zval* zreadPreference = NULL;
uint32_t server_id = 0;
zval* zsession = NULL;
Expand All @@ -466,21 +457,19 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeQuery)

intern = Z_MANAGER_OBJ_P(getThis());

options = php_phongo_prep_legacy_option(options, "readPreference", &free_options);

if (!phongo_parse_session(options, intern->client, NULL, &zsession)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

if (!phongo_parse_read_preference(options, &zreadPreference)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

if (!php_phongo_manager_select_server(false, true, zreadPreference, zsession, intern->client, &server_id)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

/* If the Manager was created in a different process, reset the client so
Expand All @@ -489,11 +478,6 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeQuery)
PHONGO_RESET_CLIENT_IF_PID_DIFFERS(intern, intern);

phongo_execute_query(getThis(), namespace, query, options, server_id, return_value);

cleanup:
if (free_options) {
php_phongo_prep_legacy_option_free(options);
}
}

/* Executes a BulkWrite (i.e. any number of insert, update, and delete ops) */
Expand All @@ -504,10 +488,9 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeBulkWrite)
size_t namespace_len;
zval* zbulk;
php_phongo_bulkwrite_t* bulk;
zval* options = NULL;
bool free_options = false;
uint32_t server_id = 0;
zval* zsession = NULL;
zval* options = NULL;
uint32_t server_id = 0;
zval* zsession = NULL;

PHONGO_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STRING_OR_NULL(namespace, namespace_len)
Expand All @@ -519,28 +502,21 @@ static PHP_METHOD(MongoDB_Driver_Manager, executeBulkWrite)
intern = Z_MANAGER_OBJ_P(getThis());
bulk = Z_BULKWRITE_OBJ_P(zbulk);

options = php_phongo_prep_legacy_option(options, "writeConcern", &free_options);

if (!phongo_parse_session(options, intern->client, NULL, &zsession)) {
/* Exception should already have been thrown */
return;
}

if (!php_phongo_manager_select_server(true, false, NULL, zsession, intern->client, &server_id)) {
/* Exception should already have been thrown */
goto cleanup;
return;
}

/* If the Server was created in a different process, reset the client so
* that its session pool is cleared. */
PHONGO_RESET_CLIENT_IF_PID_DIFFERS(intern, intern);

phongo_execute_bulk_write(getThis(), namespace, bulk, options, server_id, return_value);

cleanup:
if (free_options) {
php_phongo_prep_legacy_option_free(options);
}
}

/* Returns the autoEncryption.encryptedFieldsMap driver option */
Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB/Manager.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ final public function addSubscriber(Monitoring\Subscriber $subscriber): void {}

final public function createClientEncryption(array $options): ClientEncryption {}

final public function executeBulkWrite(string $namespace, BulkWrite $bulk, array|WriteConcern|null $options = null): WriteResult {}
final public function executeBulkWrite(string $namespace, BulkWrite $bulk, array|null $options = null): WriteResult {}

final public function executeCommand(string $db, Command $command, array|ReadPreference|null $options = null): CursorInterface {}
final public function executeCommand(string $db, Command $command, array|null $options = null): CursorInterface {}

final public function executeQuery(string $namespace, Query $query, array|ReadPreference|null $options = null): CursorInterface {}
final public function executeQuery(string $namespace, Query $query, array|null $options = null): CursorInterface {}

final public function executeReadCommand(string $db, Command $command, ?array $options = null): CursorInterface {}

Expand Down
18 changes: 7 additions & 11 deletions src/MongoDB/Manager_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 3 additions & 24 deletions src/MongoDB/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ static PHP_METHOD(MongoDB_Driver_Server, executeCommand)
char* db;
size_t db_len;
zval* command;
zval* options = NULL;
bool free_options = false;
zval* options = NULL;

intern = Z_SERVER_OBJ_P(getThis());

Expand All @@ -54,18 +53,12 @@ static PHP_METHOD(MongoDB_Driver_Server, executeCommand)
Z_PARAM_ZVAL_OR_NULL(options)
PHONGO_PARSE_PARAMETERS_END();

options = php_phongo_prep_legacy_option(options, "readPreference", &free_options);

/* If the Server was created in a different process, reset the client so
* that cursors created by this process can be differentiated and its
* session pool is cleared. */
PHONGO_RESET_CLIENT_IF_PID_DIFFERS(intern, Z_MANAGER_OBJ_P(&intern->manager));

phongo_execute_command(&intern->manager, PHONGO_COMMAND_RAW, db, command, options, intern->server_id, return_value);

if (free_options) {
php_phongo_prep_legacy_option_free(options);
}
}

/* Executes a ReadCommand on this Server */
Expand Down Expand Up @@ -153,8 +146,7 @@ static PHP_METHOD(MongoDB_Driver_Server, executeQuery)
char* namespace;
size_t namespace_len;
zval* query;
zval* options = NULL;
bool free_options = false;
zval* options = NULL;

intern = Z_SERVER_OBJ_P(getThis());

Expand All @@ -165,18 +157,12 @@ static PHP_METHOD(MongoDB_Driver_Server, executeQuery)
Z_PARAM_ZVAL_OR_NULL(options)
PHONGO_PARSE_PARAMETERS_END();

options = php_phongo_prep_legacy_option(options, "readPreference", &free_options);

/* If the Server was created in a different process, reset the client so
* that cursors created by this process can be differentiated and its
* session pool is cleared. */
PHONGO_RESET_CLIENT_IF_PID_DIFFERS(intern, Z_MANAGER_OBJ_P(&intern->manager));

phongo_execute_query(&intern->manager, namespace, query, options, intern->server_id, return_value);

if (free_options) {
php_phongo_prep_legacy_option_free(options);
}
}

/* Executes a BulkWrite (i.e. any number of insert, update, and delete ops) on
Expand All @@ -188,8 +174,7 @@ static PHP_METHOD(MongoDB_Driver_Server, executeBulkWrite)
size_t namespace_len;
zval* zbulk;
php_phongo_bulkwrite_t* bulk;
zval* options = NULL;
bool free_options = false;
zval* options = NULL;

intern = Z_SERVER_OBJ_P(getThis());

Expand All @@ -202,17 +187,11 @@ static PHP_METHOD(MongoDB_Driver_Server, executeBulkWrite)

bulk = Z_BULKWRITE_OBJ_P(zbulk);

options = php_phongo_prep_legacy_option(options, "writeConcern", &free_options);

/* If the Server was created in a different process, reset the client so
* that its session pool is cleared. */
PHONGO_RESET_CLIENT_IF_PID_DIFFERS(intern, Z_MANAGER_OBJ_P(&intern->manager));

phongo_execute_bulk_write(&intern->manager, namespace, bulk, options, intern->server_id, return_value);

if (free_options) {
php_phongo_prep_legacy_option_free(options);
}
}

/* Returns the hostname for this Server */
Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB/Server.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ final class Server

final private function __construct() {}

final public function executeBulkWrite(string $namespace, BulkWrite $bulkWrite, array|WriteConcern|null $options = null): WriteResult {}
final public function executeBulkWrite(string $namespace, BulkWrite $bulkWrite, array|null $options = null): WriteResult {}

final public function executeCommand(string $db, Command $command, array|ReadPreference|null $options = null): CursorInterface {}
final public function executeCommand(string $db, Command $command, array|null $options = null): CursorInterface {}

final public function executeQuery(string $namespace, Query $query, array|ReadPreference|null $options = null): CursorInterface {}
final public function executeQuery(string $namespace, Query $query, array|null $options = null): CursorInterface {}

final public function executeReadCommand(string $db, Command $command, ?array $options = null): CursorInterface {}

Expand Down
18 changes: 7 additions & 11 deletions src/MongoDB/Server_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/phongo_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,6 @@ const char* php_phongo_bson_type_to_string(bson_type_t type)
}
}

/* If options is not an array, insert it as a field in a newly allocated array.
* This may be used to convert legacy options (e.g. ReadPreference option for
* an executeQuery method) into an options array.
*
* A pointer to the array zval will always be returned. If allocated is set to
* true, php_phongo_prep_legacy_option_free() should be used to free the array
* zval later. */
zval* php_phongo_prep_legacy_option(zval* options, const char* key, bool* allocated)
{
*allocated = false;

if (options && Z_TYPE_P(options) != IS_ARRAY) {
zval* new_options = ecalloc(1, sizeof(zval));

array_init_size(new_options, 1);
add_assoc_zval(new_options, key, options);
Z_ADDREF_P(options);
*allocated = true;

php_error_docref(NULL, E_DEPRECATED, "Passing the \"%s\" option directly is deprecated and will be removed in ext-mongodb 2.0", key);

return new_options;
}

return options;
}

void php_phongo_prep_legacy_option_free(zval* options)
{
zval_ptr_dtor(options);
efree(options);
}

bool php_phongo_parse_int64(int64_t* retval, const char* data, size_t data_len)
{
int64_t value;
Expand Down
3 changes: 0 additions & 3 deletions src/phongo_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

const char* php_phongo_bson_type_to_string(bson_type_t type);

zval* php_phongo_prep_legacy_option(zval* options, const char* key, bool* allocated);
void php_phongo_prep_legacy_option_free(zval* options);

bool php_phongo_parse_int64(int64_t* retval, const char* data, size_t data_len);

bool phongo_split_namespace(const char* namespace, char** dbname, char** cname);
Expand Down
Loading

0 comments on commit 4f7d08a

Please sign in to comment.