/
ITJdbcSimpleStatementsTest.java
194 lines (185 loc) · 7.73 KB
/
ITJdbcSimpleStatementsTest.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2019 Google LLC
*
* Licensed 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 com.google.cloud.spanner.jdbc.it;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.Value;
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Test executing simple statements through JDBC. */
@RunWith(JUnit4.class)
@Category(IntegrationTest.class)
public class ITJdbcSimpleStatementsTest extends ITAbstractJdbcTest {
@Test
public void testSelect1() throws SQLException {
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.createStatement().executeQuery("select 1")) {
assertThat(rs.next()).isTrue();
assertThat(rs.getInt(1)).isEqualTo(1);
assertThat(rs.next()).isFalse();
}
}
}
@Test
public void testSelect1PreparedStatement() throws SQLException {
try (Connection connection = createConnection()) {
try (PreparedStatement ps = connection.prepareStatement("select 1")) {
try (ResultSet rs = ps.executeQuery()) {
assertThat(rs.next()).isTrue();
assertThat(rs.getInt(1)).isEqualTo(1);
assertThat(rs.next()).isFalse();
}
}
}
}
@Test
public void testPreparedStatement() throws SQLException {
String sql =
"select * from (select 1 as number union all select 2 union all select 3) numbers where number=?";
try (Connection connection = createConnection()) {
try (PreparedStatement ps = connection.prepareStatement(sql)) {
for (int i = 1; i <= 3; i++) {
ps.setInt(1, i);
try (ResultSet rs = ps.executeQuery()) {
assertThat(rs.next()).isTrue();
assertThat(rs.getInt(1)).isEqualTo(i);
assertThat(rs.next()).isFalse();
}
}
}
}
}
@Test
public void testBatchedDdlStatements() throws SQLException {
// Execute a successful batch of DDL statements.
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
statement.addBatch(
"CREATE TABLE FOO1 (ID INT64 NOT NULL, NAME STRING(100)) PRIMARY KEY (ID)");
statement.addBatch(
"CREATE TABLE FOO2 (ID INT64 NOT NULL, NAME STRING(100)) PRIMARY KEY (ID)");
int[] updateCounts = statement.executeBatch();
assertThat(updateCounts)
.asList()
.containsExactly(Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO);
}
try (ResultSet rs =
connection
.createStatement()
.executeQuery(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='' AND TABLE_NAME LIKE 'FOO%'")) {
assertThat(rs.next()).isTrue();
assertThat(rs.getLong(1)).isEqualTo(2L);
}
}
// Execute a batch of DDL statements that contains a statement that will fail.
try (Connection connection = createConnection()) {
// First create a couple of test records that will cause the index creation to fail.
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("INSERT INTO FOO1 (ID, NAME) VALUES (1,'TEST')");
statement.executeUpdate("INSERT INTO FOO1 (ID, NAME) VALUES (2,'TEST')");
}
boolean gotExpectedException = false;
try (Statement statement = connection.createStatement()) {
statement.addBatch(
"CREATE TABLE FOO3 (ID INT64 NOT NULL, NAME STRING(100)) PRIMARY KEY (ID)");
statement.addBatch("CREATE UNIQUE INDEX IDX_FOO1_UNIQUE ON FOO1 (NAME)");
statement.executeBatch();
} catch (SQLException e) {
gotExpectedException = true;
}
assertThat(gotExpectedException).isTrue();
// The table should have been created, the index should not.
try (ResultSet rs =
connection
.createStatement()
.executeQuery(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='' AND TABLE_NAME LIKE 'FOO%'")) {
assertThat(rs.next()).isTrue();
assertThat(rs.getLong(1)).isEqualTo(3L);
}
try (ResultSet rs =
connection
.createStatement()
.executeQuery(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_SCHEMA='' AND INDEX_NAME='IDX_FOO1_UNIQUE'")) {
assertThat(rs.next()).isTrue();
assertThat(rs.getLong(1)).isEqualTo(0L);
}
}
}
@Test
public void testAddBatchWhenAlreadyInBatch() {
try (Connection connection = createConnection()) {
connection.createStatement().execute("START BATCH DML");
connection.createStatement().addBatch("INSERT INTO Singers (SingerId) VALUES (-1)");
fail("missing expected exception");
} catch (SQLException e) {
assertThat(e.getMessage())
.contains(
"Calling addBatch() is not allowed when a DML or DDL batch has been started on the connection.");
}
}
@Test
public void testSelectArrayOfStructs() throws SQLException {
String sql =
"WITH points AS\n"
+ " (SELECT [1, 5] as point\n"
+ " UNION ALL SELECT [2, 8] as point\n"
+ " UNION ALL SELECT [3, 7] as point\n"
+ " UNION ALL SELECT [4, 1] as point\n"
+ " UNION ALL SELECT [5, 7] as point)\n"
+ "SELECT ARRAY(\n"
+ " SELECT STRUCT(point)\n"
+ " FROM points)\n"
+ " AS coordinates";
try (Connection connection = createConnection()) {
try (ResultSet resultSet = connection.createStatement().executeQuery(sql)) {
assertTrue(resultSet.next());
assertEquals(resultSet.getMetaData().getColumnCount(), 1);
Array array = resultSet.getArray(1);
assertThat((Struct[]) array.getArray())
.asList()
.containsExactly(
Struct.newBuilder().set("point").to(Value.int64Array(new long[] {1L, 5L})).build(),
Struct.newBuilder().set("point").to(Value.int64Array(new long[] {2L, 8L})).build(),
Struct.newBuilder().set("point").to(Value.int64Array(new long[] {3L, 7L})).build(),
Struct.newBuilder().set("point").to(Value.int64Array(new long[] {4L, 1L})).build(),
Struct.newBuilder().set("point").to(Value.int64Array(new long[] {5L, 7L})).build());
// Getting a result set from an array of structs is not supported, as structs are not
// supported as a valid column type in a result set.
assertThrows(SQLFeatureNotSupportedException.class, () -> array.getResultSet());
assertFalse(resultSet.next());
}
}
}
}