[Fix] update source connection (#514) (#535)

This commit is contained in:
qianmoQ 2023-12-06 21:38:39 +08:00 committed by GitHub
commit 49b62dfa48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 129 additions and 6 deletions

View File

@ -20,6 +20,7 @@ public class Configure
private Optional<String> username = Optional.empty();
private Optional<String> password = Optional.empty();
private Optional<String> database = Optional.empty();
private Optional<String> version = Optional.empty();
private Optional<Map<String, Object>> env = Optional.empty();
private Optional<Boolean> ssl = Optional.empty();
private FormatType format = FormatType.NONE;

View File

@ -0,0 +1,60 @@
package io.edurt.datacap.plugin.jdbc.dm;
import io.edurt.datacap.spi.connection.JdbcConfigure;
import io.edurt.datacap.spi.connection.JdbcConnection;
import io.edurt.datacap.spi.model.Response;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DmConnection
extends JdbcConnection
{
public DmConnection(JdbcConfigure jdbcConfigure, Response response)
{
super(jdbcConfigure, response);
}
@Override
protected String formatJdbcUrl()
{
JdbcConfigure jdbcConfigure = (JdbcConfigure) this.getConfigure();
StringBuffer buffer = new StringBuffer();
buffer.append("jdbc:");
buffer.append(jdbcConfigure.getJdbcType());
buffer.append("://");
buffer.append(jdbcConfigure.getHost());
buffer.append(":");
buffer.append(jdbcConfigure.getPort());
if (jdbcConfigure.getDatabase().isPresent()) {
buffer.append("?SCHEMA=");
buffer.append(jdbcConfigure.getDatabase().get());
}
if (jdbcConfigure.getSsl().isPresent()) {
if (!jdbcConfigure.getDatabase().isPresent()) {
buffer.append(String.format("?ssl=%s", jdbcConfigure.getSsl().get()));
}
else {
buffer.append(String.format("&ssl=%s", jdbcConfigure.getSsl().get()));
}
}
if (jdbcConfigure.getEnv().isPresent()) {
Map<String, Object> env = jdbcConfigure.getEnv().get();
List<String> flatEnv = env.entrySet()
.stream()
.map(value -> String.format("%s=%s", value.getKey(), value.getValue()))
.collect(Collectors.toList());
if (!jdbcConfigure.getDatabase().isPresent()) {
buffer.append("?");
}
else {
if (jdbcConfigure.getIsAppendChar()) {
buffer.append("&");
}
}
buffer.append(String.join("&", flatEnv));
}
return buffer.toString();
}
}

View File

@ -4,7 +4,6 @@ import io.edurt.datacap.spi.Plugin;
import io.edurt.datacap.spi.PluginType;
import io.edurt.datacap.spi.adapter.JdbcAdapter;
import io.edurt.datacap.spi.connection.JdbcConfigure;
import io.edurt.datacap.spi.connection.JdbcConnection;
import io.edurt.datacap.spi.model.Configure;
import io.edurt.datacap.spi.model.Response;
import lombok.extern.slf4j.Slf4j;
@ -16,7 +15,7 @@ public class DmPlugin
implements Plugin
{
private JdbcConfigure jdbcConfigure;
private JdbcConnection connection;
private DmConnection connection;
private Response response;
@Override
@ -46,7 +45,7 @@ public class DmPlugin
BeanUtils.copyProperties(this.jdbcConfigure, configure);
this.jdbcConfigure.setJdbcDriver("dm.jdbc.driver.DmDriver");
this.jdbcConfigure.setJdbcType("dm");
this.connection = new JdbcConnection(this.jdbcConfigure, this.response);
this.connection = new DmConnection(this.jdbcConfigure, this.response);
}
catch (Exception ex) {
this.response.setIsConnected(Boolean.FALSE);

View File

@ -0,0 +1,64 @@
package io.edurt.datacap.plugin.jdbc.sqlserver;
import io.edurt.datacap.spi.connection.JdbcConfigure;
import io.edurt.datacap.spi.connection.JdbcConnection;
import io.edurt.datacap.spi.model.Response;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SqlServerConnection
extends JdbcConnection
{
public SqlServerConnection(JdbcConfigure jdbcConfigure, Response response)
{
super(jdbcConfigure, response);
}
/**
* check version
* SQL Server 2000
* jdbc:microsoft:sqlserver://<host>:<port>;DatabaseName=<database>
* SQL Server 2005 and above:
* jdbc:sqlserver://<host>:<port>;databaseName=<database>
*/
private static final String SQL_SERVER_2000 = "SQL Server 2000";
@Override
protected String formatJdbcUrl()
{
JdbcConfigure jdbcConfigure = (JdbcConfigure) this.getConfigure();
StringBuffer buffer = new StringBuffer();
buffer.append("jdbc:");
jdbcConfigure.getVersion().ifPresent(version -> {
if (SQL_SERVER_2000.equalsIgnoreCase(version)) {
buffer.append("microsoft:");
}
});
buffer.append(jdbcConfigure.getJdbcType());
buffer.append("://");
buffer.append(jdbcConfigure.getHost());
buffer.append(":");
buffer.append(jdbcConfigure.getPort());
buffer.append(";");
if (jdbcConfigure.getDatabase().isPresent()) {
buffer.append("databaseName=");
buffer.append(jdbcConfigure.getDatabase().get());
buffer.append(";");
}
if (jdbcConfigure.getSsl().isPresent()) {
buffer.append(String.format("ssl=%s", jdbcConfigure.getSsl().get()));
buffer.append(";");
}
if (jdbcConfigure.getEnv().isPresent()) {
Map<String, Object> env = jdbcConfigure.getEnv().get();
List<String> flatEnv = env.entrySet()
.stream()
.map(value -> String.format("%s=%s", value.getKey(), value.getValue()))
.collect(Collectors.toList());
buffer.append(String.join(";", flatEnv));
}
return buffer.toString();
}
}

View File

@ -4,7 +4,6 @@ import io.edurt.datacap.spi.Plugin;
import io.edurt.datacap.spi.PluginType;
import io.edurt.datacap.spi.adapter.JdbcAdapter;
import io.edurt.datacap.spi.connection.JdbcConfigure;
import io.edurt.datacap.spi.connection.JdbcConnection;
import io.edurt.datacap.spi.model.Configure;
import io.edurt.datacap.spi.model.Response;
import lombok.extern.slf4j.Slf4j;
@ -16,7 +15,7 @@ public class SqlServerPlugin
implements Plugin
{
private JdbcConfigure jdbcConfigure;
private JdbcConnection connection;
private SqlServerConnection connection;
private Response response;
@Override
@ -46,7 +45,7 @@ public class SqlServerPlugin
BeanUtils.copyProperties(this.jdbcConfigure, configure);
this.jdbcConfigure.setJdbcDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.jdbcConfigure.setJdbcType("sqlserver");
this.connection = new JdbcConnection(this.jdbcConfigure, this.response);
this.connection = new SqlServerConnection(this.jdbcConfigure, this.response);
}
catch (Exception ex) {
this.response.setIsConnected(Boolean.FALSE);