-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JdbcMergeStatementSelfTest.java
142 lines (113 loc) · 4.62 KB
/
JdbcMergeStatementSelfTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.jdbc2;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.ignite.cache.CachePeekMode;
/**
* MERGE statement test.
*/
public class JdbcMergeStatementSelfTest extends JdbcAbstractDmlStatementSelfTest {
/** SQL query. */
private static final String SQL = "merge into Person(_key, id, firstName, lastName, age) values " +
"('p1', 1, 'John', 'White', 25), " +
"('p2', 2, 'Joe', 'Black', 35), " +
"('p3', 3, 'Mike', 'Green', 40)";
/** SQL query. */
protected static final String SQL_PREPARED = "merge into Person(_key, id, firstName, lastName, age) values " +
"(?, ?, ?, ?, ?), (?, ?, ?, ?, ?)";
/** Statement. */
protected Statement stmt;
/** Prepared statement. */
protected PreparedStatement prepStmt;
/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
super.beforeTest();
stmt = conn.createStatement();
prepStmt = conn.prepareStatement(SQL_PREPARED);
assertNotNull(stmt);
assertFalse(stmt.isClosed());
assertNotNull(prepStmt);
assertFalse(prepStmt.isClosed());
}
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
try (Statement selStmt = conn.createStatement()) {
assertTrue(selStmt.execute(SQL_SELECT));
ResultSet rs = selStmt.getResultSet();
assert rs != null;
while (rs.next()) {
int id = rs.getInt("id");
switch (id) {
case 1:
assertEquals("p1", rs.getString("_key"));
assertEquals("John", rs.getString("firstName"));
assertEquals("White", rs.getString("lastName"));
assertEquals(25, rs.getInt("age"));
break;
case 2:
assertEquals("p2", rs.getString("_key"));
assertEquals("Joe", rs.getString("firstName"));
assertEquals("Black", rs.getString("lastName"));
assertEquals(35, rs.getInt("age"));
break;
case 3:
assertEquals("p3", rs.getString("_key"));
assertEquals("Mike", rs.getString("firstName"));
assertEquals("Green", rs.getString("lastName"));
assertEquals(40, rs.getInt("age"));
break;
case 4:
assertEquals("p4", rs.getString("_key"));
assertEquals("Leah", rs.getString("firstName"));
assertEquals("Grey", rs.getString("lastName"));
assertEquals(22, rs.getInt("age"));
break;
default:
assert false : "Invalid ID: " + id;
}
}
}
grid(0).cache(null).clear();
assertEquals(0, grid(0).cache(null).size(CachePeekMode.ALL));
super.afterTest();
if (stmt != null && !stmt.isClosed())
stmt.close();
if (prepStmt != null && !prepStmt.isClosed())
prepStmt.close();
conn.close();
assertTrue(prepStmt.isClosed());
assertTrue(stmt.isClosed());
assertTrue(conn.isClosed());
}
/**
* @throws SQLException If failed.
*/
public void testExecuteUpdate() throws SQLException {
int res = stmt.executeUpdate(SQL);
assertEquals(3, res);
}
/**
* @throws SQLException If failed.
*/
public void testExecute() throws SQLException {
boolean res = stmt.execute(SQL);
assertEquals(false, res);
}
}