Permalink
Browse files

Add support for SORTOP clause for AGGREGATEs.

 * pyrseas/dbobject/function.py (Aggregate.create): Reorganize all
   optional clauses and add SORTOP.  (ProcDict.query): Retrieve
   aggsortop.  (ProcDict._from_catalog): Remove sortop if not used.
 * tests/dbobject/test_function.py: Add test for SORTOP.
  • Loading branch information...
1 parent a65b812 commit 38f8bbfbd089da39a32e79cf9572fead972250ec @jmafc jmafc committed Jan 6, 2012
Showing with 21 additions and 5 deletions.
  1. +10 −5 pyrseas/dbobject/function.py
  2. +11 −0 tests/dbobject/test_function.py
View
15 pyrseas/dbobject/function.py
@@ -151,16 +151,19 @@ def create(self):
:return: SQL statements
"""
stmts = []
- ffunc = cond = ''
+ opt_clauses = []
if hasattr(self, 'finalfunc'):
ffname = self.finalfunc[:self.finalfunc.index('(')]
- ffunc = ",\n FINALFUNC = %s" % (ffname)
+ opt_clauses.append("FINALFUNC = %s" % ffname)
if hasattr(self, 'initcond'):
- cond = ",\n INITCOND = '%s'" % (self.initcond)
+ opt_clauses.append("INITCOND = '%s'" % self.initcond)
+ if hasattr(self, 'sortop'):
+ opt_clauses.append("SORTOP = %s" % self.sortop)
stmts.append("CREATE AGGREGATE %s(%s) (\n SFUNC = %s,"
"\n STYPE = %s%s%s)" % (
self.qualname(),
- self.arguments, self.sfunc, self.stype, ffunc, cond))
+ self.arguments, self.sfunc, self.stype,
+ opt_clauses and ',\n ' or '', ',\n '.join(opt_clauses)))
if hasattr(self, 'description'):
stmts.append(self.comment())
return stmts
@@ -181,7 +184,7 @@ class ProcDict(DbObjectDict):
aggtransfn::regprocedure AS sfunc,
aggtranstype::regtype AS stype,
aggfinalfn::regprocedure AS finalfunc,
- agginitval AS initcond,
+ agginitval AS initcond, aggsortop::regoper AS sortop,
obj_description(p.oid, 'pg_proc') AS description,
prorows::integer AS rows
FROM pg_proc p
@@ -203,6 +206,8 @@ def _from_catalog(self):
del proc.cost
if proc.finalfunc == '-':
del proc.finalfunc
+ if proc.sortop == '0':
+ del proc.sortop
self[(sch, prc, arg)] = Aggregate(**proc.__dict__)
else:
self[(sch, prc, arg)] = Function(**proc.__dict__)
View
11 tests/dbobject/test_function.py
@@ -330,6 +330,17 @@ def test_map_aggregate_init_final(self):
self.assertEqual(dbmap['schema public']['aggregate a1(integer)'],
expmap)
+ def test_map_aggregate_sortop(self):
+ "Map an aggregate with a SORTOP"
+ self.db.execute(CREATE_STMT2)
+ expmap = {'sfunc': 'f1(integer,integer)', 'stype': 'integer',
+ 'sortop': 'pg_catalog.>'}
+ dbmap = self.db.execute_and_map(
+ "CREATE AGGREGATE a1 (integer) (SFUNC = f1, STYPE = integer, "
+ "SORTOP = >)")
+ self.assertEqual(dbmap['schema public']['aggregate a1(integer)'],
+ expmap)
+
class AggregateToSqlTestCase(PyrseasTestCase):
"""Test SQL generation from input aggregates"""

0 comments on commit 38f8bbf

Please sign in to comment.