22
22
#include "swoole_coroutine.h"
23
23
#include "swoole_mysql.h"
24
24
25
+ #ifdef SW_USE_MYSQLND
26
+ #include "ext/mysqlnd/mysqlnd.h"
27
+ #include "ext/mysqlnd/mysqlnd_charset.h"
28
+ #endif
29
+
25
30
static PHP_METHOD (swoole_mysql_coro , __construct ) ;
26
31
static PHP_METHOD (swoole_mysql_coro , __destruct ) ;
27
32
static PHP_METHOD (swoole_mysql_coro , connect ) ;
28
33
static PHP_METHOD (swoole_mysql_coro , query ) ;
29
34
static PHP_METHOD (swoole_mysql_coro , recv ) ;
35
+ #ifdef SW_USE_MYSQLND
36
+ static PHP_METHOD (swoole_mysql_coro , escape ) ;
37
+ #endif
30
38
static PHP_METHOD (swoole_mysql_coro , setDefer ) ;
31
39
static PHP_METHOD (swoole_mysql_coro , getDefer ) ;
32
40
static PHP_METHOD (swoole_mysql_coro , close ) ;
33
41
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
+
34
66
static zend_class_entry swoole_mysql_coro_ce ;
35
67
static zend_class_entry * swoole_mysql_coro_class_entry_ptr ;
36
68
@@ -39,14 +71,17 @@ static zend_class_entry *swoole_mysql_coro_exception_class_entry_ptr;
39
71
40
72
static const zend_function_entry swoole_mysql_coro_methods [] =
41
73
{
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 )
50
85
PHP_FE_END
51
86
};
52
87
@@ -502,6 +537,54 @@ static PHP_METHOD(swoole_mysql_coro, recv)
502
537
coro_yield ();
503
538
}
504
539
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
+
505
588
static PHP_METHOD (swoole_mysql_coro , __destruct )
506
589
{
507
590
mysql_client * client = swoole_get_object (getThis ());
@@ -542,8 +625,6 @@ static PHP_METHOD(swoole_mysql_coro, close)
542
625
{
543
626
RETURN_FALSE ;
544
627
}
545
-
546
-
547
628
#if PHP_MAJOR_VERSION < 7
548
629
sw_zval_ptr_dtor (& getThis ());
549
630
#endif
0 commit comments