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
DBZ-693 Initial SQL statements #516
Closed
Closed
Changes from 2 commits
Commits
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
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
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 |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
*/ | ||
public class JdbcConnection implements AutoCloseable { | ||
|
||
private static final char STATEMENT_DELIMITER = ';'; | ||
private final static Logger LOGGER = LoggerFactory.getLogger(JdbcConnection.class); | ||
|
||
/** | ||
|
@@ -581,11 +582,49 @@ public synchronized boolean isConnected() throws SQLException { | |
} | ||
|
||
public synchronized Connection connection() throws SQLException { | ||
return connection(true); | ||
} | ||
|
||
public synchronized Connection connection(boolean executeOnConnect) throws SQLException { | ||
if (conn == null) { | ||
conn = factory.connect(JdbcConfiguration.adapt(config)); | ||
if (conn == null) throw new SQLException("Unable to obtain a JDBC connection"); | ||
// Always run the initial operations on this new connection | ||
if (initialOps != null) execute(initialOps); | ||
final String statements = config.getString(JdbcConfiguration.ON_CONNECT_STATEMENTS); | ||
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'd suggest to extract all the parsing logic into its own method which then returns the list of statements to here. 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. @gunnarmorling Done |
||
if (statements != null && executeOnConnect) { | ||
final List<String> splitStatements = new ArrayList<>(); | ||
final char[] statementsChars = statements.toCharArray(); | ||
StringBuilder activeStatement = new StringBuilder(); | ||
for (int i = 0; i < statementsChars.length; i++) { | ||
if (statementsChars[i] == STATEMENT_DELIMITER) { | ||
if (i == statementsChars.length - 1) { | ||
// last character so it is the delimiter | ||
} | ||
else if (statementsChars[i + 1] == STATEMENT_DELIMITER) { | ||
// two semicolons in a row - escaped semicolon | ||
activeStatement.append(STATEMENT_DELIMITER); | ||
i++; | ||
} | ||
else { | ||
// semicolon as a delimiter | ||
final String trimmedStatement = activeStatement.toString().trim(); | ||
if (!trimmedStatement.isEmpty()) { | ||
splitStatements.add(trimmedStatement); | ||
} | ||
activeStatement = new StringBuilder(); | ||
} | ||
} | ||
else { | ||
activeStatement.append(statementsChars[i]); | ||
} | ||
} | ||
final String trimmedStatement = activeStatement.toString().trim(); | ||
if (!trimmedStatement.isEmpty()) { | ||
splitStatements.add(trimmedStatement); | ||
} | ||
execute(splitStatements.toArray(new String[splitStatements.size()])); | ||
} | ||
} | ||
return conn; | ||
} | ||
|
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.
I think it'd be safer to not execute the statements when calling the parameterless
connection()
. Note that when using the PG connector, I see the insert being executed three times, so something is still not quite correct.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.
@gunnarmorling It is correct, as postgres connector creates multiple connections in sequence. The functionality is not for executing DML (albeit I abuse it for tests) just mainly for session configuration and in this case I expect the session to be configured every time the connection is created.
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.
Ok, I'll expand the option's description a little bit then to clarify that fact.