PL/Python: Convert oid to long/int

oid is a numeric type, so transform it to the appropriate Python
numeric type like the other ones.
This commit is contained in:
Peter Eisentraut 2012-09-29 12:41:00 -04:00
parent 811ca1300b
commit db0af74af2
5 changed files with 77 additions and 1 deletions

View File

@ -302,7 +302,7 @@ $$ LANGUAGE plpythonu;
<para>
PostgreSQL <type>smallint</type> and <type>int</type> are
converted to Python <type>int</type>.
PostgreSQL <type>bigint</type> is converted
PostgreSQL <type>bigint</type> and <type>oid</type> are converted
to <type>long</type> in Python 2 and to <type>int</type> in
Python 3.
</para>

View File

@ -321,6 +321,34 @@ CONTEXT: PL/Python function "test_type_conversion_float8"
(1 row)
CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_oid(100);
INFO: (100L, <type 'long'>)
CONTEXT: PL/Python function "test_type_conversion_oid"
test_type_conversion_oid
--------------------------
100
(1 row)
SELECT * FROM test_type_conversion_oid(2147483649);
INFO: (2147483649L, <type 'long'>)
CONTEXT: PL/Python function "test_type_conversion_oid"
test_type_conversion_oid
--------------------------
2147483649
(1 row)
SELECT * FROM test_type_conversion_oid(null);
INFO: (None, <type 'NoneType'>)
CONTEXT: PL/Python function "test_type_conversion_oid"
test_type_conversion_oid
--------------------------
(1 row)
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
plpy.info(x, type(x))
return x

View File

@ -321,6 +321,34 @@ CONTEXT: PL/Python function "test_type_conversion_float8"
(1 row)
CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpython3u;
SELECT * FROM test_type_conversion_oid(100);
INFO: (100, <class 'int'>)
CONTEXT: PL/Python function "test_type_conversion_oid"
test_type_conversion_oid
--------------------------
100
(1 row)
SELECT * FROM test_type_conversion_oid(2147483649);
INFO: (2147483649, <class 'int'>)
CONTEXT: PL/Python function "test_type_conversion_oid"
test_type_conversion_oid
--------------------------
2147483649
(1 row)
SELECT * FROM test_type_conversion_oid(null);
INFO: (None, <class 'NoneType'>)
CONTEXT: PL/Python function "test_type_conversion_oid"
test_type_conversion_oid
--------------------------
(1 row)
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
plpy.info(x, type(x))
return x

View File

@ -39,6 +39,7 @@ static PyObject *PLyFloat_FromNumeric(PLyDatumToOb *arg, Datum d);
static PyObject *PLyInt_FromInt16(PLyDatumToOb *arg, Datum d);
static PyObject *PLyInt_FromInt32(PLyDatumToOb *arg, Datum d);
static PyObject *PLyLong_FromInt64(PLyDatumToOb *arg, Datum d);
static PyObject *PLyLong_FromOid(PLyDatumToOb *arg, Datum d);
static PyObject *PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d);
static PyObject *PLyString_FromDatum(PLyDatumToOb *arg, Datum d);
static PyObject *PLyList_FromArray(PLyDatumToOb *arg, Datum d);
@ -460,6 +461,9 @@ PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup)
case INT8OID:
arg->func = PLyLong_FromInt64;
break;
case OIDOID:
arg->func = PLyLong_FromOid;
break;
case BYTEAOID:
arg->func = PLyBytes_FromBytea;
break;
@ -546,6 +550,12 @@ PLyLong_FromInt64(PLyDatumToOb *arg, Datum d)
return PyLong_FromLong(DatumGetInt64(d));
}
static PyObject *
PLyLong_FromOid(PLyDatumToOb *arg, Datum d)
{
return PyLong_FromUnsignedLong(DatumGetObjectId(d));
}
static PyObject *
PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d)
{

View File

@ -119,6 +119,16 @@ SELECT * FROM test_type_conversion_float8(5000000000.5);
SELECT * FROM test_type_conversion_float8(null);
CREATE FUNCTION test_type_conversion_oid(x oid) RETURNS oid AS $$
plpy.info(x, type(x))
return x
$$ LANGUAGE plpythonu;
SELECT * FROM test_type_conversion_oid(100);
SELECT * FROM test_type_conversion_oid(2147483649);
SELECT * FROM test_type_conversion_oid(null);
CREATE FUNCTION test_type_conversion_text(x text) RETURNS text AS $$
plpy.info(x, type(x))
return x