1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
#! coding:utf-8
from sqlalchemy import Integer, String, ForeignKey, delete, select, and_, \
or_
from sqlalchemy.dialects import mysql
from sqlalchemy import testing
from sqlalchemy.testing import AssertsCompiledSQL, fixtures, eq_
from sqlalchemy.testing.schema import Table, Column
class _DeleteTestBase(object):
@classmethod
def define_tables(cls, metadata):
Table('mytable', metadata,
Column('myid', Integer),
Column('name', String(30)),
Column('description', String(50)))
Table('myothertable', metadata,
Column('otherid', Integer),
Column('othername', String(30)))
class DeleteTest(_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL):
__dialect__ = 'default'
def test_delete(self):
table1 = self.tables.mytable
self.assert_compile(
delete(table1, table1.c.myid == 7),
'DELETE FROM mytable WHERE mytable.myid = :myid_1')
self.assert_compile(
table1.delete().where(table1.c.myid == 7),
'DELETE FROM mytable WHERE mytable.myid = :myid_1')
self.assert_compile(
table1.delete().
where(table1.c.myid == 7).
where(table1.c.name == 'somename'),
'DELETE FROM mytable '
'WHERE mytable.myid = :myid_1 '
'AND mytable.name = :name_1')
def test_where_empty(self):
table1 = self.tables.mytable
self.assert_compile(
table1.delete().where(and_()),
"DELETE FROM mytable"
)
self.assert_compile(
table1.delete().where(or_()),
"DELETE FROM mytable"
)
def test_prefix_with(self):
table1 = self.tables.mytable
stmt = table1.delete().\
prefix_with('A', 'B', dialect='mysql').\
prefix_with('C', 'D')
self.assert_compile(stmt,
'DELETE C D FROM mytable')
self.assert_compile(stmt,
'DELETE A B C D FROM mytable',
dialect=mysql.dialect())
def test_alias(self):
table1 = self.tables.mytable
talias1 = table1.alias('t1')
stmt = delete(talias1).where(talias1.c.myid == 7)
self.assert_compile(
stmt,
'DELETE FROM mytable AS t1 WHERE t1.myid = :myid_1')
def test_correlated(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
# test a non-correlated WHERE clause
s = select([table2.c.othername], table2.c.otherid == 7)
self.assert_compile(delete(table1, table1.c.name == s),
'DELETE FROM mytable '
'WHERE mytable.name = ('
'SELECT myothertable.othername '
'FROM myothertable '
'WHERE myothertable.otherid = :otherid_1'
')')
# test one that is actually correlated...
s = select([table2.c.othername], table2.c.otherid == table1.c.myid)
self.assert_compile(table1.delete(table1.c.name == s),
'DELETE FROM mytable '
'WHERE mytable.name = ('
'SELECT myothertable.othername '
'FROM myothertable '
'WHERE myothertable.otherid = mytable.myid'
')')
class DeleteFromRoundTripTest(fixtures.TablesTest):
__backend__ = True
@classmethod
def define_tables(cls, metadata):
Table('mytable', metadata,
Column('myid', Integer),
Column('name', String(30)),
Column('description', String(50)))
Table('myothertable', metadata,
Column('otherid', Integer),
Column('othername', String(30)))
Table('users', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('name', String(30), nullable=False))
Table('addresses', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('user_id', None, ForeignKey('users.id')),
Column('name', String(30), nullable=False),
Column('email_address', String(50), nullable=False))
Table('dingalings', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('address_id', None, ForeignKey('addresses.id')),
Column('data', String(30)))
Table('update_w_default', metadata,
Column('id', Integer, primary_key=True),
Column('x', Integer),
Column('ycol', Integer, key='y'),
Column('data', String(30), onupdate=lambda: "hi"))
@classmethod
def fixtures(cls):
return dict(
users=(
('id', 'name'),
(7, 'jack'),
(8, 'ed'),
(9, 'fred'),
(10, 'chuck')
),
addresses=(
('id', 'user_id', 'name', 'email_address'),
(1, 7, 'x', 'jack@bean.com'),
(2, 8, 'x', 'ed@wood.com'),
(3, 8, 'x', 'ed@bettyboop.com'),
(4, 8, 'x', 'ed@lala.com'),
(5, 9, 'x', 'fred@fred.com')
),
dingalings=(
('id', 'address_id', 'data'),
(1, 2, 'ding 1/2'),
(2, 5, 'ding 2/5')
),
)
@testing.requires.delete_from
def test_exec_two_table(self):
users, addresses = self.tables.users, self.tables.addresses
dingalings = self.tables.dingalings
with testing.db.connect() as conn:
conn.execute(dingalings.delete()) # fk violation otherwise
conn.execute(
addresses.delete().
where(users.c.id == addresses.c.user_id).
where(users.c.name == 'ed')
)
expected = [
(1, 7, 'x', 'jack@bean.com'),
(5, 9, 'x', 'fred@fred.com')
]
self._assert_table(addresses, expected)
@testing.requires.delete_from
def test_exec_three_table(self):
users = self.tables.users
addresses = self.tables.addresses
dingalings = self.tables.dingalings
testing.db.execute(
dingalings.delete().
where(users.c.id == addresses.c.user_id).
where(users.c.name == 'ed').
where(addresses.c.id == dingalings.c.address_id))
expected = [
(2, 5, 'ding 2/5')
]
self._assert_table(dingalings, expected)
@testing.requires.delete_from
def test_exec_two_table_plus_alias(self):
users, addresses = self.tables.users, self.tables.addresses
dingalings = self.tables.dingalings
with testing.db.connect() as conn:
conn.execute(dingalings.delete()) # fk violation otherwise
a1 = addresses.alias()
conn.execute(
addresses.delete().
where(users.c.id == addresses.c.user_id).
where(users.c.name == 'ed').
where(a1.c.id == addresses.c.id)
)
expected = [
(1, 7, 'x', 'jack@bean.com'),
(5, 9, 'x', 'fred@fred.com')
]
self._assert_table(addresses, expected)
@testing.requires.delete_from
def test_exec_alias_plus_table(self):
users, addresses = self.tables.users, self.tables.addresses
dingalings = self.tables.dingalings
d1 = dingalings.alias()
testing.db.execute(
delete(d1).
where(users.c.id == addresses.c.user_id).
where(users.c.name == 'ed').
where(addresses.c.id == d1.c.address_id))
expected = [
(2, 5, 'ding 2/5')
]
self._assert_table(dingalings, expected)
def _assert_table(self, table, expected):
stmt = table.select().order_by(table.c.id)
eq_(testing.db.execute(stmt).fetchall(), expected)
|