Bläddra i källkod

系统:增加SwConsts.SysParam.RUN_PROJECTS,启动时只读取指定项目的表定义

4.0
郑根木 2 år sedan
förälder
incheckning
73e780f9ae
7 ändrade filer med 65 tillägg och 22 borttagningar
  1. +2
    -0
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/common/SwConsts.java
  2. +8
    -2
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/cache/ModelDatabaseCache.java
  3. +9
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/cache/ModelTableCache.java
  4. +5
    -3
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/impl/DatabaseUtil.java
  5. +10
    -14
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/impl/DefaultDatabaseInfoImpl.java
  6. +1
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/vo/ModelTable.java
  7. +30
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/CommUtil.java

+ 2
- 0
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/common/SwConsts.java Visa fil

@@ -7,6 +7,8 @@ public interface SwConsts {
class SysParam {
//系统启动完成
public static boolean SYS_STARTED = false;
//运行的项目,多个用半角逗号分隔
public static String RUN_PROJECTS = "bpm";
}
//开发调试模式
boolean SYS_DEBUG = true;


+ 8
- 2
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/cache/ModelDatabaseCache.java Visa fil

@@ -3,9 +3,13 @@ package cc.smtweb.framework.core.db.cache;
import cc.smtweb.framework.core.annotation.SwCache;
import cc.smtweb.framework.core.cache.AbstractCache;
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.EntityDao;
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;

@@ -32,7 +36,9 @@ public class ModelDatabaseCache extends AbstractCache<ModelDatabase> {
@Override
protected List<ModelDatabase> loadAll() {
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) {
@@ -41,6 +47,6 @@ public class ModelDatabaseCache extends AbstractCache<ModelDatabase> {

public final String getName(long id) {
ModelDatabase db = get(id);
return db != null ? db.getName() : "";
return db != null ? db.getName(): "";
}
}

+ 9
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/cache/ModelTableCache.java Visa fil

@@ -3,9 +3,13 @@ package cc.smtweb.framework.core.db.cache;
import cc.smtweb.framework.core.annotation.SwCache;
import cc.smtweb.framework.core.cache.AbstractCache;
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.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelProject;
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.jdbc.core.ResultSetExtractor;

@@ -48,6 +52,10 @@ public class ModelTableCache extends AbstractCache<ModelTable> {

@Override
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" +
"t.tb_id,\n" +
"t.tb_prj_id,\n" +
@@ -64,7 +72,7 @@ public class ModelTableCache extends AbstractCache<ModelTable> {
"t.tb_update_uid,\n" +
"t.tb_create_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
public List<ModelTable> extractData(ResultSet rs) throws SQLException, DataAccessException {
List<ModelTable> list = new ArrayList<>();


+ 5
- 3
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/impl/DatabaseUtil.java Visa fil

@@ -2,6 +2,7 @@ package cc.smtweb.framework.core.db.impl;

import cc.smtweb.framework.core.common.SwEnum;
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.vo.ModelField;
import cc.smtweb.framework.core.db.vo.ModelIndex;
@@ -76,7 +77,7 @@ public class DatabaseUtil {
ModelTable entity = ModelTableIter.next();
String entMessage = "(" + timer.timeSinceLast() + "ms) Checking #" + curEnt + "/" + totalEnt + " Entity " + entity.getName();
log.info(entMessage, MODULE);
String k = entity.getName(); //表名
String k = entity.getSchemaTableName().toUpperCase(); //表名
// -make sure all entities have a corresponding table
if (tableNames.containsKey(k)) {
IDatabaseInfo.TableCheckInfo table = tableNames.remove(k);
@@ -88,7 +89,7 @@ public class DatabaseUtil {
ModelField field = entity.getFields().get(fnum);
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;
if (colList != null) {
for (; numCols < colList.size(); numCols++) {
@@ -227,7 +228,8 @@ public class DatabaseUtil {
if (entity.getIndexes().isEmpty())
continue;
// 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<>();//有效的索引
for (ModelIndex mi: entity.getIndexes()) {
if (SwEnum.IndexType.PK.value.equalsIgnoreCase(mi.getType())) continue;


+ 10
- 14
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/impl/DefaultDatabaseInfoImpl.java Visa fil

@@ -170,7 +170,7 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo {
if (StringUtils.isNotEmpty(s)) {
table.pk = s.substring(1);
}
tables.put(tableName, table);
tables.put(catalog.toUpperCase() + "." + tableName, table);
}
}
} catch (SQLException sqle) {
@@ -249,7 +249,8 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo {
// ccInfo.tableName = lookupSchemaName + "." + ccInfo.tableName;
//}
// 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;
}
ccInfo.columnName = rsCols.getString("COLUMN_NAME");
@@ -262,11 +263,8 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo {
ccInfo.decimalDigits = rsCols.getInt("DECIMAL_DIGITS");
// NOTE: this may need a toUpperCase in some cases, keep an eye on it
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);
}
} catch (SQLException sqle) {
@@ -286,7 +284,8 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo {
final Map<String, Map<String, Set<String>>> indexInfo = new HashMap<>();
if (!tableNames.isEmpty()) {
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 -> {
try {
@@ -333,17 +332,14 @@ public class DefaultDatabaseInfoImpl implements IDatabaseInfo {
// 不取nouniqune的,
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");
if (needsUpperCase && indexName != null) {
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++;
set.add(rsCols.getString("COLUMN_NAME"));
}


+ 1
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/vo/ModelTable.java Visa fil

@@ -286,7 +286,7 @@ public class ModelTable extends DefaultEntity {

public String getSchemaTableName() {
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();
}
}

+ 30
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/CommUtil.java Visa fil

@@ -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.util.kryo.KryoTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
@@ -104,12 +105,40 @@ public class CommUtil {
StringBuilder s = new StringBuilder(128);
Set<Long> set = new HashSet<>();
for (Long id : ids) {
if (id <= 0L) continue;
if (set.contains(id)) continue;
set.add(id);
if (id <= 0) continue;
s.append(id).append(",");
}
if (s.length() == 0) return "";
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);
}
}

Laddar…
Avbryt
Spara