1 /*
2  * src/pl/plpython/plpy_typeio.h
3  */
4 
5 #ifndef PLPY_TYPEIO_H
6 #define PLPY_TYPEIO_H
7 
8 #include "access/htup.h"
9 #include "access/tupdesc.h"
10 #include "fmgr.h"
11 #include "storage/itemptr.h"
12 
13 struct PLyDatumToOb;
14 typedef PyObject *(*PLyDatumToObFunc) (struct PLyDatumToOb *, Datum);
15 
16 typedef struct PLyDatumToOb
17 {
18 	PLyDatumToObFunc func;
19 	FmgrInfo	typfunc;		/* The type's output function */
20 	FmgrInfo	typtransform;	/* from-SQL transform */
21 	Oid			typoid;			/* The OID of the type */
22 	int32		typmod;			/* The typmod of the type */
23 	Oid			typioparam;
24 	bool		typbyval;
25 	int16		typlen;
26 	char		typalign;
27 	struct PLyDatumToOb *elm;
28 } PLyDatumToOb;
29 
30 typedef struct PLyTupleToOb
31 {
32 	PLyDatumToOb *atts;
33 	int			natts;
34 } PLyTupleToOb;
35 
36 typedef union PLyTypeInput
37 {
38 	PLyDatumToOb d;
39 	PLyTupleToOb r;
40 } PLyTypeInput;
41 
42 /* convert PyObject to a Postgresql Datum or tuple.
43  * output from Python
44  */
45 struct PLyObToDatum;
46 typedef Datum (*PLyObToDatumFunc) (struct PLyObToDatum *, int32, PyObject *);
47 
48 typedef struct PLyObToDatum
49 {
50 	PLyObToDatumFunc func;
51 	FmgrInfo	typfunc;		/* The type's input function */
52 	FmgrInfo	typtransform;	/* to-SQL transform */
53 	Oid			typoid;			/* The OID of the type */
54 	int32		typmod;			/* The typmod of the type */
55 	Oid			typioparam;
56 	bool		typbyval;
57 	int16		typlen;
58 	char		typalign;
59 	struct PLyObToDatum *elm;
60 } PLyObToDatum;
61 
62 typedef struct PLyObToTuple
63 {
64 	PLyObToDatum *atts;
65 	int			natts;
66 } PLyObToTuple;
67 
68 typedef union PLyTypeOutput
69 {
70 	PLyObToDatum d;
71 	PLyObToTuple r;
72 } PLyTypeOutput;
73 
74 /* all we need to move PostgreSQL data to Python objects,
75  * and vice versa
76  */
77 typedef struct PLyTypeInfo
78 {
79 	PLyTypeInput in;
80 	PLyTypeOutput out;
81 
82 	/*
83 	 * is_rowtype can be: -1 = not known yet (initial state); 0 = scalar
84 	 * datatype; 1 = rowtype; 2 = rowtype, but I/O functions not set up yet
85 	 */
86 	int			is_rowtype;
87 	/* used to check if the type has been modified */
88 	Oid			typ_relid;
89 	TransactionId typrel_xmin;
90 	ItemPointerData typrel_tid;
91 
92 	/* context for subsidiary data (doesn't belong to this struct though) */
93 	MemoryContext mcxt;
94 } PLyTypeInfo;
95 
96 extern void PLy_typeinfo_init(PLyTypeInfo *arg, MemoryContext mcxt);
97 
98 extern void PLy_input_datum_func(PLyTypeInfo *arg, Oid typeOid, HeapTuple typeTup, Oid langid, List *trftypes);
99 extern void PLy_output_datum_func(PLyTypeInfo *arg, HeapTuple typeTup, Oid langid, List *trftypes);
100 
101 extern void PLy_input_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc);
102 extern void PLy_output_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc);
103 
104 extern void PLy_output_record_funcs(PLyTypeInfo *arg, TupleDesc desc);
105 
106 /* conversion from Python objects to composite Datums */
107 extern Datum PLyObject_ToCompositeDatum(PLyTypeInfo *info, TupleDesc desc, PyObject *plrv);
108 
109 /* conversion from heap tuples to Python dictionaries */
110 extern PyObject *PLyDict_FromTuple(PLyTypeInfo *info, HeapTuple tuple, TupleDesc desc);
111 
112 /* conversion from Python objects to C strings */
113 extern char *PLyObject_AsString(PyObject *plrv);
114 
115 #endif   /* PLPY_TYPEIO_H */
116