Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support Savepoint Add support for emulated Savepoints that are now supported in the client library. * fix: clirr check
- Loading branch information
Showing
7 changed files
with
885 additions
and
22 deletions.
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
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
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
58 changes: 58 additions & 0 deletions
58
src/main/java/com/google/cloud/spanner/jdbc/JdbcSavepoint.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,58 @@ | ||
/* | ||
* Copyright 2023 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; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Savepoint; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
class JdbcSavepoint implements Savepoint { | ||
private static final AtomicInteger COUNTER = new AtomicInteger(); | ||
|
||
static JdbcSavepoint named(String name) { | ||
return new JdbcSavepoint(-1, name); | ||
} | ||
|
||
static JdbcSavepoint unnamed() { | ||
int id = COUNTER.incrementAndGet(); | ||
return new JdbcSavepoint(id, String.format("s_%d", id)); | ||
} | ||
|
||
private final int id; | ||
private final String name; | ||
|
||
private JdbcSavepoint(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public int getSavepointId() throws SQLException { | ||
JdbcPreconditions.checkState(this.id >= 0, "This is a named savepoint"); | ||
return id; | ||
} | ||
|
||
@Override | ||
public String getSavepointName() throws SQLException { | ||
JdbcPreconditions.checkState(this.id < 0, "This is an unnamed savepoint"); | ||
return name; | ||
} | ||
|
||
String internalGetSavepointName() { | ||
return name; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/com/google/cloud/spanner/jdbc/JdbcSavepointTest.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,47 @@ | ||
/* | ||
* Copyright 2023 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; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Savepoint; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class JdbcSavepointTest { | ||
|
||
@Test | ||
public void testNamed() throws SQLException { | ||
Savepoint savepoint = JdbcSavepoint.named("test"); | ||
assertEquals("test", savepoint.getSavepointName()); | ||
assertThrows(SQLException.class, savepoint::getSavepointId); | ||
} | ||
|
||
@Test | ||
public void testUnnamed() throws SQLException { | ||
Savepoint savepoint = JdbcSavepoint.unnamed(); | ||
assertTrue( | ||
String.format("Savepoint id: %d", savepoint.getSavepointId()), | ||
savepoint.getSavepointId() > 0); | ||
assertThrows(SQLException.class, savepoint::getSavepointName); | ||
} | ||
} |
Oops, something went wrong.