Skip to content

Commit

Permalink
Merge pull request #676 from kayak/add_replace_function
Browse files Browse the repository at this point in the history
Add support for REPLACE function
  • Loading branch information
itshed authored Mar 15, 2022
2 parents a7b01da + d45eb15 commit 42256b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pypika/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 14 additions & 6 deletions pypika/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 42256b2

Please sign in to comment.