Repository: kayak/pypika
Pull request #289 (2019-08-07 12:38:41)

Support is joined
I discovered that it can be useful to have ability to check if table is already joined, because PyPika does not check this at it creates invalid SQL.

Usage:
`q.is_joined(table)  # Returns True/False`

Because _joins is protected and I did not want to change it, I created is_joined method.


Patch:
diff --git a/README.rst b/README.rst
index af8e05a..d5b9d98 100644
--- a/README.rst
+++ b/README.rst
@@ -97,7 +97,7 @@ Using ``pypika.Table`` based select alias
 .. code-block:: python
 
     customers = Table('customers')
-    q = customers.select(customers.id, customers.fname)
+    q = customers.select(custimers.id, customers.fname)
 
 query will return in the following SQL:
 
diff --git a/pypika/functions.py b/pypika/functions.py
index d4e527d..52cf3c4 100644
--- a/pypika/functions.py
+++ b/pypika/functions.py
@@ -135,11 +135,6 @@ class DateDiff(Function):
         super(DateDiff, self).__init__('DATEDIFF', interval, start_date, end_date, alias=alias)
 
 
-class TimeDiff(Function):
-    def __init__(self, start_time, end_time, alias=None):
-        super(TimeDiff, self).__init__('TIMEDIFF', start_time, end_time, alias=alias)
-
-
 class DateAdd(Function):
     def __init__(self, date_part, interval, term, alias=None):
         super(DateAdd, self).__init__('DATE_ADD', date_part, interval, term, alias=alias)
diff --git a/pypika/queries.py b/pypika/queries.py
index f25d9ba..24637b9 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -788,6 +788,9 @@ class QueryBuilder(Selectable, Term):
 
         self._joins.append(join)
 
+    def is_joined(self, table):
+        return any(table == join.item for join in self._joins)
+
     def _validate_table(self, term):
         """
         Returns False if the term references a table not already part of the


Test patch:
diff --git a/pypika/tests/test_functions.py b/pypika/tests/test_functions.py
index 75f3a09..fcb3add 100644
--- a/pypika/tests/test_functions.py
+++ b/pypika/tests/test_functions.py
@@ -576,10 +576,6 @@ class DateFunctionsTests(unittest.TestCase):
         a = fn.TimestampAdd('year', 1, '2017-10-01')
         self.assertEqual(str(a), "TIMESTAMPADD('year',1,'2017-10-01')")
 
-    def test_time_diff(self):
-        a = fn.TimeDiff('18:00:00', '10:00:00')
-        self.assertEqual(str(a), "TIMEDIFF('18:00:00','10:00:00')")
-
     def test_date_add(self):
         a = fn.DateAdd('year', 1, '2017-10-01')
         self.assertEqual(str(a), "DATE_ADD('year',1,'2017-10-01')")
diff --git a/pypika/tests/test_query.py b/pypika/tests/test_query.py
index 3397306..2b82c9d 100644
--- a/pypika/tests/test_query.py
+++ b/pypika/tests/test_query.py
@@ -95,3 +95,9 @@ class QueryTablesTests(unittest.TestCase):
                          'WHEN "fname"=\'John\' THEN \'It was John\' '
                          'ELSE \'It was someone else.\' END "who_was_it" '
                          'FROM "b"', str(query))
+
+    def test_is_joined(self):
+        q = Query.from_(self.table_a).join(self.table_b).on(self.table_a.foo == self.table_b.boo)
+
+        self.assertTrue(q.is_joined(self.table_b))
+        self.assertFalse(q.is_joined(self.table_c))