diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2018-03-19 17:35:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-19 17:35:16 +0100 |
| commit | cede5111bbeea1f02a7d35941a4264c7ff95df0a (patch) | |
| tree | 959f0785f3102ba84db7d4fcd96c17ae1baab479 /django/db/models | |
| parent | 8d67c7cffdcd5fd0c5cb0b87cd699a05b461e58d (diff) | |
Refs #28643 -- Added LPad and RPad database functions.
Thanks Tim Graham for the review.
Diffstat (limited to 'django/db/models')
| -rw-r--r-- | django/db/models/functions/__init__.py | 9 | ||||
| -rw-r--r-- | django/db/models/functions/text.py | 13 |
2 files changed, 18 insertions, 4 deletions
diff --git a/django/db/models/functions/__init__.py b/django/db/models/functions/__init__.py index 5528be8c48..a0f5a9e8b2 100644 --- a/django/db/models/functions/__init__.py +++ b/django/db/models/functions/__init__.py @@ -6,8 +6,8 @@ from .datetime import ( TruncQuarter, TruncSecond, TruncTime, TruncWeek, TruncYear, ) from .text import ( - Chr, Concat, ConcatPair, Left, Length, Lower, LTrim, Ord, Replace, Right, - RTrim, StrIndex, Substr, Trim, Upper, + Chr, Concat, ConcatPair, Left, Length, Lower, LPad, LTrim, Ord, Replace, + Right, RPad, RTrim, StrIndex, Substr, Trim, Upper, ) from .window import ( CumeDist, DenseRank, FirstValue, Lag, LastValue, Lead, NthValue, Ntile, @@ -24,8 +24,9 @@ __all__ = [ 'TruncMinute', 'TruncMonth', 'TruncQuarter', 'TruncSecond', 'TruncTime', 'TruncWeek', 'TruncYear', # text - 'Chr', 'Concat', 'ConcatPair', 'Left', 'Length', 'Lower', 'LTrim', 'Ord', - 'Replace', 'Right', 'RTrim', 'StrIndex', 'Substr', 'Trim', 'Upper', + 'Chr', 'Concat', 'ConcatPair', 'Left', 'Length', 'Lower', 'LPad', 'LTrim', + 'Ord', 'Replace', 'Right', 'RPad', 'RTrim', 'StrIndex', 'Substr', 'Trim', + 'Upper', # window 'CumeDist', 'DenseRank', 'FirstValue', 'Lag', 'LastValue', 'Lead', 'NthValue', 'Ntile', 'PercentRank', 'Rank', 'RowNumber', diff --git a/django/db/models/functions/text.py b/django/db/models/functions/text.py index 614522017f..c57545bc6a 100644 --- a/django/db/models/functions/text.py +++ b/django/db/models/functions/text.py @@ -110,6 +110,15 @@ class Lower(Transform): lookup_name = 'lower' +class LPad(Func): + function = 'LPAD' + + def __init__(self, expression, length, fill_text=Value(' '), **extra): + if not hasattr(length, 'resolve_expression') and length < 0: + raise ValueError("'length' must be greater or equal to 0.") + super().__init__(expression, length, fill_text, **extra) + + class LTrim(Transform): function = 'LTRIM' lookup_name = 'ltrim' @@ -141,6 +150,10 @@ class Right(Left): return Substr(self.source_expressions[0], self.source_expressions[1] * Value(-1)) +class RPad(LPad): + function = 'RPAD' + + class RTrim(Transform): function = 'RTRIM' lookup_name = 'rtrim' |
