Skip to content

Commit

Permalink
Add Driver#createConnectOptions
Browse files Browse the repository at this point in the history
Also deprecate generic construction of SqlConnectOptions
  • Loading branch information
aguibert committed May 29, 2020
1 parent e87b0a1 commit 6400b8c
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public DB2ConnectOptions(DB2ConnectOptions other) {
super(other);
this.pipeliningLimit = other.pipeliningLimit;
}

@Override
public DB2ConnectOptions setHost(String host) {
return (DB2ConnectOptions) super.setHost(host);
Expand Down Expand Up @@ -210,6 +210,6 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(pipeliningLimit);
return Objects.hash(super.hashCode(), pipeliningLimit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public String getSqlState() {
*/
@Override
public String getMessage() {
return super.getMessage();
return super.getMessage() + "; errorCode=" + errorCode + "; sqlState=" + sqlState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.vertx.sqlclient.spi.Driver;

public class DB2Driver implements Driver {

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
return DB2Pool.pool(wrap(options), poolOptions);
Expand All @@ -35,17 +35,24 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO
return DB2Pool.pool(vertx, wrap(options), poolOptions);
}

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof DB2ConnectOptions || SqlConnectOptions.class.equals(options.getClass());
}

private static DB2ConnectOptions wrap(SqlConnectOptions options) {
if (options instanceof DB2ConnectOptions) {
return (DB2ConnectOptions) options;
} else {
return (DB2ConnectOptions) options;
} else if (options.getClass().equals(SqlConnectOptions.class)) {
return new DB2ConnectOptions(options);
} else {
throw new IllegalArgumentException("Unsupported option type: " + options.getClass());
}
}

@Override
public SqlConnectOptions createConnectOptions() {
return new DB2ConnectOptions();
}

@Override
public String name() {
return KnownDrivers.DB2.name();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ protected SqlConnectOptions defaultOptions() {
return rule.options();
}

@Override
protected String getDriverName() {
return "DB2";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

package io.vertx.mssqlclient;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
Expand All @@ -26,11 +31,6 @@
import io.vertx.core.net.TrustOptions;
import io.vertx.sqlclient.SqlConnectOptions;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
* Connect options for configuring {@link MSSQLConnection}.
*/
Expand Down Expand Up @@ -68,7 +68,7 @@ public MSSQLConnectOptions(SqlConnectOptions other) {
public MSSQLConnectOptions(MSSQLConnectOptions other) {
super(other);
}

@Override
public MSSQLConnectOptions setHost(String host) {
return (MSSQLConnectOptions) super.setHost(host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.vertx.sqlclient.spi.Driver;

public class MSSQLDriver implements Driver {

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
return MSSQLPool.pool(wrap(options), poolOptions);
Expand All @@ -35,17 +35,24 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO
return MSSQLPool.pool(vertx, wrap(options), poolOptions);
}

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof MSSQLConnectOptions || SqlConnectOptions.class.equals(options.getClass());
}

private static MSSQLConnectOptions wrap(SqlConnectOptions options) {
if (options instanceof MSSQLConnectOptions) {
return (MSSQLConnectOptions) options;
} else {
} else if (SqlConnectOptions.class.equals(options.getClass())) {
return new MSSQLConnectOptions(options);
} else {
throw new IllegalArgumentException("Unsupported option type: " + options.getClass());
}
}

@Override
public SqlConnectOptions createConnectOptions() {
return new MSSQLConnectOptions();
}

@Override
public String name() {
return KnownDrivers.SQLSERVER.name();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ public class MSSQLDriverTest extends DriverTestBase {
protected SqlConnectOptions defaultOptions() {
return rule.options();
}


@Override
protected String getDriverName() {
return "SQLSERVER";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public MySQLConnectOptions(MySQLConnectOptions other) {
this.serverRsaPublicKeyPath = other.serverRsaPublicKeyPath;
this.serverRsaPublicKeyValue = other.serverRsaPublicKeyValue != null ? other.serverRsaPublicKeyValue.copy() : null;
}

/**
* Get the collation for the connection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.vertx.sqlclient.spi.Driver;

public class MySQLDriver implements Driver {

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
return MySQLPool.pool(wrap(options), poolOptions);
Expand All @@ -35,17 +35,24 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO
return MySQLPool.pool(vertx, wrap(options), poolOptions);
}

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof MySQLConnectOptions || SqlConnectOptions.class.equals(options.getClass());
}

private static MySQLConnectOptions wrap(SqlConnectOptions options) {
if (options instanceof MySQLConnectOptions) {
return (MySQLConnectOptions) options;
} else {
return (MySQLConnectOptions) options;
} else if (options.getClass().equals(SqlConnectOptions.class)) {
return new MySQLConnectOptions(options);
} else {
throw new IllegalArgumentException("Unsupported option type: " + options.getClass());
}
}

@Override
public SqlConnectOptions createConnectOptions() {
return new MySQLConnectOptions();
}

@Override
public String name() {
return "MYSQL";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ protected SqlConnectOptions defaultOptions() {
return rule.options();
}

@Override
protected String getDriverName() {
return "MYSQL";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public PgConnectOptions(PgConnectOptions other) {
pipeliningLimit = other.pipeliningLimit;
sslMode = other.sslMode;
}

@Override
public PgConnectOptions setHost(String host) {
return (PgConnectOptions) super.setHost(host);
Expand Down
23 changes: 15 additions & 8 deletions vertx-pg-client/src/main/java/io/vertx/pgclient/spi/PgDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.vertx.sqlclient.spi.Driver;

public class PgDriver implements Driver {

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
return PgPool.pool(wrap(options), poolOptions);
Expand All @@ -20,17 +20,24 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO
return PgPool.pool(vertx, wrap(options), poolOptions);
}

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof PgConnectOptions || SqlConnectOptions.class.equals(options.getClass());
}

private static PgConnectOptions wrap(SqlConnectOptions options) {
if (options instanceof PgConnectOptions) {
return (PgConnectOptions) options;
} else {
return (PgConnectOptions) options;
} else if (options.getClass().equals(SqlConnectOptions.class)) {
return new PgConnectOptions(options);
} else {
throw new IllegalArgumentException("Unsupported option type: " + options.getClass());
}
}

@Override
public SqlConnectOptions createConnectOptions() {
return new PgConnectOptions();
}

@Override
public String name() {
return KnownDrivers.POSTGRESQL.name();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ protected SqlConnectOptions defaultOptions() {
return rule.options();
}

@Override
protected String getDriverName() {
return "POSTGRESQL";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.NetClientOptions;
import io.vertx.sqlclient.spi.Driver;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -25,6 +26,7 @@
*/
@DataObject(generateConverter = true)
public class SqlConnectOptions extends NetClientOptions {

public static final boolean DEFAULT_CACHE_PREPARED_STATEMENTS = false;
public static final int DEFAULT_PREPARED_STATEMENT_CACHE_MAX_SIZE = 256;
public static final int DEFAULT_PREPARED_STATEMENT_CACHE_SQL_LIMIT = 2048;
Expand All @@ -39,17 +41,32 @@ public class SqlConnectOptions extends NetClientOptions {
private int preparedStatementCacheSqlLimit = DEFAULT_PREPARED_STATEMENT_CACHE_SQL_LIMIT;
private Map<String, String> properties = new HashMap<>(4);

/**
* @deprecated This constructor will be removed in the next release.
* Instead, use {@link Driver#createConnectOptions()}
*/
@Deprecated
public SqlConnectOptions() {
super();
init();
}

/**
* @deprecated This constructor will be removed in the next release.
* Instead, use {@link Driver#createConnectOptions()}
*/
@Deprecated
public SqlConnectOptions(JsonObject json) {
super(json);
init();
SqlConnectOptionsConverter.fromJson(json, this);
}

/**
* @deprecated This constructor will be removed in the next release.
* Instead, use {@link Driver#createConnectOptions()}
*/
@Deprecated
public SqlConnectOptions(SqlConnectOptions other) {
super(other);
this.host = other.host;
Expand All @@ -64,7 +81,7 @@ public SqlConnectOptions(SqlConnectOptions other) {
this.properties = new HashMap<>(other.properties);
}
}

/**
* Get the host for connecting to the server.
*
Expand Down Expand Up @@ -279,4 +296,28 @@ public JsonObject toJson() {
*/
protected void init() {
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof SqlConnectOptions))
return false;
SqlConnectOptions that = (SqlConnectOptions) obj;
return Objects.equals(host, that.host) &&
Objects.equals(port, that.port) &&
Objects.equals(database, that.database) &&
Objects.equals(user, that.user) &&
Objects.equals(password, that.password) &&
Objects.equals(properties, that.properties) &&
Objects.equals(cachePreparedStatements, that.cachePreparedStatements) &&
Objects.equals(preparedStatementCacheMaxSize, that.preparedStatementCacheMaxSize) &&
Objects.equals(preparedStatementCacheSqlLimit, that.preparedStatementCacheSqlLimit);
}

@Override
public int hashCode() {
return Objects.hash(host, port, database, user, password, properties, cachePreparedStatements,
preparedStatementCacheMaxSize, preparedStatementCacheSqlLimit);
}
}
Loading

0 comments on commit 6400b8c

Please sign in to comment.