forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_mysql.h
787 lines (707 loc) · 20.7 KB
/
swoole_mysql.h
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
/*
+----------------------------------------------------------------------+
| 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 |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#ifndef SWOOLE_MYSQL_H_
#define SWOOLE_MYSQL_H_
//#define SW_MYSQL_STRICT_TYPE
//#define SW_MYSQL_DEBUG
enum mysql_command
{
SW_MYSQL_COM_SLEEP = 0,
SW_MYSQL_COM_QUIT,
SW_MYSQL_SW_MYSQL_COM_INIT_DB,
SW_MYSQL_COM_QUERY = 3,
SW_MYSQL_COM_FIELD_LIST,
SW_MYSQL_COM_CREATE_DB,
SW_MYSQL_COM_DROP_DB,
SW_MYSQL_COM_REFRESH,
SW_MYSQL_COM_SHUTDOWN,
SW_MYSQL_COM_STATISTICS,
SW_MYSQL_COM_PROCESS_INFO,
SW_MYSQL_COM_CONNECT,
SW_MYSQL_COM_PROCESS_KILL,
SW_MYSQL_COM_DEBUG,
SW_MYSQL_COM_PING,
SW_MYSQL_COM_TIME,
SW_MYSQL_COM_DELAYED_INSERT,
SW_MYSQL_COM_CHANGE_USER,
SW_MYSQL_COM_BINLOG_DUMP,
SW_MYSQL_COM_TABLE_DUMP,
SW_MYSQL_COM_CONNECT_OUT,
SW_MYSQL_COM_REGISTER_SLAVE,
SW_MYSQL_COM_STMT_PREPARE,
SW_MYSQL_COM_STMT_EXECUTE,
SW_MYSQL_COM_STMT_SEND_LONG_DATA,
SW_MYSQL_COM_STMT_CLOSE,
SW_MYSQL_COM_STMT_RESET,
SW_MYSQL_COM_SET_OPTION,
SW_MYSQL_COM_STMT_FETCH,
SW_MYSQL_COM_DAEMON,
SW_MYSQL_COM_END
};
enum mysql_read_state
{
SW_MYSQL_STATE_QUERY,
SW_MYSQL_STATE_READ_START,
SW_MYSQL_STATE_READ_FIELD,
SW_MYSQL_STATE_READ_ROW,
SW_MYSQL_STATE_READ_END,
SW_MYSQL_STATE_CLOSED,
};
enum mysql_error_code
{
SW_MYSQL_ERR_PROTOCOL_ERROR = 1,
SW_MYSQL_ERR_BUFFER_OVERSIZE,
SW_MYSQL_ERR_PACKET_CORRUPT,
SW_MYSQL_ERR_WANT_READ,
SW_MYSQL_ERR_WANT_WRITE,
SW_MYSQL_ERR_UNKNOWN_ERROR,
SW_MYSQL_ERR_MYSQL_ERROR,
SW_MYSQL_ERR_SERVER_LOST,
SW_MYSQL_ERR_BAD_PORT,
SW_MYSQL_ERR_RESOLV_HOST,
SW_MYSQL_ERR_SYSTEM,
SW_MYSQL_ERR_CANT_CONNECT,
SW_MYSQL_ERR_BUFFER_TOO_SMALL,
SW_MYSQL_ERR_UNEXPECT_R_STATE,
SW_MYSQL_ERR_STRFIELD_CORRUPT,
SW_MYSQL_ERR_BINFIELD_CORRUPT,
SW_MYSQL_ERR_BAD_LCB,
SW_MYSQL_ERR_LEN_OVER_BUFFER,
SW_MYSQL_ERR_CONVLONG,
SW_MYSQL_ERR_CONVLONGLONG,
SW_MYSQL_ERR_CONVFLOAT,
SW_MYSQL_ERR_CONVDOUBLE,
SW_MYSQL_ERR_CONVTIME,
SW_MYSQL_ERR_CONVTIMESTAMP,
SW_MYSQL_ERR_CONVDATE
};
enum mysql_field_types
{
SW_MYSQL_TYPE_DECIMAL,
SW_MYSQL_TYPE_TINY,
SW_MYSQL_TYPE_SHORT,
SW_MYSQL_TYPE_LONG,
SW_MYSQL_TYPE_FLOAT,
SW_MYSQL_TYPE_DOUBLE,
SW_MYSQL_TYPE_NULL,
SW_MYSQL_TYPE_TIMESTAMP,
SW_MYSQL_TYPE_LONGLONG,
SW_MYSQL_TYPE_INT24,
SW_MYSQL_TYPE_DATE,
SW_MYSQL_TYPE_TIME,
SW_MYSQL_TYPE_DATETIME,
SW_MYSQL_TYPE_YEAR,
SW_MYSQL_TYPE_NEWDATE,
SW_MYSQL_TYPE_VARCHAR,
SW_MYSQL_TYPE_BIT,
SW_MYSQL_TYPE_NEWDECIMAL = 246,
SW_MYSQL_TYPE_ENUM = 247,
SW_MYSQL_TYPE_SET = 248,
SW_MYSQL_TYPE_TINY_BLOB = 249,
SW_MYSQL_TYPE_MEDIUM_BLOB = 250,
SW_MYSQL_TYPE_LONG_BLOB = 251,
SW_MYSQL_TYPE_BLOB = 252,
SW_MYSQL_TYPE_VAR_STRING = 253,
SW_MYSQL_TYPE_STRING = 254,
SW_MYSQL_TYPE_GEOMETRY = 255
};
#define SW_MYSQL_CLIENT_CONNECT_WITH_DB 8
#define SW_MYSQL_CLIENT_PROTOCOL_41 512
#define SW_MYSQL_CLIENT_PLUGIN_AUTH (1UL << 19)
#define SW_MYSQL_CLIENT_CONNECT_ATTRS (1UL << 20)
#define SW_MYSQL_CLIENT_SECURE_CONNECTION 32768
typedef struct
{
int packet_length;
int packet_number;
uint8_t protocol_version;
char *server_version;
int connection_id;
char auth_plugin_data[21];
uint8_t l_auth_plugin_data;
char filler;
int capability_flags;
char character_set;
int16_t status_flags;
char reserved[10];
char *auth_plugin_name;
uint8_t l_auth_plugin_name;
} mysql_handshake_request;
typedef struct
{
char *host;
char *user;
char *password;
char *database;
zend_size_t host_len;
zend_size_t user_len;
zend_size_t password_len;
zend_size_t database_len;
long port;
double timeout;
int capability_flags;
int max_packet_size;
char character_set;
int packet_length;
char buf[512];
uint16_t error_code;
char *error_msg;
uint16_t error_length;
} mysql_connector;
typedef struct
{
char *name; /* Name of column */
char *org_name; /* Original column name, if an alias */
char *table; /* Table of column if column was a field */
char *org_table; /* Org table name, if table was an alias */
char *db; /* Database for table */
char *catalog; /* Catalog for table */
char *def; /* Default value (set by mysql_list_fields) */
ulong_t length; /* Width of column (create length) */
ulong_t max_length; /* Max width for selected set */
uint32_t name_length;
uint32_t org_name_length;
uint32_t table_length;
uint32_t org_table_length;
uint32_t db_length;
uint32_t catalog_length;
uint32_t def_length;
uint32_t flags; /* Div flags */
uint32_t decimals; /* Number of decimals in field */
uint32_t charsetnr; /* Character set */
enum mysql_field_types type; /* Type of field. See mysql_com.h for types */
void *extension;
} mysql_field;
typedef union
{
signed char stiny;
uchar utiny;
uchar mbool;
short ssmall;
unsigned short small;
int sint;
uint32_t uint;
long long sbigint;
unsigned long long ubigint;
float mfloat;
double mdouble;
} mysql_row;
typedef struct
{
uint8_t state;
swString *buffer;
swClient *cli;
zval *object;
zval *callback;
zval *onClose;
int fd;
#if PHP_MAJOR_VERSION >= 7
zval _object;
zval _callback;
zval _onClose;
#endif
struct
{
mysql_field *columns;
uint16_t num_column;
uint32_t num_row;
uint8_t wait_recv;
uint8_t response_type;
uint32_t packet_length :24;
uint32_t packet_number :8;
uint32_t error_code;
uint32_t warnings;
uint16_t status_code;
char status_msg[6];
char *server_msg;
ulong_t affected_rows;
ulong_t insert_id;
zval *result_array;
} response;
} mysql_client;
#define mysql_uint2korr(A) (uint16_t) (((uint16_t) ((zend_uchar) (A)[0])) +\
((uint16_t) ((zend_uchar) (A)[1]) << 8))
#define mysql_uint3korr(A) (uint32_t) (((uint32_t) ((zend_uchar) (A)[0])) +\
(((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
(((uint32_t) ((zend_uchar) (A)[2])) << 16))
#define mysql_uint4korr(A) (uint32_t) (((uint32_t) ((zend_uchar) (A)[0])) +\
(((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
(((uint32_t) ((zend_uchar) (A)[2])) << 16) +\
(((uint32_t) ((zend_uchar) (A)[3])) << 24))
#define mysql_uint8korr(A) ((uint64_t)(((uint32_t) ((zend_uchar) (A)[0])) +\
(((uint32_t) ((zend_uchar) (A)[1])) << 8) +\
(((uint32_t) ((zend_uchar) (A)[2])) << 16) +\
(((uint32_t) ((zend_uchar) (A)[3])) << 24)) +\
(((uint64_t) (((uint32_t) ((zend_uchar) (A)[4])) +\
(((uint32_t) ((zend_uchar) (A)[5])) << 8) +\
(((uint32_t) ((zend_uchar) (A)[6])) << 16) +\
(((uint32_t) ((zend_uchar) (A)[7])) << 24))) << 32))
static sw_inline void mysql_pack_length(int length, char *buf)
{
buf[2] = length >> 16;
buf[1] = length >> 8;
buf[0] = length;
}
static sw_inline int mysql_lcb_ll(char *m, ulong_t *r, char *nul, int len)
{
if (len < 1)
return -1;
switch ((uchar) m[0])
{
case 251: /* fb : 1 octet */
*r = 0;
*nul = 1;
return 1;
case 252: /* fc : 2 octets */
if (len < 3)
{
return -1;
}
*r = mysql_uint2korr(m + 1);
*nul = 0;
return 3;
case 253: /* fd : 3 octets */
if (len < 5)
{
return -1;
}
*r = mysql_uint3korr(m + 1);
*nul = 0;
return 4;
case 254: /* fe */
if (len < 9)
{
return -1;
}
*r = mysql_uint8korr(m + 1);
*nul = 0;
return 9;
default:
*r = (uchar) m[0];
*nul = 0;
return 1;
}
}
static sw_inline int mysql_length_coded_binary(char *m, ulong_t *r, char *nul, int len)
{
ulong_t val = 0;
int retcode = mysql_lcb_ll(m, &val, nul, len);
*r = val;
return retcode;
}
static sw_inline int mysql_decode_field(char *buf, int len, mysql_field *col)
{
int i;
ulong_t size;
char nul;
char *wh;
int tmp_len;
wh = buf;
i = 0;
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->catalog_length = size;
memmove(wh, &buf[i], size);
col->catalog = wh;
col->catalog[size] = '\0';
wh += size + 1;
i += size;
/* n (Length Coded String) db */
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->db_length = size;
memmove(wh, &buf[i], size);
col->db = wh;
col->db[size] = '\0';
wh += size + 1;
i += size;
/* n (Length Coded String) table */
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->table_length = size;
memmove(wh, &buf[i], size);
col->table = wh;
col->table[size] = '\0';
wh += size + 1;
i += size;
/* n (Length Coded String) org_table */
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->org_table_length = size;
memmove(wh, &buf[i], size);
col->org_table = wh;
col->org_table[size] = '\0';
wh += size + 1;
i += size;
/* n (Length Coded String) name */
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->name_length = size;
memmove(wh, &buf[i], size);
col->name = wh;
col->name[size] = '\0';
wh += size + 1;
i += size;
/* n (Length Coded String) org_name */
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->org_name_length = size;
memmove(wh, &buf[i], size);
col->org_name = wh;
col->org_name[size] = '\0';
wh += size + 1;
i += size;
/* check len */
if (i + 13 > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
/* (filler) */
i += 1;
/* charset */
col->charsetnr = mysql_uint2korr(&buf[i]);
i += 2;
/* length */
col->length = mysql_uint4korr(&buf[i]);
i += 4;
/* type */
col->type = (uchar) buf[i];
i += 1;
/* flags */
col->flags = mysql_uint3korr(&buf[i]);
i += 2;
/* decimals */
col->decimals = buf[i];
i += 1;
/* filler */
i += 2;
/* default - a priori facultatif */
if (len - i > 0)
{
tmp_len = mysql_length_coded_binary(&buf[i], &size, &nul, len - i);
if (tmp_len == -1)
{
return -SW_MYSQL_ERR_BAD_LCB;
}
i += tmp_len;
if (i + size > len)
{
return -SW_MYSQL_ERR_LEN_OVER_BUFFER;
}
col->def_length = size;
memmove(wh, &buf[i], size);
col->def = wh;
col->def[size] = '\0';
wh += size + 1;
i += size;
}
else
{
col->def = NULL;
col->def_length = 0;
}
/* set write pointer */
return wh - buf;
}
static sw_inline 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);
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;
}
if (nul == 1)
{
continue;
}
int type = client->response.columns[i].type;
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:
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;
}
static sw_inline int mysql_read_eof(mysql_client *client, char *buffer, int n_buf)
{
//EOF, length (3byte) + id(1byte) + 0xFE + warning(2byte) + status(2byte)
if (n_buf < 9)
{
client->response.wait_recv = 1;
return SW_ERR;
}
client->response.packet_length = mysql_uint3korr(buffer);
client->response.packet_number = buffer[3];
//not EOF packet
uint8_t eof = buffer[4];
if (eof != 0xfe)
{
return SW_ERR;
}
client->response.warnings = mysql_uint2korr(buffer + 5);
client->response.status_code = mysql_uint2korr(buffer + 7);
return SW_OK;
}
static sw_inline int mysql_read_columns(mysql_client *client)
{
int i;
char *buffer = client->buffer->str + client->buffer->offset;
uint32_t n_buf = client->buffer->length - client->buffer->offset;
int ret;
for (i = client->response.packet_number - 1; i < client->response.num_column; i++)
{
client->response.packet_length = mysql_uint3korr(buffer);
client->response.packet_number = buffer[3];
buffer += 4;
n_buf -= 4;
//no enough data
if (n_buf < client->response.packet_length)
{
wait_recv: client->buffer->offset += buffer - (client->buffer->str + client->buffer->offset);
client->response.wait_recv = 1;
return SW_ERR;
}
ret = mysql_decode_field(buffer, client->response.packet_length, &client->response.columns[i]);
if (ret > 0)
{
buffer += client->response.packet_length;
n_buf -= client->response.packet_length;
}
else
{
break;
}
}
if (mysql_read_eof(client, buffer, n_buf) < 0)
{
goto wait_recv;
}
buffer += 9;
n_buf -= 9;
zval *result_array = client->response.result_array;
if (!result_array)
{
SW_ALLOC_INIT_ZVAL(result_array);
array_init(result_array);
client->response.result_array = result_array;
}
client->buffer->offset += buffer - (client->buffer->str + client->buffer->offset);
return SW_OK;
}
static sw_inline int mysql_read_rows(mysql_client *client)
{
char *buffer = client->buffer->str + client->buffer->offset;
uint32_t n_buf = client->buffer->length - client->buffer->offset;
int ret;
//RecordSet parse
while (n_buf > 0)
{
if (n_buf < 4)
{
client->response.wait_recv = 1;
return SW_ERR;
}
else if (n_buf == 9 && mysql_read_eof(client, buffer, n_buf) == 0)
{
return SW_OK;
}
client->response.packet_length = mysql_uint3korr(buffer);
client->response.packet_number = buffer[3];
buffer += 4;
n_buf -= 4;
//no enough data
if (n_buf < client->response.packet_length)
{
client->response.wait_recv = 1;
return SW_ERR;
}
//decode
ret = mysql_decode_row(client, buffer, client->response.packet_length);
if (ret < 0)
{
break;
}
//next row
client->response.num_row++;
buffer += client->response.packet_length;
n_buf -= client->response.packet_length;
client->buffer->offset += client->response.packet_length + 4;
}
return SW_ERR;
}
#endif /* SWOOLE_MYSQL_H_ */