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