MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JDBCQueryTest.java
1 package jdbctest;
2 
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.List;
9 
10 import com.mysql.clusterj.Session;
11 
12 import testsuite.clusterj.AbstractClusterJModelTest;
13 import testsuite.clusterj.model.AllPrimitives;
14 
15 public abstract class JDBCQueryTest extends AbstractClusterJModelTest {
16 
17  @Override
18  public void localSetUp() {
19  // initialize the jdbc driver
20  super.localSetUp();
21  // delete instances
22  deleteAll();
23  // create instances
24  createInstances(getNumberOfInstances());
25  commit(connection);
26  }
27 
28  @Override
29  public void localTearDown() {
30  deleteAll();
31  }
32 
33  abstract public void createInstances(int numberOfInstances);
34 
35  public void createAllPrimitiveInstance(int i) {
36  String sql = "insert into allprimitives (id, "
37  + "int_not_null_hash,"
38  + "int_not_null_btree,"
39  + "int_not_null_both,"
40  + "int_not_null_none,"
41  + "int_null_hash,"
42  + "int_null_btree,"
43  + "int_null_both,"
44  + "int_null_none,"
45  + "byte_not_null_hash,"
46  + "byte_not_null_btree,"
47  + "byte_not_null_both,"
48  + "byte_not_null_none,"
49  + "byte_null_hash,"
50  + "byte_null_btree,"
51  + "byte_null_both,"
52  + "byte_null_none,"
53  + "short_not_null_hash,"
54  + "short_not_null_btree,"
55  + "short_not_null_both,"
56  + "short_not_null_none,"
57  + "short_null_hash,"
58  + "short_null_btree,"
59  + "short_null_both,"
60  + "short_null_none,"
61  + "long_not_null_hash,"
62  + "long_not_null_btree,"
63  + "long_not_null_both,"
64  + "long_not_null_none,"
65  + "long_null_hash,"
66  + "long_null_btree,"
67  + "long_null_both,"
68  + "long_null_none"
69  + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?" +
70  ", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
71  if (getDebug()) System.out.println(sql);
72  PreparedStatement statement = prepareStatement(connection, sql);
73  int actual = executeUpdate(statement, Integer.valueOf(i),
74  Integer.valueOf(i), Integer.valueOf(i), Integer.valueOf(i), Integer.valueOf(i),
75  Integer.valueOf(i), Integer.valueOf(i), Integer.valueOf(i), Integer.valueOf(i),
76  Byte.valueOf((byte)i), Byte.valueOf((byte)i), Byte.valueOf((byte)i), Byte.valueOf((byte)i),
77  Byte.valueOf((byte)i), Byte.valueOf((byte)i), Byte.valueOf((byte)i), Byte.valueOf((byte)i),
78  Short.valueOf((short)i), Short.valueOf((short)i), Short.valueOf((short)i), Short.valueOf((short)i),
79  Short.valueOf((short)i), Short.valueOf((short)i), Short.valueOf((short)i), Short.valueOf((short)i),
80  Long.valueOf((long)i), Long.valueOf((long)i), Long.valueOf((long)i), Long.valueOf((long)i),
81  Long.valueOf((long)i), Long.valueOf((long)i), Long.valueOf((long)i), Long.valueOf((long)i));
82  errorIfNotEqual("createAllPrimitiveInstance: Mismatch on number of instances inserted ", 1, actual);
83  }
84 
88  @Override
89  protected int getNumberOfInstances() {
90  return 10;
91  }
92 
93  protected void betweenQuery(String column, String expectedIndex, Object low, Object high, int... expected) {
94  String sql = "select id from " + tableName() + " where " + column + " between ? and ?";
95  if(getDebug()) System.out.println(sql);
96  PreparedStatement statement = prepareStatement(connection, sql);
97  int[] actual = executeQuery(statement, low, high);
98  commit(connection);
99  errorIfNotEqual("betweenQuery: Mismatch on betweenQuery", expected, actual);
100  }
101 
102  protected void equalQuery(String column, String expectedIndex, int parameter, int... expected) {
103  String sql = "select id from " + tableName() + " where " + column + " = ?";
104  if (getDebug()) System.out.println(sql);
105  PreparedStatement statement = prepareStatement(connection, sql);
106  int[] actual = executeQuery(statement, parameter);
107  commit(connection);
108  errorIfNotEqual("equalQuery: Mismatch on equalQuery", expected, actual);
109  }
110 
111  protected void deleteAll() {
112  String sql = "delete from " + tableName();
113  if (getDebug()) System.out.println(sql);
114  PreparedStatement statement = prepareStatement(connection, sql);
115  int actual = executeUpdate(statement);
116  if (getDebug()) System.out.println("deleteAll deleted " + actual + " instances.");
117  commit(connection);
118  }
119 
120  protected void deleteEqualQuery(String column, String expectedIndex, int i, int expected) {
121  String sql = "delete from " + tableName() + " where " + column + " = ?";
122  if (getDebug()) System.out.println(sql);
123  PreparedStatement statement = prepareStatement(connection, sql);
124  int actual = executeUpdate(statement, i);
125  commit(connection);
126  errorIfNotEqual("deleteEqualQuery: Mismatch on number of instances deleted ", expected, actual);
127  }
128 
129  protected void deleteGreaterThanAndLessThanQuery(String column, String expectedIndex, int i, int j, int expected) {
130  String sql = "delete from " + tableName() + " where " + column + " > ? and " + column + " < ?";
131  if (getDebug()) System.out.println(sql);
132  PreparedStatement statement = prepareStatement(connection, sql);
133  int actual = executeUpdate(statement, i, j);
134  commit(connection);
135  errorIfNotEqual("deleteGreaterThanAndLessThanQuery: Mismatch on number of instances deleted ",
136  expected, actual);
137  }
138 
139  private PreparedStatement prepareStatement(Connection connection, String sql) {
140  try {
141  return connection.prepareStatement(sql);
142  } catch (SQLException e) {
143  error("Caught exception " + e.getMessage());
144  return null;
145  }
146  }
147 
148  private void commit(Connection connection) {
149  try {
150  connection.commit();
151  } catch (SQLException e) {
152  // TODO Auto-generated catch block
153  e.printStackTrace();
154  }
155  }
156 
157  private int[] executeQuery(PreparedStatement statement, Object... parameters) {
158  int[] result = null;
159  try {
160  int index = 1;
161  for (Object parameter: parameters) {
162  setObject(statement, index++, parameter);
163  }
164  List<Integer> results = new ArrayList<Integer>();
165  ResultSet resultSet = statement.executeQuery();
166  while (resultSet.next()) {
167  results.add(resultSet.getInt(1));
168  }
169  result = new int[results.size()];
170  for (int i = 0; i < results.size(); ++i) {
171  result[i] = results.get(i);
172  }
173  } catch (SQLException e) {
174  e.printStackTrace();
175  }
176  return result;
177  }
178 
179  private void setObject(PreparedStatement statement, int parameterIndex, Object x) throws SQLException {
180  if (x instanceof Integer) {
181  statement.setInt(parameterIndex, (Integer)x);
182  return;
183  } else if (x instanceof Byte) {
184  statement.setByte(parameterIndex, (Byte)x);
185  return;
186  } else if (x instanceof Short) {
187  statement.setShort(parameterIndex, (Short)x);
188  return;
189  } else if (x instanceof Long) {
190  statement.setLong(parameterIndex, (Long)x);
191  return;
192  }
193  throw new RuntimeException("Object is of unsupported type " + x.getClass().getName());
194  }
195 
196  private int executeUpdate(PreparedStatement statement, Object... parameters) {
197  int result = 0;
198  try {
199  int index = 1;
200  for (Object parameter: parameters) {
201  setObject(statement, index++, parameter);
202  }
203  result = statement.executeUpdate();
204  } catch (SQLException e) {
205  // TODO Auto-generated catch block
206  e.printStackTrace();
207  }
208  return result;
209  }
210 
211  public String tableName() {
212  return null;
213  }
214 
215  protected void createAllPrimitivesInstances(Session session, int number) {
216  for (int i = 0; i < number; ++i) {
217  AllPrimitives instance = createAllPrimitiveInstance(session, i);
218  instances.add(instance);
219  }
220  }
221 
222  protected AllPrimitives createAllPrimitiveInstance(Session session, int i) {
223  AllPrimitives instance = session.newInstance(AllPrimitives.class, i);
224  initialize(instance, i);
225  return instance;
226  }
227 
228 }