【7.0.4】更新数据库操作工具类

pull/19/head
fengshuonan 2021-05-26 21:37:52 +08:00
parent d5cf6a78df
commit 73bdf28e97
7 changed files with 142 additions and 11 deletions

View File

@ -46,6 +46,14 @@
<version>${roses.version}</version>
</dependency>
<!--数据源模块的api-->
<!--需要用到租户数据源名称的常量-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>ds-container-api</artifactId>
<version>${roses.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -58,7 +58,12 @@ public enum DatabaseExceptionEnum implements AbstractExceptionEnum {
/**
* sql
*/
SQL_EXEC_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "04", "sql执行错误具体信息{}");
SQL_EXEC_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "04", "sql执行错误具体信息{}"),
/**
*
*/
DATABASE_LIST_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + DbConstants.DB_EXCEPTION_STEP_CODE + "05", "查询所有库错误,具体信息:{}");
/**

View File

@ -0,0 +1,58 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.db.api.sqladapter.database;
import cn.stylefeng.roses.kernel.db.api.sqladapter.AbstractSql;
import lombok.Getter;
/**
* sql
*
* @author fengshuonan
* @date 2019-07-16-13:06
*/
@Getter
public class GetDatabasesSql extends AbstractSql {
@Override
protected String mysql() {
return "show databases;";
}
@Override
protected String sqlServer() {
return "";
}
@Override
protected String pgSql() {
return "";
}
@Override
protected String oracle() {
return "";
}
}

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.db.api.util;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.exception.DaoException;
import cn.stylefeng.roses.kernel.db.api.exception.enums.DatabaseExceptionEnum;
@ -7,8 +8,10 @@ import cn.stylefeng.roses.kernel.db.api.pojo.db.TableFieldInfo;
import cn.stylefeng.roses.kernel.db.api.pojo.db.TableInfo;
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
import cn.stylefeng.roses.kernel.db.api.sqladapter.database.CreateDatabaseSql;
import cn.stylefeng.roses.kernel.db.api.sqladapter.database.GetDatabasesSql;
import cn.stylefeng.roses.kernel.db.api.sqladapter.table.TableFieldListSql;
import cn.stylefeng.roses.kernel.db.api.sqladapter.table.TableListSql;
import cn.stylefeng.roses.kernel.dsctn.api.constants.DatasourceContainerConstants;
import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
@ -27,6 +30,39 @@ import java.util.List;
@Slf4j
public class DatabaseUtil {
/**
*
*
* @author fengshuonan
* @date 2021/5/26 20:42
*/
public static List<String> getDatabases(DruidProperties druidProperties) {
Connection conn = null;
List<String> databasesList = new ArrayList<>();
try {
Class.forName(druidProperties.getDriverClassName());
conn = DriverManager.getConnection(
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
PreparedStatement preparedStatement = conn.prepareStatement(new GetDatabasesSql().getSql(druidProperties.getUrl()));
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String database = resultSet.getString("database");
if (StrUtil.isNotBlank(database)) {
if (StrUtil.startWith(database, DatasourceContainerConstants.TENANT_DB_PREFIX)) {
database = database.replaceAll(DatasourceContainerConstants.TENANT_DB_PREFIX, "");
databasesList.add(database);
}
}
}
return databasesList;
} catch (Exception e) {
log.error("查询所有库错误!", e);
throw new DaoException(DatabaseExceptionEnum.DATABASE_LIST_ERROR, e.getMessage());
} finally {
IoUtil.close(conn);
}
}
/**
*
*
@ -35,9 +71,10 @@ public class DatabaseUtil {
*/
public static List<TableInfo> selectTables(DruidProperties druidProperties) {
List<TableInfo> tables = new ArrayList<>();
Connection conn = null;
try {
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
conn = DriverManager.getConnection(
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
// 获取数据库名称
@ -65,6 +102,8 @@ public class DatabaseUtil {
} catch (Exception ex) {
log.error("查询所有表错误!", ex);
throw new DaoException(DatabaseExceptionEnum.TABLE_LIST_ERROR, ex.getMessage());
} finally {
IoUtil.close(conn);
}
}
@ -76,9 +115,10 @@ public class DatabaseUtil {
*/
public static List<TableFieldInfo> getTableFields(DruidProperties druidProperties, String tableName) {
ArrayList<TableFieldInfo> fieldList = new ArrayList<>();
Connection conn = null;
try {
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
conn = DriverManager.getConnection(
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
PreparedStatement preparedStatement = conn.prepareStatement(new TableFieldListSql().getSql(druidProperties.getUrl()));
@ -111,6 +151,8 @@ public class DatabaseUtil {
} catch (Exception ex) {
log.error("查询表的所有字段错误!", ex);
throw new DaoException(DatabaseExceptionEnum.FIELD_GET_ERROR, ex.getMessage());
} finally {
IoUtil.close(conn);
}
}
@ -121,9 +163,10 @@ public class DatabaseUtil {
* @date 2021/5/19 10:39
*/
public static void createDatabase(DruidProperties druidProperties, String databaseName) {
Connection conn = null;
try {
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
conn = DriverManager.getConnection(druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
//创建sql
String sql = new CreateDatabaseSql().getSql(druidProperties.getUrl());
@ -137,6 +180,8 @@ public class DatabaseUtil {
} catch (Exception ex) {
log.error("执行sql出现问题", ex);
throw new DaoException(DatabaseExceptionEnum.CREATE_DATABASE_ERROR, ex.getMessage());
} finally {
IoUtil.close(conn);
}
}

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.db.api.util;
import cn.hutool.core.io.IoUtil;
import cn.stylefeng.roses.kernel.db.api.exception.DaoException;
import cn.stylefeng.roses.kernel.db.api.exception.enums.DatabaseExceptionEnum;
import lombok.extern.slf4j.Slf4j;
@ -29,9 +30,10 @@ public class SqlRunUtil {
* @date 2021/5/19 10:52
*/
public static void runClassPathSql(String classpathFileName, String driverClassName, String url, String username, String password) {
Connection conn = null;
try {
Class.forName(driverClassName);
Connection conn = DriverManager.getConnection(url, username, password);
conn = DriverManager.getConnection(url, username, password);
ClassPathResource classPathResource = new ClassPathResource(classpathFileName);
EncodedResource encodedResource = new EncodedResource(classPathResource, "utf-8");
@ -39,8 +41,9 @@ public class SqlRunUtil {
} catch (Exception e) {
log.error("执行sql错误", e);
throw new DaoException(DatabaseExceptionEnum.SQL_EXEC_ERROR, e.getMessage());
} finally {
IoUtil.close(conn);
}
}
/**
@ -50,9 +53,10 @@ public class SqlRunUtil {
* @date 2021/5/19 10:52
*/
public static void runFileSystemSql(SqlSessionFactory sqlSessionFactory, String sqlPath) {
Connection conn = null;
try {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection conn = sqlSession.getConnection();
conn = sqlSession.getConnection();
FileSystemResource classPathResource = new FileSystemResource(sqlPath);
EncodedResource encodedResource = new EncodedResource(classPathResource, "GBK");
@ -60,6 +64,8 @@ public class SqlRunUtil {
} catch (Exception e) {
log.error("执行sql错误", e);
throw new DaoException(DatabaseExceptionEnum.SQL_EXEC_ERROR, e.getMessage());
} finally {
IoUtil.close(conn);
}
}

View File

@ -24,7 +24,6 @@
<version>${roses.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -25,6 +25,7 @@
package cn.stylefeng.roses.kernel.dsctn.persist;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
import cn.stylefeng.roses.kernel.dsctn.api.exception.DatasourceContainerException;
@ -66,9 +67,10 @@ public class DataBaseInfoPersistence {
*/
public Map<String, DruidProperties> getAllDataBaseInfo() {
Map<String, DruidProperties> dataSourceList = new HashMap<>(16);
Connection conn = null;
try {
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
conn = DriverManager.getConnection(
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
PreparedStatement preparedStatement = conn.prepareStatement(new DatabaseListSql().getSql(druidProperties.getUrl()));
@ -86,6 +88,8 @@ public class DataBaseInfoPersistence {
log.error("查询数据源信息错误!", exception);
String userTip = StrUtil.format(QUERY_DBS_DAO_ERROR.getUserTip(), exception.getMessage());
throw new DatasourceContainerException(QUERY_DBS_DAO_ERROR, userTip);
} finally {
IoUtil.close(conn);
}
}
@ -96,9 +100,10 @@ public class DataBaseInfoPersistence {
* @date 2020/10/31 23:55
*/
public void createMasterDatabaseInfo() {
Connection conn = null;
try {
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
conn = DriverManager.getConnection(
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
PreparedStatement preparedStatement = conn.prepareStatement(new AddDatabaseInfoSql().getSql(druidProperties.getUrl()));
@ -119,6 +124,8 @@ public class DataBaseInfoPersistence {
log.error("初始化master的databaseInfo信息错误", exception);
String userTip = StrUtil.format(INSERT_DBS_DAO_ERROR.getUserTip(), exception.getMessage());
throw new DatasourceContainerException(INSERT_DBS_DAO_ERROR, userTip);
} finally {
IoUtil.close(conn);
}
}
@ -129,9 +136,10 @@ public class DataBaseInfoPersistence {
* @date 2020/10/31 23:55
*/
public void deleteMasterDatabaseInfo() {
Connection conn = null;
try {
Class.forName(druidProperties.getDriverClassName());
Connection conn = DriverManager.getConnection(
conn = DriverManager.getConnection(
druidProperties.getUrl(), druidProperties.getUsername(), druidProperties.getPassword());
PreparedStatement preparedStatement = conn.prepareStatement(new DeleteDatabaseInfoSql().getSql(druidProperties.getUrl()));
@ -142,6 +150,8 @@ public class DataBaseInfoPersistence {
log.info("删除master的databaseInfo信息失败", exception);
String userTip = StrUtil.format(DELETE_DBS_DAO_ERROR.getUserTip(), exception.getMessage());
throw new DatasourceContainerException(DELETE_DBS_DAO_ERROR, userTip);
} finally {
IoUtil.close(conn);
}
}