From eaf0f24b8abc7e0841368572e302cbe053674ff7 Mon Sep 17 00:00:00 2001 From: Brian Levasseur <1198476+itshed@users.noreply.github.com> Date: Mon, 14 Mar 2022 09:56:30 +0100 Subject: [PATCH 1/2] Add support for REPLACE function --- pypika/functions.py | 5 +++++ pypika/tests/test_functions.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/pypika/functions.py b/pypika/functions.py index 83584297..c8200408 100644 --- a/pypika/functions.py +++ b/pypika/functions.py @@ -249,6 +249,11 @@ def __init__(self, term, pattern, modifiers=None, alias=None): super(RegexpLike, self).__init__("REGEXP_LIKE", term, pattern, modifiers, alias=alias) +class Replace(Function): + def __init__(self, term, find_string, replace_with, alias=None): + super(Replace, self).__init__("REPLACE", term, find_string, replace_with, alias=alias) + + # Date/Time Functions class Now(Function): def __init__(self, alias=None): diff --git a/pypika/tests/test_functions.py b/pypika/tests/test_functions.py index f00d032e..2c4236e3 100644 --- a/pypika/tests/test_functions.py +++ b/pypika/tests/test_functions.py @@ -570,6 +570,14 @@ def test__substring(self): self.assertEqual('SELECT SUBSTRING("foo",2,6) FROM "abc"', str(q)) + def test__replace__str(self): + q = Q.select(fn.Replace("ABC_DEF", "_", " ")) + self.assertEqual("SELECT REPLACE('ABC_DEF','_',' ')", str(q)) + + def test__replace__field(self): + q = Q.from_(self.t).select(fn.Replace(self.t.foobar, self.t.foo, self.t.bar)) + self.assertEqual('SELECT REPLACE("foobar","foo","bar") FROM "abc"', str(q)) + class SplitPartFunctionTests(unittest.TestCase): t = T("abc") From d45eb1520dc73fb04dd8696782ed37089190101f Mon Sep 17 00:00:00 2001 From: Brian Levasseur <1198476+itshed@users.noreply.github.com> Date: Mon, 14 Mar 2022 17:39:42 +0100 Subject: [PATCH 2/2] Attempt to satisfy linter. --- pypika/tests/test_functions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pypika/tests/test_functions.py b/pypika/tests/test_functions.py index 2c4236e3..f526966e 100644 --- a/pypika/tests/test_functions.py +++ b/pypika/tests/test_functions.py @@ -274,11 +274,11 @@ def test__complex_op_div_no_parentheses(self): def test__complex_op_exponent_parentheses(self): q1 = Q.from_("abc").select(F("a") / (F("b") ** 2)) - q2 = Q.from_(self.t).select(self.t.a / (self.t.b ** 2)) + q2 = Q.from_(self.t).select(self.t.a / (self.t.b**2)) q3 = Q.from_("abc").select(F("a") ** (F("b") / 2)) q4 = Q.from_(self.t).select(self.t.a ** (self.t.b / 2)) q5 = Q.from_("abc").select((F("a") ** F("b")) ** 2) - q6 = Q.from_(self.t).select((self.t.a ** self.t.b) ** 2) + q6 = Q.from_(self.t).select((self.t.a**self.t.b) ** 2) self.assertEqual('SELECT "a"/POW("b",2) FROM "abc"', str(q1)) self.assertEqual('SELECT "a"/POW("b",2) FROM "abc"', str(q2)) @@ -289,9 +289,9 @@ def test__complex_op_exponent_parentheses(self): def test__complex_op_exponent_no_parentheses(self): q1 = Q.from_("abc").select(F("a") ** F("b") ** 2) - q2 = Q.from_(self.t).select(self.t.a ** self.t.b ** 2) + q2 = Q.from_(self.t).select(self.t.a**self.t.b**2) q3 = Q.from_("abc").select(F("a") / F("b") ** 2) - q4 = Q.from_(self.t).select(self.t.a / self.t.b ** 2) + q4 = Q.from_(self.t).select(self.t.a / self.t.b**2) self.assertEqual('SELECT POW("a",POW("b",2)) FROM "abc"', str(q1)) self.assertEqual('SELECT POW("a",POW("b",2)) FROM "abc"', str(q2)) @@ -369,14 +369,14 @@ def test__arithmetic_with_function(self): def test__exponent__number(self): q1 = Q.from_("abc").select(F("a") ** 2) - q2 = Q.from_(self.t).select(self.t.a ** 2) + q2 = Q.from_(self.t).select(self.t.a**2) self.assertEqual('SELECT POW("a",2) FROM "abc"', str(q1)) self.assertEqual('SELECT POW("a",2) FROM "abc"', str(q2)) def test__exponent__decimal(self): q1 = Q.from_("abc").select(F("a") ** 0.5) - q2 = Q.from_(self.t).select(self.t.a ** 0.5) + q2 = Q.from_(self.t).select(self.t.a**0.5) self.assertEqual('SELECT POW("a",0.5) FROM "abc"', str(q1)) self.assertEqual('SELECT POW("a",0.5) FROM "abc"', str(q2))