Skip to content

Commit 7039b7f

Browse files
committed
fixed swoole#1117
1 parent 5736128 commit 7039b7f

File tree

1 file changed

+91
-10
lines changed

1 file changed

+91
-10
lines changed

swoole_mysql_coro.c

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,47 @@
2222
#include "swoole_coroutine.h"
2323
#include "swoole_mysql.h"
2424

25+
#ifdef SW_USE_MYSQLND
26+
#include "ext/mysqlnd/mysqlnd.h"
27+
#include "ext/mysqlnd/mysqlnd_charset.h"
28+
#endif
29+
2530
static PHP_METHOD(swoole_mysql_coro, __construct);
2631
static PHP_METHOD(swoole_mysql_coro, __destruct);
2732
static PHP_METHOD(swoole_mysql_coro, connect);
2833
static PHP_METHOD(swoole_mysql_coro, query);
2934
static PHP_METHOD(swoole_mysql_coro, recv);
35+
#ifdef SW_USE_MYSQLND
36+
static PHP_METHOD(swoole_mysql_coro, escape);
37+
#endif
3038
static PHP_METHOD(swoole_mysql_coro, setDefer);
3139
static PHP_METHOD(swoole_mysql_coro, getDefer);
3240
static PHP_METHOD(swoole_mysql_coro, close);
3341

42+
43+
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
44+
ZEND_END_ARG_INFO()
45+
46+
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_coro_connect, 0, 0, 2)
47+
ZEND_ARG_ARRAY_INFO(0, server_config, 0)
48+
ZEND_END_ARG_INFO()
49+
50+
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_coro_query, 0, 0, 1)
51+
ZEND_ARG_INFO(0, sql)
52+
ZEND_ARG_INFO(0, timeout)
53+
ZEND_END_ARG_INFO()
54+
55+
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_coro_setDefer, 0, 0, 0)
56+
ZEND_ARG_INFO(0, defer)
57+
ZEND_END_ARG_INFO()
58+
59+
#ifdef SW_USE_MYSQLND
60+
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_coro_escape, 0, 0, 1)
61+
ZEND_ARG_INFO(0, string)
62+
ZEND_ARG_INFO(0, flags)
63+
ZEND_END_ARG_INFO()
64+
#endif
65+
3466
static zend_class_entry swoole_mysql_coro_ce;
3567
static zend_class_entry *swoole_mysql_coro_class_entry_ptr;
3668

@@ -39,14 +71,17 @@ static zend_class_entry *swoole_mysql_coro_exception_class_entry_ptr;
3971

4072
static const zend_function_entry swoole_mysql_coro_methods[] =
4173
{
42-
PHP_ME(swoole_mysql_coro, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
43-
PHP_ME(swoole_mysql_coro, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
44-
PHP_ME(swoole_mysql_coro, connect, NULL, ZEND_ACC_PUBLIC)
45-
PHP_ME(swoole_mysql_coro, query, NULL, ZEND_ACC_PUBLIC)
46-
PHP_ME(swoole_mysql_coro, recv, NULL, ZEND_ACC_PUBLIC)
47-
PHP_ME(swoole_mysql_coro, setDefer, NULL, ZEND_ACC_PUBLIC)
48-
PHP_ME(swoole_mysql_coro, getDefer, NULL, ZEND_ACC_PUBLIC)
49-
PHP_ME(swoole_mysql_coro, close, NULL, ZEND_ACC_PUBLIC)
74+
PHP_ME(swoole_mysql_coro, __construct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
75+
PHP_ME(swoole_mysql_coro, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
76+
PHP_ME(swoole_mysql_coro, connect, arginfo_swoole_mysql_coro_connect, ZEND_ACC_PUBLIC)
77+
PHP_ME(swoole_mysql_coro, query, arginfo_swoole_mysql_coro_query, ZEND_ACC_PUBLIC)
78+
PHP_ME(swoole_mysql_coro, recv, arginfo_swoole_void, ZEND_ACC_PUBLIC)
79+
#ifdef SW_USE_MYSQLND
80+
PHP_ME(swoole_mysql_coro, escape, arginfo_swoole_mysql_coro_escape, ZEND_ACC_PUBLIC)
81+
#endif
82+
PHP_ME(swoole_mysql_coro, setDefer, arginfo_swoole_mysql_coro_setDefer, ZEND_ACC_PUBLIC)
83+
PHP_ME(swoole_mysql_coro, getDefer, arginfo_swoole_void, ZEND_ACC_PUBLIC)
84+
PHP_ME(swoole_mysql_coro, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
5085
PHP_FE_END
5186
};
5287

@@ -502,6 +537,54 @@ static PHP_METHOD(swoole_mysql_coro, recv)
502537
coro_yield();
503538
}
504539

540+
#ifdef SW_USE_MYSQLND
541+
static PHP_METHOD(swoole_mysql_coro, escape)
542+
{
543+
swString str;
544+
bzero(&str, sizeof(str));
545+
long flags;
546+
547+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str.str, &str.length, &flags) == FAILURE)
548+
{
549+
return;
550+
}
551+
552+
if (str.length <= 0)
553+
{
554+
swoole_php_fatal_error(E_WARNING, "String is empty.");
555+
RETURN_FALSE;
556+
}
557+
558+
mysql_client *client = swoole_get_object(getThis());
559+
if (!client)
560+
{
561+
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_mysql.");
562+
RETURN_FALSE;
563+
}
564+
if (!client->cli)
565+
{
566+
swoole_php_fatal_error(E_WARNING, "mysql connection#%d is closed.", client->fd);
567+
RETURN_FALSE;
568+
}
569+
570+
char *newstr = safe_emalloc(2, str.length + 1, 1);
571+
if (newstr == NULL)
572+
{
573+
swoole_php_fatal_error(E_ERROR, "emalloc(%ld) failed.", str.length + 1);
574+
RETURN_FALSE;
575+
}
576+
577+
const MYSQLND_CHARSET* cset = mysqlnd_find_charset_nr(client->connector.character_set);
578+
int newstr_len = mysqlnd_cset_escape_slashes(cset, newstr, str.str, str.length TSRMLS_CC);
579+
if (newstr_len < 0)
580+
{
581+
swoole_php_fatal_error(E_ERROR, "mysqlnd_cset_escape_slashes() failed.");
582+
RETURN_FALSE;
583+
}
584+
SW_RETURN_STRINGL(newstr, newstr_len, 0);
585+
}
586+
#endif
587+
505588
static PHP_METHOD(swoole_mysql_coro, __destruct)
506589
{
507590
mysql_client *client = swoole_get_object(getThis());
@@ -542,8 +625,6 @@ static PHP_METHOD(swoole_mysql_coro, close)
542625
{
543626
RETURN_FALSE;
544627
}
545-
546-
547628
#if PHP_MAJOR_VERSION < 7
548629
sw_zval_ptr_dtor(&getThis());
549630
#endif

0 commit comments

Comments
 (0)