MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultiplePKTest.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package testsuite.clusterj;
19 
20 import com.mysql.clusterj.Query;
21 
22 import com.mysql.clusterj.query.QueryDomainType;
23 import com.mysql.clusterj.query.Predicate;
24 import com.mysql.clusterj.query.QueryBuilder;
25 
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 
31 import testsuite.clusterj.model.Dn2id;
32 
34 
35  private static final int NUMBER_OF_INSTANCES = 10;
36 
37  // QueryBuilder is the sessionFactory for queries
38  QueryBuilder builder;
39  // QueryDomainType is the main interface
40  QueryDomainType dobj;
41 
42  // compare the columns with the parameters
43  Predicate equalA0;
44  Predicate equalA1;
45  Predicate equalA2;
46  Predicate equalA3;
47  Predicate lessEqualA3;
48  Predicate greaterEqualA3;
49  Predicate lessA3;
50  Predicate greaterA3;
51  Predicate betweenA3;
52  Predicate equalA4;
53  Predicate equalA5;
54  Predicate equalA6;
55  Predicate equalA7;
56  Predicate equalA8;
57  Predicate equalA9;
58  Predicate equalA10;
59  Predicate equalA11;
60  Predicate equalA12;
61  Predicate equalA13;
62  Predicate equalA14;
63  Predicate equalA15;
64 
65  @Override
66  public void localSetUp() {
67  createSessionFactory();
68  session = sessionFactory.getSession();
69  try {
70  tx.begin();
71  session.deletePersistentAll(Dn2id.class);
72  tx.commit();
73  } catch (Throwable t) {
74  // ignore errors while deleting
75  }
76  addTearDownClasses(Dn2id.class);
77  }
78 
79  protected void setupQuery() {
80  // QueryBuilder is the sessionFactory for queries
81  builder = session.getQueryBuilder();
82  // QueryDomainType is the main interface
83  dobj = builder.createQueryDefinition(Dn2id.class);
84 
85  // compare the columns with the parameters
86  equalA0 = dobj.get("a0").equal(dobj.param("a0"));
87  equalA1 = dobj.get("a1").equal(dobj.param("a1"));
88  equalA2 = dobj.get("a2").equal(dobj.param("a2"));
89  equalA3 = dobj.get("a3").equal(dobj.param("a3"));
90  lessA3 = dobj.get("a3").lessThan(dobj.param("a3Upper"));
91  lessEqualA3 = dobj.get("a3").lessEqual(dobj.param("a3Upper"));
92  greaterA3 = dobj.get("a3").greaterThan(dobj.param("a3Lower"));
93  greaterEqualA3 = dobj.get("a3").greaterEqual(dobj.param("a3Lower"));
94  betweenA3 = dobj.get("a3").between(dobj.param("a3Lower"), dobj.param("a3Upper"));
95  equalA4 = dobj.get("a4").equal(dobj.param("a4"));
96  equalA5 = dobj.get("a5").equal(dobj.param("a5"));
97  equalA6 = dobj.get("a6").equal(dobj.param("a6"));
98  equalA7 = dobj.get("a7").equal(dobj.param("a7"));
99  equalA8 = dobj.get("a8").equal(dobj.param("a8"));
100  equalA9 = dobj.get("a9").equal(dobj.param("a9"));
101  equalA10 = dobj.get("a10").equal(dobj.param("a10"));
102  equalA11 = dobj.get("a11").equal(dobj.param("a11"));
103  equalA12 = dobj.get("a12").equal(dobj.param("a12"));
104  equalA13 = dobj.get("a13").equal(dobj.param("a13"));
105  equalA14 = dobj.get("a14").equal(dobj.param("a14"));
106  equalA15 = dobj.get("a15").equal(dobj.param("a15"));
107  }
108 
109  public void test() {
110  insert();
111  findByPrimaryKey();
112  queryEqualPartialPrimaryKey();
113  queryLessEqualGreaterEqualPartialPrimaryKey();
114  queryLessGreaterEqualPartialPrimaryKey();
115  queryLessEqualGreaterPartialPrimaryKey();
116  queryLessGreaterPartialPrimaryKey();
117  queryBetweenPartialPrimaryKey();
118  queryCompletePrimaryKey();
119 // queryUniqueKey();
120  update();
121  delete();
122  failOnError();
123  }
124 
125  private void insert() {
126  createDn2idInstances(NUMBER_OF_INSTANCES);
127  tx = session.currentTransaction();
128  tx.begin();
129  session.makePersistentAll(dn2ids);
130  tx.commit();
131  }
132 
133  protected void findByPrimaryKey() {
134  tx.begin();
135  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
136  dn2idPK[1] = getA1for(NUMBER_OF_INSTANCES, i);
137  dn2idPK[3] = "employeenumber=100000" + i;
138  Dn2id d = session.find(Dn2id.class, dn2idPK);
139  // verify eid
140  int expected = i;
141  long actual = d.getEid();
142  if (expected != actual) {
143  error("findByPrimaryKey failed to find dn2id " + i
144  + " expected eid " + expected
145  + " actual eid " + actual);
146  }
147  }
148  tx.commit();
149  }
150 
151  private void queryEqualPartialPrimaryKey() {
152  tx.begin();
153  setupQuery();
154  // compare the column with the parameter
155  dobj.where(equalA0.and(equalA1.and(equalA2.and(equalA3))));
156  // create a query instance
157  Query query = session.createQuery(dobj);
158 
159  // set the parameter values
160  query.setParameter("a0", "dc=com");
161  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
162  query.setParameter("a2", "ou=people");
163  query.setParameter("a3", getA3for(8));
164  // get the results
165  List<Dn2id> results = query.getResultList();
166  // consistency check the results
167  consistencyCheck(results);
168  // verify we got the right instances
169  Set<Long> expected = new HashSet<Long>();
170  expected.add((long)8);
171  Set<Long> actual = new HashSet<Long>();
172  for (Dn2id d: results) {
173  actual.add(d.getEid());
174  }
175  errorIfNotEqual("queryEqualPartialPrimaryKey wrong Dn2id eids returned from query: ",
176  expected, actual);
177  tx.commit();
178  }
179 
180  private void queryLessEqualGreaterEqualPartialPrimaryKey() {
181  tx.begin();
182  setupQuery();
183  // compare the column with the parameter
184  dobj.where(equalA0.and(equalA1.and(equalA2.and(
185  lessEqualA3.and(greaterEqualA3)))));
186  // create a query instance
187  Query query = session.createQuery(dobj);
188 
189  // set the parameter values
190  query.setParameter("a0", "dc=com");
191  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
192  query.setParameter("a2", "ou=people");
193  query.setParameter("a3Upper", getA3for(9));
194  query.setParameter("a3Lower", getA3for(8));
195  // get the results
196  List<Dn2id> results = query.getResultList();
197  // consistency check the results
198  consistencyCheck(results);
199  // verify we got the right instances
200  Set<Long> expected = new HashSet<Long>();
201  expected.add((long)8);
202  expected.add((long)9);
203  Set<Long> actual = new HashSet<Long>();
204  for (Dn2id d: results) {
205  actual.add(d.getEid());
206  }
207  errorIfNotEqual("queryLessEqualGreaterEqualPartialPrimaryKey wrong Dn2id eids returned from query: ",
208  expected, actual);
209  tx.commit();
210  }
211 
212  private void queryLessGreaterEqualPartialPrimaryKey() {
213  tx.begin();
214  setupQuery();
215  // compare the column with the parameter
216  dobj.where(equalA0.and(equalA1.and(equalA2.and(
217  lessA3.and(greaterEqualA3)))));
218  // create a query instance
219  Query query = session.createQuery(dobj);
220 
221  // set the parameter values
222  query.setParameter("a0", "dc=com");
223  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
224  query.setParameter("a2", "ou=people");
225  query.setParameter("a3Lower", getA3for(8));
226  query.setParameter("a3Upper", getA3for(9));
227  // get the results
228  List<Dn2id> results = query.getResultList();
229  // consistency check the results
230  consistencyCheck(results);
231  // verify we got the right instances
232  Set<Long> expected = new HashSet<Long>();
233  expected.add((long)8);
234  Set<Long> actual = new HashSet<Long>();
235  for (Dn2id d: results) {
236  actual.add(d.getEid());
237  }
238  errorIfNotEqual("queryLessGreaterEqualPartialPrimaryKey wrong Dn2id eids returned from query: ",
239  expected, actual);
240  tx.commit();
241  }
242 
243  private void queryLessEqualGreaterPartialPrimaryKey() {
244  tx.begin();
245  setupQuery();
246  // compare the column with the parameter
247  dobj.where(equalA0.and(equalA1.and(equalA2.and(
248  lessEqualA3.and(greaterA3)))));
249  // create a query instance
250  Query query = session.createQuery(dobj);
251 
252  // set the parameter values
253  query.setParameter("a0", "dc=com");
254  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
255  query.setParameter("a2", "ou=people");
256  query.setParameter("a3Lower", getA3for(7));
257  query.setParameter("a3Upper", getA3for(8));
258  // get the results
259  List<Dn2id> results = query.getResultList();
260  // consistency check the results
261  consistencyCheck(results);
262  // verify we got the right instances
263  Set<Long> expected = new HashSet<Long>();
264  expected.add((long)8);
265  Set<Long> actual = new HashSet<Long>();
266  for (Dn2id d: results) {
267  actual.add(d.getEid());
268  }
269  errorIfNotEqual("queryLessEqualGreaterPartialPrimaryKey wrong Dn2id eids returned from query: ",
270  expected, actual);
271  tx.commit();
272  }
273 
274  private void queryLessGreaterPartialPrimaryKey() {
275  tx.begin();
276  setupQuery();
277  // compare the column with the parameter
278  dobj.where(equalA0.and(equalA1.and(equalA2.and(
279  lessA3.and(greaterA3)))));
280  // create a query instance
281  Query query = session.createQuery(dobj);
282 
283  // set the parameter values
284  query.setParameter("a0", "dc=com");
285  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
286  query.setParameter("a2", "ou=people");
287  query.setParameter("a3Lower", getA3for(7));
288  query.setParameter("a3Upper", getA3for(9));
289  // get the results
290  List<Dn2id> results = query.getResultList();
291  // consistency check the results
292  consistencyCheck(results);
293  // verify we got the right instances
294  Set<Long> expected = new HashSet<Long>();
295  expected.add((long)8);
296  Set<Long> actual = new HashSet<Long>();
297  for (Dn2id d: results) {
298  actual.add(d.getEid());
299  }
300  errorIfNotEqual("queryLessGreaterPartialPrimaryKey wrong Dn2id eids returned from query: ",
301  expected, actual);
302  tx.commit();
303  }
304 
305  private void queryBetweenPartialPrimaryKey() {
306  tx.begin();
307  setupQuery();
308  // compare the column with the parameter
309  dobj.where(equalA0.and(equalA1.and(equalA2.and(
310  betweenA3))));
311  // create a query instance
312  Query query = session.createQuery(dobj);
313 
314  // set the parameter values
315  query.setParameter("a0", "dc=com");
316  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
317  query.setParameter("a2", "ou=people");
318  query.setParameter("a3Lower", getA3for(8));
319  query.setParameter("a3Upper", getA3for(9));
320  // get the results
321  List<Dn2id> results = query.getResultList();
322  // consistency check the results
323  consistencyCheck(results);
324  // verify we got the right instances
325  Set<Long> expected = new HashSet<Long>();
326  expected.add((long)8);
327  expected.add((long)9);
328  Set<Long> actual = new HashSet<Long>();
329  for (Dn2id d: results) {
330  actual.add(d.getEid());
331  }
332  errorIfNotEqual("queryBetweenPartialPrimaryKey wrong Dn2id eids returned from query: ",
333  expected, actual);
334  tx.commit();
335  }
336 
337  private void queryCompletePrimaryKey() {
338  tx.begin();
339  setupQuery();
340 
341  // compare the columns with the parameters
342  dobj.where(equalA0.and(equalA1.and(equalA2.and(equalA3)))
343  .and(equalA4.and(equalA5.and(equalA6.and(equalA7))))
344  .and(equalA8.and(equalA9.and(equalA10.and(equalA11))))
345  .and(equalA12.and(equalA13.and(equalA14.and(equalA15)))));
346  // create a query instance
347  Query query = session.createQuery(dobj);
348 
349  // set the parameter values
350  query.setParameter("a0", "dc=com");
351  query.setParameter("a1", getA1for(NUMBER_OF_INSTANCES, 8));
352  query.setParameter("a2", "ou=people");
353  query.setParameter("a3", getA3for(8));
354  query.setParameter("a4", "");
355  query.setParameter("a5", "");
356  query.setParameter("a6", "");
357  query.setParameter("a7", "");
358  query.setParameter("a8", "");
359  query.setParameter("a9", "");
360  query.setParameter("a10", "");
361  query.setParameter("a11", "");
362  query.setParameter("a12", "");
363  query.setParameter("a13", "");
364  query.setParameter("a14", "");
365  query.setParameter("a15", "");
366  // get the results
367  List<Dn2id> results = query.getResultList();
368  // consistency check the results
369  consistencyCheck(results);
370  // verify we got the right instances
371  Set<Long> expected = new HashSet<Long>();
372  expected.add((long)8);
373  Set<Long> actual = new HashSet<Long>();
374  for (Dn2id d: results) {
375  actual.add(d.getEid());
376  }
377  errorIfNotEqual("queryCompletePrimaryKey wrong Dn2id eids returned from query: ",
378  expected, actual);
379  tx.commit();
380  }
381 
382  private void queryUniqueKey() {
383  throw new UnsupportedOperationException("Not yet implemented");
384  }
385 
389  protected void update() {
390  tx.begin();
391  List<Dn2id> updatedInstances = new ArrayList<Dn2id>();
392  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
393  dn2idPK[1] = getA1for(NUMBER_OF_INSTANCES, i);
394  dn2idPK[3] = "employeenumber=100000" + i;
395  Dn2id d = session.find(Dn2id.class, dn2idPK);
396  // verify eid
397  long expected = i;
398  long actual = d.getEid();
399  if (expected != actual) {
400  error("Failed to find dn2id " + i
401  + " expected eid " + expected
402  + " actual eid " + actual);
403  }
404  // now update the eid field
405  d.setEid(NUMBER_OF_INSTANCES + d.getEid());
406  updatedInstances.add(d);
407  }
408  session.updatePersistentAll(updatedInstances);
409  tx.commit();
410  // now find and verify
411  tx.begin();
412  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
413  dn2idPK[1] = getA1for(NUMBER_OF_INSTANCES, i);
414  dn2idPK[3] = "employeenumber=100000" + i;
415  Dn2id d = session.find(Dn2id.class, dn2idPK);
416  // verify eid
417  long expected = NUMBER_OF_INSTANCES + i;
418  long actual = d.getEid();
419  if (expected != actual) {
420  error("Failed to find updated dn2id " + i
421  + " expected eid " + expected
422  + " actual eid " + actual);
423  }
424  }
425  tx.commit();
426  }
427 
428  private void delete() {
429  tx.begin();
430  for (int i = 0; i < NUMBER_OF_INSTANCES; ++i) {
431  dn2idPK[1] = getA1for(NUMBER_OF_INSTANCES, i);
432  dn2idPK[3] = "employeenumber=100000" + i;
433  session.deletePersistent(Dn2id.class, dn2idPK);
434  }
435  tx.commit();
436  }
437 
438 }