Task: Add connection properties custom interface and add junit test platforms and updated the connection factory

This commit is contained in:
Sambo Chea 2022-09-15 16:31:16 +07:00
parent 95c95f1481
commit 499ad711ea
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
5 changed files with 159 additions and 2 deletions

View File

@ -7,4 +7,11 @@ version = '1.0.0'
java.sourceCompatibility = JavaVersion.VERSION_1_8 java.sourceCompatibility = JavaVersion.VERSION_1_8
dependencies {} dependencies {
testRuntimeOnly("mysql:mysql-connector-java:8.0.30")
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
}
test {
useJUnitPlatform()
}

View File

@ -50,7 +50,9 @@ public final class ConnectionFactory {
this.user = properties.getUser(); this.user = properties.getUser();
this.password = properties.getPassword(); this.password = properties.getPassword();
if (properties.getProperties() != null && !properties.getProperties().isEmpty()) { if (properties.getProperties() != null && !properties.getProperties().isEmpty()) {
if (this.info == null) {
this.info = new Properties(); this.info = new Properties();
}
this.info.putAll(properties.getProperties()); this.info.putAll(properties.getProperties());
} }
} }
@ -148,4 +150,12 @@ public final class ConnectionFactory {
public static DataSource createDataSource(ConnectionBuilder builder) { public static DataSource createDataSource(ConnectionBuilder builder) {
return new SimpleDataSource(builder); return new SimpleDataSource(builder);
} }
public static JdbcDataFactory createDataFactory(ConnectionFactory.ConnectionBuilder builder) {
if (builder == null) {
return null;
}
return JdbcDataFactory.create(new JdbcDataQueryImpl(builder::build));
}
} }

View File

@ -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);
}
}
}

View File

@ -3,6 +3,7 @@ package com.cubetiqs.sql;
import com.cubetiqs.sql.mapper.MapperProvider; import com.cubetiqs.sql.mapper.MapperProvider;
import com.cubetiqs.sql.mapper.RowMapperProvider; import com.cubetiqs.sql.mapper.RowMapperProvider;
import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -112,4 +113,12 @@ public final class JdbcDataFactory {
public long count(ISqlMapParameter parameter) { public long count(ISqlMapParameter parameter) {
return queryManager.count(parameter); return queryManager.count(parameter);
} }
public boolean isConnected() {
try {
return !queryManager.getManager().getConnection().isClosed();
} catch (SQLException e) {
return false;
}
}
} }

View File

@ -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<List<Object>> result = factory.queryForList("select * from um");
System.out.println(result.getData());
}
}