diff --git a/CHANGELOG b/CHANGELOG index 72db12e43f..4c0374004e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add sql_pairing tool * Add format_datetime to Report * Allow sharing view searches * Allow overriding digits in Lang.currency and Report.format_currency diff --git a/doc/ref/tools/misc.rst b/doc/ref/tools/misc.rst index ac195d0057..488db583fc 100644 --- a/doc/ref/tools/misc.rst +++ b/doc/ref/tools/misc.rst @@ -8,3 +8,7 @@ Miscellaneous .. method:: resolve(name) Resolve a dotted name to a global object. + +.. method:: sql_pairing(x, y) + +Return an SQL expression that pairs SQL integers x and y. diff --git a/trytond/tools/__init__.py b/trytond/tools/__init__.py index 26d10f988f..e8afa93657 100644 --- a/trytond/tools/__init__.py +++ b/trytond/tools/__init__.py @@ -9,14 +9,14 @@ from .misc import ( file_open, get_smtp_server, reduce_ids, reduce_domain, grouped_slice, is_instance_method, resolve, strip_wildcard, lstrip_wildcard, rstrip_wildcard, slugify, sortable_values, - escape_wildcard) + escape_wildcard, sql_pairing) from .decimal_ import decistmt __all__ = ['file_open', 'get_smtp_server', 'reduce_ids', 'reduce_domain', 'grouped_slice', 'is_instance_method', 'resolve', 'strip_wildcard', 'lstrip_wildcard', 'rstrip_wildcard', 'slugify', 'decistmt', 'ClassProperty', 'cursor_dict', 'cached_property', - 'sortable_values', 'escape_wildcard'] + 'sortable_values', 'escape_wildcard', 'sql_pairing'] class ClassProperty(property): diff --git a/trytond/tools/misc.py b/trytond/tools/misc.py index cb29c72984..c8d078b690 100644 --- a/trytond/tools/misc.py +++ b/trytond/tools/misc.py @@ -17,6 +17,7 @@ from functools import wraps from itertools import islice from sql import Literal +from sql.conditionals import Case from sql.operators import Or from trytond.const import OPERATORS @@ -241,3 +242,11 @@ def sortable_values(func): result[i] = (name, value is None, value) return result return wrapper + + +def sql_pairing(x, y): + """Return SQL expression to pair x and y + Pairing function from http://szudzik.com/ElegantPairing.pdf""" + return Case( + (x < y, (y * y) + x), + else_=(x * x) + x + y)