@@ -7,6 +7,8 @@ public interface SwConsts { | |||||
class SysParam { | class SysParam { | ||||
//系统启动完成 | //系统启动完成 | ||||
public static boolean SYS_STARTED = false; | public static boolean SYS_STARTED = false; | ||||
//运行的项目,多个用半角逗号分隔 | |||||
public static String RUN_PROJECTS = "bpm"; | |||||
} | } | ||||
//开发调试模式 | //开发调试模式 | ||||
boolean SYS_DEBUG = true; | boolean SYS_DEBUG = true; | ||||
@@ -3,9 +3,13 @@ package cc.smtweb.framework.core.db.cache; | |||||
import cc.smtweb.framework.core.annotation.SwCache; | import cc.smtweb.framework.core.annotation.SwCache; | ||||
import cc.smtweb.framework.core.cache.AbstractCache; | import cc.smtweb.framework.core.cache.AbstractCache; | ||||
import cc.smtweb.framework.core.cache.CacheManager; | import cc.smtweb.framework.core.cache.CacheManager; | ||||
import cc.smtweb.framework.core.common.SwConsts; | |||||
import cc.smtweb.framework.core.db.DbEngine; | import cc.smtweb.framework.core.db.DbEngine; | ||||
import cc.smtweb.framework.core.db.EntityDao; | import cc.smtweb.framework.core.db.EntityDao; | ||||
import cc.smtweb.framework.core.db.vo.ModelDatabase; | import cc.smtweb.framework.core.db.vo.ModelDatabase; | ||||
import cc.smtweb.framework.core.db.vo.ModelProject; | |||||
import cc.smtweb.framework.core.util.CommUtil; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import java.util.List; | import java.util.List; | ||||
@@ -32,7 +36,9 @@ public class ModelDatabaseCache extends AbstractCache<ModelDatabase> { | |||||
@Override | @Override | ||||
protected List<ModelDatabase> loadAll() { | protected List<ModelDatabase> loadAll() { | ||||
EntityDao<ModelDatabase> dao = DbEngine.getInstance().findDao(ModelDatabase.class); | EntityDao<ModelDatabase> dao = DbEngine.getInstance().findDao(ModelDatabase.class); | ||||
return dao.query(); | |||||
if (StringUtils.isEmpty(SwConsts.SysParam.RUN_PROJECTS)) | |||||
return dao.query(); | |||||
return dao.queryEx(" inner join " + ModelProject.ENTITY_NAME + " on db_prj_id=prj_id where prj_module in (" + CommUtil.getSqlInStrs(SwConsts.SysParam.RUN_PROJECTS.split(",")) + ")"); | |||||
} | } | ||||
public final ModelDatabase getByName(String key) { | public final ModelDatabase getByName(String key) { | ||||
@@ -41,6 +47,6 @@ public class ModelDatabaseCache extends AbstractCache<ModelDatabase> { | |||||
public final String getName(long id) { | public final String getName(long id) { | ||||
ModelDatabase db = get(id); | ModelDatabase db = get(id); | ||||
return db != null ? db.getName() : ""; | |||||
return db != null ? db.getName(): ""; | |||||
} | } | ||||
} | } |
@@ -3,9 +3,13 @@ package cc.smtweb.framework.core.db.cache; | |||||
import cc.smtweb.framework.core.annotation.SwCache; | import cc.smtweb.framework.core.annotation.SwCache; | ||||
import cc.smtweb.framework.core.cache.AbstractCache; | import cc.smtweb.framework.core.cache.AbstractCache; | ||||
import cc.smtweb.framework.core.cache.CacheManager; | import cc.smtweb.framework.core.cache.CacheManager; | ||||
import cc.smtweb.framework.core.common.SwConsts; | |||||
import cc.smtweb.framework.core.db.DbEngine; | import cc.smtweb.framework.core.db.DbEngine; | ||||
import cc.smtweb.framework.core.db.vo.ModelField; | import cc.smtweb.framework.core.db.vo.ModelField; | ||||
import cc.smtweb.framework.core.db.vo.ModelProject; | |||||
import cc.smtweb.framework.core.db.vo.ModelTable; | import cc.smtweb.framework.core.db.vo.ModelTable; | ||||
import cc.smtweb.framework.core.util.CommUtil; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.springframework.dao.DataAccessException; | import org.springframework.dao.DataAccessException; | ||||
import org.springframework.jdbc.core.ResultSetExtractor; | import org.springframework.jdbc.core.ResultSetExtractor; | ||||
@@ -48,6 +52,10 @@ public class ModelTableCache extends AbstractCache<ModelTable> { | |||||
@Override | @Override | ||||
protected List<ModelTable> loadAll() { | protected List<ModelTable> loadAll() { | ||||
String sql; | |||||
if (!StringUtils.isEmpty(SwConsts.SysParam.RUN_PROJECTS)) { | |||||
sql = " inner join " + ModelProject.ENTITY_NAME + " on tb_prj_id=prj_id where prj_module in (" + CommUtil.getSqlInStrs(SwConsts.SysParam.RUN_PROJECTS.split(",")) + ")"; | |||||
} else sql = ""; | |||||
return DbEngine.getInstance().query("SELECT\n" + | return DbEngine.getInstance().query("SELECT\n" + | ||||
"t.tb_id,\n" + | "t.tb_id,\n" + | ||||
"t.tb_prj_id,\n" + | "t.tb_prj_id,\n" + | ||||
@@ -64,7 +72,7 @@ public class ModelTableCache extends AbstractCache<ModelTable> { | |||||
"t.tb_update_uid,\n" + | "t.tb_update_uid,\n" + | ||||
"t.tb_create_at,\n" + | "t.tb_create_at,\n" + | ||||
"t.tb_update_at\n" + | "t.tb_update_at\n" + | ||||
"from asp_model_table t\n", new ResultSetExtractor<List<ModelTable>>() { | |||||
"from asp_model_table t\n" + sql, new ResultSetExtractor<List<ModelTable>>() { | |||||
@Override | @Override | ||||
public List<ModelTable> extractData(ResultSet rs) throws SQLException, DataAccessException { | public List<ModelTable> extractData(ResultSet rs) throws SQLException, DataAccessException { | ||||
List<ModelTable> list = new ArrayList<>(); | List<ModelTable> list = new ArrayList<>(); | ||||
@@ -2,6 +2,7 @@ package cc.smtweb.framework.core.db.impl; | |||||
import cc.smtweb.framework.core.common.SwEnum; | import cc.smtweb.framework.core.common.SwEnum; | ||||
import cc.smtweb.framework.core.db.DbEngine; | import cc.smtweb.framework.core.db.DbEngine; | ||||
import cc.smtweb.framework.core.db.EntityHelper; | |||||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | import cc.smtweb.framework.core.db.cache.ModelTableCache; | ||||
import cc.smtweb.framework.core.db.vo.ModelField; | import cc.smtweb.framework.core.db.vo.ModelField; | ||||
import cc.smtweb.framework.core.db.vo.ModelIndex; | import cc.smtweb.framework.core.db.vo.ModelIndex; | ||||
@@ -76,7 +77,7 @@ public class DatabaseUtil { | |||||
ModelTable entity = ModelTableIter.next(); | ModelTable entity = ModelTableIter.next(); | ||||
String entMessage = "(" + timer.timeSinceLast() + "ms) Checking #" + curEnt + "/" + totalEnt + " Entity " + entity.getName(); | String entMessage = "(" + timer.timeSinceLast() + "ms) Checking #" + curEnt + "/" + totalEnt + " Entity " + entity.getName(); | ||||
log.info(entMessage, MODULE); | log.info(entMessage, MODULE); | ||||
String k = entity.getName(); //表名 | |||||
String k = entity.getSchemaTableName().toUpperCase(); //表名 | |||||
// -make sure all entities have a corresponding table | // -make sure all entities have a corresponding table | ||||
if (tableNames.containsKey(k)) { | if (tableNames.containsKey(k)) { | ||||
IDatabaseInfo.TableCheckInfo table = tableNames.remove(k); | IDatabaseInfo.TableCheckInfo table = tableNames.remove(k); | ||||
@@ -88,7 +89,7 @@ public class DatabaseUtil { | |||||
ModelField field = entity.getFields().get(fnum); | ModelField field = entity.getFields().get(fnum); | ||||
fieldColNames.put(field.getName().toUpperCase(), field); // XML读出来是小写,数据库读取来是大写. | fieldColNames.put(field.getName().toUpperCase(), field); // XML读出来是小写,数据库读取来是大写. | ||||
} | } | ||||
List<IDatabaseInfo.ColumnCheckInfo> colList = colInfo.get(entity.getName()); | |||||
List<IDatabaseInfo.ColumnCheckInfo> colList = colInfo.get(k); | |||||
int numCols = 0; | int numCols = 0; | ||||
if (colList != null) { | if (colList != null) { | ||||
for (; numCols < colList.size(); numCols++) { | for (; numCols < colList.size(); numCols++) { | ||||
@@ -227,7 +228,8 @@ public class DatabaseUtil { | |||||
if (entity.getIndexes().isEmpty()) | if (entity.getIndexes().isEmpty()) | ||||
continue; | continue; | ||||
// get existing index map for this table | // get existing index map for this table | ||||
Map<String, Set<String>> exists_indexes = tableIndexListMap.get(entity.getName()); | |||||
String k = entity.getSchemaTableName().toUpperCase(); //表名 | |||||
Map<String, Set<String>> exists_indexes = tableIndexListMap.get(k); | |||||
Set<String> setEnabled = new HashSet<>();//有效的索引 | Set<String> setEnabled = new HashSet<>();//有效的索引 | ||||
for (ModelIndex mi: entity.getIndexes()) { | for (ModelIndex mi: entity.getIndexes()) { | ||||
if (SwEnum.IndexType.PK.value.equalsIgnoreCase(mi.getType())) continue; | if (SwEnum.IndexType.PK.value.equalsIgnoreCase(mi.getType())) continue; | ||||
@@ -170,7 +170,7 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo { | |||||
if (StringUtils.isNotEmpty(s)) { | if (StringUtils.isNotEmpty(s)) { | ||||
table.pk = s.substring(1); | table.pk = s.substring(1); | ||||
} | } | ||||
tables.put(tableName, table); | |||||
tables.put(catalog.toUpperCase() + "." + tableName, table); | |||||
} | } | ||||
} | } | ||||
} catch (SQLException sqle) { | } catch (SQLException sqle) { | ||||
@@ -249,7 +249,8 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo { | |||||
// ccInfo.tableName = lookupSchemaName + "." + ccInfo.tableName; | // ccInfo.tableName = lookupSchemaName + "." + ccInfo.tableName; | ||||
//} | //} | ||||
// ignore the column info if the table name is not in the list we are concerned with | // ignore the column info if the table name is not in the list we are concerned with | ||||
if (!dbTables.containsKey(ccInfo.tableName)) { | |||||
String k = catalog.toUpperCase() + "." + ccInfo.tableName; | |||||
if (!dbTables.containsKey(k)) { | |||||
continue; | continue; | ||||
} | } | ||||
ccInfo.columnName = rsCols.getString("COLUMN_NAME"); | ccInfo.columnName = rsCols.getString("COLUMN_NAME"); | ||||
@@ -262,11 +263,8 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo { | |||||
ccInfo.decimalDigits = rsCols.getInt("DECIMAL_DIGITS"); | ccInfo.decimalDigits = rsCols.getInt("DECIMAL_DIGITS"); | ||||
// NOTE: this may need a toUpperCase in some cases, keep an eye on it | // NOTE: this may need a toUpperCase in some cases, keep an eye on it | ||||
ccInfo.isNullable = rsCols.getString("IS_NULLABLE"); | ccInfo.isNullable = rsCols.getString("IS_NULLABLE"); | ||||
List<ColumnCheckInfo> tableColInfo = colInfo.get(ccInfo.tableName); | |||||
if (tableColInfo == null) { | |||||
tableColInfo = new ArrayList<>(); | |||||
colInfo.put(ccInfo.tableName, tableColInfo); | |||||
} | |||||
List<ColumnCheckInfo> tableColInfo = colInfo.computeIfAbsent(k, k1->new ArrayList<>()); | |||||
tableColInfo.add(ccInfo); | tableColInfo.add(ccInfo); | ||||
} | } | ||||
} catch (SQLException sqle) { | } catch (SQLException sqle) { | ||||
@@ -286,7 +284,8 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo { | |||||
final Map<String, Map<String, Set<String>>> indexInfo = new HashMap<>(); | final Map<String, Map<String, Set<String>>> indexInfo = new HashMap<>(); | ||||
if (!tableNames.isEmpty()) { | if (!tableNames.isEmpty()) { | ||||
for (TableCheckInfo c : tableNames) { | for (TableCheckInfo c : tableNames) { | ||||
indexInfo.put(c.tableName, new HashMap<>()); | |||||
String k = c.catalog.toUpperCase() + "." + c.tableName; | |||||
indexInfo.put(k, new HashMap<>()); | |||||
} | } | ||||
dbEngine.doConn(connection -> { | dbEngine.doConn(connection -> { | ||||
try { | try { | ||||
@@ -333,17 +332,14 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo { | |||||
// 不取nouniqune的, | // 不取nouniqune的, | ||||
if (!unique) continue; | if (!unique) continue; | ||||
} | } | ||||
String tableName = rsCols.getString("TABLE_NAME"); | |||||
if (needsUpperCase && tableName != null) { | |||||
tableName = tableName.toUpperCase(); | |||||
} | |||||
String k = curTableName.catalog.toUpperCase() + "." + curTableName.tableName; | |||||
String indexName = rsCols.getString("INDEX_NAME"); | String indexName = rsCols.getString("INDEX_NAME"); | ||||
if (needsUpperCase && indexName != null) { | if (needsUpperCase && indexName != null) { | ||||
indexName = indexName.toUpperCase(); | indexName = indexName.toUpperCase(); | ||||
} | } | ||||
Map<String, Set<String>> tableIndexList = indexInfo.computeIfAbsent(tableName, k -> new HashMap<>()); | |||||
Set<String> set = tableIndexList.computeIfAbsent(indexName, k -> new LinkedHashSet<>()); | |||||
Map<String, Set<String>> tableIndexList = indexInfo.computeIfAbsent(k, k1 -> new HashMap<>()); | |||||
Set<String> set = tableIndexList.computeIfAbsent(indexName, k1 -> new LinkedHashSet<>()); | |||||
// totalIndices++; | // totalIndices++; | ||||
set.add(rsCols.getString("COLUMN_NAME")); | set.add(rsCols.getString("COLUMN_NAME")); | ||||
} | } | ||||
@@ -286,7 +286,7 @@ public class ModelTable extends DefaultEntity { | |||||
public String getSchemaTableName() { | public String getSchemaTableName() { | ||||
String dbName = getDbName(); | String dbName = getDbName(); | ||||
if (StringUtils.isEmpty(dbName) || dbName.equals(SwConsts.DEF_DB_NAME)) return getName(); | |||||
if (StringUtils.isEmpty(dbName) || dbName.equals(SwConsts.DEF_DB_NAME)) return DbEngine.getInstance().getDbSchema() + "." + getName(); | |||||
return DbEngine.getInstance().getDbSchema() + "_" + dbName + "." + getName(); | return DbEngine.getInstance().getDbSchema() + "_" + dbName + "." + getName(); | ||||
} | } | ||||
} | } |
@@ -4,6 +4,7 @@ import cc.smtweb.framework.core.db.impl.BaseBean; | |||||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | import cc.smtweb.framework.core.db.impl.DefaultEntity; | ||||
import cc.smtweb.framework.core.util.kryo.KryoTool; | import cc.smtweb.framework.core.util.kryo.KryoTool; | ||||
import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||
import org.apache.commons.lang3.StringUtils; | |||||
import java.io.Serializable; | import java.io.Serializable; | ||||
import java.lang.reflect.ParameterizedType; | import java.lang.reflect.ParameterizedType; | ||||
@@ -104,12 +105,40 @@ public class CommUtil { | |||||
StringBuilder s = new StringBuilder(128); | StringBuilder s = new StringBuilder(128); | ||||
Set<Long> set = new HashSet<>(); | Set<Long> set = new HashSet<>(); | ||||
for (Long id : ids) { | for (Long id : ids) { | ||||
if (id <= 0L) continue; | |||||
if (set.contains(id)) continue; | if (set.contains(id)) continue; | ||||
set.add(id); | set.add(id); | ||||
if (id <= 0) continue; | |||||
s.append(id).append(","); | s.append(id).append(","); | ||||
} | } | ||||
if (s.length() == 0) return ""; | if (s.length() == 0) return ""; | ||||
return s.substring(0, s.length() - 1); | return s.substring(0, s.length() - 1); | ||||
} | } | ||||
public static String getSqlInStrs(Collection<String> ids) { | |||||
if (null == ids || ids.isEmpty()) return ""; | |||||
StringBuilder s = new StringBuilder(128); | |||||
Set<String> set = new HashSet<>(); | |||||
for (String id : ids) { | |||||
if (StringUtils.isEmpty(id)) continue; | |||||
if (set.contains(id)) continue; | |||||
set.add(id); | |||||
s.append("'").append(id).append("',"); | |||||
} | |||||
if (s.length() == 0) return ""; | |||||
return s.substring(0, s.length() - 1); | |||||
} | |||||
public static String getSqlInStrs(String[] ids) { | |||||
if (null == ids || ids.length == 0) return ""; | |||||
StringBuilder s = new StringBuilder(128); | |||||
Set<String> set = new HashSet<>(); | |||||
for (String id : ids) { | |||||
if (StringUtils.isEmpty(id)) continue; | |||||
if (set.contains(id)) continue; | |||||
set.add(id); | |||||
s.append("'").append(id).append("',"); | |||||
} | |||||
if (s.length() == 0) return ""; | |||||
return s.substring(0, s.length() - 1); | |||||
} | |||||
} | } |