-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable native JDBC connections #28
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/test/java/com/google/cloud/spanner/pgadapter/ITJdbcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2022 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.pgadapter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.cloud.ByteArray; | ||
import com.google.cloud.Timestamp; | ||
import com.google.cloud.spanner.Database; | ||
import com.google.cloud.spanner.KeySet; | ||
import com.google.cloud.spanner.Mutation; | ||
import com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata; | ||
import com.google.common.collect.ImmutableList; | ||
import java.math.BigDecimal; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@Category(IntegrationTest.class) | ||
@RunWith(JUnit4.class) | ||
public class ITJdbcTest implements IntegrationTest { | ||
private static final PgAdapterTestEnv testEnv = new PgAdapterTestEnv(); | ||
private static ProxyServer server; | ||
private static Database database; | ||
|
||
@BeforeClass | ||
public static void setup() throws Exception { | ||
// Make sure the PG JDBC driver is loaded. | ||
Class.forName("org.postgresql.Driver"); | ||
|
||
testEnv.setUp(); | ||
if (testEnv.isUseExistingDb()) { | ||
database = testEnv.getExistingDatabase(); | ||
} else { | ||
database = testEnv.createDatabase(); | ||
testEnv.updateDdl( | ||
database.getId().getDatabase(), | ||
Arrays.asList( | ||
"create table numbers (num int not null primary key, name varchar(100))", | ||
"create table all_types (" | ||
+ "col_bigint bigint not null primary key, " | ||
+ "col_bool bool, " | ||
+ "col_bytea bytea, " | ||
+ "col_float8 float8, " | ||
+ "col_int int, " | ||
+ "col_numeric numeric, " | ||
+ "col_timestamptz timestamptz, " | ||
+ "col_varchar varchar(100))")); | ||
} | ||
String credentials = testEnv.getCredentials(); | ||
ImmutableList.Builder<String> argsListBuilder = | ||
ImmutableList.<String>builder() | ||
.add( | ||
"-p", | ||
testEnv.getProjectId(), | ||
"-i", | ||
testEnv.getInstanceId(), | ||
"-d", | ||
database.getId().getDatabase(), | ||
"-s", | ||
String.valueOf(testEnv.getPort()), | ||
"-e", | ||
testEnv.getUrl().getHost()); | ||
if (credentials != null) { | ||
argsListBuilder.add("-c", testEnv.getCredentials()); | ||
} | ||
String[] args = argsListBuilder.build().toArray(new String[0]); | ||
server = new ProxyServer(new OptionsMetadata(args)); | ||
server.startServer(); | ||
} | ||
|
||
@AfterClass | ||
public static void teardown() { | ||
if (server != null) { | ||
server.stopServer(); | ||
} | ||
testEnv.cleanUp(); | ||
} | ||
|
||
@Before | ||
public void insertTestData() { | ||
String databaseId = database.getId().getDatabase(); | ||
testEnv.write(databaseId, Collections.singleton(Mutation.delete("numbers", KeySet.all()))); | ||
testEnv.write(databaseId, Collections.singleton(Mutation.delete("all_types", KeySet.all()))); | ||
testEnv.write( | ||
databaseId, | ||
Arrays.asList( | ||
Mutation.newInsertBuilder("numbers").set("num").to(1L).set("name").to("One").build(), | ||
Mutation.newInsertBuilder("all_types") | ||
.set("col_bigint") | ||
.to(1L) | ||
.set("col_bool") | ||
.to(true) | ||
.set("col_bytea") | ||
.to(ByteArray.copyFrom("test")) | ||
.set("col_float8") | ||
.to(3.14d) | ||
.set("col_int") | ||
.to(1) | ||
.set("col_numeric") | ||
.to(new BigDecimal("3.14")) | ||
.set("col_timestamptz") | ||
.to(Timestamp.parseTimestamp("2022-01-27T17:51:30+01:00")) | ||
.set("col_varchar") | ||
.to("test") | ||
.build())); | ||
} | ||
|
||
@Test | ||
public void testSelectHelloWorld() throws SQLException { | ||
try (Connection connection = | ||
DriverManager.getConnection( | ||
String.format("jdbc:postgresql://localhost:%d/", testEnv.getPort()))) { | ||
try (ResultSet resultSet = | ||
connection.createStatement().executeQuery("SELECT 'Hello World!'")) { | ||
assertTrue(resultSet.next()); | ||
assertEquals("Hello World!", resultSet.getString(1)); | ||
assertFalse(resultSet.next()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work with all other queries as well using pg-jdbc? I assume the wireprotocol is the same for pg-jdbc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the wireprotocol is the same for pg-jdbc, so yes it will in theory work with other queries. However:
timestamp
anddate
parameter values as untyped parameters. Support for that is also included in feat: add support incoming binary values #27 (only for timestamp at this moment, as we currently do not support date).