diff --git a/build.gradle b/build.gradle index 029f9d7..f924040 100644 --- a/build.gradle +++ b/build.gradle @@ -7,4 +7,11 @@ version = '1.0.0' java.sourceCompatibility = JavaVersion.VERSION_1_8 -dependencies {} \ No newline at end of file +dependencies { + testRuntimeOnly("mysql:mysql-connector-java:8.0.30") + testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/src/main/java/com/cubetiqs/sql/ConnectionFactory.java b/src/main/java/com/cubetiqs/sql/ConnectionFactory.java index 8444f64..02d22ff 100644 --- a/src/main/java/com/cubetiqs/sql/ConnectionFactory.java +++ b/src/main/java/com/cubetiqs/sql/ConnectionFactory.java @@ -50,7 +50,9 @@ public final class ConnectionFactory { this.user = properties.getUser(); this.password = properties.getPassword(); if (properties.getProperties() != null && !properties.getProperties().isEmpty()) { - this.info = new Properties(); + if (this.info == null) { + this.info = new Properties(); + } this.info.putAll(properties.getProperties()); } } @@ -148,4 +150,12 @@ public final class ConnectionFactory { public static DataSource createDataSource(ConnectionBuilder builder) { return new SimpleDataSource(builder); } + + public static JdbcDataFactory createDataFactory(ConnectionFactory.ConnectionBuilder builder) { + if (builder == null) { + return null; + } + + return JdbcDataFactory.create(new JdbcDataQueryImpl(builder::build)); + } } diff --git a/src/main/java/com/cubetiqs/sql/ConnectionProperties.java b/src/main/java/com/cubetiqs/sql/ConnectionProperties.java new file mode 100644 index 0000000..a8008d0 --- /dev/null +++ b/src/main/java/com/cubetiqs/sql/ConnectionProperties.java @@ -0,0 +1,106 @@ +package com.cubetiqs.sql; + +import java.util.Properties; + +public interface ConnectionProperties { + String getType(); + + String getHost(); + + int getPort(); + + String getUser(); + + String getPassword(); + + String getDatabase(); + + default boolean useSSL() { + return false; + } + + default Properties getProperties() { + return null; + } + + default StringBuilder getURLBuilder() { + return new StringBuilder("jdbc:" + getType() + "://") + .append(getHost()) + .append(":") + .append(getPort()) + .append("/") + .append(getDatabase()); + } + + class MySQL implements ConnectionProperties { + private String host; + private int port; + private String user; + private String password; + private String database; + private boolean useSSL; + private Properties properties; + + private boolean autoReconnect = true; + private int cmaxReconnects = 5; + private int initialTimeout = 1; + + + @Override + public String getType() { + return "mysql"; + } + + @Override + public String getHost() { + return host; + } + + @Override + public int getPort() { + if (port > 0) { + return port; + } + + return 3306; + } + + @Override + public String getUser() { + if (user == null || user.equals("")) { + return "root"; + } + + return user; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getDatabase() { + return database; + } + + @Override + public boolean useSSL() { + return useSSL; + } + + @Override + public Properties getProperties() { + return properties; + } + + @Override + public StringBuilder getURLBuilder() { + return ConnectionProperties.super.getURLBuilder() + .append("?").append("useSSL=").append(useSSL) + .append("&").append("autoReconnect=").append(autoReconnect) + .append("&").append("cmaxReconnets=").append(cmaxReconnects) + .append("&").append("initialTimeout").append(initialTimeout); + } + } +} diff --git a/src/main/java/com/cubetiqs/sql/JdbcDataFactory.java b/src/main/java/com/cubetiqs/sql/JdbcDataFactory.java index 250a04f..42ff356 100644 --- a/src/main/java/com/cubetiqs/sql/JdbcDataFactory.java +++ b/src/main/java/com/cubetiqs/sql/JdbcDataFactory.java @@ -3,6 +3,7 @@ package com.cubetiqs.sql; import com.cubetiqs.sql.mapper.MapperProvider; import com.cubetiqs.sql.mapper.RowMapperProvider; +import java.sql.SQLException; import java.util.List; import java.util.Map; @@ -112,4 +113,12 @@ public final class JdbcDataFactory { public long count(ISqlMapParameter parameter) { return queryManager.count(parameter); } + + public boolean isConnected() { + try { + return !queryManager.getManager().getConnection().isClosed(); + } catch (SQLException e) { + return false; + } + } } diff --git a/src/test/java/com/cubetiqs/sql/ConnectionTests.java b/src/test/java/com/cubetiqs/sql/ConnectionTests.java new file mode 100644 index 0000000..c63203a --- /dev/null +++ b/src/test/java/com/cubetiqs/sql/ConnectionTests.java @@ -0,0 +1,25 @@ +package com.cubetiqs.sql; + +import org.junit.jupiter.api.Test; + +import java.sql.SQLException; +import java.util.List; + +public class ConnectionTests { + private final String MYSQL_URL = "jdbc:mysql://192.168.0.202:3306/cosmetic_leang"; + + @Test + public void connectToMySQL() { + ConnectionFactory.ConnectionBuilder builder = ConnectionFactory.builder() + .driver("com.mysql.cj.jdbc.Driver") + .url(MYSQL_URL) + .user("test") + .password("test"); + + JdbcDataFactory factory = ConnectionFactory + .createDataFactory(builder); + + IExecuteResult> result = factory.queryForList("select * from um"); + System.out.println(result.getData()); + } +}