-
Notifications
You must be signed in to change notification settings - Fork 14
/
handlersocketi_index.c
1958 lines (1641 loc) · 58.7 KB
/
handlersocketi_index.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
#include "php.h"
#include "php_ini.h"
#include "ext/standard/php_smart_str.h"
#include "ext/standard/php_string.h"
#include "php_verdep.h"
#include "php_handlersocketi.h"
#include "handlersocketi_exception.h"
#include "handlersocketi_class.h"
#include "handlersocketi_index.h"
#include "util/common.h"
#include "util/request.h"
#include "util/response.h"
ZEND_EXTERN_MODULE_GLOBALS(handlersocketi);
#define HS_DEFAULT_LIMIT 1
#define HS_DEFAULT_OFFSET 0
static zend_class_entry *hs_ce_index;
static zend_object_handlers hs_index_object_handlers;
typedef struct hs_index_obj {
zend_object std;
long id;
zval *name;
zval *db;
zval *table;
zval *field;
long field_num;
zval *filter;
zval *link;
zval *error;
} hs_index_obj_t;
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method__construct, 0, 0, 4)
ZEND_ARG_INFO(0, hs)
ZEND_ARG_INFO(0, db)
ZEND_ARG_INFO(0, table)
ZEND_ARG_INFO(0, fields)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_find, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_insert, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_update, 0, 0, 2)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, update)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_remove, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_multi, 0, 0, 1)
ZEND_ARG_INFO(0, args)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_error, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_id, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_name, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_db, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_table, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_field, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_filter, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_hs_index_method_get_operator, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_METHOD(HandlerSocketi_Index, __construct);
ZEND_METHOD(HandlerSocketi_Index, find);
ZEND_METHOD(HandlerSocketi_Index, insert);
ZEND_METHOD(HandlerSocketi_Index, update);
ZEND_METHOD(HandlerSocketi_Index, remove);
ZEND_METHOD(HandlerSocketi_Index, multi);
ZEND_METHOD(HandlerSocketi_Index, get_error);
ZEND_METHOD(HandlerSocketi_Index, get_id);
ZEND_METHOD(HandlerSocketi_Index, get_name);
ZEND_METHOD(HandlerSocketi_Index, get_db);
ZEND_METHOD(HandlerSocketi_Index, get_table);
ZEND_METHOD(HandlerSocketi_Index, get_field);
ZEND_METHOD(HandlerSocketi_Index, get_filter);
ZEND_METHOD(HandlerSocketi_Index, get_operator);
const zend_function_entry hs_index_methods[] = {
ZEND_ME(HandlerSocketi_Index, __construct,
arginfo_hs_index_method__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
ZEND_ME(HandlerSocketi_Index, find,
arginfo_hs_index_method_find, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, insert,
arginfo_hs_index_method_insert, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, update,
arginfo_hs_index_method_update, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, remove,
arginfo_hs_index_method_remove, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, multi,
arginfo_hs_index_method_multi, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_error,
arginfo_hs_index_method_get_error, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_id,
arginfo_hs_index_method_get_id, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_name,
arginfo_hs_index_method_get_name, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_db,
arginfo_hs_index_method_get_db, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_table,
arginfo_hs_index_method_get_table, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_field,
arginfo_hs_index_method_get_field, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_filter,
arginfo_hs_index_method_get_filter, ZEND_ACC_PUBLIC)
ZEND_ME(HandlerSocketi_Index, get_operator,
arginfo_hs_index_method_get_operator, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getError, get_error,
arginfo_hs_index_method_get_error, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getId, get_id,
arginfo_hs_index_method_get_id, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getName, get_name,
arginfo_hs_index_method_get_name, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getDb, get_db,
arginfo_hs_index_method_get_db, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getTable, get_table,
arginfo_hs_index_method_get_table, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getField, get_field,
arginfo_hs_index_method_get_field, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getFilter, get_filter,
arginfo_hs_index_method_get_filter, ZEND_ACC_PUBLIC)
ZEND_MALIAS(HandlerSocketi_Index, getOperator, get_operator,
arginfo_hs_index_method_get_operator, ZEND_ACC_PUBLIC)
ZEND_FE_END
};
#define HS_EXCEPTION(...) \
zend_throw_exception_ex(handlersocketi_get_ce_exception(), \
0 TSRMLS_CC, \
"HandlerSocketi_Index::" __VA_ARGS__)
#define HS_CHECK_OBJECT(object, classname) \
if (!(object)) { \
HS_EXCEPTION("The " #classname " object has not been " \
"correctly initialized by its constructor"); \
RETURN_FALSE; \
}
#define HS_ERROR_RESET(error) \
if (error) { \
zval_ptr_dtor(&error); \
} \
MAKE_STD_ZVAL(error); \
ZVAL_NULL(error)
static inline void
hs_index_object_free_storage(void *object TSRMLS_DC)
{
hs_index_obj_t *intern = (hs_index_obj_t *)object;
zend_object_std_dtor(&intern->std TSRMLS_CC);
if (intern->link) {
zval_ptr_dtor(&intern->link);
}
if (intern->name) {
zval_ptr_dtor(&intern->name);
}
if (intern->db) {
zval_ptr_dtor(&intern->db);
}
if (intern->table) {
zval_ptr_dtor(&intern->table);
}
if (intern->field) {
zval_ptr_dtor(&intern->field);
}
if (intern->filter) {
zval_ptr_dtor(&intern->filter);
}
if (intern->error) {
zval_ptr_dtor(&intern->error);
}
efree(object);
}
static inline zend_object_value
hs_index_object_new_ex(zend_class_entry *ce, hs_index_obj_t **ptr TSRMLS_DC)
{
hs_index_obj_t *intern;
zend_object_value retval;
#if ZEND_MODULE_API_NO < 20100525
zval *tmp;
#endif
intern = emalloc(sizeof(hs_index_obj_t));
memset(intern, 0, sizeof(hs_index_obj_t));
if (ptr) {
*ptr = intern;
}
zend_object_std_init(&intern->std, ce TSRMLS_CC);
#if ZEND_MODULE_API_NO >= 20100525
object_properties_init(&intern->std, ce);
#else
zend_hash_copy(intern->std.properties, &ce->default_properties,
(copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#endif
retval.handle = zend_objects_store_put(
intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t)hs_index_object_free_storage,
NULL TSRMLS_CC);
retval.handlers = &hs_index_object_handlers;
intern->id = 0;
intern->name = NULL;
intern->db = NULL;
intern->table = NULL;
intern->field = NULL;
intern->field_num = 0;
intern->filter = NULL;
intern->link = NULL;
intern->error = NULL;
return retval;
}
static inline zend_object_value
hs_index_object_new(zend_class_entry *ce TSRMLS_DC)
{
return hs_index_object_new_ex(ce, NULL TSRMLS_CC);
}
static inline zend_object_value
hs_index_object_clone(zval *this_ptr TSRMLS_DC)
{
hs_index_obj_t *new_obj = NULL;
hs_index_obj_t *old_obj =
(hs_index_obj_t *)zend_object_store_get_object(this_ptr TSRMLS_CC);
zend_object_value new_ov = hs_index_object_new_ex(old_obj->std.ce,
&new_obj TSRMLS_CC);
zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std,
Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
new_obj->id = old_obj->id;
MAKE_STD_ZVAL(new_obj->name);
*new_obj->name = *old_obj->name;
zval_copy_ctor(new_obj->name);
MAKE_STD_ZVAL(new_obj->db);
*new_obj->db = *old_obj->db;
zval_copy_ctor(new_obj->db);
MAKE_STD_ZVAL(new_obj->table);
*new_obj->table = *old_obj->table;
zval_copy_ctor(new_obj->table);
MAKE_STD_ZVAL(new_obj->field);
*new_obj->field = *old_obj->field;
zval_copy_ctor(new_obj->field);
new_obj->field_num = old_obj->field_num;
if (old_obj->filter) {
MAKE_STD_ZVAL(new_obj->filter);
*new_obj->filter = *old_obj->filter;
zval_copy_ctor(new_obj->filter);
} else {
new_obj->filter = NULL;
}
MAKE_STD_ZVAL(new_obj->link);
*new_obj->link = *old_obj->link;
zval_copy_ctor(new_obj->link);
new_obj->error = NULL;
return new_ov;
}
static inline zval
*hs_zval_to_comma_string(zval *val)
{
smart_str comma = {0};
zval *retval;
TSRMLS_FETCH();
if (Z_TYPE_P(val) == IS_ARRAY) {
zval **tmp;
HashTable *ht;
HashPosition pos;
ht = HASH_OF(val);
if (zend_hash_num_elements(ht) >= 0) {
zend_hash_internal_pointer_reset_ex(ht, &pos);
while (zend_hash_get_current_data_ex(ht, (void **)&tmp,
&pos) == SUCCESS) {
switch ((*tmp)->type) {
case IS_STRING:
if (Z_STRLEN_PP(tmp) > 0) {
smart_str_appendl(&comma, Z_STRVAL_PP(tmp),
Z_STRLEN_PP(tmp));
smart_str_appendl(&comma, ",", strlen(","));
}
break;
default:
convert_to_string(*tmp);
if (Z_STRLEN_PP(tmp) > 0) {
smart_str_appendl(&comma, Z_STRVAL_PP(tmp),
Z_STRLEN_PP(tmp));
smart_str_appendl(&comma, ",", strlen(","));
}
break;
}
zend_hash_move_forward_ex(ht, &pos);
}
if (comma.len > 0) {
comma.len--;
comma.a--;
}
}
} else if (Z_TYPE_P(val) == IS_STRING) {
if (Z_STRLEN_P(val) > 0) {
smart_str_appendl(&comma, Z_STRVAL_P(val), Z_STRLEN_P(val));
}
} else {
convert_to_string(val);
if (Z_STRLEN_P(val) > 0) {
smart_str_appendl(&comma, Z_STRVAL_P(val), Z_STRLEN_P(val));
}
}
smart_str_0(&comma);
MAKE_STD_ZVAL(retval);
ZVAL_STRINGL(retval, comma.c, comma.len, 1);
smart_str_free(&comma);
return retval;
}
static inline zval
*hs_zval_to_comma_array(zval *val)
{
zval *retval;
MAKE_STD_ZVAL(retval);
if (Z_TYPE_P(val) == IS_ARRAY) {
long n, i;
/*
array_init(retval);
n = zend_hash_num_elements(HASH_OF(*tmp));
for (i = 0; i < n; i++) {
zval **val;
if (zend_hash_index_find(HASH_OF(*tmp), i,
(void **)&val) == SUCCESS) {
convert_to_string(*val);
add_next_index_stringl(retval, Z_STRVAL_PP(val),
Z_STRLEN_PP(val), 1);
}
}
*/
*retval = *val;
zval_copy_ctor(retval);
} else if (Z_TYPE_P(val) == IS_STRING) {
zval delim;
ZVAL_STRINGL(&delim, ",", strlen(","), 0);
array_init(retval);
php_explode(&delim, val, retval, LONG_MAX);
} else {
ZVAL_NULL(retval);
}
return retval;
}
static inline int
hs_get_options_long(HashTable *options, char *item, long def TSRMLS_DC)
{
zval **tmp;
if (zend_hash_find(options, item, strlen(item)+1,
(void**)&tmp) == SUCCESS &&
Z_TYPE_PP(tmp) == IS_LONG) {
return Z_LVAL_PP(tmp);
}
return def;
}
static inline int
hs_is_options_safe(HashTable *options TSRMLS_DC)
{
zval **tmp;
if (zend_hash_find(options, "safe", sizeof("safe"),
(void**)&tmp) == SUCCESS) {
if (Z_TYPE_PP(tmp) == IS_STRING ||
((Z_TYPE_PP(tmp) == IS_LONG || Z_TYPE_PP(tmp) == IS_BOOL) &&
Z_LVAL_PP(tmp) >= 1)) {
return 1;
}
}
return 0;
}
static inline int
hs_zval_to_operate_criteria(zval *query, zval *operate,
zval **criteria, char *defaults TSRMLS_DC)
{
if (query == NULL) {
return FAILURE;
}
if (Z_TYPE_P(query) == IS_ARRAY) {
char *key;
uint key_len;
ulong index;
HashTable *ht;
zval **tmp;
ht = HASH_OF(query);
if (zend_hash_get_current_data_ex(ht, (void **)&tmp, NULL) != SUCCESS) {
return FAILURE;
}
if (zend_hash_get_current_key_ex(ht, &key, &key_len, &index,
0, NULL) == HASH_KEY_IS_STRING) {
ZVAL_STRINGL(operate, key, key_len-1, 1);
*criteria = *tmp;
} else {
ZVAL_STRINGL(operate, defaults, strlen(defaults), 1);
*criteria = query;
}
} else {
ZVAL_STRINGL(operate, defaults, strlen(defaults), 1);
*criteria = query;
}
return SUCCESS;
}
static inline zval
*hs_zval_search_key(zval *value, zval *array TSRMLS_DC)
{
zval *return_value, **entry, res;
HashPosition pos;
HashTable *ht;
ulong index;
uint key_len;
char *key;
int (*is_equal_func)(zval *, zval *, zval * TSRMLS_DC) = is_equal_function;
MAKE_STD_ZVAL(return_value);
ht = HASH_OF(array);
zend_hash_internal_pointer_reset_ex(ht, &pos);
while (zend_hash_get_current_data_ex(ht, (void **)&entry,
&pos) == SUCCESS) {
is_equal_func(&res, value, *entry TSRMLS_CC);
if (Z_LVAL(res)) {
switch (zend_hash_get_current_key_ex(ht, &key, &key_len,
&index, 0, &pos)) {
case HASH_KEY_IS_STRING:
ZVAL_STRINGL(return_value, key, key_len - 1, 1);
break;
case HASH_KEY_IS_LONG:
ZVAL_LONG(return_value, index);
break;
default:
ZVAL_NULL(return_value);
break;
}
return return_value;
}
zend_hash_move_forward_ex(ht, &pos);
}
ZVAL_NULL(return_value);
return return_value;
}
static inline void
hs_zval_to_filter(zval **return_value, zval *filter,
zval *value, char *type TSRMLS_DC)
{
HashTable *ht;
HashPosition pos;
zval **tmp, **ftmp, **vtmp, *index, *item;
long n;
if (value == NULL || Z_TYPE_P(value) != IS_ARRAY) {
return;
}
ht = HASH_OF(value);
n = zend_hash_num_elements(ht);
if (n <= 0 || zend_hash_index_find(ht, 0, (void **)&ftmp) != SUCCESS) {
return;
}
zend_hash_internal_pointer_reset_ex(ht, &pos);
if (Z_TYPE_PP(ftmp) == IS_ARRAY) {
do {
if (zend_hash_move_forward_ex(ht, &pos) < 0) {
break;
}
hs_zval_to_filter(return_value, filter, *ftmp, type TSRMLS_CC);
} while (zend_hash_get_current_data_ex(
ht, (void **)&ftmp, &pos) == SUCCESS);
return;
} else if (n < 3) {
return;
}
if (zend_hash_index_find(ht, 1, (void **)&tmp) != SUCCESS) {
return;
}
index = hs_zval_search_key(*ftmp, filter TSRMLS_CC);
if (Z_TYPE_P(index) != IS_LONG) {
zval_ptr_dtor(&index);
return;
}
if (zend_hash_index_find(ht, 2, (void **)&vtmp) != SUCCESS) {
zval_ptr_dtor(&index);
return;
}
MAKE_STD_ZVAL(item);
array_init(item);
add_next_index_stringl(item, type, strlen(type), 1);
convert_to_string(*tmp);
add_next_index_stringl(item, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), 1);
add_next_index_long(item, Z_LVAL_P(index));
if (Z_TYPE_PP(vtmp) == IS_NULL) {
add_next_index_null(item);
} else if (Z_TYPE_PP(vtmp) == IS_LONG) {
add_next_index_long(item, Z_LVAL_PP(vtmp));
} else if (Z_TYPE_PP(vtmp) == IS_DOUBLE) {
add_next_index_double(item, Z_DVAL_PP(vtmp));
} else {
convert_to_string(*tmp);
add_next_index_stringl(item, Z_STRVAL_PP(vtmp), Z_STRLEN_PP(vtmp), 1);
}
if (!(*return_value)) {
MAKE_STD_ZVAL(*return_value);
array_init(*return_value);
}
add_next_index_zval(*return_value, item);
zval_ptr_dtor(&index);
}
static inline void
hs_array_to_in_filter(HashTable *ht, zval *filter, zval **filters,
long *in_key, zval **in_values TSRMLS_DC)
{
HashPosition pos;
zval **val;
char *key;
ulong key_index;
uint key_len;
if (!filter) {
return;
}
zend_hash_internal_pointer_reset_ex(ht, &pos);
while (zend_hash_get_current_data_ex(ht, (void **)&val, &pos) == SUCCESS) {
if (zend_hash_get_current_key_ex(ht, &key, &key_len,
&key_index, 0, &pos)
!= HASH_KEY_IS_STRING) {
zend_hash_move_forward_ex(ht, &pos);
continue;
}
if (strcmp(key, "in") == 0) {
/* in */
if (Z_TYPE_PP(val) == IS_ARRAY) {
HashTable *in_ht;
HashPosition in_pos;
zval **tmp;
char *in_key_name;
ulong in_key_index;
uint in_key_len;
in_ht = HASH_OF(*val);
zend_hash_internal_pointer_reset_ex(in_ht, &in_pos);
if (zend_hash_get_current_data_ex(in_ht, (void **)&tmp,
&in_pos) == SUCCESS) {
if (Z_TYPE_PP(tmp) == IS_ARRAY) {
switch (zend_hash_get_current_key_ex(
in_ht, &in_key_name, &in_key_len,
&in_key_index, 0, &in_pos)) {
case HASH_KEY_NON_EXISTANT:
*in_key = 0;
break;
case HASH_KEY_IS_LONG:
*in_key = in_key_index;
break;
default:
{
zval *key;
MAKE_STD_ZVAL(key);
ZVAL_STRINGL(key, in_key_name, in_key_len, 1);
convert_to_long(key);
*in_key = Z_LVAL_P(key);
zval_ptr_dtor(&key);
break;
}
}
*in_values = *tmp;
} else {
*in_key = 0;
*in_values = *val;
}
}
} else {
*in_key = 0;
*in_values = *val;
}
} else if (strcmp(key, "filter") == 0 && filter != NULL) {
/* filter */
hs_zval_to_filter(filters, filter, *val,
HS_PROTOCOL_FILTER TSRMLS_CC);
} else if (strcmp(key, "while") == 0 && filter != NULL) {
/* while */
hs_zval_to_filter(filters, filter, *val,
HS_PROTOCOL_WHILE TSRMLS_CC);
}
zend_hash_move_forward_ex(ht, &pos);
}
}
static inline void
hs_index_object_init(hs_index_obj_t *hsi,
zval *link, char *db, long db_len,
char *table, long table_len,
zval *fields, zval *options TSRMLS_DC)
{
int id = 0;
zval *index = NULL, *fields_str = NULL, *filter = NULL, *retval;
php_stream *stream;
long timeout;
smart_str request = {0};
if (db_len == 0) {
HS_EXCEPTION("__construct(): invalid database %s", db);
return;
}
if (table_len == 0) {
HS_EXCEPTION("__construct(): invalid table %s", table);
return;
}
fields_str = hs_zval_to_comma_string(fields);
if (!fields_str) {
HS_EXCEPTION("__construct(): invalid fields");
return;
}
if (options && Z_TYPE_P(options) == IS_ARRAY) {
zval **tmp;
if (zend_hash_find(Z_ARRVAL_P(options), "id",
sizeof("id"), (void **)&tmp) == SUCCESS) {
convert_to_long_ex(tmp);
id = Z_LVAL_PP(tmp);
}
if (zend_hash_find(Z_ARRVAL_P(options), "index",
sizeof("index"), (void **)&tmp) == SUCCESS) {
convert_to_string_ex(tmp);
MAKE_STD_ZVAL(index);
ZVAL_STRINGL(index, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), 1);
}
if (zend_hash_find(Z_ARRVAL_P(options), "filter",
sizeof("filter"), (void **)&tmp) == SUCCESS) {
filter = hs_zval_to_comma_array(*tmp);
}
}
if (!index) {
MAKE_STD_ZVAL(index);
ZVAL_STRINGL(index, "PRIMARY", sizeof("PRIMARY")-1, 1);
} else if (Z_STRLEN_P(index) <= 0) {
if (index) {
zval_ptr_dtor(&index);
}
if (fields_str) {
zval_ptr_dtor(&fields_str);
}
if (filter) {
zval_ptr_dtor(&filter);
}
HS_EXCEPTION("__construct(): invalid index");
return;
}
if (id <= 0) {
id = HANDLERSOCKETI_G(id)++;
}
/* handerlsocket: object */
//hsi = (hs_index_obj_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
//HS_CHECK_OBJECT(hsi, HandlersocketiIndex);
if (!hsi) {
HS_EXCEPTION("The HandlerSocketi_Index object has not been "
"correctly initialized by its constructor");
return;
}
hsi->link = link;
zval_add_ref(&hsi->link);
/* id */
hsi->id = id;
/* name */
MAKE_STD_ZVAL(hsi->name);
*hsi->name = *index;
zval_copy_ctor(hsi->name);
/* db */
MAKE_STD_ZVAL(hsi->db);
ZVAL_STRINGL(hsi->db, db, db_len, 1);
/* table */
MAKE_STD_ZVAL(hsi->table);
ZVAL_STRINGL(hsi->table, table, db_len, 1);
/* field */
MAKE_STD_ZVAL(hsi->field);
if (Z_TYPE_P(fields) == IS_STRING) {
zval delim, *arr;
MAKE_STD_ZVAL(arr);
array_init(arr);
ZVAL_STRINGL(&delim, ",", strlen(","), 0);
php_explode(&delim, fields, arr, LONG_MAX);
hsi->field_num = zend_hash_num_elements(HASH_OF(arr));
*hsi->field = *arr;
zval_copy_ctor(hsi->field);
zval_ptr_dtor(&arr);
} else {
hsi->field_num = zend_hash_num_elements(HASH_OF(fields));
*hsi->field = *fields;
zval_copy_ctor(hsi->field);
}
if (filter) {
MAKE_STD_ZVAL(hsi->filter);
*hsi->filter = *filter;
zval_copy_ctor(hsi->filter);
}
/* stream */
stream = handlersocketi_object_store_get_stream(hsi->link);
timeout = handlersocketi_object_store_get_timeout(hsi->link);
hs_request_string(&request, HS_PROTOCOL_OPEN, 1);
hs_request_delim(&request);
/* id */
hs_request_long(&request, hsi->id);
hs_request_delim(&request);
/* db */
hs_request_string(&request, db, db_len);
hs_request_delim(&request);
/* table */
hs_request_string(&request, table, table_len);
hs_request_delim(&request);
/* index */
hs_request_string(&request, Z_STRVAL_P(index), Z_STRLEN_P(index));
hs_request_delim(&request);
/* fields */
hs_request_string(&request, Z_STRVAL_P(fields_str), Z_STRLEN_P(fields_str));
/* filters */
if (hsi->filter) {
hs_request_filter(&request, HASH_OF(hsi->filter) TSRMLS_CC);
}
/* eol */
hs_request_next(&request);
/* request: send */
if (hs_request_send(stream, &request TSRMLS_CC) < 0) {
smart_str_free(&request);
zval_ptr_dtor(&index);
if (fields_str) {
zval_ptr_dtor(&fields_str);
}
HS_EXCEPTION("__construct(): invalid request send");
return;
}
smart_str_free(&request);
/* response */
MAKE_STD_ZVAL(retval);
hs_response_value(stream, timeout, retval, hsi->error, 0 TSRMLS_CC);
if (Z_TYPE_P(retval) == IS_BOOL && Z_LVAL_P(retval) == 0) {
HS_EXCEPTION("__construct(): unable to open index: %ld: %s",
id,
hsi->error == NULL ?
"Unknwon error" : Z_STRVAL_P(hsi->error));
return;
}
/* cleanup */
zval_ptr_dtor(&retval);
zval_ptr_dtor(&index);
if (fields_str) {
zval_ptr_dtor(&fields_str);
}
}
PHP_HANDLERSOCKETI_API int
handlersocketi_register_index(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "HandlerSocketi_Index", hs_index_methods);
ce.create_object = hs_index_object_new;
hs_ce_index = zend_register_internal_class(&ce TSRMLS_CC);
if (hs_ce_index == NULL) {
return FAILURE;
}
memcpy(&hs_index_object_handlers, zend_get_std_object_handlers(),
sizeof(zend_object_handlers));
hs_index_object_handlers.clone_obj = hs_index_object_clone;
return SUCCESS;
}
PHP_HANDLERSOCKETI_API zend_class_entry
*handlersocketi_get_ce_index(void) {
return hs_ce_index;
}
PHP_HANDLERSOCKETI_API void
handlersocketi_create_index(zval *return_value,
zval *link, char *db, long db_len,
char *table, long table_len,
zval *fields, zval *options TSRMLS_DC)
{
object_init_ex(return_value, hs_ce_index);
hs_index_object_init(zend_object_store_get_object(return_value TSRMLS_CC),
link, db, db_len, table, table_len,
fields, options TSRMLS_CC);
}
ZEND_METHOD(HandlerSocketi_Index, __construct)
{
zval *link;
char *db, *table;
int db_len, table_len, id = 0;
zval *fields, *options = NULL;
zval *index = NULL, *fields_str = NULL, *filter = NULL;
hs_index_obj_t *hsi;
php_stream *stream;
long timeout;
smart_str request = {0};
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ossz|z",
&link, handlersocketi_get_ce(),
&db, &db_len, &table, &table_len,
&fields, &options) == FAILURE) {
HS_EXCEPTION("__construct(): expects parameters");
zval *object = getThis();
ZVAL_NULL(object);
return;
}
if (db_len == 0) {
HS_EXCEPTION("__construct(): invalid database %s", db);
return;
}
if (table_len == 0) {
HS_EXCEPTION("__construct(): invalid table %s", table);
return;
}
fields_str = hs_zval_to_comma_string(fields);
if (!fields_str) {
HS_EXCEPTION("__construct(): invalid fields");
return;
}
if (options && Z_TYPE_P(options) == IS_ARRAY) {
zval **tmp;
if (zend_hash_find(Z_ARRVAL_P(options), "id",
sizeof("id"), (void **)&tmp) == SUCCESS) {
convert_to_long_ex(tmp);
id = Z_LVAL_PP(tmp);
}
if (zend_hash_find(Z_ARRVAL_P(options), "index",
sizeof("index"), (void **)&tmp) == SUCCESS) {
convert_to_string_ex(tmp);
MAKE_STD_ZVAL(index);
ZVAL_STRINGL(index, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), 1);
}
if (zend_hash_find(Z_ARRVAL_P(options), "filter",
sizeof("filter"), (void **)&tmp) == SUCCESS) {
filter = hs_zval_to_comma_array(*tmp);
}
}
if (!index) {
MAKE_STD_ZVAL(index);
ZVAL_STRINGL(index, "PRIMARY", sizeof("PRIMARY")-1, 1);
} else if (Z_STRLEN_P(index) <= 0) {
if (index) {
zval_ptr_dtor(&index);
}
if (fields_str) {
zval_ptr_dtor(&fields_str);
}
if (filter) {
zval_ptr_dtor(&filter);
}
HS_EXCEPTION("__construct(): invalid index");
return;
}
if (id <= 0) {
id = HANDLERSOCKETI_G(id)++;
}
/* handerlsocket: object */
hsi = (hs_index_obj_t *)zend_object_store_get_object(getThis() TSRMLS_CC);
HS_CHECK_OBJECT(hsi, HandlerSocketi_Index);
hsi->link = link;
zval_add_ref(&hsi->link);
/* id */
hsi->id = id;
/* name */
MAKE_STD_ZVAL(hsi->name);
*hsi->name = *index;