-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype.c
More file actions
2470 lines (2137 loc) · 50.8 KB
/
Copy pathtype.c
File metadata and controls
2470 lines (2137 loc) · 50.8 KB
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
/*
* Postgres type interfaces
*
* This file contains the necessary functionality for creating and
* initializing [PG]type interface types. The PyPgType_Type is a PyType_Type
* subclass that instantiates into [Python] types that represent PostgreSQL
* types. The structure of [Postgres] objects, instances of PyPgType_Type
* instances, is described in type/object.c
* [pypg/type/object.h]--PyPgType_Type instances instantiate into
* PyPgObject's.
*/
#include <setjmp.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "postgres.h"
#include "access/heapam.h"
#include "access/transam.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_namespace.h"
#include "commands/typecmds.h"
#include "executor/executor.h"
#include "nodes/params.h"
#include "parser/parse_type.h"
#include "tcop/dest.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/array.h"
#include "utils/datum.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
#include "utils/tuplestore.h"
#include "mb/pg_wchar.h"
#include "pypg/python.h"
#include "pypg/postgres.h"
#include "pypg/extension.h"
#include "pypg/pl.h"
#include "pypg/error.h"
#include "pypg/tupledesc.h"
#include "pypg/type/type.h"
#include "pypg/type/object.h"
#include "pypg/type/array.h"
#include "pypg/type/bitwise.h"
#include "pypg/type/record.h"
#include "pypg/type/numeric.h"
#include "pypg/type/string.h"
#include "pypg/type/timewise.h"
#include "pypg/type/system.h"
static PyObj type_anonymous_composites = NULL;
static PyObj type_cache = NULL;
/*
* Initialized in PyPgType_Init for tupledesc.c.
*/
PyObj PyPg_pg_attribute_Type = NULL;
/* For pg_attribute */
PyObj PyPg_aclitem_Array_Type = NULL;
/* For PyPgType_modin() */
PyObj PyPg_cstring_Array_Type = NULL;
void
PyPgClearTypeCache(void)
{
PyDict_Clear(type_cache);
}
static bool
is_builtin(Oid typoid)
{
switch (typoid)
{
case CSTRINGARRAYOID:
case AttributeRelation_Rowtype_Id:
case ACLITEMOID + 1: /* ACLITEMARRAYOID? */
#define TYP(name) \
case PyPg_##name##_Type_oid:
PYPG_DB_TYPES()
#undef TYP
return(true);
break;
default:
return(false);
break;
}
Assert(false);
}
/*
* Decrement all non-NULL PyObject's in the given typinfo
* and free any palloc'd data.
*/
static void
clear_typinfo(PyPgTypeInfo typinfo)
{
PyObj ob;
/*
* For built-ins, this is used to identify if the
* typinfo has been initialized.
*/
typinfo->typ_xmin = InvalidTransactionId;
/*
* DECREF all the Python objects.
*/
ob = typinfo->typname_PyUnicode;
typinfo->typname_PyUnicode = NULL;
Py_XDECREF(ob);
ob = typinfo->nspname_PyUnicode;
typinfo->nspname_PyUnicode = NULL;
Py_XDECREF(ob);
ob = typinfo->typoid_PyLong;
typinfo->typoid_PyLong = NULL;
Py_XDECREF(ob);
ob = typinfo->typnamespace_PyLong;
typinfo->typnamespace_PyLong = NULL;
Py_XDECREF(ob);
if (typinfo->typtype == TYPTYPE_ARRAY)
{
ob = typinfo->array.x_yes.typelem_Type;
typinfo->array.x_yes.typelem_Type = NULL;
Py_XDECREF(ob);
}
else
{
ob = typinfo->array.x_no.composite.typrel_TupleDesc;
typinfo->array.x_no.composite.typrel_TupleDesc = NULL;
Py_XDECREF(ob);
}
/*
* Free any allocated type data
*/
if (typinfo->typmemory != PythonMemoryContext &&
typinfo->typmemory != NULL)
{
PG_TRY();
{
MemoryContextDelete(typinfo->typmemory);
/*
* When PLPY_STRANGE_THINGS is defined.
*/
RaiseAStrangeError
}
PG_CATCH();
{
PyErr_EmitPgErrorAsWarning("deleting type memory context caused error");
}
PG_END_TRY();
typinfo->typmemory = NULL;
}
}
/*
* inherit_typinfo - fill in the typinfo field using super.
*
* This is used for filling in the typinfo for a DOMAIN subtype.
*/
static void
inherit_typinfo(PyPgTypeInfo dst, PyPgTypeInfo src)
{
dst->typtype = src->typtype;
dst->typalign = src->typalign;
dst->typbyval = src->typbyval;
dst->typlen = src->typlen;
if (dst->typtype == TYPTYPE_ARRAY)
{
dst->array.x_yes.typelem =
src->array.x_yes.typelem;
dst->array.x_yes.typelem_Type =
src->array.x_yes.typelem_Type;
Py_XINCREF(dst->array.x_yes.typelem_Type);
}
else
{
dst->array.x_no.typarray =
src->array.x_no.typarray;
if (dst->typtype == TYPTYPE_COMPOSITE)
{
dst->array.x_no.composite.typrelid =
src->array.x_no.composite.typrelid;
dst->array.x_no.composite.typrel_TupleDesc =
src->array.x_no.composite.typrel_TupleDesc;
Py_XINCREF(dst->array.x_no.composite.typrel_TupleDesc);
}
}
}
/*
* PyPgType_IsCurrent - determine if the current pg_type entry is newer
* than 'typ'.
*
* Note: This can THROW().
*/
bool
PyPgType_IsCurrent(PyObj typ)
{
HeapTuple ht;
ItemPointerData typ_tid;
TransactionId typ_xmin;
Oid typoid;
typoid = PyPgType_GetOid(typ);
if (is_builtin(typoid))
return(true);
ht = SearchSysCache(TYPEOID, typoid, 0, 0, 0);
/* no longer exists? yeah, that's not current. */
if (!HeapTupleIsValid(ht))
return(false);
/*
* copy visibility info and release pg_type entry
*/
typ_xmin = HeapTupleHeaderGetXmin(ht->t_data);
typ_tid = ht->t_self;
ReleaseSysCache(ht);
/*
* If the entry changed, don't bother with worrying about the relation's
* natts for reltypes.
*/
if (PyPgType_IsComposite(typ) && (
PyPgType_GetXMin(typ) != typ_xmin ||
!ItemPointerEquals(PyPgType_GetItemPointer(typ), &typ_tid)))
{
return(false);
}
/*
* Was the structure of the type's relation modified?
*/
if (PyPgType_IsComposite(typ))
{
Oid typrelid = PyPgType_GetTableOid(typ);
TupleDesc td = PyPgType_GetTupleDesc(typ);
/*
* Registered composite? Compare natts and attisdropped status.
*
* If it's an anonymous composite, there is no reference point at
* this level. It's up to the caller to make sure things are consistent
* with its source.
*/
if (OidIsValid(typrelid))
{
Relation typrel;
typrel = RelationIdGetRelation(typrelid);
/* it doesn't exist? okay, it's not up-to-date */
if (typrel == NULL)
return(false);
if (typrel->rd_att->natts != td->natts)
{
RelationClose(typrel);
return(false);
}
else
{
/*
* Were any attributes dropped?
*/
Form_pg_attribute *cur_atts = typrel->rd_att->attrs;
Form_pg_attribute *typ_atts = td->attrs;
int i;
for (i = 0; i < td->natts; ++i)
{
if (!(cur_atts[i]->attisdropped) != !(typ_atts[i]->attisdropped))
break; /* break to close relation */
}
RelationClose(typrel);
if (i < td->natts) /* it break'd out */
return(false);
}
}
/*
* Same attributes, or it was anonymous.
* Now analyze the types of the attributes in the composite.
*
* XXX: recursive types would mean infinite recursion here.
*/
if (!PyPgTupleDesc_IsCurrent(PyPgType_GetPyPgTupleDesc(typ)))
return(false);
}
else if (PyPgType_IsArray(typ))
{
PyObj elem_Type = PyPgType_GetElementType(typ);
if (!PyPgType_IsCurrent(elem_Type))
return(false);
}
return(true);
}
/*
* Fill in the FmgrInfo for:
*
* typinput, typoutput,
* typreceive, typsend,
* typmodin and typmodout.
*
* elog() on failure.
*/
static void
fill_fmgrinfo(struct pypg_type_func *typfunc, MemoryContext typcontext)
{
FmgrInfo *flinfo;
flinfo = &typfunc->typinput;
if (OidIsValid(flinfo->fn_oid))
fmgr_info_cxt(flinfo->fn_oid, flinfo, typcontext);
flinfo = &typfunc->typoutput;
if (OidIsValid(flinfo->fn_oid))
fmgr_info_cxt(flinfo->fn_oid, flinfo, typcontext);
flinfo = &typfunc->typreceive;
if (OidIsValid(flinfo->fn_oid))
fmgr_info_cxt(flinfo->fn_oid, flinfo, typcontext);
flinfo = &typfunc->typsend;
if (OidIsValid(flinfo->fn_oid))
fmgr_info_cxt(flinfo->fn_oid, flinfo, typcontext);
flinfo = &typfunc->typmodin;
if (OidIsValid(flinfo->fn_oid))
fmgr_info_cxt(flinfo->fn_oid, flinfo, typcontext);
flinfo = &typfunc->typmodout;
if (OidIsValid(flinfo->fn_oid))
fmgr_info_cxt(flinfo->fn_oid, flinfo, typcontext);
}
/*
* Given a PyPgTypeInfo pointer *with* OidIsValid(typinfo->typoid),
* fill out the pg_type information.
*/
static int
fill_pg_type(PyPgTypeInfo typinfo)
{
MemoryContext former = CurrentMemoryContext;
volatile HeapTuple ht = NULL;
int r = 0;
TransactionId xmin;
ItemPointerData tid;
Assert(typinfo != NULL);
Assert(OidIsValid(typinfo->typoid));
Assert(!ext_state);
typinfo->typoid_PyLong = NULL;
typinfo->typnamespace_PyLong = NULL;
typinfo->typname_PyUnicode = NULL;
typinfo->nspname_PyUnicode = NULL;
PG_TRY();
{
Form_pg_type ts;
Form_pg_namespace ns;
ht = SearchSysCache(TYPEOID,
ObjectIdGetDatum(typinfo->typoid),
0, 0, 0);
if (!HeapTupleIsValid(ht))
{
ereport(ERROR, (
errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("type does not exist")));
}
/*
* For the is_current checks.
*/
xmin = HeapTupleHeaderGetXmin(ht->t_data);
tid = ht->t_self;
/*
* Get the necessary information from pg_type.
*/
ts = (Form_pg_type) GETSTRUCT(ht);
if (!ts->typisdefined)
{
ereport(ERROR, (
errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("type is not defined")));
}
typinfo->typioparam = getTypeIOParam(ht);
/*
* Build a context if it's not a builtin.
*/
if (is_builtin(typinfo->typoid) == false)
{
typinfo->typmemory = AllocSetContextCreate(PythonMemoryContext,
NameStr(ts->typname),
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
}
else
typinfo->typmemory = PythonMemoryContext;
if (OidIsValid(ts->typbasetype))
{
/*
* Domains will inherit all the type info from the basetype.
* All that is needed is the constraint information.
*/
typinfo->domain.x_yes.typbasetype = ts->typbasetype;
typinfo->domain.x_yes.typndims = ts->typndims;
typinfo->domain.x_yes.typnotnull = ts->typnotnull;
/*
* This is used by create_heap_type to identify domains.
*/
typinfo->typfunc = NULL;
MemoryContextSwitchTo(typinfo->typmemory);
typinfo->domain.x_yes.constraint_list =
GetDomainConstraints(typinfo->typoid);
MemoryContextSwitchTo(former);
/*
* Builtin DOMAIN? nah.
*/
Assert(!is_builtin(typinfo->typoid));
}
else
{
typinfo->typbyval = ts->typbyval;
typinfo->typtype = ts->typtype;
/*
* typfunc points to domain.x_no, so
* it will *not* be identified as a domain.
*/
typinfo->typfunc = &(typinfo->domain.x_no);
typinfo->typfunc->typinput.fn_oid = ts->typinput;
typinfo->typfunc->typoutput.fn_oid = ts->typoutput;
typinfo->typfunc->typreceive.fn_oid = ts->typreceive;
typinfo->typfunc->typsend.fn_oid = ts->typsend;
typinfo->typfunc->typmodin.fn_oid = ts->typmodin;
typinfo->typfunc->typmodout.fn_oid = ts->typmodout;
/*
* throws on failure
*/
fill_fmgrinfo(typinfo->typfunc, typinfo->typmemory);
if (OidIsValid(ts->typelem))
{
/*
* It's an ARRAY type.
*/
typinfo->array.x_yes.typelem = ts->typelem;
/*
* Use the contrived typtype for array types.
*/
typinfo->typtype = TYPTYPE_ARRAY;
Assert(ts->typelem != typinfo->typoid);
typinfo->array.x_yes.typelem_Type = PyPgType_FromOid(ts->typelem);
if (typinfo->array.x_yes.typelem_Type == NULL)
{
/*
* No element type..?
*/
RELEASESYSCACHE(&ht);
PyErr_RelayException();
}
}
else
{
if (OidIsValid(ts->typarray))
typinfo->array.x_no.typarray = ts->typarray;
/*
* Set the relid and get the PyPgTupleDesc
*/
if (OidIsValid(ts->typrelid))
{
Relation rd;
rd = RelationIdGetRelation(ts->typrelid);
if (rd == NULL)
{
RELEASESYSCACHE(&ht);
elog(ERROR, "relation %u of type %u not found", ts->typrelid, typinfo->typoid);
}
typinfo->array.x_no.composite.typrelid = ts->typrelid;
typinfo->array.x_no.composite.typrel_TupleDesc =
PyPgTupleDesc_FromCopy(rd->rd_att);
RelationClose(rd);
}
}
typinfo->typcategory = _PG_GET_TYPCATEGORY(ts);
typinfo->typalign = ts->typalign;
typinfo->typlen = ts->typlen;
typinfo->typnamespace = ts->typnamespace;
}
/*
* Check for the error later.
*/
typinfo->typname_PyUnicode =
PyUnicode_FromCString(NameStr(ts->typname));
RELEASESYSCACHE(&ht); /* done with pg_type entry */
/*
* We want the namespace information as well.
*/
ht = SearchSysCache(NAMESPACEOID,
ObjectIdGetDatum(typinfo->typnamespace), 0, 0, 0);
if (!HeapTupleIsValid(ht))
elog(ERROR,
"cache lookup failed for namespace %u", typinfo->typnamespace);
ns = (Form_pg_namespace) GETSTRUCT(ht);
typinfo->nspname_PyUnicode =
PyUnicode_FromCString(NameStr(ns->nspname));
RELEASESYSCACHE(&ht); /* done with pg_namespace entry */
/*
* Fill in the stored item pointer and xmin.
*/
typinfo->typ_xmin = xmin;
typinfo->typ_tid = tid;
/*
* Create and set; check for failures later.
*/
typinfo->typoid_PyLong = PyLong_FromUnsignedLong(typinfo->typoid);
typinfo->typnamespace_PyLong =
PyLong_FromUnsignedLong(typinfo->typnamespace);
}
PG_CATCH();
{
r = -1;
if (ht != NULL)
ReleaseSysCache(ht);
MemoryContextSwitchTo(former);
clear_typinfo(typinfo);
PyErr_SetPgError(false);
}
PG_END_TRY();
/*
* Make sure that all the PyObjects got created.
*/
if (r == 0 && (
typinfo->typoid_PyLong == NULL ||
typinfo->typname_PyUnicode == NULL ||
typinfo->nspname_PyUnicode == NULL ||
typinfo->typnamespace_PyLong == NULL))
{
clear_typinfo(typinfo);
r = -1;
}
Assert((r != 0 && PyErr_Occurred()) || (r == 0 && !PyErr_Occurred()));
return(r);
}
/*
* Create a PyPgHeapType_Type instance using the given PyPgTypeInfo
* (Does not initialize PyPgTypeInfo in the new object)
*/
static PyObj
create_heap_type(PyPgTypeInfo typinfo)
{
PyPgTypeInfo btypinfo;
PyObj base, bases, nbd, d, rob;
/*
* Always *one* base class.
*/
bases = PyTuple_New(1);
if (bases == NULL)
return(NULL);
d = PyDict_New();
if (d == NULL)
{
Py_DECREF(bases);
return(NULL);
}
/*
* Set this for consistent tp_name fields.
*/
if (PyDict_SetItemString(d, "__module__", typinfo->nspname_PyUnicode))
{
Py_DECREF(bases);
Py_DECREF(d);
return(NULL);
}
/*
* The name, bases, dict tuple.
*/
nbd = PyTuple_New(3);
if (nbd == NULL)
{
Py_DECREF(bases);
Py_DECREF(d);
return(NULL);
}
/*
* Fill the (name, bases, dict) tuple.
*/
PyTuple_SET_ITEM(nbd, 0, typinfo->typname_PyUnicode);
Py_INCREF(typinfo->typname_PyUnicode);
PyTuple_SET_ITEM(nbd, 1, bases);
PyTuple_SET_ITEM(nbd, 2, d);
/*
* If a domain, get the basetype to use as the type's base.
*/
if (typinfo_is_domain(typinfo))
{
base = PyPgType_FromOid(typinfo->domain.x_yes.typbasetype);
if (base == NULL)
{
Py_DECREF(nbd);
return(NULL);
}
btypinfo = PyPgTypeInfo(base);
/*
* Finish the initialization of the domain's typinfo.
*/
if (typinfo_is_domain(btypinfo))
{
typinfo->domain.x_yes.typubase_Type =
btypinfo->domain.x_yes.typubase_Type;
}
else
{
typinfo->domain.x_yes.typubase_Type = base;
}
inherit_typinfo(typinfo, btypinfo);
typinfo->typfunc = btypinfo->typfunc;
/* Should be the ultimate base's domain.x_no */
Assert(typinfo->typfunc == &(PyPgTypeInfo(typinfo->domain.x_yes.typubase_Type)->domain.x_no));
}
else
{
switch (typinfo->typtype)
{
case TYPTYPE_COMPOSITE:
base = (PyObj) &PyPg_record_Type;
break;
case TYPTYPE_PSEUDO:
base = (PyObj) &PyPgPseudo_Type;
break;
/* Note: TYPTYPE_ARRAY is contrived in pypg/type/type.h */
case TYPTYPE_ARRAY:
base = (PyObj) &PyPgArray_Type;
break;
default:
{
switch (typinfo->typcategory)
{
case TYPCATEGORY_STRING:
base = (PyObj) &PyPgString_Type;
break;
default:
base = (PyObj) &PyPgObject_Type;
break;
}
}
break;
}
Py_INCREF(base);
btypinfo = PyPgTypeInfo(base);
}
typinfo->tp_new_datum = btypinfo->tp_new_datum;
/*
* give 'bases' the reference to 'base'.
*/
PyTuple_SET_ITEM(bases, 0, base);
/*
* Py_TYPE(base) should be PyPgType_Type.
*/
Assert(Py_TYPE(base) == (PyTypeObject *) &PyPgType_Type);
rob = PyType_Type.tp_new((PyTypeObject *) Py_TYPE(base), nbd, NULL);
Py_DECREF(nbd);
/*
* If this fails, the user is obligated to clear_typinfo.
*/
if (rob != NULL)
{
PyPgTypeInfo rtypinfo = PyPgTypeInfo(rob);
memcpy(rtypinfo, typinfo, sizeof(struct PyPgTypeInfo));
if (btypinfo->typfunc != rtypinfo->typfunc)
{
/*
* It's not a domain, so the typfunc pointer needs to be updated.
*/
rtypinfo->typfunc = &(rtypinfo->domain.x_no);
}
}
return(rob);
}
PyObj
PyPgType_LookupBuiltin(Oid typoid)
{
PyObj rob;
switch (typoid)
{
#define TYP(name) \
case PyPg_##name##_Type_oid: rob = ((PyObj) &PyPg_##name##_Type); break;
PYPG_DB_TYPES()
#undef TYP
/*
* These are specially initialized.
*/
case AttributeRelation_Rowtype_Id:
rob = PyPg_pg_attribute_Type;
break;
case CSTRINGARRAYOID:
rob = PyPg_cstring_Array_Type;
break;
/*
* XXX: ACLITEMARRAYOID?
*/
case ACLITEMOID + 1:
rob = PyPg_aclitem_Array_Type;
break;
default:
/*
* No such builtin.
*/
rob = NULL;
break;
}
return(rob);
}
/*
* PyPgType_FromOid - Get a PyPgType from the given Oid
*
* There are four main paths in this function:
*
* 1. Uninitialized builtin is referenced and then initialized
* 2. Initialized builtin is referenced and quickly returned
* 3. Type exists in type cache and is quickly returned if current
* 4. Type not in cache or is not current, a new heap type created
*/
PyObj
PyPgType_FromOid(Oid typoid)
{
PyObj typoid_ob, rob = NULL;
if (DB_IS_NOT_READY())
return(NULL);
rob = PyPgType_LookupBuiltin(typoid);
if (rob == NULL)
{
/*
* It's not a statically allocated built-in.
* Check the type_cache, return the entry iff
* it's up-to-date.
* Otherwise, create a new type.
*/
struct PyPgTypeInfo typinfo = PYPG_INIT_TYPINFO(invalid);
int contains;
typoid_ob = PyLong_FromUnsignedLong(typoid);
if (typoid_ob == NULL)
return(NULL);
contains = PySequence_Contains(type_cache, typoid_ob);
if (contains == -1)
{
Py_DECREF(typoid_ob);
return(NULL);
}
if (contains)
{
bool is_current;
rob = PyDict_GetItem(type_cache, typoid_ob);
Assert(rob != NULL); /* "typoid_ob in type_cache" said yessir. */
/*
* Make sure it's up-to-date.
*/
PG_TRY();
{
is_current = PyPgType_IsCurrent(rob);
}
PG_CATCH();
{
Py_DECREF(typoid_ob);
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
if (is_current)
{
/*
* It's up-to-date. Return the object.
*/
Py_DECREF(typoid_ob);
Py_INCREF(rob);
return(rob);
}
/*
* It's not up-to-date. Remove the cache entry.
*/
if (PyDict_DelItem(type_cache, typoid_ob))
{
/* not likely, but okay, fail out... */
Py_DECREF(typoid_ob);
return(NULL);
}
rob = NULL;
}
/*
* Type was not in cache, or was not up-to-date.
* Make a new one.
*/
typinfo.typoid = typoid;
if (fill_pg_type(&typinfo))
{
/*
* unlikely to happen, but it's probably that the type does not exist
*/
Py_DECREF(typoid_ob);
return(NULL);
}
/*
* typinfo needed to be filled first because
* that's how the new type's base is identified.
*/
rob = create_heap_type(&typinfo);
if (rob == NULL)
{
Py_DECREF(typoid_ob);
clear_typinfo(&typinfo);
return(NULL);
}
if (PyDict_SetItem(type_cache, typoid_ob, rob))
{
/*
* SetItem failed, DECREF and return failure.
*/
Py_DECREF(typoid_ob);
Py_DECREF(rob);
return(NULL);
}
}
else
{
/*
* Built-in type.
*/
PyPgTypeInfo typinfo = PyPgTypeInfo(rob);
if (typinfo->typ_xmin == InvalidTransactionId)
{
/*
* Hasn't been initialized yet, so do so now.
*/
if (fill_pg_type(typinfo))
{
/*
* fill_pg_type will clear_typinfo on failure.
*/
return(NULL);
}
/*
* Builtins are assumed to *not* be domains.
*/
Assert(!typinfo_is_domain(typinfo));
}
/*
* Everything is good. Grab a reference for the caller and return.
*/
Py_INCREF(rob);
}
return(rob);
}
/*
* Create an anonymous record type from a PyPgTupleDesc
*/
PyObj
PyPgType_FromPyPgTupleDesc(PyObj td_ob, PyObj typname)
{
PyPgTypeInfo btypinfo;
struct PyPgTypeInfo typinfo;
PyObj rob;
bzero(&typinfo, sizeof(struct PyPgTypeInfo));
btypinfo = PyPgTypeInfo(&PyPg_record_Type);
typinfo.typoid = RECORDOID;
typinfo.typtype = TYPTYPE_COMPOSITE;
typinfo.typnamespace = InvalidOid;
typinfo.typtype = btypinfo->typtype;
typinfo.typalign = btypinfo->typalign;
typinfo.typbyval = btypinfo->typbyval;
typinfo.typlen = btypinfo->typlen;
/*
* Used to "trick" any IsCurrent checks for RECORD returning
* multiple OUT parameter functions.
*/
typinfo.typ_xmin = btypinfo->typ_xmin;
typinfo.typ_tid = btypinfo->typ_tid;
typinfo.typfunc = &(typinfo.domain.x_no);
typinfo.domain.x_no.typinput.fn_oid = btypinfo->domain.x_no.typinput.fn_oid;
typinfo.domain.x_no.typoutput.fn_oid = btypinfo->domain.x_no.typoutput.fn_oid;
typinfo.domain.x_no.typreceive.fn_oid = btypinfo->domain.x_no.typreceive.fn_oid;
typinfo.domain.x_no.typsend.fn_oid = btypinfo->domain.x_no.typsend.fn_oid;
typinfo.domain.x_no.typmodin.fn_oid = btypinfo->domain.x_no.typmodin.fn_oid;
typinfo.domain.x_no.typmodout.fn_oid = btypinfo->domain.x_no.typmodout.fn_oid;
PG_TRY();
{
typinfo.typmemory = AllocSetContextCreate(PythonMemoryContext,
"PythonRecordTypeMemoryContext",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
/*
* These should be empty, but refill anyways.
*/
fill_fmgrinfo(typinfo.typfunc, typinfo.typmemory);
}
PG_CATCH();