cloud.agileframework.common.util.db.DataBaseUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of agile-sql Show documentation
Show all versions of agile-sql Show documentation
Cache util for Spring Boot
package cloud.agileframework.common.util.db;
import cloud.agileframework.common.constant.Constant;
import cloud.agileframework.common.util.clazz.TypeReference;
import cloud.agileframework.common.util.object.ObjectUtil;
import cloud.agileframework.common.util.pattern.PatternUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Stream;
/**
* @author 佟盟 on 2018/9/7
*/
public class DataBaseUtil {
private static final Logger logger = LoggerFactory.getLogger(DataBaseUtil.class);
private static Connection conn;
private static ResultSet getResultSet(PATTERN type, String url, String username, String password, String pattern) {
DBInfo dbInfo = parseDBUrl(url);
if (dbInfo == null) {
throw new RuntimeException("无法识别的数据库类型");
}
try {
conn = getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
pattern = pattern == null ? "%" : pattern;
ResultSet rs = null;
try {
// 获取Meta信息对象
DatabaseMetaData meta = conn.getMetaData();
// 数据库的用户
String schemaPattern = null;
String catalog = null;
switch (type) {
case TABLE:
String[] types = {"TABLE", "VIEW"};
if (dbInfo.type == DB.ORACLE) {
schemaPattern = username;
if (null != schemaPattern) {
schemaPattern = schemaPattern.toUpperCase();
}
rs = meta.getTables(null, schemaPattern, pattern, types);
} else if (dbInfo.type == DB.MYSQL) {
schemaPattern = dbInfo.getName();
rs = meta.getTables(schemaPattern, schemaPattern, pattern, types);
} else if (dbInfo.type == DB.DB2) {
schemaPattern = dbInfo.getName();
rs = meta.getTables(null, schemaPattern, pattern, types);
} else {
rs = meta.getTables(null, null, pattern, types);
}
break;
case COLUMN:
// pattern = pattern.toUpperCase();
String columnNamePattern = null;
if (DB.ORACLE == dbInfo.type) {
schemaPattern = username;
if (null != schemaPattern) {
schemaPattern = schemaPattern.toUpperCase();
}
} else if (DB.MYSQL == dbInfo.type) {
catalog = dbInfo.getName();
schemaPattern = dbInfo.getName();
}
rs = meta.getColumns(catalog, schemaPattern, pattern, columnNamePattern);
break;
case PRIMARY_KEY:
pattern = pattern.toUpperCase();
if (DB.ORACLE == dbInfo.type) {
schemaPattern = username;
if (null != schemaPattern) {
schemaPattern = schemaPattern.toUpperCase();
}
}
rs = meta.getPrimaryKeys(null, schemaPattern, pattern);
break;
case FK_EXPORT:
pattern = pattern.toUpperCase();
if (DB.ORACLE == dbInfo.type) {
schemaPattern = username;
if (null != schemaPattern) {
schemaPattern = schemaPattern.toUpperCase();
}
}
rs = meta.getExportedKeys(null, schemaPattern, pattern);
break;
case FK_IMPORT:
pattern = pattern.toUpperCase();
if (DB.ORACLE == dbInfo.type) {
schemaPattern = username;
if (null != schemaPattern) {
schemaPattern = schemaPattern.toUpperCase();
}
}
rs = meta.getImportedKeys(null, schemaPattern, pattern);
break;
default:
}
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
/**
* 列出数据库的所有表
*/
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy