Skip to content

Commit a4f8bd8

Browse files
author
James William Pye
committed
Add an interface to lookup a type by its corresponding relation id.
This will be used by FDW and probably should have existed prior.
1 parent 8c08894 commit a4f8bd8

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/tupledesc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@ tupd_item(PyObj self, Py_ssize_t i)
628628
pg_att_nulls[Anum_pg_attribute_attacl-1] = true;
629629
#endif
630630

631+
#ifdef Anum_pg_attribute_attoptions
632+
pg_att_nulls[Anum_pg_attribute_attoptions-1] = true;
633+
#endif
634+
631635
/*
632636
* XXX: Need a better way to construct a pg_attribute Datum.
633637
*/

src/type/type.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "pypg/python.h"
4343
#include "pypg/postgres.h"
4444
#include "pypg/extension.h"
45+
#include "pypg/pl.h"
4546
#include "pypg/error.h"
4647
#include "pypg/tupledesc.h"
4748
#include "pypg/type/type.h"
@@ -1874,7 +1875,27 @@ type_check_constraints(PyObj self, PyObj ob)
18741875
return(Py_None);
18751876
}
18761877

1878+
static PyObj
1879+
type_from_relation_id(PyObj subtype, PyObj oid_ob)
1880+
{
1881+
Oid oid;
1882+
1883+
if (Oid_FromPyObject(oid_ob, &oid))
1884+
return(NULL);
1885+
1886+
return(PyPgType_FromTableOid(oid));
1887+
}
1888+
18771889
static PyMethodDef PyPgType_Methods[] = {
1890+
{"from_relation_id", (PyCFunction) type_from_relation_id, METH_O|METH_CLASS,
1891+
PyDoc_STR(
1892+
"Postgres.Type.from_relation_id(oid)\n\n"
1893+
":param oid: The Oid of the relation to lookup.\n"
1894+
":type oid: An object suitable for use as an :py:class:`Postgres.types.oid`.\n"
1895+
":returns: The corresponding Postgres.Type instance.\n"
1896+
":rtype: :py:class:`Postgres.Type`\n\n"
1897+
"Construct a type instance from the given table Oid."
1898+
)},
18781899
{"typinput", (PyCFunction) PyPgType_typinput_method, METH_VARARGS,
18791900
PyDoc_STR("create an instance of the type using the given string data")},
18801901
{"typoutput", (PyCFunction) PyPgType_typoutput, METH_O,

test/expected/pg_9_1/type.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,20 @@ Traceback (most recent call last):
152152
Postgres.Exception
153153

154154
[public.check_typisdefined(oid)]
155+
-- Lookup the Type using the table's oid
156+
DROP TABLE IF EXISTS explicit_lookup;
157+
NOTICE: table "explicit_lookup" does not exist, skipping
158+
CREATE TABLE explicit_lookup (i int, t text);
159+
CREATE OR REPLACE FUNCTION lookup_tables_type(oid) RETURNS TEXT LANGUAGE python AS
160+
$$
161+
import Postgres
162+
163+
def main(oid):
164+
return str(list(Postgres.Type.from_relation_id(oid).descriptor))
165+
$$;
166+
SELECT lookup_tables_type('explicit_lookup'::regclass);
167+
lookup_tables_type
168+
--------------------------------------------------------------------------------------------------------------------------------------
169+
[pg_attribute('(44800,i,23,-1,4,1,0,0,-1,t,p,i,f,f,f,t,0,0,,)'), pg_attribute('(44800,t,25,-1,-1,2,0,-1,-1,f,x,i,f,f,f,t,0,100,,)')]
170+
(1 row)
171+

test/sql/type.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,16 @@ def main(oid):
119119
$python$;
120120

121121
SELECT check_typisdefined((SELECT oid FROM pg_type WHERE typname = 'shell_check' LIMIT 1));
122+
123+
-- Lookup the Type using the table's oid
124+
DROP TABLE IF EXISTS explicit_lookup;
125+
CREATE TABLE explicit_lookup (i int, t text);
126+
CREATE OR REPLACE FUNCTION lookup_tables_type(oid) RETURNS TEXT LANGUAGE python AS
127+
$$
128+
import Postgres
129+
130+
def main(oid):
131+
return str(list(Postgres.Type.from_relation_id(oid).descriptor))
132+
$$;
133+
134+
SELECT lookup_tables_type('explicit_lookup'::regclass);

0 commit comments

Comments
 (0)