18 package testsuite.clusterj;
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;
28 import testsuite.clusterj.AbstractQueryTest.QueryHolder;
29 import testsuite.clusterj.model.AllPrimitives;
80 void createInstances(
int number) {
81 createAllPrimitivesInstances(10);
84 public void testExplainWithNoWhereClause() {
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);
95 public void testExplainBeforeBindingParameters() {
98 dobj.where(dobj.get(
"int_null_none").equal(dobj.param(
"equal")));
99 Query<AllPrimitives> query = session.
createQuery(dobj);
102 fail(
"Explain before binding parameters should throw ClusterJUserException");
103 }
catch (ClusterJUserException ex) {
105 assertTrue(
"Message should include parameter name \"equal\"", ex.getMessage().contains(
"equal"));
109 public void testExplainAfterBindingParametersNoIndexEqual() {
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);
122 public void testExplainAfterBindingParametersUniqueEqual() {
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);
135 public void testExplainAfterBindingParametersPrimaryEqual() {
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);
148 public void testExplainAfterBindingParametersPrimaryLessThan() {
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);
161 public void testExplainAfterBindingParametersPrimaryLessThanNull() {
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);