MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryExplainTest.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 java.util.Map;
21 
22 import com.mysql.clusterj.ClusterJUserException;
23 import com.mysql.clusterj.Constants;
24 import com.mysql.clusterj.Query;
25 import com.mysql.clusterj.query.QueryBuilder;
26 import com.mysql.clusterj.query.QueryDomainType;
27 
28 import testsuite.clusterj.AbstractQueryTest.QueryHolder;
29 import testsuite.clusterj.model.AllPrimitives;
30 
72 public class QueryExplainTest extends AbstractQueryTest {
73 
74  @Override
75  public Class getInstanceType() {
76  return AllPrimitives.class;
77  }
78 
79  @Override
80  void createInstances(int number) {
81  createAllPrimitivesInstances(10);
82  }
83 
84  public void testExplainWithNoWhereClause() {
85  QueryBuilder builder = session.getQueryBuilder();
86  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
87  Query<AllPrimitives> query = session.createQuery(dobj);
88  Map<String, Object> result = query.explain();
89  String indexUsed = result.get(Query.INDEX_USED).toString();
90  String scanType = result.get(Query.SCAN_TYPE).toString();
91  assertEquals("Query explain with no where clause should have index none", "none", indexUsed);
92  assertEquals("Query explain with no where clause should have scan type TABLE_SCAN", "TABLE_SCAN", scanType);
93  }
94 
95  public void testExplainBeforeBindingParameters() {
96  QueryBuilder builder = session.getQueryBuilder();
97  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
98  dobj.where(dobj.get("int_null_none").equal(dobj.param("equal")));
99  Query<AllPrimitives> query = session.createQuery(dobj);
100  try {
101  query.explain();
102  fail("Explain before binding parameters should throw ClusterJUserException");
103  } catch (ClusterJUserException ex) {
104  // good catch; make sure message includes parameter name "equal"
105  assertTrue("Message should include parameter name \"equal\"", ex.getMessage().contains("equal"));
106  }
107  }
108 
109  public void testExplainAfterBindingParametersNoIndexEqual() {
110  QueryBuilder builder = session.getQueryBuilder();
111  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
112  dobj.where(dobj.get("int_null_none").equal(dobj.param("equal")));
113  Query<AllPrimitives> query = session.createQuery(dobj);
114  query.setParameter("equal", 1);
115  Map<String, Object> result = query.explain();
116  String indexUsed = result.get(Query.INDEX_USED).toString();
117  String scanType = result.get(Query.SCAN_TYPE).toString();
118  assertEquals("Query explain with no index should have index none", "none", indexUsed);
119  assertEquals("Query explain with no index should have scan type TABLE_SCAN", Query.SCAN_TYPE_TABLE_SCAN, scanType);
120  }
121 
122  public void testExplainAfterBindingParametersUniqueEqual() {
123  QueryBuilder builder = session.getQueryBuilder();
124  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
125  dobj.where(dobj.get("int_not_null_hash").equal(dobj.param("equal")));
126  Query<AllPrimitives> query = session.createQuery(dobj);
127  query.setParameter("equal", 1);
128  Map<String, Object> result = query.explain();
129  String indexUsed = result.get(Query.INDEX_USED).toString();
130  String scanType = result.get(Query.SCAN_TYPE).toString();
131  assertEquals("Query explain with PRIMARY key equal should have index int_not_null_hash", "idx_int_not_null_hash", indexUsed);
132  assertEquals("Query explain with PRIMARY key equal should have scan type UNIQUE_KEY", Query.SCAN_TYPE_UNIQUE_KEY, scanType);
133  }
134 
135  public void testExplainAfterBindingParametersPrimaryEqual() {
136  QueryBuilder builder = session.getQueryBuilder();
137  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
138  dobj.where(dobj.get("id").equal(dobj.param("equal")));
139  Query<AllPrimitives> query = session.createQuery(dobj);
140  query.setParameter("equal", 1);
141  Map<String, Object> result = query.explain();
142  String indexUsed = result.get(Query.INDEX_USED).toString();
143  String scanType = result.get(Query.SCAN_TYPE).toString();
144  assertEquals("Query explain with PRIMARY key equal should have index PRIMARY", "PRIMARY", indexUsed);
145  assertEquals("Query explain with PRIMARY key equal should have scan type PRIMARY_KEY", Query.SCAN_TYPE_PRIMARY_KEY, scanType);
146  }
147 
148  public void testExplainAfterBindingParametersPrimaryLessThan() {
149  QueryBuilder builder = session.getQueryBuilder();
150  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
151  dobj.where(dobj.get("id").lessThan(dobj.param("lessThan")));
152  Query<AllPrimitives> query = session.createQuery(dobj);
153  query.setParameter("lessThan", 1);
154  Map<String, Object> result = query.explain();
155  String indexUsed = result.get(Query.INDEX_USED).toString();
156  String scanType = result.get(Query.SCAN_TYPE).toString();
157  assertEquals("Query explain with PRIMARY key lessThan should have index PRIMARY", "PRIMARY", indexUsed);
158  assertEquals("Query explain with PRIMARY key lessThan should have scan type INDEX_SCAN", Query.SCAN_TYPE_INDEX_SCAN, scanType);
159  }
160 
161  public void testExplainAfterBindingParametersPrimaryLessThanNull() {
162  QueryBuilder builder = session.getQueryBuilder();
163  QueryDomainType<AllPrimitives> dobj = builder.createQueryDefinition(AllPrimitives.class);
164  dobj.where(dobj.get("id").lessThan(dobj.param("lessThan")));
165  Query<AllPrimitives> query = session.createQuery(dobj);
166  query.setParameter("lessThan", null);
167  Map<String, Object> result = query.explain();
168  String indexUsed = result.get(Query.INDEX_USED).toString();
169  String scanType = result.get(Query.SCAN_TYPE).toString();
170  assertEquals("Query explain with PRIMARY key lessThan null should have index none", "none", indexUsed);
171  assertEquals("Query explain with PRIMARY key lessThan null should have scan type TABLE_SCAN", Query.SCAN_TYPE_TABLE_SCAN, scanType);
172  }
173 
174 }