plugin: Adapt the new plug-in mechanism for postgresql

This commit is contained in:
qianmoQ 2022-09-28 00:48:18 +08:00
parent 891084ba14
commit 3d91523e53
9 changed files with 44 additions and 117 deletions

View File

@ -56,6 +56,8 @@ Here are some of the major database solutions that are supported:
<a href="https://www.mysql.com" target="_blank"><img src="./shared/plugin/mysql.png" alt="MySQL" class="a" width="auto" height="50"/></a>
<a href="https://prestodb.io/" target="_blank"><img src="./shared/plugin/presto.png" alt="Presto" class="a" width="133" height="34" /></a>
<a href="https://redis.io/" target="_blank"><img src="./shared/plugin/redis.svg" alt="Redis" class="a" width="auto" height="40" /></a>
<a href="https://www.postgresql.org/" target="_blank"><img src="./shared/plugin/postgresql.png" alt="PostgreSQL" class="a" width="auto" height="40" /></a>
<a href="https://trino.io/" target="_blank"><img src="./shared/plugin/trino.jpg" alt="Trino" class="a" width="auto" height="50" /></a>
</p>
## Join Us

View File

@ -28,16 +28,8 @@
<version>${pgsql.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
</dependencies>

View File

@ -1,10 +1,15 @@
package io.edurt.datacap.plugin.jdbc.postgresql;
import io.edurt.datacap.spi.adapter.JdbcAdapter;
import io.edurt.datacap.spi.connection.JdbcConnection;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PostgreSQLAdapter
extends JdbcAdapter
{
public PostgreSQLAdapter(JdbcConnection jdbcConnection)
{
super(jdbcConnection);
}
}

View File

@ -1,103 +1,16 @@
package io.edurt.datacap.plugin.jdbc.postgresql;
import io.edurt.datacap.spi.model.Configure;
import io.edurt.datacap.spi.connection.JdbcConfigure;
import io.edurt.datacap.spi.connection.JdbcConnection;
import io.edurt.datacap.spi.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
public class PostgreSQLConnection
extends JdbcConnection
{
private static String DRIVER = "com.pgsql.jdbc.Driver";
private final Configure configure;
private final Response response;
private Connection connection;
public PostgreSQLConnection(Configure configure, Response response)
public PostgreSQLConnection(JdbcConfigure jdbcConfigure, Response response)
{
this.configure = configure;
this.response = response;
}
private String appendURL()
{
StringBuffer buffer = new StringBuffer();
buffer.append("jdbc:postgresql://");
buffer.append(configure.getHost());
buffer.append(":");
buffer.append(configure.getPort());
if (configure.getDatabase().isPresent()) {
buffer.append("/");
buffer.append(configure.getDatabase().get());
}
if (configure.getEnv().isPresent()) {
Map<String, Object> env = configure.getEnv().get();
List<String> flatEnv = env.entrySet().stream()
.map(value -> String.format("%s=%s", value.getKey(), value.getValue()))
.collect(Collectors.toList());
buffer.append("?");
buffer.append(String.join("&", flatEnv));
}
return buffer.toString();
}
public Connection openConnection()
{
try {
Class.forName(DRIVER);
String url = appendURL();
log.info("Connection driver {}", DRIVER);
log.info("Connection url {}", url);
if (configure.getUsername().isPresent() || configure.getPassword().isPresent()) {
log.info("Connection username with {} password with {}", configure.getUsername().get(), "***");
this.connection = DriverManager.getConnection(url, configure.getUsername().get(), configure.getPassword().get());
}
else {
log.info("Connection username and password not present");
this.connection = DriverManager.getConnection(url);
}
response.setIsConnected(Boolean.TRUE);
}
catch (SQLException | ClassNotFoundException ex) {
log.error("Connection failed ", ex);
response.setIsConnected(Boolean.FALSE);
response.setMessage(ex.getMessage());
}
return this.connection;
}
public Connection getConnection()
{
return this.connection;
}
public Response getResponse()
{
return this.response;
}
public Configure getConfigure()
{
return this.configure;
}
public void destroy()
{
if (ObjectUtils.isNotEmpty(this.connection)) {
try {
this.connection.close();
}
catch (SQLException ex) {
log.error("Connection close failed ", ex);
}
}
super(jdbcConfigure, response);
}
}

View File

@ -3,15 +3,18 @@ package io.edurt.datacap.plugin.jdbc.postgresql;
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.model.Configure;
import io.edurt.datacap.spi.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
@Slf4j
public class PostgreSQLPlugin
implements Plugin
{
private PostgreSQLConnection connection;
private JdbcConfigure jdbcConfigure;
private PostgreSQLConnection postgreSQLConnection;
private Response response;
@Override
@ -35,25 +38,35 @@ public class PostgreSQLPlugin
@Override
public void connect(Configure configure)
{
try {
this.response = new Response();
this.connection = new PostgreSQLConnection(configure, response);
this.connection.openConnection();
this.jdbcConfigure = new JdbcConfigure();
BeanUtils.copyProperties(this.jdbcConfigure, configure);
this.jdbcConfigure.setJdbcDriver("org.postgresql.Driver");
this.jdbcConfigure.setJdbcType("postgresql");
this.postgreSQLConnection = new PostgreSQLConnection(this.jdbcConfigure, this.response);
}
catch (Exception ex) {
this.response.setIsSuccessful(Boolean.FALSE);
this.response.setMessage(ex.getMessage());
throw new RuntimeException(ex);
}
}
@Override
public Response execute(String content)
{
log.info("Execute plugin logic started");
response = this.connection.getResponse();
JdbcAdapter processor = new PostgreSQLAdapter();
processor.handlerJDBCExecute(this.connection.getConfigure().getFormat(), content, this.connection.getConnection(), response);
log.info("Execute plugin logic end");
return response;
log.info("Execute postgresql plugin logic started");
this.response = this.postgreSQLConnection.getResponse();
JdbcAdapter processor = new PostgreSQLAdapter(this.postgreSQLConnection);
this.response = processor.handlerJDBCExecute(content);
log.info("Execute postgresql plugin logic end");
return this.response;
}
@Override
public void destroy()
{
this.connection.destroy();
this.postgreSQLConnection.destroy();
}
}

View File

@ -11,7 +11,7 @@ import org.junit.Test;
import java.util.Set;
public class PostgreSQLPluginModuleTestService
public class PostgreSQLPluginModuleTest
{
private Injector injector;

View File

@ -12,7 +12,7 @@ import org.junit.Test;
import java.util.Optional;
import java.util.Set;
public class PostgreSQLPluginTestService
public class PostgreSQLPluginTest
{
private Injector injector;
private Configure configure;
@ -32,11 +32,13 @@ public class PostgreSQLPluginTestService
public void test()
{
Set<Plugin> plugins = injector.getInstance(Key.get(new TypeLiteral<Set<Plugin>>() {}));
Optional<Plugin> pluginOptional = plugins.stream().filter(v -> v.name().equalsIgnoreCase("PostgreSQL")).findFirst();
Optional<Plugin> pluginOptional = plugins.stream()
.filter(v -> v.name().equalsIgnoreCase("PostgreSQL"))
.findFirst();
if (pluginOptional.isPresent()) {
Plugin plugin = pluginOptional.get();
plugin.connect(configure);
System.out.println(plugin.execute("SELECT * FROM bootstack.system_interface LIMIT 100"));
System.out.println(plugin.execute("SHOW CATALOGS"));
plugin.destroy();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

BIN
shared/plugin/trino.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB