Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86f15cc

Browse files
Refactor SELECT command.
* Use our common command handler logic for SELECT. * Shift updating `redis_sock->dbNumber` from the command itself to the reply handler, so we aren't erroneously pointing to a non-existent database before the command succeeds.
1 parent b6cf636 commit 86f15cc

5 files changed

Lines changed: 34 additions & 26 deletions

File tree

library.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,17 @@ redis_lpos_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z
15291529
return res;
15301530
}
15311531

1532+
PHP_REDIS_API int
1533+
redis_select_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab,
1534+
void *ctx)
1535+
{
1536+
if (redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, z_tab, NULL) < 0)
1537+
return FAILURE;
1538+
1539+
redis_sock->dbNumber = (long)(uintptr_t)ctx;
1540+
return SUCCESS;
1541+
}
1542+
15321543
PHP_REDIS_API int
15331544
redis_boolean_response_impl(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
15341545
zval *z_tab, void *ctx,

library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ PHP_REDIS_API int redis_read_lpos_response(zval *zdst, RedisSock *redis_sock, ch
195195

196196
PHP_REDIS_API int redis_client_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
197197
PHP_REDIS_API int redis_command_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
198+
PHP_REDIS_API int redis_select_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
198199

199200
/* Helper methods to get configuration values from a HashTable. */
200201

redis.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,32 +1632,7 @@ PHP_METHOD(Redis, info) {
16321632

16331633
/* {{{ proto bool Redis::select(long dbNumber) */
16341634
PHP_METHOD(Redis, select) {
1635-
1636-
zval *object;
1637-
RedisSock *redis_sock;
1638-
1639-
char *cmd;
1640-
int cmd_len;
1641-
zend_long dbNumber;
1642-
1643-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol",
1644-
&object, redis_ce, &dbNumber) == FAILURE) {
1645-
RETURN_FALSE;
1646-
}
1647-
1648-
if (dbNumber < 0 || (redis_sock = redis_sock_get(object, 0)) == NULL) {
1649-
RETURN_FALSE;
1650-
}
1651-
1652-
redis_sock->dbNumber = dbNumber;
1653-
cmd_len = REDIS_SPPRINTF(&cmd, "SELECT", "d", dbNumber);
1654-
1655-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
1656-
if (IS_ATOMIC(redis_sock)) {
1657-
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
1658-
NULL, NULL);
1659-
}
1660-
REDIS_PROCESS_RESPONSE(redis_boolean_response);
1635+
REDIS_PROCESS_CMD(select, redis_select_response);
16611636
}
16621637
/* }}} */
16631638

redis_commands.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,24 @@ redis_hrandfield_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
36643664
return SUCCESS;
36653665
}
36663666

3667+
int redis_select_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
3668+
char **cmd, int *cmd_len, short *slot, void **ctx)
3669+
{
3670+
zend_long db = 0;
3671+
3672+
ZEND_PARSE_PARAMETERS_START(1, 1)
3673+
Z_PARAM_LONG(db)
3674+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
3675+
3676+
if (db < 0 || db > INT_MAX)
3677+
return FAILURE;
3678+
3679+
*ctx = (void*)(uintptr_t)db;
3680+
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SELECT", "d", db);
3681+
3682+
return SUCCESS;
3683+
}
3684+
36673685
/* SRANDMEMBER */
36683686
int redis_srandmember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
36693687
char **cmd, int *cmd_len, short *slot, void **ctx,

redis_commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ int redis_hsetnx_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
266266
int redis_srandmember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
267267
char **cmd, int *cmd_len, short *slot, void **ctx, short *have_count);
268268

269+
int redis_select_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
270+
char **cmd, int *cmd_len, short *slot, void **ctx);
271+
269272
int redis_zincrby_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
270273
char **cmd, int *cmd_len, short *slot, void **ctx);
271274

0 commit comments

Comments
 (0)