add
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
package com.qiaoba.common.base.code;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 数据源错误信息
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/27 9:22
|
||||
*/
|
||||
@Getter
|
||||
public enum DatasourceErrorCode {
|
||||
|
||||
/**
|
||||
* 未找到
|
||||
*/
|
||||
NOT_FIND(50101, "未找到数据源信息"),
|
||||
|
||||
/**
|
||||
* 连接错误
|
||||
*/
|
||||
CONNECT_ERROR(50102, "数据源无法连接"),
|
||||
|
||||
/**
|
||||
* 切换模式错误
|
||||
*/
|
||||
SWITCH_SCHEMA_ERROR(50103, "切换模式错误"),
|
||||
|
||||
/**
|
||||
* 创建表错误
|
||||
*/
|
||||
CREATE_TABLE_ERROR(50104, "创建表错误");
|
||||
|
||||
private final Integer code;
|
||||
private final String msg;
|
||||
|
||||
DatasourceErrorCode(Integer code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
}
|
@ -38,7 +38,7 @@ public enum DataBaseEnum {
|
||||
* PostgreSQL
|
||||
*/
|
||||
POSTGRE_SQL("PostgreSQL",
|
||||
"jdbc:postgresql://{}:{}/mydb?currentSchema={}&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowMultiQueries=true",
|
||||
"jdbc:postgresql://{}:{}/{}?currentSchema={}&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&allowMultiQueries=true",
|
||||
"org.postgresql.Driver",
|
||||
"SELECT 1"),
|
||||
|
||||
@ -65,10 +65,10 @@ public enum DataBaseEnum {
|
||||
throw new ServiceException(StrUtil.format("未找到数据库驱动, Type: {}", type));
|
||||
}
|
||||
|
||||
public static String getUrl(String type, String ip, String port, String database) {
|
||||
public static String getUrl(String type, String ip, String port, String dbName, String schema) {
|
||||
for (DataBaseEnum dataBaseEnum : values()) {
|
||||
if (dataBaseEnum.getType().equals(type)) {
|
||||
return StrUtil.format(dataBaseEnum.url, ip, port, database);
|
||||
return StrUtil.format(dataBaseEnum.url, ip, port, dbName, schema);
|
||||
}
|
||||
}
|
||||
throw new ServiceException(StrUtil.format("未找到数据库Url, Type: {}", type));
|
||||
|
@ -17,7 +17,7 @@ public class AjaxResult extends HashMap<String, Object> {
|
||||
/**
|
||||
* 状态码
|
||||
*/
|
||||
public static final String CODE_TAG = "code";
|
||||
public static final String CODE_TAG = "result";
|
||||
|
||||
/**
|
||||
* 返回内容
|
||||
|
@ -3,6 +3,7 @@ package com.qiaoba.common.database.handlers.schema;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.utils.SqlUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -32,14 +33,10 @@ public class MysqlSchemaHandler implements SchemaHandler {
|
||||
// eg: use qiaoba-1;
|
||||
String sql = StrUtil.format("use `{}-{}`;", baseDatabase, BaseContext.getTenantId());
|
||||
log.debug("Run MysqlSchemaHandler, Sql: {}", sql);
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = conn.createStatement();
|
||||
statement.execute(sql);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
SqlUtil.runSql(conn, sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.common.base.enums.DataBaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.config.DynamicDataSourceConfig;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.Connection;
|
||||
@ -49,7 +50,7 @@ public class JdbcUtil {
|
||||
/**
|
||||
* 检查数据源连接可用性
|
||||
*
|
||||
* @param conn 数据源
|
||||
* @param conn 数据源
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean checkConnect(Connection conn) {
|
||||
@ -81,4 +82,28 @@ public class JdbcUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static CheckResult check(String driver, String url, String username, String password) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName(driver);
|
||||
//建立连接
|
||||
conn = DriverManager.getConnection(url, username, password);
|
||||
return new CheckResult(true, null);
|
||||
} catch (Exception e) {
|
||||
return new CheckResult(false, e.getMessage());
|
||||
} finally {
|
||||
IoUtil.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class CheckResult {
|
||||
private boolean result;
|
||||
private String msg;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(check("oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@//192.168.0.205:1521/ORCL", "system", "root"));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.qiaoba.common.database.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* SqlUtil
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/27 17:30
|
||||
*/
|
||||
public class SqlUtil {
|
||||
|
||||
public static void runSql(Connection connection, String sql) throws SQLException {
|
||||
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = connection.createStatement();
|
||||
statement.execute(sql);
|
||||
} finally {
|
||||
IoUtil.close(statement);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user