-
Notifications
You must be signed in to change notification settings - Fork 45
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: support default schema and catalog for PostgreSQL databases #1375
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c49a9cb
feat: support default schema and catalog for PostgreSQL databases
olavloite 0089703
Merge branch 'main' into support-pg-schema-and-catalog
olavloite 0ca1ed9
test: add additional testing for getSchema and getCatalog
olavloite af51766
fix: column name should be TABLE_CAT
olavloite 32f7d09
fix: emulator now supports spanner_sys
olavloite File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
import javax.annotation.Nonnull; | ||
|
||
/** Jdbc Connection class for Google Cloud Spanner */ | ||
class JdbcConnection extends AbstractJdbcConnection { | ||
|
@@ -394,31 +395,66 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti | |
@Override | ||
public void setCatalog(String catalog) throws SQLException { | ||
// This method could be changed to allow the user to change to another database. | ||
// For now we only support setting an empty string in order to support frameworks | ||
// For now, we only support setting the default catalog in order to support frameworks | ||
// and applications that set this when no catalog has been specified in the connection | ||
// URL. | ||
checkClosed(); | ||
JdbcPreconditions.checkArgument("".equals(catalog), "Only catalog \"\" is supported"); | ||
checkValidCatalog(catalog); | ||
} | ||
|
||
void checkValidCatalog(String catalog) throws SQLException { | ||
String defaultCatalog = getDefaultCatalog(); | ||
JdbcPreconditions.checkArgument( | ||
defaultCatalog.equals(catalog), | ||
String.format("Only catalog %s is supported", defaultCatalog)); | ||
} | ||
|
||
@Override | ||
public String getCatalog() throws SQLException { | ||
checkClosed(); | ||
return ""; | ||
return getDefaultCatalog(); | ||
} | ||
|
||
@Nonnull | ||
String getDefaultCatalog() { | ||
switch (getDialect()) { | ||
case POSTGRESQL: | ||
String database = getConnectionOptions().getDatabaseName(); | ||
// It should not be possible that database is null, but it's better to be safe than sorry. | ||
return database == null ? "" : database; | ||
case GOOGLE_STANDARD_SQL: | ||
default: | ||
return ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are returning empty string at multiple places, would it be better to use a constant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say no for a couple of reasons:
|
||
} | ||
} | ||
|
||
@Override | ||
public void setSchema(String schema) throws SQLException { | ||
checkClosed(); | ||
// Cloud Spanner does not support schemas, but does contain a pseudo 'empty string' schema that | ||
// might be set by frameworks and applications that read the database metadata. | ||
JdbcPreconditions.checkArgument("".equals(schema), "Only schema \"\" is supported"); | ||
checkValidSchema(schema); | ||
} | ||
|
||
void checkValidSchema(String schema) throws SQLException { | ||
String defaultSchema = getDefaultSchema(); | ||
JdbcPreconditions.checkArgument( | ||
defaultSchema.equals(schema), String.format("Only schema %s is supported", defaultSchema)); | ||
} | ||
|
||
@Override | ||
public String getSchema() throws SQLException { | ||
checkClosed(); | ||
return ""; | ||
return getDefaultSchema(); | ||
} | ||
|
||
@Nonnull | ||
String getDefaultSchema() { | ||
switch (getDialect()) { | ||
case POSTGRESQL: | ||
return "public"; | ||
case GOOGLE_STANDARD_SQL: | ||
default: | ||
return ""; | ||
} | ||
} | ||
|
||
@Override | ||
|
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
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
Oops, something went wrong.
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.
Do we need the variable here?
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.
We use it twice on the next line... So we don't need it, but otherwise we would have to call the method twice on the next line, which also does not feel great.