MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryMultipleParameterTest.java
1 /*
2  Copyright (c) 2011, 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 java.util.List;
21 
22 import testsuite.clusterj.model.Employee;
23 
24 import com.mysql.clusterj.ClusterJUserException;
25 import com.mysql.clusterj.Query;
26 
27 import com.mysql.clusterj.query.PredicateOperand;
28 import com.mysql.clusterj.query.QueryBuilder;
29 import com.mysql.clusterj.query.QueryDomainType;
30 
32 
53  @Override
54  public Class<?> getInstanceType() {
55  return Employee.class;
56  }
57 
58  @Override
59  void createInstances(int number) {
60  createEmployeeInstances(10);
61  instances.addAll(employees);
62  }
63 
64  public void test() {
65  // QueryBuilder is the sessionFactory for queries
66  QueryBuilder builder = session.getQueryBuilder();
67  // QueryDomainType is the main interface
68  QueryDomainType<Employee>dobj = builder.createQueryDefinition(Employee.class);
69  // parameter
70  PredicateOperand param1 = dobj.param("param1");
71  PredicateOperand param2 = dobj.param("param2");
72  // property
73  PredicateOperand propertyMagic = dobj.get("magic");
74  PredicateOperand propertyId = dobj.get("id");
75  // where
76  // param1 is used in two different places but same type (int) in both
77  dobj.where(propertyMagic.equal(param1).and(propertyId.between(param1, param2)));
78  Query<Employee> query = session.createQuery(dobj);
79  query.setParameter("param1", 4);
80  query.setParameter("param2", 5);
81  List<Employee> result = query.getResultList();
82  errorIfNotEqual("Wrong size of result", 1, result.size());
83  if (result.size() == 1) {
84  errorIfNotEqual("Wrong result", 4, result.get(0).getId());
85  }
86  failOnError();
87  }
88 
89  public void testNegative() {
90  try {
91  // QueryBuilder is the sessionFactory for queries
92  QueryBuilder builder = session.getQueryBuilder();
93  // QueryDomainType is the main interface
94  QueryDomainType<Employee>dobj = builder.createQueryDefinition(Employee.class);
95  // parameter
96  PredicateOperand param1 = dobj.param("param1");
97  PredicateOperand param2 = dobj.param("param2");
98  // property
99  PredicateOperand propertyAge = dobj.get("magic");
100  PredicateOperand propertyMagic = dobj.get("name");
101  // where
102  // expect an exception here because param1 is used for String name and int magic
103  dobj.where(propertyAge.equal(param1).and(propertyMagic.between(param1, param2)));
104  } catch (ClusterJUserException e) {
105  // good catch
106  }
107  }
108 
109 }