Skip to content

Commit 1583b24

Browse files
authored
test: verify that executeUpdate can be used for DDL (googleapis#1694)
The JDBC driver should now support executing DDL using the executeUpdate methods of java.sql.Statement and java.sql.PreparedStatement. Closes googleapis#1150
1 parent 3933cf2 commit 1583b24

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/test/java/com/google/cloud/spanner/jdbc/DdlMockServerTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
3232
import java.sql.Connection;
3333
import java.sql.DriverManager;
34+
import java.sql.PreparedStatement;
3435
import java.sql.SQLException;
3536
import org.junit.Test;
3637
import org.junit.runner.RunWith;
@@ -98,4 +99,45 @@ public void testDdlInAutoCommitIsFalse_failsWithActiveTransaction() throws SQLEx
9899
assertEquals(Code.FAILED_PRECONDITION, jdbcSqlException.getCode());
99100
}
100101
}
102+
103+
@Test
104+
public void testDdlUsingStatementAndExecuteUpdate() throws SQLException {
105+
for (boolean autoCommit : new boolean[] {true, false}) {
106+
mockDatabaseAdmin.addResponse(
107+
Operation.newBuilder()
108+
.setDone(true)
109+
.setResponse(Any.pack(Empty.getDefaultInstance()))
110+
.setMetadata(Any.pack(UpdateDatabaseDdlMetadata.getDefaultInstance()))
111+
.build());
112+
113+
try (Connection connection = createConnection(autoCommit)) {
114+
try (java.sql.Statement statement = connection.createStatement()) {
115+
assertEquals(0, statement.executeUpdate("create table foo (id int64) primary key (id)"));
116+
}
117+
}
118+
assertEquals(1, mockDatabaseAdmin.getRequests().size());
119+
mockDatabaseAdmin.getRequests().clear();
120+
}
121+
}
122+
123+
@Test
124+
public void testDdlUsingPreparedStatementAndExecuteUpdate() throws SQLException {
125+
for (boolean autoCommit : new boolean[] {true, false}) {
126+
mockDatabaseAdmin.addResponse(
127+
Operation.newBuilder()
128+
.setDone(true)
129+
.setResponse(Any.pack(Empty.getDefaultInstance()))
130+
.setMetadata(Any.pack(UpdateDatabaseDdlMetadata.getDefaultInstance()))
131+
.build());
132+
133+
try (Connection connection = createConnection(autoCommit)) {
134+
try (PreparedStatement preparedStatement =
135+
connection.prepareStatement("create table foo (id int64) primary key (id)")) {
136+
assertEquals(0, preparedStatement.executeUpdate());
137+
}
138+
}
139+
assertEquals(1, mockDatabaseAdmin.getRequests().size());
140+
mockDatabaseAdmin.getRequests().clear();
141+
}
142+
}
101143
}

0 commit comments

Comments
 (0)