18
18
import java .util .ArrayList ;
19
19
import java .util .Arrays ;
20
20
import java .util .Collection ;
21
+ import java .util .HashMap ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
23
24
@@ -49,20 +50,19 @@ public class SQLUpdateTest {
49
50
public SQLUpdateTest (String iURL ) {
50
51
database = new ODatabaseDocumentTx (iURL );
51
52
}
52
-
53
+
53
54
@ BeforeTest
54
55
private void before () {
55
56
database .open ("admin" , "admin" );
56
57
}
57
-
58
+
58
59
@ AfterTest
59
60
private void after () {
60
- database .close ();
61
+ database .close ();
61
62
}
62
-
63
+
63
64
@ Test
64
65
public void updateWithWhereOperator () {
65
-
66
66
67
67
List <OClusterPosition > positions = getValidPositions (4 );
68
68
@@ -72,13 +72,10 @@ public void updateWithWhereOperator() {
72
72
73
73
Assert .assertEquals (records .intValue (), 3 );
74
74
75
-
76
75
}
77
76
78
-
79
77
@ Test
80
78
public void updateWithWhereRid () {
81
-
82
79
83
80
List <ODocument > result = database .command (new OCommandSQL ("select @rid as rid from Profile where surname = 'Obama'" )).execute ();
84
81
@@ -89,32 +86,26 @@ public void updateWithWhereRid() {
89
86
90
87
Assert .assertEquals (records .intValue (), 1 );
91
88
92
-
93
89
}
94
90
95
91
@ Test (dependsOnMethods = "updateWithWhereOperator" )
96
92
public void updateCollectionsAddWithWhereOperator () {
97
-
98
93
99
94
updatedRecords = (Integer ) database .command (new OCommandSQL ("update Account add addresses = #13:0" )).execute ();
100
95
101
-
102
96
}
103
97
104
98
@ Test (dependsOnMethods = "updateCollectionsAddWithWhereOperator" )
105
99
public void updateCollectionsRemoveWithWhereOperator () {
106
-
107
100
108
101
final int records = (Integer ) database .command (new OCommandSQL ("update Account remove addresses = #13:0" )).execute ();
109
102
110
103
Assert .assertEquals (records , updatedRecords );
111
104
112
-
113
105
}
114
106
115
107
@ Test (dependsOnMethods = "updateCollectionsRemoveWithWhereOperator" )
116
108
public void updateCollectionsWithSetOperator () {
117
-
118
109
119
110
List <ODocument > docs = database .query (new OSQLSynchQuery <ODocument >("select from Account" ));
120
111
@@ -136,12 +127,10 @@ public void updateCollectionsWithSetOperator() {
136
127
database .save (loadedDoc );
137
128
}
138
129
139
-
140
130
}
141
131
142
132
@ Test (dependsOnMethods = "updateCollectionsRemoveWithWhereOperator" )
143
133
public void updateMapsWithSetOperator () {
144
-
145
134
146
135
ODocument doc = (ODocument ) database
147
136
.command (
@@ -170,12 +159,10 @@ public void updateMapsWithSetOperator() {
170
159
Assert .assertEquals (entries .get ("bla" ), "zagzig" );
171
160
Assert .assertEquals (entries .get ("testTestTEST" ), "okOkOK" );
172
161
173
-
174
162
}
175
163
176
164
@ Test (dependsOnMethods = "updateCollectionsRemoveWithWhereOperator" )
177
165
public void updateMapsWithPutOperatorAndWhere () {
178
-
179
166
180
167
ODocument doc = (ODocument ) database .command (
181
168
new OCommandSQL (
@@ -201,38 +188,32 @@ public void updateMapsWithPutOperatorAndWhere() {
201
188
202
189
Assert .assertEquals (entries .get ("one" ), "two" );
203
190
204
-
205
191
}
206
192
207
193
@ Test (dependsOnMethods = "updateCollectionsRemoveWithWhereOperator" )
208
194
public void updateAllOperator () {
209
-
210
195
211
196
Long total = database .countClass ("Profile" );
212
197
213
198
Integer records = (Integer ) database .command (new OCommandSQL ("update Profile set sex = 'male'" )).execute ();
214
199
215
200
Assert .assertEquals (records .intValue (), total .intValue ());
216
201
217
-
218
202
}
219
203
220
204
@ Test
221
205
public void updateWithWildcards () {
222
-
223
206
224
207
int updated = (Integer ) database .command (new OCommandSQL ("update Profile set sex = ? where sex = 'male' limit 1" )).execute (
225
208
"male" );
226
209
227
210
Assert .assertEquals (updated , 1 );
228
211
229
-
230
212
}
231
213
232
214
@ Test
233
215
public void updateWithWildcardsOnSetAndWhere () {
234
216
235
-
236
217
ODocument doc = new ODocument ("Person" );
237
218
doc .field ("name" , "Raf" );
238
219
doc .field ("city" , "Torino" );
@@ -261,23 +242,22 @@ public void updateWithWildcardsOnSetAndWhere() {
261
242
database .command (updatecommand ).execute ("f" , "Raf" );
262
243
checkUpdatedDoc (database , "Raf" , "TORINO" , "f" );
263
244
264
-
265
245
}
266
246
267
247
@ Test
268
- public void updateWithNamedParameters (){
248
+ public void updateWithNamedParameters () {
269
249
ODocument doc = new ODocument ("Data" );
270
250
doc .field ("name" , "Raf" );
271
251
doc .field ("city" , "Torino" );
272
252
doc .field ("gender" , "fmale" );
273
253
doc .save ();
274
254
275
255
OCommandSQL updatecommand = new OCommandSQL ("update Data set gender = :gender , city = :city where name = :name" );
276
- Map <String ,Object > params = new HashMap <String , Object >();
256
+ Map <String , Object > params = new HashMap <String , Object >();
277
257
params .put ("gender" , "f" );
278
258
params .put ("city" , "TOR" );
279
259
params .put ("name" , "Raf" );
280
-
260
+
281
261
database .command (updatecommand ).execute (params );
282
262
List <ODocument > result = database .query (new OSQLSynchQuery <Object >("select * from Data" ));
283
263
ODocument oDoc = result .get (0 );
@@ -286,10 +266,7 @@ public void updateWithNamedParameters(){
286
266
Assert .assertEquals ("f" , oDoc .field ("gender" ));
287
267
}
288
268
289
-
290
-
291
269
public void updateIncrement () {
292
-
293
270
294
271
List <ODocument > result1 = database .command (new OCommandSQL ("select salary from Account where salary is defined" )).execute ();
295
272
Assert .assertFalse (result1 .isEmpty ());
@@ -321,11 +298,10 @@ public void updateIncrement() {
321
298
float salary3 = (Float ) result3 .get (i ).field ("salary" );
322
299
Assert .assertEquals (salary3 , salary1 );
323
300
}
324
-
301
+
325
302
}
326
303
327
304
public void updateSetMultipleFields () {
328
-
329
305
330
306
List <ODocument > result1 = database .command (new OCommandSQL ("select salary from Account where salary is defined" )).execute ();
331
307
Assert .assertFalse (result1 .isEmpty ());
@@ -345,11 +321,9 @@ public void updateSetMultipleFields() {
345
321
Assert .assertEquals (result2 .get (i ).field ("checkpoint" ), true );
346
322
}
347
323
348
-
349
324
}
350
325
351
326
public void updateAddMultipleFields () {
352
-
353
327
354
328
updatedRecords = (Integer ) database .command (new OCommandSQL ("update Account add myCollection = 1, myCollection = 2 limit 1" ))
355
329
.execute ();
@@ -362,7 +336,6 @@ public void updateAddMultipleFields() {
362
336
363
337
Assert .assertTrue (myCollection .containsAll (Arrays .asList (new Integer [] { 1 , 2 })));
364
338
365
-
366
339
}
367
340
368
341
private void checkUpdatedDoc (ODatabaseDocument database , String expectedName , String expectedCity , String expectedGender ) {
0 commit comments