-
Notifications
You must be signed in to change notification settings - Fork 11
/
SDO_DataObject.cpp
1532 lines (1358 loc) · 45.4 KB
/
SDO_DataObject.cpp
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
/*
+----------------------------------------------------------------------+
| Copyright IBM Corporation 2005, 2007. |
| All Rights Reserved. |
+----------------------------------------------------------------------+
| |
| Licensed under the Apache License, Version 2.0 (the "License"); you |
| may not use this file except in compliance with the License. You may |
| obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
| Author: Caroline Maynard |
+----------------------------------------------------------------------+
*/
static char rcs_id[] = "$Id: SDO_DataObject.cpp 254122 2008-03-03 17:56:38Z mfp $";
#ifdef PHP_WIN32
#include <iostream>
#include <math.h>
#include <string>
#include "zend_config.w32.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "php_sdo_int.h"
#include "commonj/sdo/DataObjectImpl.h"
#define CLASS_NAME "SDO_DataObject"
/** {{{ sdo_do_object
* The internal structure for an SDO_DataObjectImpl
* This extends the standard zend_object
*/
typedef struct {
zend_object zo; /* The standard zend_object */
DataObjectPtr dop; /* The C++ DataObject */
} sdo_do_object;
/* }}} */
/* {{{ sdo_do_iterator
* The iterator data for this class - extends the standard zend_object_iterator
*/
typedef struct {
zend_object_iterator zoi; /* The standard zend_object_iterator */
ulong index; /* current index */
zend_bool valid;
/* PropertyList pl; sdolib crashes when I copy the list here */
zval *value;
} sdo_do_iterator;
/* }}} */
static zend_object_handlers sdo_do_object_handlers;
static zend_object_iterator_funcs sdo_do_iterator_funcs;
/* {{{ sdo_do_get_instance
*/
static sdo_do_object *sdo_do_get_instance(zval *me TSRMLS_DC)
{
return (sdo_do_object *)zend_object_store_get_object(me TSRMLS_CC);
}
/* }}} */
/* {{{ sdo_do_object_free_storage
*/
static void sdo_do_object_free_storage(void *object TSRMLS_DC)
{
sdo_do_object *my_object;
// char *space;
// char *class_name;
SDO_DEBUG_FREE(object);
my_object = (sdo_do_object *)object;
zend_hash_destroy(my_object->zo.properties);
FREE_HASHTABLE(my_object->zo.properties);
if (my_object->zo.guards) {
zend_hash_destroy(my_object->zo.guards);
FREE_HASHTABLE(my_object->zo.guards);
}
/* just release the reference, and the reference counting will kick in */
if (my_object->dop) {
try {
my_object->dop->setUserData((void *)SDO_USER_DATA_EMPTY);
my_object->dop = NULL;
} catch (SDORuntimeException e) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_WARNING, "%s%s%s(): internal error - caught exception freeing DataObject",
class_name, space, get_active_function_name(TSRMLS_C));
}
}
efree(object);
}
/* }}} */
/* {{{ debug macro functions
*/
SDO_DEBUG_ADDREF(do)
SDO_DEBUG_DELREF(do)
SDO_DEBUG_DESTROY(do)
/* }}} */
/* {{{ sdo_do_object_create
*/
static zend_object_value sdo_do_object_create(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
zval *tmp; /* this must be passed to hash_copy, but doesn't seem to be used */
sdo_do_object *my_object;
my_object = (sdo_do_object *)emalloc(sizeof(sdo_do_object));
memset(my_object, 0, sizeof(sdo_do_object));
my_object->zo.ce = ce;
my_object->zo.guards = NULL;
ALLOC_HASHTABLE(my_object->zo.properties);
zend_hash_init(my_object->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
// zend_hash_copy(my_object->zo.properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#if PHP_VERSION_ID < 50399
zend_hash_copy(my_object->zo.properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#else
object_properties_init(&my_object->zo, ce);
#endif
retval.handle = zend_objects_store_put(my_object, SDO_FUNC_DESTROY(do), sdo_do_object_free_storage, NULL TSRMLS_CC);
retval.handlers = &sdo_do_object_handlers;
SDO_DEBUG_ALLOCATE(retval.handle, my_object);
return retval;
}
/* }}} */
/* {{{ sdo_do_get
* utility function to get C++ DataObject from zval
*/
DataObjectPtr sdo_do_get(zval *me TSRMLS_DC)
{
sdo_do_object *my_object;
my_object = (sdo_do_object *)zend_object_store_get_object(me TSRMLS_CC);
if (my_object)
return my_object->dop;
else
return NULL;
}
/* }}} */
/* {{{ sdo_do_new
* Find the PHP object corresponding to the CPP object, or create one.
* The DataObject has no public constructor, this is in effect its factory method.
*/
void sdo_do_new(zval *me, DataObjectPtr dop TSRMLS_DC)
{
sdo_do_object *my_object;
// char *class_name, *space;
long object_handle = (long)dop->getUserData();
Z_TYPE_P(me) = IS_OBJECT;
/* If the object is already known to php, its user data will contain
* the value of the object handle.
*/
if (object_handle == SDO_USER_DATA_EMPTY) {
if (object_init_ex(me, sdo_dataobjectimpl_class_entry) == FAILURE) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - failed to instantiate %s object",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__, CLASS_NAME);
return;
}
my_object = (sdo_do_object *)zend_object_store_get_object(me TSRMLS_CC);
my_object->dop = dop;
/* add a back pointer to the C++ object */
try {
dop->setUserData ((void *)(long)me->value.obj.handle);
} catch (SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
}
} else {
me->value.obj.handle = object_handle;
me->value.obj.handlers = &sdo_do_object_handlers;
me->value.obj.handlers->add_ref(me TSRMLS_CC);
/* Sanity check */
my_object = (sdo_do_object *)sdo_do_get_instance(me TSRMLS_CC);
if (!my_object) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - SDO_DataObject #%d not found in store",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__, object_handle);
return;
}
}
}
/* }}} */
/* {{{ sdo_do_clone_obj
*/
static zend_object_value sdo_do_clone_obj(zval *object TSRMLS_DC)
{
sdo_do_object *my_old_object;
DataObjectPtr new_dop;
zval z_new;
my_old_object = sdo_do_get_instance(object TSRMLS_CC);
try {
new_dop = CopyHelper::copy(my_old_object->dop);
sdo_do_new(&z_new, new_dop TSRMLS_CC);
} catch (SDORuntimeException e) {
/*
* If there's an exception, the calling routine in the engine still
* attempts to discard the returned object. So we give it a real one,
* to avoid a crash. (The zval itself is irrelevant, only the object
* data is used.)
*/
object_init(&z_new);
sdo_throw_runtimeexception(&e TSRMLS_CC);
}
return z_new.value.obj;
}
/* }}} */
/* {{{ sdo_do_has_dimension
*/
static int sdo_do_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC)
{
const char *xpath;
const Property *propertyp;
sdo_do_object *my_object = (sdo_do_object *)NULL;
DataObjectPtr dop;
int return_value = 0;
// char *class_name, *space;
my_object = sdo_do_get_instance(object TSRMLS_CC);
dop = my_object->dop;
try {
if (sdo_parse_offset_param(dop, offset, &propertyp, &xpath, 1, 1 TSRMLS_CC)
== FAILURE) {
return 0;
}
/* Note: although we now have a reference to the Property,
* if the offset was an xpath, then it may not be a property of the
* DataObject instance. So we don't use the Property as a parameter
* to the DataObject methods, since this can only work if the xpath
* is a simple property name, otherwise an exception will be thrown.
*/
return_value = dop->isSet(xpath);
if (return_value && check_empty) {
/* check_empty says we should additionally test if the value is equivalent to 0 */
if (dop->isNull(xpath)) {
return_value = 0;
} else {
switch (propertyp->getTypeEnum()) {
case Type::OtherTypes: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'OtherTypes'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
return_value = 0;
break;
}
case Type::BigDecimalType:
case Type::BigIntegerType:
case Type::BooleanType:
case Type::ByteType: {
return_value = dop->getBoolean(xpath);
break;
}
case Type::BytesType: {
return_value = (dop->getLength(xpath) != 0);
break;
}
case Type::CharacterType: {
return_value = dop->getBoolean(xpath);
break;
}
case Type::DateType: {
return_value = (dop->getDate(xpath).getTime() != 0);
break;
}
case Type::DoubleType:
case Type::FloatType:
case Type::IntegerType:
case Type::LongType:
case Type::ShortType: {
return_value = dop->getBoolean(xpath);
break;
}
case Type::StringType:
case Type::UriType: {
/* TODO is this the buffer length or the string length ??? */
return_value = (dop->getLength(xpath) > 0);
break;
}
case Type::DataObjectType:
case Type::OpenDataObjectType: {
/* since the property is set, the data object cannot be 'empty' */
break;
}
case Type::ChangeSummaryType: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'ChangeSummaryType'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
return_value = 0;
break;
}
case Type::TextType: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'TextType'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
return_value = 0;
break;
}
default: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type '%s' for property '%s'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__,
propertyp->getType().getName(), xpath);
return_value = 0;
}
}
}
}
} catch(SDORuntimeException e) {
return_value = 0;
}
return return_value;
}
/* }}} */
/* {{{ sdo_do_read_list
*/
static zval *sdo_do_read_list(sdo_do_object *sdo, const char *xpath, const Property *propertyp TSRMLS_DC)
{
zval *return_value;
try {
DataObjectList& list_value = sdo->dop->getList(xpath);
if (&list_value == NULL) {
return_value = EG(uninitialized_zval_ptr);
} else {
ALLOC_INIT_ZVAL(return_value);
Z_SET_REFCOUNT_P(return_value, 0);
/* make a new SDO_DataObjectList */
sdo_dataobjectlist_new(return_value, propertyp->getType(), sdo->dop, &list_value TSRMLS_CC);
}
} catch (SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
return_value = EG(uninitialized_zval_ptr);
}
return return_value;
}
/* }}} */
/* {{{ sdo_do_read_value
*/
static zval *sdo_do_read_value(sdo_do_object *sdo, const char *xpath, const Property *propertyp TSRMLS_DC)
{
DataObjectPtr dop = sdo->dop;
uint bytes_len;
char *bytes_value;
char char_value;
wchar_t wchar_value;
DataObjectPtr doh_value;
zval *return_value = NULL;
// char *class_name, *space;
try {
if (propertyp->isMany()) {
/* If the property is many-valued and the list is uninitialized,
* all bets are off. The C++ library does not catch this
* consistently, so ...
*/
if (! dop->isSet(xpath)) {
zend_throw_exception_ex(sdo_indexoutofboundsexception_class_entry,
0 TSRMLS_CC,
"Cannot read list at index '%s' because the list is empty",
(char *)xpath);
return EG(uninitialized_zval_ptr);
}
} else {
/*
* If the property is single-valued, we should just leave it to the
* C++ library to decide whether the property is set, but currently
* it fails to detect this error, so we shall catch it here instead.
*/
if (!dop->isValid(xpath)) {
zend_throw_exception_ex(sdo_propertynotsetexception_class_entry,
0 TSRMLS_CC,
"Cannot read property '%s' because it is not set",
propertyp->getName());
return EG(uninitialized_zval_ptr);
}
}
ALLOC_INIT_ZVAL(return_value);
Z_SET_REFCOUNT_P(return_value, 0);
if (dop->isNull(xpath)) {
RETVAL_NULL();
} else {
switch(propertyp->getTypeEnum()) {
case Type::OtherTypes: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'OtherTypes' for '%s'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__, propertyp->getName());
break;
}
case Type::BigDecimalType:
case Type::BigIntegerType: {
RETVAL_STRING((char *)(dop->getCString(xpath)), 1);
break;
}
case Type::BooleanType: {
RETVAL_BOOL(dop->getBoolean(xpath));
break;
}
case Type::ByteType: {
RETVAL_LONG(dop->getByte(xpath));
break;
}
case Type::BytesType: {
bytes_len = dop->getLength(xpath);
bytes_value = (char *)emalloc(1 + bytes_len);
bytes_len = dop->getBytes(xpath, bytes_value, bytes_len);
bytes_value[bytes_len] = '\0';
RETVAL_STRINGL(bytes_value, bytes_len, 0);
break;
}
case Type::CharacterType: {
wchar_value = dop->getCharacter(xpath);
if (wchar_value > INT_MAX) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_WARNING, "%s%s%s(): wide character data lost for '%s'",
class_name, space, get_active_function_name(TSRMLS_C), propertyp->getName());
}
char_value = dop->getByte(xpath);
RETVAL_STRINGL(&char_value, 1, 1);
break;
}
case Type::DateType: {
RETVAL_LONG(dop->getDate(xpath).getTime());
break;
}
case Type::DoubleType: {
RETVAL_DOUBLE(dop->getDouble(xpath));
break;
}
case Type::FloatType: {
RETVAL_DOUBLE(dop->getFloat(xpath));
break;
}
case Type::IntegerType: {
RETVAL_LONG(dop->getInteger(xpath));
break;
}
case Type::LongType: {
/* An SDO long (64 bits) may overflow a PHP int, so we return it as a string */
RETVAL_STRING((char *)dop->getCString(xpath), 1);
break;
}
case Type::ShortType: {
RETVAL_LONG(dop->getShort(xpath));
break;
}
case Type::StringType:
case Type::UriType: {
RETVAL_STRING((char *)dop->getCString(xpath), 1);
break;
}
case Type::DataObjectType:
case Type::OpenDataObjectType: {
doh_value = dop->getDataObject(xpath);
if (!doh_value) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_WARNING, "%s%s%s(): read a NULL DataObject for property '%s'",
class_name, space, get_active_function_name(TSRMLS_C), propertyp->getName());
RETVAL_NULL();
} else {
sdo_do_new(return_value, doh_value TSRMLS_CC);
}
break;
}
case Type::ChangeSummaryType: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'ChangeSummaryType'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
break;
}
case Type::TextType: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'TextType'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
break;
}
default: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type '%s' for property '%s'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__,
propertyp->getType().getName(), propertyp->getName());
}
}
}
return return_value;
} catch (SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
if (return_value) {
efree(return_value);
}
return_value = EG(uninitialized_zval_ptr);
}
return return_value;
}
/* }}} */
static zval *read_property(zval *object, zval *offset, int type TSRMLS_DC)
{
return sdo_do_read_dimension(object, offset, type TSRMLS_DC);
}
/* {{{ sdo_do_read_dimension
*/
static zval *sdo_do_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
{
const char *xpath;
const Property *propertyp;
sdo_do_object *my_object;
DataObjectPtr dop;
zval *return_value;
my_object = sdo_do_get_instance(object TSRMLS_CC);
dop = my_object->dop;
try {
if (sdo_parse_offset_param(
dop, offset, &propertyp, &xpath, 1, 0 TSRMLS_CC) == FAILURE) {
return EG(uninitialized_zval_ptr);
}
/* Note: although we now have a reference to the Property,
* if the offset was an xpath, then it may not be a property of the
* DataObject instance. So we don't use the Property as a parameter
* to the DataObject methods, since this can only work if the xpath
* is a simple property name, otherwise an exception will be thrown.
*/
/*
* We need to discover whether the xpath should result in returning the list itself, or
* a list element, hence the XpathHelper test
*/
if (propertyp->isMany() && ! XpathHelper::isIndexed(xpath)) {
return_value = sdo_do_read_list (my_object, xpath, propertyp TSRMLS_CC);
} else {
return_value = sdo_do_read_value(my_object, xpath, propertyp TSRMLS_CC);
}
} catch(SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
return_value = EG(uninitialized_zval_ptr);
}
return return_value;
}
/* }}} */
/* {{{ sdo_do_unset_dimension
*/
static void sdo_do_unset_dimension(zval *object, zval *offset TSRMLS_DC)
{
const char *xpath;
sdo_do_object *my_object;
my_object = sdo_do_get_instance(object TSRMLS_CC);
try {
if (sdo_parse_offset_param(
my_object->dop, offset, NULL, &xpath, 1, 0 TSRMLS_CC) == FAILURE) {
return;
}
my_object->dop->unset(xpath);
} catch(SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
}
}
/* }}} */
/* {{{ sdo_do_write_dimension
*/
static void sdo_do_write_dimension(zval *object, zval *offset, zval *z_propertyValue TSRMLS_DC)
{
const char *xpath;
const Property *property_p;
sdo_do_object *my_object, *value_object;
DataObjectPtr dop;
zval temp_zval;
Type::Types type_enum;
// char *class_name, *space;
my_object = sdo_do_get_instance(object TSRMLS_CC);
dop = my_object->dop;
ZVAL_NULL(&temp_zval);
try {
if (sdo_parse_offset_param(
dop, offset, &property_p, &xpath, ! dop->getType().isOpenType(), 0 TSRMLS_CC) == FAILURE) {
return;
}
if (property_p == NULL) {
/* open type, so we'll derive the sdo type from the php type */
type_enum = sdo_map_zval_type(z_propertyValue);
} else {
/* known type, so we'll coerce the php type to the sdo type */
type_enum = property_p->getTypeEnum();
}
/* Note: although we now have a reference to the Property,
* if the offset was an xpath, then it may not be a property of the
* DataObject instance. So we don't use the Property as a parameter
* to the DataObject methods, since this can only work if the xpath
* is a simple property name, otherwise an exception will be thrown.
*/
if (Z_TYPE_P(z_propertyValue) == IS_NULL) {
dop->setNull(xpath);
} else {
/*
* Since we may have to coerce the type, we make a local copy of the zval, so that the
* original is unaffected.
*
* TODO This could be optimized to only copy if we do actually change the type.
*/
temp_zval = *z_propertyValue;
zval_copy_ctor(&temp_zval);
switch(type_enum) {
case Type::OtherTypes: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'OtherTypes'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
break;
}
case Type::BigDecimalType:
case Type::BigIntegerType: {
convert_to_string(&temp_zval);
dop->setCString(xpath, Z_STRVAL(temp_zval));
break;
}
case Type::BooleanType: {
convert_to_boolean(&temp_zval);
dop->setBoolean(xpath, ZEND_TRUTH(Z_BVAL(temp_zval)));
break;
}
case Type::ByteType: {
convert_to_long(&temp_zval);
dop->setByte(xpath, Z_LVAL(temp_zval));
break;
}
case Type::BytesType: {
convert_to_string(&temp_zval);
dop->setBytes(xpath, Z_STRVAL(temp_zval), Z_STRLEN(temp_zval));
break;
}
case Type::CharacterType: {
convert_to_string(&temp_zval);
dop->setCharacter(xpath, (char)(Z_STRVAL(temp_zval)[0]));
break;
}
case Type::DateType: {
convert_to_long(&temp_zval);
dop->setDate(xpath, (SDODate)Z_LVAL(temp_zval));
break;
}
case Type::DoubleType: {
convert_to_double(&temp_zval);
dop->setDouble(xpath, Z_DVAL(temp_zval));
break;
}
case Type::FloatType: {
convert_to_double(&temp_zval);
dop->setFloat(xpath, (float)Z_DVAL(temp_zval));
break;
}
case Type::IntegerType: {
convert_to_long(&temp_zval);
dop->setInteger(xpath, (int)Z_LVAL(temp_zval));
break;
}
case Type::LongType: {
if (Z_TYPE(temp_zval) == IS_LONG) {
dop->setLong(xpath, Z_LVAL(temp_zval));
} else {
convert_to_string(&temp_zval);
dop->setCString(xpath, Z_STRVAL(temp_zval));
}
break;
}
case Type::ShortType: {
convert_to_long(&temp_zval);
dop->setShort(xpath, (short)Z_LVAL(temp_zval));
break;
}
case Type::StringType:
case Type::UriType: {
convert_to_string(&temp_zval);
dop->setCString(xpath, Z_STRVAL(temp_zval));
break;
}
case Type::DataObjectType:
case Type::OpenDataObjectType: {
if (Z_TYPE_P(z_propertyValue) != IS_OBJECT) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
sdo_throw_exception_ex (sdo_unsupportedoperationexception_class_entry, 0,0 TSRMLS_CC,
"%s%s%s(): cannot cast %s to %s for '%s'",
class_name, space, get_active_function_name(TSRMLS_C),
zend_zval_type_name(z_propertyValue), CLASS_NAME, xpath);
} else if (!instanceof_function(Z_OBJCE_P(z_propertyValue), sdo_dataobjectimpl_class_entry TSRMLS_CC)) {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
sdo_throw_exception_ex (sdo_unsupportedoperationexception_class_entry, 0,0 TSRMLS_CC,
"%s%s%s(): cannot cast %s to %s for '%s'",
class_name, space, get_active_function_name(TSRMLS_C),
Z_OBJCE_P(z_propertyValue)->name,
sdo_dataobjectimpl_class_entry->name, xpath);
} else {
value_object = (sdo_do_object *)zend_object_store_get_object(z_propertyValue TSRMLS_CC);
dop->setDataObject(xpath, value_object->dop);
}
break;
}
case Type::ChangeSummaryType: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'ChangeSummaryType'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
break;
}
case Type::TextType: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type 'TextType'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__);
break;
}
default: {
const char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - unexpected DataObject type '%s' for property '%s'",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__,
(property_p ? property_p->getType().getName() : ""), xpath);
}
}
}
} catch (SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
}
zval_dtor(&temp_zval);
}
/* }}} */
/* {{{ sdo_do_get_properties
* called as a result of print_r() or vardump(), but doesn't get called for reflection
* Returns an array of name=>value pairs for the properties of the data object
*/
static HashTable *sdo_do_get_properties(zval *object TSRMLS_DC)
{
sdo_do_object *my_object;
int entries;
zval *tmp;
my_object = sdo_do_get_instance(object TSRMLS_CC);
zend_hash_clean(my_object->zo.properties);
try {
PropertyList pl = my_object->dop->getInstanceProperties();
entries = pl.size();
for (long index = 0; index < entries; index++) {
/* It's safe to use the property directly here, it cannot be an xpath */
const Property& property = pl[index];
const char *property_name = property.getName();
/* Usually we check isMany() before isSet(), because we should not return NULL just because
* the List is empty. But for get_properties the return values are readonly, so it's
* safer and clearer to omit empty lists.
*/
if (my_object->dop->isSet(property)) {
if (property.isMany()) {
long count = 0;
tmp = sdo_do_read_list(my_object, property_name, &property TSRMLS_CC);
sdo_list_count_elements (tmp, &count TSRMLS_CC);
if (count == 0)
continue;
} else {
tmp = sdo_do_read_value(my_object, property_name, &property TSRMLS_CC);
}
} else {
continue;
}
zval_add_ref(&tmp);
zend_hash_add(my_object->zo.properties, (char *)property_name, 1 + strlen(property_name),
&tmp, sizeof(zval *), NULL);
}
} catch (SDORuntimeException e) {
sdo_throw_runtimeexception(&e TSRMLS_CC);
}
return my_object->zo.properties;
}
/* }}} */
/* {{{ sdo_do_compare_objects
* gets called as a consequence of an == comparison
* we do a deep compare of property names, types, and values
*/
static int sdo_do_compare_objects(zval *object1, zval *object2 TSRMLS_DC)
{
sdo_do_object *my_object1, *my_object2;
DataObjectPtr dop1, dop2;
PropertyList pl;
int entries;
const char *propertyName;
zval offset;
zval result;
INIT_PZVAL(&offset);
INIT_PZVAL(&result);
my_object1 = sdo_do_get_instance(object1 TSRMLS_CC);
my_object2 = sdo_do_get_instance(object2 TSRMLS_CC);
dop1 = my_object1->dop;
dop2 = my_object2->dop;
if (dop1 == dop2)
return SUCCESS;
try {
pl = dop1->getInstanceProperties();
entries = pl.size();
if (entries != dop2->getInstanceProperties().size())
return FAILURE;
for (int i = 0; i < entries; i++) {
const Property& prop1 = pl[i];
propertyName = prop1.getName();
const Property& prop2 = dop2->getType().getProperty(propertyName);
/* if we get here, then the object does have a property of the same name */
if (&prop1 != &prop2) {
if ((prop1.isMany() != prop2.isMany()) ||
/*
* When a property is many-valued, it may not return consistent values for isSet,
* Let it carry on to the value equality tests.
*/
((!prop1.isMany()) && dop1->isSet(prop1) != dop2->isSet(prop2)) ||
(prop1.getTypeEnum() != prop2.getTypeEnum()) ||
/*
* The meaning of containment for primitives is somewhat unclear in the spec.
* I'm going to ignore it to avoid a discrepancy in unserializing
*/
(prop1.getType().isDataObjectType() && (prop1.isContainment() != prop2.isContainment())) ||
(prop1.isReadOnly() != prop2.isReadOnly()))
return FAILURE;
}
/* OK we can consider the properties equal */
if (dop1->isSet(prop1)) {
/* the property is set, so we must also compare its value */
ZVAL_STRING(&offset, (char *)propertyName, 0);
zval *value1 = sdo_do_read_dimension(object1, &offset, BP_VAR_R TSRMLS_CC);
zval_add_ref(&value1);
zval *value2 = sdo_do_read_dimension(object2, &offset, BP_VAR_R TSRMLS_CC);
zval_add_ref(&value2);
int rc = compare_function(&result, value1, value2 TSRMLS_CC);
zval_ptr_dtor(&value1);
zval_ptr_dtor(&value2);
if (rc || Z_LVAL(result))
return FAILURE;
}
}
return SUCCESS;
} catch (SDORuntimeException e) {
/* In this case we won't rethrow the exception - suffice it to say that the objects are not equal */
return FAILURE;
}
}
/* }}} */
/* {{{ sdo_do_cast_object
*/
#if PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1)
static int sdo_do_cast_object(zval *readobj, zval *writeobj, int type TSRMLS_DC)
{
int should_free = 0;
#else
static int sdo_do_cast_object(zval *readobj, zval *writeobj, int type, int should_free TSRMLS_DC)
{
#endif
sdo_do_object *my_object;
ostringstream print_buf;
zval free_obj;
int rc = SUCCESS;
if (should_free) {
free_obj = *writeobj;
}
my_object = sdo_do_get_instance(readobj TSRMLS_CC);
try {
const Type& type = my_object->dop->getType();
PropertyList pl = my_object->dop->getInstanceProperties();
print_buf << "object(" << CLASS_NAME << ")#" <<
readobj->value.obj.handle << " (" << pl.size() << ") {";
for (unsigned int i = 0; i < pl.size(); i++) {
const Property& prop = pl[i];
if (i > 0) {
print_buf << "; ";
}
print_buf << prop.getName();
/* We'll try to print the value for single-valued primitives only.
* Multi-valued properties just get a dimension.
*/
if (prop.isMany()) {
print_buf << '[' << my_object->dop->getList(prop).size() << ']';
} else if (my_object->dop->isSet(i) && prop.getType().isDataType()) {
print_buf << "=>";
if (my_object->dop->isNull(i)) {
print_buf << "NULL";
} else {
print_buf << '\"' << my_object->dop->getCString(i) << '\"';
}
}
}
print_buf << '}';
std::string print_string = print_buf.str()/*.substr(0, SDO_TOSTRING_MAX)*/;
ZVAL_STRINGL(writeobj, (char *)print_string.c_str(), print_string.length(), 1);
} catch (SDORuntimeException e) {
ZVAL_NULL(writeobj);
sdo_throw_runtimeexception(&e TSRMLS_CC);
rc = FAILURE;
}
switch(type) {
case IS_STRING: {
convert_to_string(writeobj);
break;
}
case IS_BOOL: {
convert_to_boolean(writeobj);
break;
}
case IS_LONG: {
convert_to_long(writeobj);
break;
}
case IS_DOUBLE: {
convert_to_double(writeobj);
break;
}
default:
rc = FAILURE;
}
if (should_free) {
zval_dtor(&free_obj);
}
return rc;
}
/* }}} */
/* {{{ sdo_do_count_elements
*/
static int sdo_do_count_elements(zval *object, long *count TSRMLS_DC)
{
sdo_do_object *my_object;