Skip to content

Commit b7501c7

Browse files
committed
suuport host/port arguments for postgres
1 parent 003cae5 commit b7501c7

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/sqlancer/MainOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public class MainOptions {
4949
@Parameter(names = "--password", description = "The password used to log into the DBMS")
5050
private String password = "sqlancer"; // NOPMD
5151

52+
@Parameter(names = "--host", description = "The host used to log into the DBMS")
53+
private String host = "sqlancer"; // NOPMD
54+
55+
@Parameter(names = "--port", description = "The port used to log into the DBMS")
56+
private String port = "sqlancer"; // NOPMD
57+
58+
5259
@Parameter(names = "--print-progress-information", description = "Whether to print progress information such as the number of databases generated or queries issued", arity = 1)
5360
private boolean printProgressInformation = true; // NOPMD
5461

@@ -151,6 +158,14 @@ public String getPassword() {
151158
return password;
152159
}
153160

161+
public String getHost(){
162+
return host;
163+
}
164+
165+
public String getPort(){
166+
return port;
167+
}
168+
154169
public boolean printProgressInformation() {
155170
return printProgressInformation;
156171
}

src/sqlancer/postgres/PostgresProvider.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class PostgresProvider extends SQLProviderAdapter<PostgresGlobalState, Po
5454
protected String password;
5555
protected String entryPath;
5656
protected String host;
57+
protected String port;
58+
protected String Scheme;
5759
protected String testURL;
5860
protected String databaseName;
5961
protected String createDatabaseCommand;
@@ -200,12 +202,17 @@ public SQLConnection createDatabase(PostgresGlobalState globalState) throws SQLE
200202

201203
username = globalState.getOptions().getUserName();
202204
password = globalState.getOptions().getPassword();
205+
host = globalState.getOptions().getHost();
206+
port = globalState.getOptions().getPort();
203207
entryPath = "/test";
204208
entryURL = globalState.getDmbsSpecificOptions().connectionURL;
205209
// trim URL to exclude "jdbc:"
206210
if (entryURL.startsWith("jdbc:")) {
207211
entryURL = entryURL.substring(5);
208212
}
213+
String entryDatabaseName = entryPath.substring(1);
214+
databaseName = globalState.getDatabaseName();
215+
209216
try {
210217
URI uri = new URI(entryURL);
211218
String userInfoURI = uri.getUserInfo();
@@ -228,12 +235,16 @@ public SQLConnection createDatabase(PostgresGlobalState globalState) throws SQLE
228235
if (pathURI != null) {
229236
entryPath = pathURI;
230237
}
231-
host = uri.getHost();
238+
if ("sqlancer".equals(host)){
239+
host = uri.getHost();
240+
}
241+
if("sqlancer".equals(port)){
242+
port = Integer.toString(uri.getPort());
243+
}
244+
entryURL = uri.getScheme() + "://" + host + ":" + port + "/" + entryDatabaseName;
232245
} catch (URISyntaxException e) {
233246
throw new AssertionError(e);
234247
}
235-
String entryDatabaseName = entryPath.substring(1);
236-
databaseName = globalState.getDatabaseName();
237248
Connection con = DriverManager.getConnection("jdbc:" + entryURL, username, password);
238249
globalState.getState().logStatement(String.format("\\c %s;", entryDatabaseName));
239250
globalState.getState().logStatement("DROP DATABASE IF EXISTS " + databaseName);
@@ -251,6 +262,7 @@ public SQLConnection createDatabase(PostgresGlobalState globalState) throws SQLE
251262
String postDatabaseName = entryURL.substring(databaseIndex + entryDatabaseName.length());
252263
testURL = preDatabaseName + databaseName + postDatabaseName;
253264
globalState.getState().logStatement(String.format("\\c %s;", databaseName));
265+
254266
con = DriverManager.getConnection("jdbc:" + testURL, username, password);
255267
return new SQLConnection(con);
256268
}

0 commit comments

Comments
 (0)