Skip to content

Commit

Permalink
Add database kind property to SqlConnectOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aguibert committed May 19, 2020
1 parent 0a1a058 commit 0f2fda0
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 5 deletions.
1 change: 1 addition & 0 deletions vertx-db2-client/src/main/asciidoc/dataobjects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
|[[hostnameVerificationAlgorithm]]`@hostnameVerificationAlgorithm`|`String`|-
|[[idleTimeout]]`@idleTimeout`|`Number (int)`|-
|[[idleTimeoutUnit]]`@idleTimeoutUnit`|`link:enums.html#TimeUnit[TimeUnit]`|-
|[[kind]]`@kind`|`String`|-
|[[localAddress]]`@localAddress`|`String`|-
|[[logActivity]]`@logActivity`|`Boolean`|-
|[[metricsName]]`@metricsName`|`String`|-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.vertx.db2client.impl.DB2ConnectionUriParser;
import io.vertx.db2client.impl.drda.SQLState;
import io.vertx.db2client.impl.drda.SqlCode;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.sqlclient.SqlConnectOptions;

/**
Expand Down Expand Up @@ -88,6 +89,22 @@ public DB2ConnectOptions(DB2ConnectOptions other) {
super(other);
this.pipeliningLimit = other.pipeliningLimit;
}

@Override
public String getKind() {
return DB2Driver.KIND;
}

@Override
public DB2ConnectOptions setKind(String kind) {
Objects.requireNonNull(kind, "Database kind must be non-null");
// Tolerate setting the kind to DB2, but raise an error otherwise
if (!DB2Driver.KIND.equalsIgnoreCase(kind)) {
throw new IllegalArgumentException("Database kind for " + getClass() +
" must be of type " + DB2Driver.KIND + " but " + kind + " was specified");
}
return this;
}

@Override
public DB2ConnectOptions setHost(String host) {
Expand Down Expand Up @@ -182,6 +199,7 @@ public DB2ConnectOptions addProperty(String key, String value) {
protected void init() {
this.setHost(DEFAULT_HOST);
this.setPort(DEFAULT_PORT);
this.setKind(DB2Driver.KIND);
this.setProperties(new HashMap<>(DEFAULT_CONNECTION_ATTRIBUTES));
}

Expand Down
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 @@ -18,12 +18,15 @@
import io.vertx.core.Vertx;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.DB2Pool;
import io.vertx.sqlclient.DatabaseKind;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnectOptions;
import io.vertx.sqlclient.spi.Driver;

public class DB2Driver implements Driver {

public static final String KIND = DatabaseKind.DB2;

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
Expand All @@ -37,7 +40,14 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof DB2ConnectOptions || SqlConnectOptions.class.equals(options.getClass());
if (options instanceof DB2ConnectOptions) {
return true;
}
if (SqlConnectOptions.class.equals(options.getClass())) {
return KIND.equalsIgnoreCase(options.getKind()) ||
SqlConnectOptions.DEFAULT_KIND.equalsIgnoreCase(options.getKind());
}
return false;
}

private static DB2ConnectOptions wrap(SqlConnectOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ public void printTestName(TestContext ctx) throws Exception {
protected SqlConnectOptions defaultOptions() {
return rule.options();
}

@Override
protected String getKind() {
return "db2";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
import io.vertx.core.net.ProxyOptions;
import io.vertx.core.net.SSLEngineOptions;
import io.vertx.core.net.TrustOptions;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.mssqlclient.spi.MSSQLDriver;
import io.vertx.sqlclient.SqlConnectOptions;

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

Expand Down Expand Up @@ -68,6 +72,22 @@ public MSSQLConnectOptions(SqlConnectOptions other) {
public MSSQLConnectOptions(MSSQLConnectOptions other) {
super(other);
}

@Override
public String getKind() {
return MSSQLDriver.KIND;
}

@Override
public MSSQLConnectOptions setKind(String kind) {
Objects.requireNonNull(kind, "Database kind must be non-null");
// Tolerate setting the kind to the same thing, but raise an error otherwise
if (!MSSQLDriver.KIND.equalsIgnoreCase(kind)) {
throw new IllegalArgumentException("Database kind for " + getClass() +
" must be of type " + MSSQLDriver.KIND + " but " + kind + " was specified");
}
return this;
}

@Override
public MSSQLConnectOptions setHost(String host) {
Expand Down Expand Up @@ -323,6 +343,7 @@ protected void init() {
this.setUser(DEFAULT_USER);
this.setPassword(DEFAULT_PASSWORD);
this.setDatabase(DEFAULT_SCHEMA);
this.setKind(MSSQLDriver.KIND);
this.setProperties(new HashMap<>(DEFAULT_PROPERTIES));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import io.vertx.core.Vertx;
import io.vertx.mssqlclient.MSSQLConnectOptions;
import io.vertx.mssqlclient.MSSQLPool;
import io.vertx.sqlclient.DatabaseKind;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnectOptions;
import io.vertx.sqlclient.spi.Driver;

public class MSSQLDriver implements Driver {

public static final String KIND = DatabaseKind.MICROSOFT_SQL_SERVER;

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
Expand All @@ -37,7 +40,14 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof MSSQLConnectOptions || SqlConnectOptions.class.equals(options.getClass());
if (options instanceof MSSQLConnectOptions) {
return true;
}
if (SqlConnectOptions.class.equals(options.getClass())) {
return KIND.equalsIgnoreCase(options.getKind()) ||
SqlConnectOptions.DEFAULT_KIND.equalsIgnoreCase(options.getKind());
}
return false;
}

private static MSSQLConnectOptions wrap(SqlConnectOptions options) {
Expand Down
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 getKind() {
return "sqlserver";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.vertx.core.net.*;
import io.vertx.mysqlclient.impl.MySQLCollation;
import io.vertx.mysqlclient.impl.MySQLConnectionUriParser;
import io.vertx.mysqlclient.spi.MySQLDriver;
import io.vertx.sqlclient.DatabaseKind;
import io.vertx.sqlclient.SqlConnectOptions;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -92,6 +94,22 @@ public MySQLConnectOptions(MySQLConnectOptions other) {
this.serverRsaPublicKeyPath = other.serverRsaPublicKeyPath;
this.serverRsaPublicKeyValue = other.serverRsaPublicKeyValue != null ? other.serverRsaPublicKeyValue.copy() : null;
}

@Override
public String getKind() {
return MySQLDriver.KIND;
}

@Override
public MySQLConnectOptions setKind(String kind) {
Objects.requireNonNull(kind, "Database kind must be non-null");
// Tolerate setting the kind to the same thing, but raise an error otherwise
if (!MySQLDriver.KIND.equalsIgnoreCase(kind) && !DatabaseKind.MARIADB.equalsIgnoreCase(kind)) {
throw new IllegalArgumentException("Database kind for " + getClass() +
" must be of type " + MySQLDriver.KIND + " or " + DatabaseKind.MARIADB + " but " + kind + " was specified");
}
return this;
}

/**
* Get the collation for the connection.
Expand Down Expand Up @@ -528,6 +546,7 @@ protected void init() {
this.setUser(DEFAULT_USER);
this.setPassword(DEFAULT_PASSWORD);
this.setDatabase(DEFAULT_SCHEMA);
this.setKind(MySQLDriver.KIND);
this.setProperties(new HashMap<>(DEFAULT_CONNECTION_ATTRIBUTES));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import io.vertx.core.Vertx;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.sqlclient.DatabaseKind;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnectOptions;
import io.vertx.sqlclient.spi.Driver;

public class MySQLDriver implements Driver {

public static final String KIND = DatabaseKind.MYSQL;

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
Expand All @@ -37,7 +40,15 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof MySQLConnectOptions || SqlConnectOptions.class.equals(options.getClass());
if (options instanceof MySQLConnectOptions) {
return true;
}
if (SqlConnectOptions.class.equals(options.getClass())) {
return KIND.equalsIgnoreCase(options.getKind()) ||
DatabaseKind.MARIADB.equalsIgnoreCase(options.getKind()) ||
SqlConnectOptions.DEFAULT_KIND.equalsIgnoreCase(options.getKind());
}
return false;
}

private static MySQLConnectOptions wrap(SqlConnectOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public class MySQLDriverTest extends DriverTestBase {
protected SqlConnectOptions defaultOptions() {
return rule.options();
}

@Override
protected String getKind() {
return "mysql";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.pgclient.impl.PgConnectionUriParser;
import io.vertx.pgclient.spi.PgDriver;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.*;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.sqlclient.SqlConnectOptions;

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

Expand Down Expand Up @@ -135,6 +139,24 @@ public PgConnectOptions(PgConnectOptions other) {
pipeliningLimit = other.pipeliningLimit;
sslMode = other.sslMode;
}

@Override
public String getKind() {
return PgDriver.KIND;
}

@Override
public PgConnectOptions setKind(String kind) {
Objects.requireNonNull(kind, "Database kind must be non-null");
// Tolerate setting the kind to the same thing, but raise an error otherwise
if (!PgDriver.KIND.equalsIgnoreCase(kind) &&
!"postgre".equalsIgnoreCase(kind) &&
!"postgres".equalsIgnoreCase(kind)) {
throw new IllegalArgumentException("Database kind for " + getClass() +
" must be of type " + PgDriver.KIND + ", postgre, or postgres but " + kind + " was specified");
}
return this;
}

@Override
public PgConnectOptions setHost(String host) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.DatabaseKind;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnectOptions;
import io.vertx.sqlclient.spi.Driver;

public class PgDriver implements Driver {

public static final String KIND = DatabaseKind.POSTGRESQL;

@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
Expand All @@ -22,7 +25,16 @@ public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolO

@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof PgConnectOptions || SqlConnectOptions.class.equals(options.getClass());
if (options instanceof PgConnectOptions) {
return true;
}
if (SqlConnectOptions.class.equals(options.getClass())) {
return KIND.equalsIgnoreCase(options.getKind()) ||
"postgre".equalsIgnoreCase(options.getKind()) ||
"postgres".equalsIgnoreCase(options.getKind()) ||
SqlConnectOptions.DEFAULT_KIND.equalsIgnoreCase(options.getKind());
}
return false;
}

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

@Override
protected String getKind() {
return "postgresql";
}

}
3 changes: 3 additions & 0 deletions vertx-sql-client/src/main/asciidoc/dataobjects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Specify the host for connecting to the server.
|[[hostnameVerificationAlgorithm]]`@hostnameVerificationAlgorithm`|`String`|-
|[[idleTimeout]]`@idleTimeout`|`Number (int)`|-
|[[idleTimeoutUnit]]`@idleTimeoutUnit`|`link:enums.html#TimeUnit[TimeUnit]`|-
|[[kind]]`@kind`|`String`|+++
Specify the kind of database the connect options are for.
+++
|[[localAddress]]`@localAddress`|`String`|-
|[[logActivity]]`@logActivity`|`Boolean`|-
|[[metricsName]]`@metricsName`|`String`|-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
obj.setHost((String)member.getValue());
}
break;
case "kind":
if (member.getValue() instanceof String) {
obj.setKind((String)member.getValue());
}
break;
case "password":
if (member.getValue() instanceof String) {
obj.setPassword((String)member.getValue());
Expand Down Expand Up @@ -81,6 +86,9 @@ public static void toJson(SqlConnectOptions obj, java.util.Map<String, Object> j
if (obj.getHost() != null) {
json.put("host", obj.getHost());
}
if (obj.getKind() != null) {
json.put("kind", obj.getKind());
}
if (obj.getPassword() != null) {
json.put("password", obj.getPassword());
}
Expand Down
Loading

0 comments on commit 0f2fda0

Please sign in to comment.