summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-08-26 18:01:21 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-08-26 18:01:21 -0400
commitc7ceb00792bb0d22c02887021115f0f607bf07ec (patch)
tree2e35149c11706732a8007abc9563b13beaa92d83
parentb0e0bbb816b578910d0e08f034a732fc5fc898fd (diff)
downloadsqlalchemy-c7ceb00792bb0d22c02887021115f0f607bf07ec.tar.gz
- add "identifier", can differentiate between "name" rendered and "identifier" in func.
-rw-r--r--CHANGES5
-rw-r--r--lib/sqlalchemy/sql/functions.py22
-rw-r--r--test/sql/test_functions.py29
3 files changed, 53 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index 39102110c..ee7517756 100644
--- a/CHANGES
+++ b/CHANGES
@@ -424,7 +424,10 @@ underneath "0.7.xx".
to allow for user-defined GenericFunction
subclasses to be available via the func.*
namespace automatically by classname,
- optionally using a package name as well.
+ optionally using a package name, as well
+ as with the ability to have the rendered
+ name different from the identified name
+ in func.*.
- [changed] Most classes in expression.sql
are no longer preceded with an underscore,
diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py
index 24f7e81b4..b24f8cbec 100644
--- a/lib/sqlalchemy/sql/functions.py
+++ b/lib/sqlalchemy/sql/functions.py
@@ -17,12 +17,13 @@ _registry = util.defaultdict(dict)
class _GenericMeta(VisitableType):
def __init__(cls, clsname, bases, clsdict):
cls.name = name = clsdict.get('name', clsname)
+ cls.identifier = identifier = clsdict.get('identifier', name)
package = clsdict.pop('package', '_default')
# legacy
if '__return_type__' in clsdict:
cls.type = clsdict['__return_type__']
reg = _registry[package]
- reg[name] = cls
+ reg[identifier] = cls
super(_GenericMeta, cls).__init__(clsname, bases, clsdict)
class GenericFunction(Function):
@@ -69,9 +70,26 @@ class GenericFunction(Function):
print select([func.time.as_utc()])
+ A final option is to allow the function to be accessed
+ from one name in :data:`.func` but to render as a different name.
+ The ``identifier`` attribute will override the name used to
+ access the function as loaded from :data:`.func`, but will retain
+ the usage of ``name`` as the rendered name::
+
+ class GeoBuffer(GenericFunction):
+ type = Geometry
+ package = "geo"
+ name = "ST_Buffer"
+ identifier = "buffer"
+
+ The above function will render as follows::
+
+ >>> print func.geo.buffer()
+ ST_Buffer()
+
.. versionadded:: 0.8 :class:`.GenericFunction` now supports
automatic registration of new functions as well as package
- support.
+ and custom naming support.
.. versionchanged:: 0.8 The attribute name ``type`` is used
to specify the function's return type at the class level.
diff --git a/test/sql/test_functions.py b/test/sql/test_functions.py
index fc227f375..f0fcd4b72 100644
--- a/test/sql/test_functions.py
+++ b/test/sql/test_functions.py
@@ -127,6 +127,35 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
"my_func(:param_1, :param_2, :param_3)"
)
+ def test_custom_registered_identifier(self):
+ class GeoBuffer(GenericFunction):
+ type = Integer
+ package = "geo"
+ name = "BufferOne"
+ identifier = "buf1"
+
+ class GeoBuffer2(GenericFunction):
+ type = Integer
+ name = "BufferTwo"
+ identifier = "buf2"
+
+ class BufferThree(GenericFunction):
+ type = Integer
+ identifier = "buf3"
+
+ self.assert_compile(
+ func.geo.buf1(),
+ "BufferOne()"
+ )
+ self.assert_compile(
+ func.buf2(),
+ "BufferTwo()"
+ )
+ self.assert_compile(
+ func.buf3(),
+ "BufferThree()"
+ )
+
def test_custom_args(self):
class myfunc(GenericFunction):
pass