diff --git a/README.md b/README.md index 54f78b1e..8a2eeb4f 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,9 @@ Here are some of the major database solutions that are supported:   H2 +   + + SqlServer

diff --git a/assets/plugin/sqlserver.svg b/assets/plugin/sqlserver.svg new file mode 100644 index 00000000..30758ba3 --- /dev/null +++ b/assets/plugin/sqlserver.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/plugin/jdbc/pom.xml b/plugin/jdbc/pom.xml index a83417d7..66156ec6 100644 --- a/plugin/jdbc/pom.xml +++ b/plugin/jdbc/pom.xml @@ -32,5 +32,6 @@ monetdb phoenix h2 + sqlserver \ No newline at end of file diff --git a/plugin/jdbc/sqlserver/pom.xml b/plugin/jdbc/sqlserver/pom.xml new file mode 100644 index 00000000..d980c0b5 --- /dev/null +++ b/plugin/jdbc/sqlserver/pom.xml @@ -0,0 +1,61 @@ + + + + datacap-plugin-jdbc + io.edurt.datacap.plugin.jdbc + 1.1.0.20221115-SNAPSHOT + + 4.0.0 + + datacap-plugin-jdbc-sqlserver + DataCap plugin for jdbc (SqlServer) + + + 11.2.1.jre8 + jdbc-sqlserver + + + + + io.edurt.datacap + datacap-spi + provided + + + commons-beanutils + commons-beanutils + + + com.microsoft.sqlserver + mssql-jdbc + ${sqlserver.version} + + + + + + + maven-assembly-plugin + ${assembly-plugin.version} + + ${plugin.name} + + ../../../configure/assembly/assembly-plugin.xml + + ../../../dist/plugins/${plugin.name} + + + + make-assembly + package + + single + + + + + + + diff --git a/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerAdapter.java b/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerAdapter.java new file mode 100644 index 00000000..f7d2fb77 --- /dev/null +++ b/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerAdapter.java @@ -0,0 +1,13 @@ +package io.edurt.datacap.plugin.jdbc.sqlserver; + +import io.edurt.datacap.spi.adapter.JdbcAdapter; +import io.edurt.datacap.spi.connection.JdbcConnection; + +public class SqlServerAdapter + extends JdbcAdapter +{ + public SqlServerAdapter(JdbcConnection jdbcConnection) + { + super(jdbcConnection); + } +} diff --git a/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPlugin.java b/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPlugin.java new file mode 100644 index 00000000..707c63a1 --- /dev/null +++ b/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPlugin.java @@ -0,0 +1,77 @@ +package io.edurt.datacap.plugin.jdbc.sqlserver; + +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; +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.lang3.ObjectUtils; + +@Slf4j +public class SqlServerPlugin + implements Plugin +{ + private JdbcConfigure jdbcConfigure; + private JdbcConnection connection; + private Response response; + + @Override + public String name() + { + return "SqlServer"; + } + + @Override + public String description() + { + return "Integrate Microsoft SqlServer data sources"; + } + + @Override + public PluginType type() + { + return PluginType.SOURCE; + } + + @Override + public void connect(Configure configure) + { + try { + this.response = new Response(); + this.jdbcConfigure = new JdbcConfigure(); + 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); + } + catch (Exception ex) { + this.response.setIsConnected(Boolean.FALSE); + this.response.setMessage(ex.getMessage()); + } + } + + @Override + public Response execute(String content) + { + if (ObjectUtils.isNotEmpty(this.connection)) { + log.info("Execute sqlserver plugin logic started"); + this.response = this.connection.getResponse(); + JdbcAdapter processor = new SqlServerAdapter(this.connection); + this.response = processor.handlerExecute(content); + log.info("Execute sqlserver plugin logic end"); + } + return this.response; + } + + @Override + public void destroy() + { + if (ObjectUtils.isNotEmpty(this.connection)) { + this.connection.destroy(); + } + } +} diff --git a/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginModule.java b/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginModule.java new file mode 100644 index 00000000..8bd24ff2 --- /dev/null +++ b/plugin/jdbc/sqlserver/src/main/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginModule.java @@ -0,0 +1,38 @@ +package io.edurt.datacap.plugin.jdbc.sqlserver; + +import com.google.inject.multibindings.Multibinder; +import io.edurt.datacap.spi.AbstractPluginModule; +import io.edurt.datacap.spi.Plugin; +import io.edurt.datacap.spi.PluginModule; +import io.edurt.datacap.spi.PluginType; + +public class SqlServerPluginModule + extends AbstractPluginModule + implements PluginModule +{ + @Override + public String getName() + { + return "SqlServer"; + } + + @Override + public PluginType getType() + { + return PluginType.SOURCE; + } + + @Override + public AbstractPluginModule get() + { + return this; + } + + protected void configure() + { + Multibinder module = Multibinder.newSetBinder(this.binder(), String.class); + module.addBinding().toInstance(this.getClass().getSimpleName()); + Multibinder plugin = Multibinder.newSetBinder(this.binder(), Plugin.class); + plugin.addBinding().to(SqlServerPlugin.class); + } +} diff --git a/plugin/jdbc/sqlserver/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule b/plugin/jdbc/sqlserver/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule new file mode 100644 index 00000000..a2f07c62 --- /dev/null +++ b/plugin/jdbc/sqlserver/src/main/resources/META-INF/services/io.edurt.datacap.spi.PluginModule @@ -0,0 +1 @@ +io.edurt.datacap.plugin.jdbc.sqlserver.SqlServerPluginModule diff --git a/plugin/jdbc/sqlserver/src/test/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginModuleTest.java b/plugin/jdbc/sqlserver/src/test/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginModuleTest.java new file mode 100644 index 00000000..69a551d7 --- /dev/null +++ b/plugin/jdbc/sqlserver/src/test/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginModuleTest.java @@ -0,0 +1,30 @@ +package io.edurt.datacap.plugin.jdbc.sqlserver; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import io.edurt.datacap.spi.Plugin; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; + +public class SqlServerPluginModuleTest +{ + private Injector injector; + + @Before + public void before() + { + this.injector = Guice.createInjector(new SqlServerPluginModule()); + } + + @Test + public void test() + { + Set plugins = injector.getInstance(Key.get(new TypeLiteral>() {})); + Assert.assertTrue(plugins.size() > 0); + } +} \ No newline at end of file diff --git a/plugin/jdbc/sqlserver/src/test/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginTest.java b/plugin/jdbc/sqlserver/src/test/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginTest.java new file mode 100644 index 00000000..545201a8 --- /dev/null +++ b/plugin/jdbc/sqlserver/src/test/java/io/edurt/datacap/plugin/jdbc/sqlserver/SqlServerPluginTest.java @@ -0,0 +1,44 @@ +package io.edurt.datacap.plugin.jdbc.sqlserver; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import io.edurt.datacap.spi.Plugin; +import io.edurt.datacap.spi.model.Configure; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Optional; +import java.util.Set; + +public class SqlServerPluginTest +{ + private Injector injector; + private Configure configure; + + @Before + public void before() + { + injector = Guice.createInjector(new SqlServerPluginModule()); + configure = new Configure(); + configure.setHost("127.0.0.1"); + configure.setPort(1433); + } + + @Test + public void test() + { + Set plugins = injector.getInstance(Key.get(new TypeLiteral>() {})); + Optional pluginOptional = plugins.stream() + .filter(v -> v.name().equalsIgnoreCase("SqlServer")) + .findFirst(); + if (pluginOptional.isPresent()) { + Plugin plugin = pluginOptional.get(); + plugin.connect(configure); + Assert.assertNotNull(plugin.execute(plugin.validator()).getConnection()); + plugin.destroy(); + } + } +} \ No newline at end of file diff --git a/server/pom.xml b/server/pom.xml index 8c0fd535..ef2dff31 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -179,6 +179,11 @@ datacap-plugin-jdbc-h2 ${project.version} + + io.edurt.datacap.plugin.jdbc + datacap-plugin-jdbc-sqlserver + ${project.version} + diff --git a/server/src/main/java/io/edurt/datacap/server/service/impl/ExecuteServiceImpl.java b/server/src/main/java/io/edurt/datacap/server/service/impl/ExecuteServiceImpl.java index 27ffce24..7028b3a7 100644 --- a/server/src/main/java/io/edurt/datacap/server/service/impl/ExecuteServiceImpl.java +++ b/server/src/main/java/io/edurt/datacap/server/service/impl/ExecuteServiceImpl.java @@ -11,6 +11,7 @@ import io.edurt.datacap.server.repository.SourceRepository; import io.edurt.datacap.server.service.ExecuteService; import io.edurt.datacap.spi.Plugin; import io.edurt.datacap.spi.model.Configure; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.util.Optional; @@ -49,7 +50,8 @@ public class ExecuteServiceImpl _configure.setPort(entity.getPort()); _configure.setUsername(Optional.ofNullable(entity.getUsername())); _configure.setPassword(Optional.ofNullable(entity.getPassword())); - _configure.setDatabase(Optional.ofNullable(entity.getDatabase())); + Optional _database = StringUtils.isNotEmpty(entity.getDatabase()) ? Optional.ofNullable(entity.getDatabase()) : Optional.empty(); + _configure.setDatabase(_database); _configure.setSsl(Optional.ofNullable(entity.getSsl())); _configure.setEnv(Optional.ofNullable(entity.getConfigures())); _configure.setFormat(configure.getFormat()); diff --git a/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java b/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java index 781f1de0..3f030e5c 100644 --- a/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java +++ b/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java @@ -21,6 +21,7 @@ import io.edurt.datacap.spi.FormatType; import io.edurt.datacap.spi.Plugin; import io.edurt.datacap.spi.PluginType; import io.edurt.datacap.spi.model.Configure; +import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -81,7 +82,8 @@ public class SourceServiceImpl _configure.setPort(configure.getPort()); _configure.setUsername(Optional.ofNullable(configure.getUsername())); _configure.setPassword(Optional.ofNullable(configure.getPassword())); - _configure.setDatabase(Optional.ofNullable(configure.getDatabase())); + Optional _database = StringUtils.isNotEmpty(configure.getDatabase()) ? Optional.ofNullable(configure.getDatabase()) : Optional.empty(); + _configure.setDatabase(_database); _configure.setEnv(Optional.ofNullable(configure.getConfigures())); _configure.setSsl(Optional.ofNullable(configure.getSsl())); _configure.setFormat(FormatType.JSON);