-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support (ignore) show and set search_path (#288)
* feat: support show search_path * fix: only include public in search path * feat: add support for set search_path * feat: add support for set search_path * fix: remove special handling of UNKNOWN We can safely remove the special handling of UNKNOWN statements, as the case where there is no command tag is already handled at the beginning of the method. * docs: add todo to remove later
- Loading branch information
Showing
8 changed files
with
231 additions
and
12 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
69 changes: 69 additions & 0 deletions
69
...main/java/com/google/cloud/spanner/pgadapter/statements/local/SetSearchPathStatement.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,69 @@ | ||
// 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.statements.local; | ||
|
||
import com.google.api.core.InternalApi; | ||
import com.google.cloud.spanner.connection.StatementResult; | ||
import com.google.cloud.spanner.pgadapter.statements.BackendConnection; | ||
import com.google.cloud.spanner.pgadapter.statements.BackendConnection.NoResult; | ||
|
||
/** | ||
* A no-op SET search_path implementation. This should be removed once support has been added to the | ||
* Connection API. | ||
*/ | ||
// TODO: Remove this once search_path support has been added to the Connection API. | ||
@InternalApi | ||
public class SetSearchPathStatement implements LocalStatement { | ||
public static final SetSearchPathStatement INSTANCE = new SetSearchPathStatement(); | ||
|
||
private SetSearchPathStatement() {} | ||
|
||
@Override | ||
public String[] getSql() { | ||
return new String[] { | ||
"set search_path to public", | ||
"SET search_path TO public", | ||
"SET SEARCH_PATH TO public", | ||
"set search_path to \"public\"", | ||
"SET search_path TO \"public\"", | ||
"SET SEARCH_PATH TO \"public\"", | ||
"set search_path to \"$user\", public", | ||
"SET search_path TO \"$user\", public", | ||
"SET SEARCH_PATH TO \"$user\", public", | ||
"set search_path to \"$user\", \"public\"", | ||
"SET search_path TO \"$user\", \"public\"", | ||
"SET SEARCH_PATH TO \"$user\", \"public\"", | ||
// Some tools prepend the current search_path with 'public' without properly checking whether | ||
// 'public' is already part of the existing search path. | ||
"set search_path to public, public", | ||
"SET search_path TO public, public", | ||
"SET SEARCH_PATH TO public, public", | ||
"set search_path to public, \"public\"", | ||
"SET search_path TO public, \"public\"", | ||
"SET SEARCH_PATH TO public, \"public\"", | ||
"set search_path to public, \"$user\", public", | ||
"SET search_path TO public, \"$user\", public", | ||
"SET SEARCH_PATH TO public, \"$user\", public", | ||
"set search_path to public, \"$user\", \"public\"", | ||
"SET search_path TO public, \"$user\", \"public\"", | ||
"SET SEARCH_PATH TO public, \"$user\", \"public\"", | ||
}; | ||
} | ||
|
||
@Override | ||
public StatementResult execute(BackendConnection backendConnection) { | ||
return new NoResult("SET"); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ain/java/com/google/cloud/spanner/pgadapter/statements/local/ShowSearchPathStatement.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,53 @@ | ||
// 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.statements.local; | ||
|
||
import com.google.api.core.InternalApi; | ||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.ResultSets; | ||
import com.google.cloud.spanner.Struct; | ||
import com.google.cloud.spanner.Type; | ||
import com.google.cloud.spanner.Type.StructField; | ||
import com.google.cloud.spanner.connection.StatementResult; | ||
import com.google.cloud.spanner.pgadapter.statements.BackendConnection; | ||
import com.google.cloud.spanner.pgadapter.statements.BackendConnection.QueryResult; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
@InternalApi | ||
public class ShowSearchPathStatement implements LocalStatement { | ||
public static final ShowSearchPathStatement INSTANCE = new ShowSearchPathStatement(); | ||
|
||
private ShowSearchPathStatement() {} | ||
|
||
@Override | ||
public String[] getSql() { | ||
return new String[] { | ||
"show search_path", "SHOW search_path", "SHOW SEARCH_PATH", | ||
}; | ||
} | ||
|
||
@Override | ||
public StatementResult execute(BackendConnection backendConnection) { | ||
ResultSet resultSet = | ||
ResultSets.forRows( | ||
Type.struct(StructField.of("search_path", Type.string())), | ||
ImmutableList.of( | ||
Struct.newBuilder() | ||
.set("search_path") | ||
.to(backendConnection.getCurrentSchema()) | ||
.build())); | ||
return new QueryResult(resultSet); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...java/com/google/cloud/spanner/pgadapter/statements/local/ShowSearchPathStatementTest.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 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.statements.local; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.pgadapter.statements.BackendConnection; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ShowSearchPathStatementTest { | ||
|
||
@Test | ||
public void testExecute() { | ||
for (String schema : new String[] {"public", "my_schema"}) { | ||
BackendConnection backendConnection = mock(BackendConnection.class); | ||
when(backendConnection.getCurrentSchema()).thenReturn(schema); | ||
|
||
try (ResultSet resultSet = | ||
ShowSearchPathStatement.INSTANCE.execute(backendConnection).getResultSet()) { | ||
assertTrue(resultSet.next()); | ||
assertEquals(1, resultSet.getColumnCount()); | ||
assertEquals(schema, resultSet.getString("search_path")); | ||
assertFalse(resultSet.next()); | ||
} | ||
} | ||
} | ||
} |