forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_mysql.c
2573 lines (2291 loc) · 78.5 KB
/
swoole_mysql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2015 The Swoole Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "swoole_mysql.h"
#ifdef SW_USE_MYSQLND
#include "ext/mysqlnd/mysqlnd.h"
#include "ext/mysqlnd/mysqlnd_charset.h"
#endif
static PHP_METHOD(swoole_mysql, __construct);
static PHP_METHOD(swoole_mysql, __destruct);
static PHP_METHOD(swoole_mysql, connect);
#ifdef SW_USE_MYSQLND
static PHP_METHOD(swoole_mysql, escape);
#endif
static PHP_METHOD(swoole_mysql, query);
static PHP_METHOD(swoole_mysql, begin);
static PHP_METHOD(swoole_mysql, commit);
static PHP_METHOD(swoole_mysql, rollback);
static PHP_METHOD(swoole_mysql, getState);
static PHP_METHOD(swoole_mysql, close);
static PHP_METHOD(swoole_mysql, on);
static zend_class_entry swoole_mysql_ce;
static zend_class_entry *swoole_mysql_class_entry_ptr;
static zend_class_entry swoole_mysql_exception_ce;
static zend_class_entry *swoole_mysql_exception_class_entry_ptr;
#define UTF8_MB4 "utf8mb4"
#define UTF8_MB3 "utf8"
typedef struct _mysql_charset
{
unsigned int nr;
const char *name;
const char *collation;
} mysql_charset;
static const mysql_charset swoole_mysql_charsets[] =
{
{ 1, "big5", "big5_chinese_ci" },
{ 3, "dec8", "dec8_swedish_ci" },
{ 4, "cp850", "cp850_general_ci" },
{ 6, "hp8", "hp8_english_ci" },
{ 7, "koi8r", "koi8r_general_ci" },
{ 8, "latin1", "latin1_swedish_ci" },
{ 5, "latin1", "latin1_german1_ci" },
{ 9, "latin2", "latin2_general_ci" },
{ 2, "latin2", "latin2_czech_cs" },
{ 10, "swe7", "swe7_swedish_ci" },
{ 11, "ascii", "ascii_general_ci" },
{ 12, "ujis", "ujis_japanese_ci" },
{ 13, "sjis", "sjis_japanese_ci" },
{ 16, "hebrew", "hebrew_general_ci" },
{ 17, "filename", "filename" },
{ 18, "tis620", "tis620_thai_ci" },
{ 19, "euckr", "euckr_korean_ci" },
{ 21, "latin2", "latin2_hungarian_ci" },
{ 27, "latin2", "latin2_croatian_ci" },
{ 22, "koi8u", "koi8u_general_ci" },
{ 24, "gb2312", "gb2312_chinese_ci" },
{ 25, "greek", "greek_general_ci" },
{ 26, "cp1250", "cp1250_general_ci" },
{ 28, "gbk", "gbk_chinese_ci" },
{ 30, "latin5", "latin5_turkish_ci" },
{ 31, "latin1", "latin1_german2_ci" },
{ 15, "latin1", "latin1_danish_ci" },
{ 32, "armscii8", "armscii8_general_ci" },
{ 33, UTF8_MB3, UTF8_MB3"_general_ci" },
{ 35, "ucs2", "ucs2_general_ci" },
{ 36, "cp866", "cp866_general_ci" },
{ 37, "keybcs2", "keybcs2_general_ci" },
{ 38, "macce", "macce_general_ci" },
{ 39, "macroman", "macroman_general_ci" },
{ 40, "cp852", "cp852_general_ci" },
{ 41, "latin7", "latin7_general_ci" },
{ 20, "latin7", "latin7_estonian_cs" },
{ 57, "cp1256", "cp1256_general_ci" },
{ 59, "cp1257", "cp1257_general_ci" },
{ 63, "binary", "binary" },
{ 97, "eucjpms", "eucjpms_japanese_ci" },
{ 29, "cp1257", "cp1257_lithuanian_ci" },
{ 31, "latin1", "latin1_german2_ci" },
{ 34, "cp1250", "cp1250_czech_cs" },
{ 42, "latin7", "latin7_general_cs" },
{ 43, "macce", "macce_bin" },
{ 44, "cp1250", "cp1250_croatian_ci" },
{ 45, UTF8_MB4, UTF8_MB4"_general_ci" },
{ 46, UTF8_MB4, UTF8_MB4"_bin" },
{ 47, "latin1", "latin1_bin" },
{ 48, "latin1", "latin1_general_ci" },
{ 49, "latin1", "latin1_general_cs" },
{ 51, "cp1251", "cp1251_general_ci" },
{ 14, "cp1251", "cp1251_bulgarian_ci" },
{ 23, "cp1251", "cp1251_ukrainian_ci" },
{ 50, "cp1251", "cp1251_bin" },
{ 52, "cp1251", "cp1251_general_cs" },
{ 53, "macroman", "macroman_bin" },
{ 54, "utf16", "utf16_general_ci" },
{ 55, "utf16", "utf16_bin" },
{ 56, "utf16le", "utf16le_general_ci" },
{ 58, "cp1257", "cp1257_bin" },
{ 60, "utf32", "utf32_general_ci" },
{ 61, "utf32", "utf32_bin" },
{ 62, "utf16le", "utf16le_bin" },
{ 64, "armscii8", "armscii8_bin" },
{ 65, "ascii", "ascii_bin" },
{ 66, "cp1250", "cp1250_bin" },
{ 67, "cp1256", "cp1256_bin" },
{ 68, "cp866", "cp866_bin" },
{ 69, "dec8", "dec8_bin" },
{ 70, "greek", "greek_bin" },
{ 71, "hebrew", "hebrew_bin" },
{ 72, "hp8", "hp8_bin" },
{ 73, "keybcs2", "keybcs2_bin" },
{ 74, "koi8r", "koi8r_bin" },
{ 75, "koi8u", "koi8u_bin" },
{ 77, "latin2", "latin2_bin" },
{ 78, "latin5", "latin5_bin" },
{ 79, "latin7", "latin7_bin" },
{ 80, "cp850", "cp850_bin" },
{ 81, "cp852", "cp852_bin" },
{ 82, "swe7", "swe7_bin" },
{ 83, UTF8_MB3, UTF8_MB3"_bin" },
{ 84, "big5", "big5_bin" },
{ 85, "euckr", "euckr_bin" },
{ 86, "gb2312", "gb2312_bin" },
{ 87, "gbk", "gbk_bin" },
{ 88, "sjis", "sjis_bin" },
{ 89, "tis620", "tis620_bin" },
{ 90, "ucs2", "ucs2_bin" },
{ 91, "ujis", "ujis_bin" },
{ 92, "geostd8", "geostd8_general_ci" },
{ 93, "geostd8", "geostd8_bin" },
{ 94, "latin1", "latin1_spanish_ci" },
{ 95, "cp932", "cp932_japanese_ci" },
{ 96, "cp932", "cp932_bin" },
{ 97, "eucjpms", "eucjpms_japanese_ci" },
{ 98, "eucjpms", "eucjpms_bin" },
{ 99, "cp1250", "cp1250_polish_ci" },
{ 128, "ucs2", "ucs2_unicode_ci" },
{ 129, "ucs2", "ucs2_icelandic_ci" },
{ 130, "ucs2", "ucs2_latvian_ci" },
{ 131, "ucs2", "ucs2_romanian_ci" },
{ 132, "ucs2", "ucs2_slovenian_ci" },
{ 133, "ucs2", "ucs2_polish_ci" },
{ 134, "ucs2", "ucs2_estonian_ci" },
{ 135, "ucs2", "ucs2_spanish_ci" },
{ 136, "ucs2", "ucs2_swedish_ci" },
{ 137, "ucs2", "ucs2_turkish_ci" },
{ 138, "ucs2", "ucs2_czech_ci" },
{ 139, "ucs2", "ucs2_danish_ci" },
{ 140, "ucs2", "ucs2_lithuanian_ci" },
{ 141, "ucs2", "ucs2_slovak_ci" },
{ 142, "ucs2", "ucs2_spanish2_ci" },
{ 143, "ucs2", "ucs2_roman_ci" },
{ 144, "ucs2", "ucs2_persian_ci" },
{ 145, "ucs2", "ucs2_esperanto_ci" },
{ 146, "ucs2", "ucs2_hungarian_ci" },
{ 147, "ucs2", "ucs2_sinhala_ci" },
{ 148, "ucs2", "ucs2_german2_ci" },
{ 149, "ucs2", "ucs2_croatian_ci" },
{ 150, "ucs2", "ucs2_unicode_520_ci" },
{ 151, "ucs2", "ucs2_vietnamese_ci" },
{ 160, "utf32", "utf32_unicode_ci" },
{ 161, "utf32", "utf32_icelandic_ci" },
{ 162, "utf32", "utf32_latvian_ci" },
{ 163, "utf32", "utf32_romanian_ci" },
{ 164, "utf32", "utf32_slovenian_ci" },
{ 165, "utf32", "utf32_polish_ci" },
{ 166, "utf32", "utf32_estonian_ci" },
{ 167, "utf32", "utf32_spanish_ci" },
{ 168, "utf32", "utf32_swedish_ci" },
{ 169, "utf32", "utf32_turkish_ci" },
{ 170, "utf32", "utf32_czech_ci" },
{ 171, "utf32", "utf32_danish_ci" },
{ 172, "utf32", "utf32_lithuanian_ci" },
{ 173, "utf32", "utf32_slovak_ci" },
{ 174, "utf32", "utf32_spanish2_ci" },
{ 175, "utf32", "utf32_roman_ci" },
{ 176, "utf32", "utf32_persian_ci" },
{ 177, "utf32", "utf32_esperanto_ci" },
{ 178, "utf32", "utf32_hungarian_ci" },
{ 179, "utf32", "utf32_sinhala_ci" },
{ 180, "utf32", "utf32_german2_ci" },
{ 181, "utf32", "utf32_croatian_ci" },
{ 182, "utf32", "utf32_unicode_520_ci" },
{ 183, "utf32", "utf32_vietnamese_ci" },
{ 192, UTF8_MB3, UTF8_MB3"_unicode_ci" },
{ 193, UTF8_MB3, UTF8_MB3"_icelandic_ci" },
{ 194, UTF8_MB3, UTF8_MB3"_latvian_ci" },
{ 195, UTF8_MB3, UTF8_MB3"_romanian_ci" },
{ 196, UTF8_MB3, UTF8_MB3"_slovenian_ci" },
{ 197, UTF8_MB3, UTF8_MB3"_polish_ci" },
{ 198, UTF8_MB3, UTF8_MB3"_estonian_ci" },
{ 199, UTF8_MB3, UTF8_MB3"_spanish_ci" },
{ 200, UTF8_MB3, UTF8_MB3"_swedish_ci" },
{ 201, UTF8_MB3, UTF8_MB3"_turkish_ci" },
{ 202, UTF8_MB3, UTF8_MB3"_czech_ci" },
{ 203, UTF8_MB3, UTF8_MB3"_danish_ci" },
{ 204, UTF8_MB3, UTF8_MB3"_lithuanian_ci" },
{ 205, UTF8_MB3, UTF8_MB3"_slovak_ci" },
{ 206, UTF8_MB3, UTF8_MB3"_spanish2_ci" },
{ 207, UTF8_MB3, UTF8_MB3"_roman_ci" },
{ 208, UTF8_MB3, UTF8_MB3"_persian_ci" },
{ 209, UTF8_MB3, UTF8_MB3"_esperanto_ci" },
{ 210, UTF8_MB3, UTF8_MB3"_hungarian_ci" },
{ 211, UTF8_MB3, UTF8_MB3"_sinhala_ci" },
{ 212, UTF8_MB3, UTF8_MB3"_german2_ci" },
{ 213, UTF8_MB3, UTF8_MB3"_croatian_ci" },
{ 214, UTF8_MB3, UTF8_MB3"_unicode_520_ci" },
{ 215, UTF8_MB3, UTF8_MB3"_vietnamese_ci" },
{ 224, UTF8_MB4, UTF8_MB4"_unicode_ci" },
{ 225, UTF8_MB4, UTF8_MB4"_icelandic_ci" },
{ 226, UTF8_MB4, UTF8_MB4"_latvian_ci" },
{ 227, UTF8_MB4, UTF8_MB4"_romanian_ci" },
{ 228, UTF8_MB4, UTF8_MB4"_slovenian_ci" },
{ 229, UTF8_MB4, UTF8_MB4"_polish_ci" },
{ 230, UTF8_MB4, UTF8_MB4"_estonian_ci" },
{ 231, UTF8_MB4, UTF8_MB4"_spanish_ci" },
{ 232, UTF8_MB4, UTF8_MB4"_swedish_ci" },
{ 233, UTF8_MB4, UTF8_MB4"_turkish_ci" },
{ 234, UTF8_MB4, UTF8_MB4"_czech_ci" },
{ 235, UTF8_MB4, UTF8_MB4"_danish_ci" },
{ 236, UTF8_MB4, UTF8_MB4"_lithuanian_ci" },
{ 237, UTF8_MB4, UTF8_MB4"_slovak_ci" },
{ 238, UTF8_MB4, UTF8_MB4"_spanish2_ci" },
{ 239, UTF8_MB4, UTF8_MB4"_roman_ci" },
{ 240, UTF8_MB4, UTF8_MB4"_persian_ci" },
{ 241, UTF8_MB4, UTF8_MB4"_esperanto_ci" },
{ 242, UTF8_MB4, UTF8_MB4"_hungarian_ci" },
{ 243, UTF8_MB4, UTF8_MB4"_sinhala_ci" },
{ 244, UTF8_MB4, UTF8_MB4"_german2_ci" },
{ 245, UTF8_MB4, UTF8_MB4"_croatian_ci" },
{ 246, UTF8_MB4, UTF8_MB4"_unicode_520_ci" },
{ 247, UTF8_MB4, UTF8_MB4"_vietnamese_ci" },
{ 248, "gb18030", "gb18030_chinese_ci" },
{ 249, "gb18030", "gb18030_bin" },
{ 254, UTF8_MB3, UTF8_MB3"_general_cs" },
{ 0, NULL, NULL},
};
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_on, 0, 0, 2)
ZEND_ARG_INFO(0, event_name)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_connect, 0, 0, 2)
ZEND_ARG_ARRAY_INFO(0, server_config, 0)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_begin, 0, 0, 1)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_commit, 0, 0, 1)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_rollback, 0, 0, 1)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
#ifdef SW_USE_MYSQLND
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_escape, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
#endif
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mysql_query, 0, 0, 2)
ZEND_ARG_INFO(0, sql)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_mysql_methods[] =
{
PHP_ME(swoole_mysql, __construct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_mysql, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_mysql, connect, arginfo_swoole_mysql_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_mysql, begin, arginfo_swoole_mysql_begin, ZEND_ACC_PUBLIC)
PHP_ME(swoole_mysql, commit, arginfo_swoole_mysql_commit, ZEND_ACC_PUBLIC)
PHP_ME(swoole_mysql, rollback, arginfo_swoole_mysql_rollback, ZEND_ACC_PUBLIC)
#ifdef SW_USE_MYSQLND
PHP_ME(swoole_mysql, escape, arginfo_swoole_mysql_escape, ZEND_ACC_PUBLIC)
#endif
PHP_ME(swoole_mysql, query, arginfo_swoole_mysql_query, ZEND_ACC_PUBLIC)
PHP_ME(swoole_mysql, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_mysql, getState, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_mysql, on, arginfo_swoole_mysql_on, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static void mysql_client_free(mysql_client *client, zval* zobject);
static void mysql_columns_free(mysql_client *client);
static void mysql_client_free(mysql_client *client, zval* zobject)
{
if (client->cli->timer)
{
swTimer_del(&SwooleG.timer, client->cli->timer);
client->cli->timer = NULL;
}
//close the connection
client->cli->close(client->cli);
//release client object memory
swClient_free(client->cli);
efree(client->cli);
client->cli = NULL;
client->connected = 0;
}
static void mysql_columns_free(mysql_client *client)
{
int i;
for (i = 0; i < client->response.num_column; i++)
{
if (client->response.columns[i].buffer)
{
efree(client->response.columns[i].buffer);
client->response.columns[i].buffer = NULL;
}
}
efree(client->response.columns);
}
#ifdef SW_MYSQL_DEBUG
static void mysql_client_info(mysql_client *client);
static void mysql_column_info(mysql_field *field);
#endif
static void swoole_mysql_onTimeout(swTimer *timer, swTimer_node *tnode);
static int swoole_mysql_onRead(swReactor *reactor, swEvent *event);
static int swoole_mysql_onWrite(swReactor *reactor, swEvent *event);
static int swoole_mysql_onError(swReactor *reactor, swEvent *event);
static void swoole_mysql_onConnect(mysql_client *client TSRMLS_DC);
swString *mysql_request_buffer = NULL;
void swoole_mysql_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_mysql_ce, "swoole_mysql", "Swoole\\MySQL", swoole_mysql_methods);
swoole_mysql_class_entry_ptr = zend_register_internal_class(&swoole_mysql_ce TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_mysql, "Swoole\\MySQL");
SWOOLE_INIT_CLASS_ENTRY(swoole_mysql_exception_ce, "swoole_mysql_exception", "Swoole\\MySQL\\Exception", NULL);
swoole_mysql_exception_class_entry_ptr = sw_zend_register_internal_class_ex(&swoole_mysql_exception_ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_mysql_exception, "Swoole\\MySQL\\Exception");
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("serverInfo"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("sock"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_bool(swoole_mysql_class_entry_ptr, ZEND_STRL("connected"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(swoole_mysql_class_entry_ptr, ZEND_STRL("errno"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(swoole_mysql_class_entry_ptr, ZEND_STRL("connect_errno"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("error"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("connect_error"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("insert_id"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("affected_rows"), ZEND_ACC_PUBLIC TSRMLS_CC);
/**
* event callback
*/
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("onConnect"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_mysql_class_entry_ptr, ZEND_STRL("onClose"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_class_constant_long(swoole_mysql_class_entry_ptr, SW_STRL("STATE_QUERY")-1, SW_MYSQL_STATE_QUERY TSRMLS_CC);
zend_declare_class_constant_long(swoole_mysql_class_entry_ptr, SW_STRL("STATE_READ_START")-1, SW_MYSQL_STATE_READ_START TSRMLS_CC);
zend_declare_class_constant_long(swoole_mysql_class_entry_ptr, SW_STRL("STATE_READ_FIELD ")-1, SW_MYSQL_STATE_READ_FIELD TSRMLS_CC);
zend_declare_class_constant_long(swoole_mysql_class_entry_ptr, SW_STRL("STATE_READ_ROW")-1, SW_MYSQL_STATE_READ_ROW TSRMLS_CC);
zend_declare_class_constant_long(swoole_mysql_class_entry_ptr, SW_STRL("STATE_READ_END")-1, SW_MYSQL_STATE_READ_END TSRMLS_CC);
zend_declare_class_constant_long(swoole_mysql_class_entry_ptr, SW_STRL("STATE_CLOSED")-1, SW_MYSQL_STATE_CLOSED TSRMLS_CC);
}
int mysql_request(swString *sql, swString *buffer)
{
bzero(buffer->str, 5);
//length
mysql_pack_length(sql->length + 1, buffer->str);
//command
buffer->str[4] = SW_MYSQL_COM_QUERY;
buffer->length = 5;
return swString_append(buffer, sql);
}
int mysql_prepare(swString *sql, swString *buffer)
{
bzero(buffer->str, 5);
//length
mysql_pack_length(sql->length + 1, buffer->str);
//command
buffer->str[4] = SW_MYSQL_COM_STMT_PREPARE;
buffer->length = 5;
return swString_append(buffer, sql);
}
int mysql_get_charset(char *name)
{
const mysql_charset *c = swoole_mysql_charsets;
while (c[0].nr != 0)
{
if (!strcasecmp(c->name, name))
{
return c->nr;
}
++c;
}
return -1;
}
int mysql_get_result(mysql_connector *connector, char *buf, int len)
{
char *tmp = buf;
int packet_length = mysql_uint3korr(tmp);
if (len < packet_length + 4)
{
return 0;
}
//int packet_number = tmp[3];
tmp += 4;
uint8_t opcode = *tmp;
tmp += 1;
//ERROR Packet
if (opcode == 0xff)
{
connector->error_code = *(uint16_t *) tmp;
connector->error_msg = tmp + 2;
connector->error_length = packet_length - 3;
return -1;
}
else
{
return 1;
}
}
/**
1 [0a] protocol version
string[NUL] server version
4 connection id
string[8] auth-plugin-data-part-1
1 [00] filler
2 capability flags (lower 2 bytes)
if more data in the packet:
1 character set
2 status flags
2 capability flags (upper 2 bytes)
if capabilities & CLIENT_PLUGIN_AUTH {
1 length of auth-plugin-data
} else {
1 [00]
}
string[10] reserved (all [00])
if capabilities & CLIENT_SECURE_CONNECTION {
string[$len] auth-plugin-data-part-2 ($len=MAX(13, length of auth-plugin-data - 8))
if capabilities & CLIENT_PLUGIN_AUTH {
string[NUL] auth-plugin name
}
*/
int mysql_handshake(mysql_connector *connector, char *buf, int len)
{
char *tmp = buf;
/**
* handshake request
*/
mysql_handshake_request request;
bzero(&request, sizeof(request));
request.packet_length = mysql_uint3korr(tmp);
//continue to wait for data
if (len < request.packet_length + 4)
{
return 0;
}
request.packet_number = tmp[3];
tmp += 4;
request.protocol_version = *tmp;
tmp += 1;
//ERROR Packet
if (request.protocol_version == 0xff)
{
connector->error_code = *(uint16_t *) tmp;
connector->error_msg = tmp + 2;
connector->error_length = request.packet_length - 3;
return -1;
}
//1 [0a] protocol version
request.server_version = tmp;
tmp += (strlen(request.server_version) + 1);
//4 connection id
request.connection_id = *((int *) tmp);
tmp += 4;
//string[8] auth-plugin-data-part-1
memcpy(request.auth_plugin_data, tmp, 8);
tmp += 8;
//1 [00] filler
request.filler = *tmp;
tmp += 1;
//2 capability flags (lower 2 bytes)
memcpy(((char *) (&request.capability_flags)), tmp, 2);
tmp += 2;
if (tmp - tmp < len)
{
//1 character set
request.character_set = *tmp;
tmp += 1;
//2 status flags
memcpy(&request.status_flags, tmp, 2);
tmp += 2;
//2 capability flags (upper 2 bytes)
memcpy(((char *) (&request.capability_flags) + 2), tmp, 2);
tmp += 2;
request.l_auth_plugin_data = *tmp;
tmp += 1;
memcpy(&request.reserved, tmp, sizeof(request.reserved));
tmp += sizeof(request.reserved);
if (request.capability_flags & SW_MYSQL_CLIENT_SECURE_CONNECTION)
{
int len = MAX(13, request.l_auth_plugin_data - 8);
memcpy(request.auth_plugin_data + 8, tmp, len);
tmp += len;
}
if (request.capability_flags & SW_MYSQL_CLIENT_PLUGIN_AUTH)
{
request.auth_plugin_name = tmp;
request.l_auth_plugin_name = MIN(strlen(tmp), len - (tmp - buf));
}
}
int value;
tmp = connector->buf + 4;
//capability flags, CLIENT_PROTOCOL_41 always set
value = SW_MYSQL_CLIENT_PROTOCOL_41 | SW_MYSQL_CLIENT_SECURE_CONNECTION | SW_MYSQL_CLIENT_CONNECT_WITH_DB | SW_MYSQL_CLIENT_PLUGIN_AUTH;
memcpy(tmp, &value, sizeof(value));
tmp += 4;
//max-packet size
value = 300;
memcpy(tmp, &value, sizeof(value));
tmp += 4;
//use the server character_set when the character_set is not set.
if (connector->character_set == 0)
{
connector->character_set = request.character_set;
}
//character set
*tmp = connector->character_set;
tmp += 1;
//string[23] reserved (all [0])
tmp += 23;
//string[NUL] username
memcpy(tmp, connector->user, connector->user_len);
tmp[connector->user_len] = '\0';
tmp += (connector->user_len + 1);
if (connector->password_len > 0)
{
//auth-response
char hash_0[20];
bzero(hash_0, sizeof (hash_0));
php_swoole_sha1(connector->password, connector->password_len, (uchar *) hash_0);
char hash_1[20];
bzero(hash_1, sizeof (hash_1));
php_swoole_sha1(hash_0, sizeof (hash_0), (uchar *) hash_1);
char str[40];
memcpy(str, request.auth_plugin_data, 20);
memcpy(str + 20, hash_1, 20);
char hash_2[20];
php_swoole_sha1(str, sizeof (str), (uchar *) hash_2);
char hash_3[20];
int *a = (int *) hash_2;
int *b = (int *) hash_0;
int *c = (int *) hash_3;
int i;
for (i = 0; i < 5; i++)
{
c[i] = a[i] ^ b[i];
}
*tmp = 20;
memcpy(tmp + 1, hash_3, 20);
tmp += 21;
}
else
{
*tmp = 0;
tmp++;
}
//string[NUL] database
memcpy(tmp, connector->database, connector->database_len);
tmp[connector->database_len] = '\0';
tmp += (connector->database_len + 1);
//string[NUL] auth plugin name
memcpy(tmp, request.auth_plugin_name, request.l_auth_plugin_name);
tmp[request.l_auth_plugin_name] = '\0';
tmp += (request.l_auth_plugin_name + 1);
connector->packet_length = tmp - connector->buf - 4;
mysql_pack_length(connector->packet_length, connector->buf);
connector->buf[3] = 1;
return 1;
}
static int mysql_parse_prepare_result(mysql_client *client, char *buf, size_t n_buf)
{
if (n_buf < 11)
{
return SW_ERR;
}
mysql_statement *stmt = emalloc(sizeof(mysql_statement));
stmt->id = mysql_uint4korr(buf);
buf += 4;
stmt->field_count = mysql_uint2korr(buf);
buf += 2;
stmt->unreaded_param_count = stmt->param_count = mysql_uint2korr(buf);
buf += 2;
//skip 1 byte
buf += 1;
stmt->warning_count = mysql_uint2korr(buf);
client->statement = stmt;
stmt->client = client;
swTraceLog(SW_TRACE_MYSQL_CLIENT, "id=%d, field_count=%d, param_count=%d, warning_count=%d.", stmt->id, stmt->field_count, stmt->param_count,
stmt->warning_count);
return 11;
}
static int mysql_decode_row(mysql_client *client, char *buf, int packet_len)
{
int read_n = 0, i;
int tmp_len;
ulong_t len;
char nul;
#ifdef SW_MYSQL_STRICT_TYPE
mysql_row row;
char value_buffer[32];
bzero(&row, sizeof(row));
char *error;
char mem;
#endif
zval *result_array = client->response.result_array;
zval *row_array = NULL;
SW_ALLOC_INIT_ZVAL(row_array);
array_init(row_array);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "mysql_decode_row begin, num_column=%d, packet_len=%d.", client->response.num_column, packet_len);
for (i = 0; i < client->response.num_column; i++)
{
tmp_len = mysql_length_coded_binary(&buf[read_n], &len, &nul, packet_len - read_n);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
read_n += tmp_len;
if (read_n + len > packet_len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
swTraceLog(SW_TRACE_MYSQL_CLIENT, "n=%d, fname=%s, name_length=%d", i, client->response.columns[i].name,
client->response.columns[i].name_length);
if (nul == 1)
{
add_assoc_null(row_array, client->response.columns[i].name);
continue;
}
int type = client->response.columns[i].type;
swTraceLog(SW_TRACE_MYSQL_CLIENT, "value: name=%s, type=%d, value=%s, len=%ld",
client->response.columns[i].name, type, swoole_strndup(buf + read_n, len), len);
switch (type)
{
case SW_MYSQL_TYPE_NULL:
add_assoc_null(row_array, client->response.columns[i].name);
break;
/* String */
case SW_MYSQL_TYPE_TINY_BLOB:
case SW_MYSQL_TYPE_MEDIUM_BLOB:
case SW_MYSQL_TYPE_LONG_BLOB:
case SW_MYSQL_TYPE_BLOB:
case SW_MYSQL_TYPE_DECIMAL:
case SW_MYSQL_TYPE_NEWDECIMAL:
case SW_MYSQL_TYPE_BIT:
case SW_MYSQL_TYPE_STRING:
case SW_MYSQL_TYPE_VAR_STRING:
case SW_MYSQL_TYPE_VARCHAR:
case SW_MYSQL_TYPE_NEWDATE:
/* Date Time */
case SW_MYSQL_TYPE_TIME:
case SW_MYSQL_TYPE_YEAR:
case SW_MYSQL_TYPE_TIMESTAMP:
case SW_MYSQL_TYPE_DATETIME:
case SW_MYSQL_TYPE_DATE:
case SW_MYSQL_TYPE_JSON:
sw_add_assoc_stringl(row_array, client->response.columns[i].name, buf + read_n, len, 1);
break;
/* Integer */
case SW_MYSQL_TYPE_TINY:
case SW_MYSQL_TYPE_SHORT:
case SW_MYSQL_TYPE_INT24:
case SW_MYSQL_TYPE_LONG:
#ifdef SW_MYSQL_STRICT_TYPE
memcpy(value_buffer, buf + read_n, len);
value_buffer[len] = 0;
row.sint = strtol(value_buffer, &error, 10);
if (*error != '\0')
{
return -SW_MYSQL_ERR_CONVLONG;
}
add_assoc_long(row_array, client->response.columns[i].name, row.sint);
#else
sw_add_assoc_stringl(row_array, client->response.columns[i].name, buf + read_n, len, 1);
#endif
break;
case SW_MYSQL_TYPE_LONGLONG:
#ifdef SW_MYSQL_STRICT_TYPE
memcpy(value_buffer, buf + read_n, len);
value_buffer[len] = 0;
row.sbigint = strtoll(value_buffer, &error, 10);
if (*error != '\0')
{
return -SW_MYSQL_ERR_CONVLONG;
}
add_assoc_long(row_array, client->response.columns[i].name, row.sbigint);
#else
sw_add_assoc_stringl(row_array, client->response.columns[i].name, buf + read_n, len, 1);
#endif
break;
case SW_MYSQL_TYPE_FLOAT:
#ifdef SW_MYSQL_STRICT_TYPE
memcpy(value_buffer, buf + read_n, len);
value_buffer[len] = 0;
row.mfloat = strtof(value_buffer, &error);
if (*error != '\0')
{
return -SW_MYSQL_ERR_CONVFLOAT;
}
add_assoc_double(row_array, client->response.columns[i].name, row.mfloat);
#else
sw_add_assoc_stringl(row_array, client->response.columns[i].name, buf + read_n, len, 1);
#endif
break;
case SW_MYSQL_TYPE_DOUBLE:
#ifdef SW_MYSQL_STRICT_TYPE
memcpy(value_buffer, buf + read_n, len);
value_buffer[len] = 0;
row.mdouble = strtod(value_buffer, &error);
if (*error != '\0')
{
return -SW_MYSQL_ERR_CONVDOUBLE;
}
add_assoc_double(row_array, client->response.columns[i].name, row.mdouble);
#else
sw_add_assoc_stringl(row_array, client->response.columns[i].name, buf + read_n, len, 1);
#endif
break;
}
read_n += len;
}
add_next_index_zval(result_array, row_array);
#if PHP_MAJOR_VERSION > 5
if (row_array)
{
efree(row_array);
}
#endif
return read_n;
}
#define DATETIME_MAX_SIZE 20
static int mysql_decode_datetime(char *buf, char *result)
{
uint16_t y = 0;
uint8_t M = 0, d = 0, h = 0, m = 0, s = 0, n;
n = *(uint8_t *) (buf);
if (n != 0)
{
y = *(uint16_t *) (buf + 1);
M = *(uint8_t *) (buf + 3);
d = *(uint8_t *) (buf + 4);
h = *(uint8_t *) (buf + 5);
m = *(uint8_t *) (buf + 6);
s = *(uint8_t *) (buf + 7);
}
snprintf(result, DATETIME_MAX_SIZE, "%04d-%02d-%02d %02d:%02d:%02d", y, M, d, h, m, s);
swTrace("n=%d\n", n);
return n;
}
static int mysql_decode_time(char *buf, char *result)
{
uint8_t h = 0, m = 0, s = 0;
uint8_t n = *(uint8_t *) (buf);
if (n != 0)
{
h = *(uint8_t *) (buf + 6);
m = *(uint8_t *) (buf + 7);
s = *(uint8_t *) (buf + 8);
}
snprintf(result, DATETIME_MAX_SIZE, "%02d:%02d:%02d", h, m, s);
return n;
}
static int mysql_decode_date(char *buf, char *result)
{
uint8_t M = 0, d = 0, n;
uint16_t y = 0;
n = *(uint8_t *) (buf);
if (n != 0)
{
y = *(uint16_t *) (buf + 1);
M = *(uint8_t *) (buf + 3);
d = *(uint8_t *) (buf + 4);
}
snprintf(result, DATETIME_MAX_SIZE, "%04d-%02d-%02d", y, M, d);
return n;
}
static void mysql_decode_year(char *buf, char *result)
{
uint16_t y;
y = *(uint16_t *) (buf + 1);
snprintf(result, DATETIME_MAX_SIZE, "%04d", y);
}
static int mysql_decode_row_prepare(mysql_client *client, char *buf, int packet_len)
{
int read_n = 0, i;
int tmp_len;
ulong_t len = 0;
char nul;
unsigned int null_count = ((client->response.num_column + 9) / 8) + 1;
buf += null_count;
packet_len -= null_count;
swTraceLog(SW_TRACE_MYSQL_CLIENT, "null_count=%d", null_count);
char datetime_buffer[DATETIME_MAX_SIZE];
mysql_row row;
zval *result_array = client->response.result_array;
zval *row_array = NULL;
SW_ALLOC_INIT_ZVAL(row_array);
array_init(row_array);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "mysql_decode_row begin, num_column=%d, packet_len=%d.", client->response.num_column, packet_len);
for (i = 0; i < client->response.num_column; i++)
{
int type = client->response.columns[i].type;
swTraceLog(SW_TRACE_MYSQL_CLIENT, "value: name=%s, type=%d", client->response.columns[i].name, type);
switch (type)
{
/* Date Time */
case SW_MYSQL_TYPE_TIME:
len = mysql_decode_time(buf + read_n, datetime_buffer);
sw_add_assoc_stringl(row_array, client->response.columns[i].name, datetime_buffer, 8, 1);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%s", client->response.columns[i].name, datetime_buffer);
break;
case SW_MYSQL_TYPE_YEAR:
mysql_decode_year(buf + read_n, datetime_buffer);
sw_add_assoc_stringl(row_array, client->response.columns[i].name, datetime_buffer, 4, 1);
len = 3;
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%s", client->response.columns[i].name, datetime_buffer);
break;
case SW_MYSQL_TYPE_DATE:
len = mysql_decode_date(buf + read_n, datetime_buffer) + 1;
sw_add_assoc_stringl(row_array, client->response.columns[i].name, datetime_buffer, 10, 1);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%s", client->response.columns[i].name, datetime_buffer);
break;
case SW_MYSQL_TYPE_TIMESTAMP:
case SW_MYSQL_TYPE_DATETIME:
len = mysql_decode_datetime(buf + read_n, datetime_buffer) + 1;
sw_add_assoc_stringl(row_array, client->response.columns[i].name, datetime_buffer, 19, 1);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%s", client->response.columns[i].name, datetime_buffer);
break;
case SW_MYSQL_TYPE_NULL:
add_assoc_null(row_array, client->response.columns[i].name);
break;
/* String */
case SW_MYSQL_TYPE_TINY_BLOB:
case SW_MYSQL_TYPE_MEDIUM_BLOB:
case SW_MYSQL_TYPE_LONG_BLOB:
case SW_MYSQL_TYPE_BLOB:
case SW_MYSQL_TYPE_DECIMAL:
case SW_MYSQL_TYPE_NEWDECIMAL:
case SW_MYSQL_TYPE_BIT:
case SW_MYSQL_TYPE_JSON:
case SW_MYSQL_TYPE_STRING:
case SW_MYSQL_TYPE_VAR_STRING:
case SW_MYSQL_TYPE_VARCHAR:
case SW_MYSQL_TYPE_NEWDATE:
tmp_len = mysql_length_coded_binary(&buf[read_n], &len, &nul, packet_len - read_n);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
read_n += tmp_len;
sw_add_assoc_stringl(row_array, client->response.columns[i].name, buf + read_n, len, 1);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%s", client->response.columns[i].name, swoole_strndup(buf + read_n, len));
break;
/* Integer */
case SW_MYSQL_TYPE_TINY:
row.stiny = *(int8_t *) (buf + read_n);
add_assoc_long(row_array, client->response.columns[i].name, row.stiny);
len = sizeof(row.stiny);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%d", client->response.columns[i].name, row.stiny);
break;
case SW_MYSQL_TYPE_SHORT:
row.ssmall = *(int16_t *) (buf + read_n);
add_assoc_long(row_array, client->response.columns[i].name, row.ssmall);
len = sizeof(row.ssmall);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%d", client->response.columns[i].name, row.ssmall);
break;
case SW_MYSQL_TYPE_INT24:
case SW_MYSQL_TYPE_LONG:
row.sint = *(int32_t *) (buf + read_n);
add_assoc_long(row_array, client->response.columns[i].name, row.sint);
len = sizeof(row.sint);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "%s=%d", client->response.columns[i].name, row.sint);
break;