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;
10 import com.mysql.clusterj.Session;
12 import testsuite.clusterj.AbstractClusterJModelTest;
13 import testsuite.clusterj.model.AllPrimitives;
33 abstract public void createInstances(
int numberOfInstances);
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,"
45 +
"byte_not_null_hash,"
46 +
"byte_not_null_btree,"
47 +
"byte_not_null_both,"
48 +
"byte_not_null_none,"
53 +
"short_not_null_hash,"
54 +
"short_not_null_btree,"
55 +
"short_not_null_both,"
56 +
"short_not_null_none,"
61 +
"long_not_null_hash,"
62 +
"long_not_null_btree,"
63 +
"long_not_null_both,"
64 +
"long_not_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);
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);
99 errorIfNotEqual(
"betweenQuery: Mismatch on betweenQuery", expected, actual);
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);
108 errorIfNotEqual(
"equalQuery: Mismatch on equalQuery", expected, actual);
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.");
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);
126 errorIfNotEqual(
"deleteEqualQuery: Mismatch on number of instances deleted ", expected, actual);
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);
135 errorIfNotEqual(
"deleteGreaterThanAndLessThanQuery: Mismatch on number of instances deleted ",
139 private PreparedStatement prepareStatement(
Connection connection,
String sql) {
141 return connection.prepareStatement(sql);
142 }
catch (SQLException e) {
143 error(
"Caught exception " + e.getMessage());
151 }
catch (SQLException e) {
157 private int[] executeQuery(PreparedStatement statement, Object... parameters) {
161 for (Object parameter: parameters) {
162 setObject(statement, index++, parameter);
165 ResultSet resultSet = statement.executeQuery();
166 while (resultSet.next()) {
167 results.add(resultSet.getInt(1));
169 result =
new int[results.size()];
170 for (
int i = 0; i < results.size(); ++
i) {
171 result[
i] = results.get(i);
173 }
catch (SQLException e) {
179 private void setObject(PreparedStatement statement,
int parameterIndex, Object x)
throws SQLException {
180 if (x instanceof Integer) {
181 statement.setInt(parameterIndex, (Integer)x);
183 }
else if (x instanceof Byte) {
184 statement.setByte(parameterIndex, (Byte)x);
186 }
else if (x instanceof Short) {
187 statement.setShort(parameterIndex, (Short)x);
189 }
else if (x instanceof Long) {
190 statement.setLong(parameterIndex, (Long)x);
193 throw new RuntimeException(
"Object is of unsupported type " + x.getClass().getName());
196 private int executeUpdate(PreparedStatement statement, Object... parameters) {
200 for (Object parameter: parameters) {
201 setObject(statement, index++, parameter);
203 result = statement.executeUpdate();
204 }
catch (SQLException e) {
211 public String tableName() {
215 protected void createAllPrimitivesInstances(Session session,
int number) {
216 for (
int i = 0; i < number; ++
i) {
217 AllPrimitives instance = createAllPrimitiveInstance(session, i);
222 protected AllPrimitives createAllPrimitiveInstance(Session session,
int i) {
223 AllPrimitives instance = session.newInstance(AllPrimitives.class, i);
224 initialize(instance, i);