Fixed search of types into schemas.

We don't need to look for stuff implicitly into pg_catalog as all
the builtin ranges are already registered. So just search into
'public' if the schema is not specified.
This commit is contained in:
Daniele Varrazzo 2012-09-24 00:33:49 +01:00
parent a1a2772a79
commit a858987844
2 changed files with 30 additions and 3 deletions

View File

@ -291,7 +291,7 @@ class RangeCaster(object):
schema, tname = name.split('.', 1) schema, tname = name.split('.', 1)
else: else:
tname = name tname = name
schema = None schema = 'public'
# get the type oid and attributes # get the type oid and attributes
try: try:
@ -301,8 +301,8 @@ select rngtypid, rngsubtype,
from pg_range r from pg_range r
join pg_type t on t.oid = rngtypid join pg_type t on t.oid = rngtypid
join pg_namespace ns on ns.oid = typnamespace join pg_namespace ns on ns.oid = typnamespace
where typname = %s and (%s is null or ns.nspname = %s); where typname = %s and ns.nspname = %s;
""", (tname, schema, schema)) """, (tname, schema))
except ProgrammingError: except ProgrammingError:
if not conn.autocommit: if not conn.autocommit:

View File

@ -1242,6 +1242,33 @@ class RangeCasterTestCase(unittest.TestCase):
self.assertRaises(psycopg2.ProgrammingError, self.assertRaises(psycopg2.ProgrammingError,
register_range, 'nosuchrange', 'FailRange', cur) register_range, 'nosuchrange', 'FailRange', cur)
def test_schema_range(self):
cur = self.conn.cursor()
cur.execute("create schema rs")
cur.execute("create type r1 as range (subtype=text)")
cur.execute("create type r2 as range (subtype=text)")
cur.execute("create type rs.r2 as range (subtype=text)")
cur.execute("create type rs.r3 as range (subtype=text)")
cur.execute("savepoint x")
from psycopg2.extras import register_range
ra1 = register_range('r1', 'r1', cur)
ra2 = register_range('r2', 'r2', cur)
rars2 = register_range('rs.r2', 'r2', cur)
rars3 = register_range('rs.r3', 'r3', cur)
self.assertNotEqual(
ra2.typecaster.values[0],
rars2.typecaster.values[0])
self.assertRaises(psycopg2.ProgrammingError,
register_range, 'r3', 'FailRange', cur)
cur.execute("rollback to savepoint x;")
self.assertRaises(psycopg2.ProgrammingError,
register_range, 'rs.r1', 'FailRange', cur)
cur.execute("rollback to savepoint x;")
decorate_all_tests(RangeCasterTestCase, skip_if_no_range) decorate_all_tests(RangeCasterTestCase, skip_if_no_range)