Repository navigation
Add option to specify connection URL - #133
Conversation
25aead8 to
9c450c1
Compare
| password = userInfo[1]; | ||
| int userInfoIndex = entryURL.indexOf(userInfoURI); | ||
| entryURL = entryURL.substring(0, userInfoIndex) | ||
| + entryURL.substring(userInfoIndex + userInfoURI.length() + 1); |
There was a problem hiding this comment.
I think it would be more readable to concatenate username and password, rather than doing entryURL.substring(userInfoIndex + userInfoURI.length() + 1) (if that's correct). Efficiency is not really a concern here, and I think doing so is less error prone.
There was a problem hiding this comment.
Actually, I am removing username and password here instead. Otherwise, if username and password are specified both in the DriverManager call and in the url passed to the Driver Manager, the conflict causes errors. So the concatenation I am doing is [urlBeforeUserInfo]+[urlAfterUserInfo].
There was a problem hiding this comment.
Okay, thanks for the explanation! If you think that additional variable names would not convey additional information, we can also leave it like it is.
| username = userInfo[0]; | ||
| password = userInfo[1]; | ||
| int userInfoIndex = entryURL.indexOf(userInfoURI); | ||
| entryURL = entryURL.substring(0, userInfoIndex) |
There was a problem hiding this comment.
I think extracting entryURL.substring(0, userInfoIndex) to a variable would improve readability. Is it the host name?
There was a problem hiding this comment.
It is everything that comes before the userInfo, so "postgresql:".
| con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + databaseName, | ||
| globalState.getOptions().getUserName(), globalState.getOptions().getPassword()); | ||
| int pathIndex = entryURL.indexOf(entryPath); | ||
| String testURL = entryURL.substring(0, pathIndex + 1) + databaseName |
There was a problem hiding this comment.
Same here: I think it would be cleaner to define the variables once, and then use these, rather than using the substring method.
There was a problem hiding this comment.
The only issue is that these are a combination of many possible things that the user can decide to include in the URL. This makes it hard to define variables, but allows more flexibility in setting optional parameters.
There was a problem hiding this comment.
Okay, I see. Thanks for the explanation! Maybe as a future PR, we could consider adding test cases to check if connection strings are correctly parsed (although it doesn't have a high priority at the moment).
| String databaseName = globalState.getDatabaseName(); | ||
| Connection con = DriverManager.getConnection(url, globalState.getOptions().getUserName(), | ||
| globalState.getOptions().getPassword()); | ||
| globalState.getState().logStatement("\\c test;"); |
There was a problem hiding this comment.
Do we need to remove this? Logging a \c test is useful when reducing a test case manually, since changing to the default database test makes it possible to drop the current database of the test case.
| globalState.getOptions().getPassword()); | ||
| globalState.getState().logStatement("\\c test;"); | ||
| Connection con = DriverManager.getConnection("jdbc:" + entryURL, username, password); | ||
| globalState.getState().logStatement(entryURL); |
There was a problem hiding this comment.
What is the entry URL here? If we log it without appending a -- first, is the log still executable as a SQL script?
There was a problem hiding this comment.
This is the connection string. Does logging involve executing SQL statements? I thought they were for printing, more clarification on that would be great in case I missed something!
There was a problem hiding this comment.
Things written to the log should typically either be SQL statements or comments (prefixed by --), so that the log file can basically be directly used as an input to a DBMS. I should update the documentation about this.
|
Thanks! Let me know when the PR is ready to be merged. Please squash the two commits. |
thanodnl
left a comment
There was a problem hiding this comment.
Left a small comment on the handling of the user password.
| String[] userInfo = userInfoURI.split(":", 2); | ||
| username = userInfo[0]; | ||
| password = userInfo[1]; |
There was a problem hiding this comment.
What happens when only the username is provided and no password?
I think we will get an out of bound exception on the last line here.
connection url to test: postgres://foo@localhost:5432/test
There was a problem hiding this comment.
I added a check to see if a : separator exists and parse it accordingly. DriverManager.getConnection() seems to safely accept null as a valid password, so I set the password to null if there is no password provided
The PR is ready to be merged, thank you! |
|
Thanks! Sorry for the delay with merging, I was on holidays until including yesterday. |
This PR adds the option for the user to specify the initial connection URL in the Postgres implementation. This allows more flexibility in connecting with different users, hosts, ports, and optional connection parameters. The logging also includes the full URL instead of only the database name. The provided URL is checked for correct format, and parsed. If a username and password is specified in the URL, these overwrite the username and password set by the MainOptions (with sqlancer as default). When calling DriverManager to get connection, the username and password are removed from the URL string to avoid confusion.