From 147aa84c1a0c4ed6444696e04881e938f38c3c20 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 15 Aug 2002 03:31:45 +0000 Subject: [PATCH] http://archives.postgresql.org/pgsql-bugs/2002-06/msg00086.php and never saw a fix offered up. Since I'm gearing up to use Postgres and Python soon, I figured I'd have a hand at trying to get this sucker addressed. Apologies if this has already been plugged. I looked in the archives and never saw a response. At any rate, I must admit I don't think I fully understand the implications of some of the changes I made even though they appear to be straight forward. We all know the devil is in the details. Anyone more knowledgeable is requested to review my changes. :( I also updated the advanced.py script in a somewhat nonsensical fashion to make use of an int8 field in an effort to test this change. It seems to run okay, however, this is by no means an all exhaustive test. So, it's possible that a bumpy road may lay ahead for some. On the other hand...overflows (hopefully) previously lurked (long -> int conversion). Greg Copeland --- src/interfaces/python/pgmodule.c | 47 +++++++++++++++------- src/interfaces/python/tutorial/advanced.py | 20 +++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/interfaces/python/pgmodule.c b/src/interfaces/python/pgmodule.c index df685902531..355c3e4725d 100644 --- a/src/interfaces/python/pgmodule.c +++ b/src/interfaces/python/pgmodule.c @@ -289,24 +289,27 @@ get_type_array(PGresult *result, int nfields) { case INT2OID: case INT4OID: - case INT8OID: case OIDOID: typ[j] = 1; break; + case INT8OID: + typ[j] = 2; + break; + case FLOAT4OID: case FLOAT8OID: case NUMERICOID: - typ[j] = 2; - break; - - case CASHOID: typ[j] = 3; break; - default: + case CASHOID: typ[j] = 4; break; + + default: + typ[j] = 5; + break; } } @@ -1797,24 +1800,27 @@ pgquery_getresult(pgqueryobject * self, PyObject * args) { case INT2OID: case INT4OID: - case INT8OID: case OIDOID: typ[j] = 1; break; + case INT8OID: + typ[j] = 2; + break; + case FLOAT4OID: case FLOAT8OID: case NUMERICOID: - typ[j] = 2; - break; - - case CASHOID: typ[j] = 3; break; - default: + case CASHOID: typ[j] = 4; break; + + default: + typ[j] = 5; + break; } } @@ -1846,10 +1852,14 @@ pgquery_getresult(pgqueryobject * self, PyObject * args) break; case 2: - val = PyFloat_FromDouble(strtod(s, NULL)); + val = PyLong_FromLong(strtol(s, NULL, 10)); break; case 3: + val = PyFloat_FromDouble(strtod(s, NULL)); + break; + + case 4: { int mult = 1; @@ -1946,11 +1956,14 @@ pgquery_dictresult(pgqueryobject * self, PyObject * args) { case INT2OID: case INT4OID: - case INT8OID: case OIDOID: typ[j] = 1; break; + case INT8OID: + typ[j] = 2; + break; + case FLOAT4OID: case FLOAT8OID: case NUMERICOID: @@ -1995,10 +2008,14 @@ pgquery_dictresult(pgqueryobject * self, PyObject * args) break; case 2: - val = PyFloat_FromDouble(strtod(s, NULL)); + val = PyLong_FromLong(strtol(s, NULL, 10)); break; case 3: + val = PyFloat_FromDouble(strtod(s, NULL)); + break; + + case 4: { int mult = 1; diff --git a/src/interfaces/python/tutorial/advanced.py b/src/interfaces/python/tutorial/advanced.py index 49e6e436e8c..41a5bc457a3 100755 --- a/src/interfaces/python/tutorial/advanced.py +++ b/src/interfaces/python/tutorial/advanced.py @@ -109,11 +109,13 @@ def array_demo(pgcnx): print "CREATE TABLE sal_emp (" print " name text," print " pay_by_quarter int4[]," + print " pay_by_extra_quarter int8[]," print " schedule text[][]" print ")" pgcnx.query("""CREATE TABLE sal_emp ( name text, pay_by_quarter int4[], + pay_by_extra_quarter int8[], schedule text[][])""") wait_key() print @@ -123,18 +125,22 @@ def array_demo(pgcnx): print "INSERT INTO sal_emp VALUES (" print " 'Bill'," print " '{10000,10000,10000,10000}'," + print " '{9223372036854775800,9223372036854775800,9223372036854775800}'," print " '{{\"meeting\", \"lunch\"}, {}}')" print print "INSERT INTO sal_emp VALUES (" print " 'Carol'," print " '{20000,25000,25000,25000}'," + print " '{9223372036854775807,9223372036854775807,9223372036854775807}'," print " '{{\"talk\", \"consult\"}, {\"meeting\"}}')" print pgcnx.query("""INSERT INTO sal_emp VALUES ( 'Bill', '{10000,10000,10000,10000}', + '{9223372036854775800,9223372036854775800,9223372036854775800}', '{{\"meeting\", \"lunch\"}, {}}')""") pgcnx.query("""INSERT INTO sal_emp VALUES ( 'Carol', '{20000,25000,25000,25000}', + '{9223372036854775807,9223372036854775807,9223372036854775807}', '{{\"talk\", \"consult\"}, {\"meeting\"}}')""") wait_key() print @@ -148,12 +154,26 @@ def array_demo(pgcnx): print pgcnx.query("""SELECT name FROM sal_emp WHERE sal_emp.pay_by_quarter[1] <> sal_emp.pay_by_quarter[2]""") print + print pgcnx.query("""SELECT name FROM sal_emp WHERE + sal_emp.pay_by_extra_quarter[1] <> sal_emp.pay_by_extra_quarter[2]""") + print print "-- retrieve third quarter pay of all employees" print print "SELECT sal_emp.pay_by_quarter[3] FROM sal_emp" print print pgcnx.query("SELECT sal_emp.pay_by_quarter[3] FROM sal_emp") print + print "-- retrieve third quarter extra pay of all employees" + print + print "SELECT sal_emp.pay_by_extra_quarter[3] FROM sal_emp" + print pgcnx.query("SELECT sal_emp.pay_by_extra_quarter[3] FROM sal_emp") + print + print "-- retrieve first two quarters of extra quarter pay of all employees" + print + print "SELECT sal_emp.pay_by_extra_quarter[1:2] FROM sal_emp" + print + print pgcnx.query("SELECT sal_emp.pay_by_extra_quarter[1:2] FROM sal_emp") + print print "-- select subarrays" print print "SELECT sal_emp.schedule[1:2][1:1] FROM sal_emp WHERE"