Skip to content

Commit a7b01da

Browse files
authored
Merge pull request #631 from elisong/add-use/force-indexes-shallow-copy
fix: add _use/_force_indexes into __copy__ with shallow copy
2 parents a2acad0 + b9aa50e commit a7b01da

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

pypika/queries.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,8 @@ def __copy__(self) -> "QueryBuilder":
752752
newone._joins = copy(self._joins)
753753
newone._unions = copy(self._unions)
754754
newone._updates = copy(self._updates)
755+
newone._force_indexes = copy(self._force_indexes)
756+
newone._use_indexes = copy(self._use_indexes)
755757
return newone
756758

757759
@builder

pypika/tests/test_immutability.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import unittest
22

33
from pypika import (
4+
AliasedQuery,
45
Query,
56
Tables,
67
)
78

89

910
class ImmutabilityTests(unittest.TestCase):
10-
table_a, table_b = Tables("a", "b")
11+
table_a, table_b, table_c = Tables("a", "b", "c")
1112

1213
def test_select_returns_new_query_instance(self):
1314
query_a = Query.from_(self.table_a).select(self.table_a.foo)
@@ -16,6 +17,72 @@ def test_select_returns_new_query_instance(self):
1617
self.assertIsNot(query_a, query_b)
1718
self.assertNotEqual(str(query_a), str(query_b))
1819

20+
def test_groupby_returns_new_query_instance(self):
21+
query_a = Query.from_(self.table_a).select(self.table_a.foo).groupby(self.table_a.foo)
22+
query_b = query_a.groupby(self.table_a.bar)
23+
24+
self.assertIsNot(query_a, query_b)
25+
self.assertNotEqual(str(query_a), str(query_b))
26+
27+
def test_orderby_return_new_query_instance(self):
28+
query_a = Query.from_(self.table_a).select(self.table_a.foo).orderby(self.table_a.foo)
29+
query_b = query_a.orderby(self.table_a.bar)
30+
31+
self.assertIsNot(query_a, query_b)
32+
self.assertNotEqual(str(query_a), str(query_b))
33+
34+
def test_join_return_new_query_instance(self):
35+
base = Query.from_(self.table_a).select(self.table_a.foo)
36+
query_a = base.join(self.table_b).on(self.table_a.foo == self.table_b.bar).select(self.table_b.bar)
37+
query_b = query_a.join(self.table_c).on(self.table_a.foo == self.table_c.baz).select(self.table_c.baz)
38+
39+
self.assertIsNot(query_a, query_b)
40+
self.assertNotEqual(str(query_a), str(query_b))
41+
42+
def test_use_index_return_new_query_instance(self):
43+
query_a = Query.from_(self.table_a).select(self.table_a.foo).use_index('idx1')
44+
query_b = query_a.use_index('idx2')
45+
46+
self.assertIsNot(query_a, query_b)
47+
self.assertNotEqual(str(query_a), str(query_b))
48+
49+
def test_force_index_return_new_query_instance(self):
50+
query_a = Query.from_(self.table_a).select(self.table_a.foo).force_index('idx1')
51+
query_b = query_a.force_index('idx2')
52+
53+
self.assertIsNot(query_a, query_b)
54+
self.assertNotEqual(str(query_a), str(query_b))
55+
56+
def test_with_return_new_query_instance(self):
57+
alias_1 = Query.from_(self.table_a).select(self.table_a.foo)
58+
alias_2 = Query.from_(self.table_b).select(self.table_b.bar)
59+
query_a = Query.from_(AliasedQuery('a1')).select('foo').with_(alias_1, 'a1')
60+
query_b = query_a.with_(alias_2, 'a2')
61+
62+
self.assertIsNot(query_a, query_b)
63+
self.assertNotEqual(str(query_a), str(query_b))
64+
65+
def test_insert_into_return_new_query_instance(self):
66+
query_a = Query.into(self.table_a).insert('foo1')
67+
query_b = query_a.insert('foo2')
68+
69+
self.assertIsNot(query_a, query_b)
70+
self.assertNotEqual(str(query_a), str(query_b))
71+
72+
def test_replace_into_return_new_query_instance(self):
73+
query_a = Query.into(self.table_a).replace('foo')
74+
query_b = query_a.replace('bar')
75+
76+
self.assertIsNot(query_a, query_b)
77+
self.assertNotEqual(str(query_a), str(query_b))
78+
79+
def test_update_set_return_new_query_instance(self):
80+
query_a = Query.update(self.table_a).set(self.table_a.foo, 'foo')
81+
query_b = query_a.set(self.table_a.bar, 'bar')
82+
83+
self.assertIsNot(query_a, query_b)
84+
self.assertNotEqual(str(query_a), str(query_b))
85+
1986
def test_queries_after_join(self):
2087
query1 = Query.from_(self.table_a).select(self.table_a.foo)
2188
query2 = query1.join(self.table_b).on(self.table_a.foo == self.table_b.bar).select(self.table_b.buz)

0 commit comments

Comments
 (0)