@@ -3,6 +3,8 @@ package cc.smtweb.framework.core.common; | |||||
import lombok.Data; | import lombok.Data; | ||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import java.sql.Types; | |||||
/** | /** | ||||
* Created by Akmm at 2022/3/23 9:39 | * Created by Akmm at 2022/3/23 9:39 | ||||
* 系统的一些枚举变量 | * 系统的一些枚举变量 | ||||
@@ -85,10 +87,12 @@ public interface SwEnum { | |||||
public String sqlType; | public String sqlType; | ||||
public int dataLength; | public int dataLength; | ||||
public String javaType; | public String javaType; | ||||
//java.sql.Types里的值 | |||||
public int type; | |||||
public String defaultValue; | public String defaultValue; | ||||
public String editor; | public String editor; | ||||
public DataTypeBean(String value, String name, String sqlType, int dataLength, String javaType, String defaultValue, String editor) { | |||||
public DataTypeBean(String value, String name, String sqlType, int dataLength, String javaType, int type, String defaultValue, String editor) { | |||||
super(value, name); | super(value, name); | ||||
this.sqlType = sqlType; | this.sqlType = sqlType; | ||||
this.dataLength = dataLength; | this.dataLength = dataLength; | ||||
@@ -112,26 +116,26 @@ public interface SwEnum { | |||||
public final static String TYPE_DATETIME = "datetime"; | public final static String TYPE_DATETIME = "datetime"; | ||||
public static DataType instance = new DataType(); | public static DataType instance = new DataType(); | ||||
public static DataTypeBean ID = instance.addEnum("id", "ID", "bigint", 0, "long", "", EditorType.INPUT.value); | |||||
public static DataTypeBean CODE = instance.addEnum("code", "编码", "varchar", 32, "string", "", EditorType.INPUT.value); | |||||
public static DataTypeBean NAME = instance.addEnum("name", "名称", "varchar", 100, "string", "", EditorType.INPUT.value); | |||||
public static DataTypeBean REMARK = instance.addEnum("remark", "备注", "varchar", 255, "string", "", EditorType.INPUT.value); | |||||
public static DataTypeBean TEXT = instance.addEnum("text", "大文本", "text", 0, "string", "", EditorType.TEXT.value); | |||||
public static DataTypeBean INT = instance.addEnum("int", "整型", "int", 0, "int", "0", EditorType.NUMBER.value); | |||||
public static DataTypeBean SHORT = instance.addEnum("short", "短整型", "smallint", 0, "short", "0", EditorType.NUMBER.value); | |||||
public static DataTypeBean BOOL = instance.addEnum("bool", "布尔型", "tinyint", 0, "boolean", "0", EditorType.COMBO.value); | |||||
public static DataTypeBean CURRENCY = instance.addEnum("currency", "金额型", "bigint", 0, "long", "0", EditorType.NUMBER.value); | |||||
public static DataTypeBean DATE = instance.addEnum("date", "日期型", "bigint", 0, "long", "0", EditorType.DATE.value); | |||||
public static DataTypeBean TIME = instance.addEnum("time", "时间型", "bigint", 0, "long", "0", EditorType.TIME.value); | |||||
public static DataTypeBean DATETIME = instance.addEnum("datetime", "日期时间型", "bigint", 0, "long", "0", EditorType.DATETIME.value); | |||||
public static DataTypeBean ID = instance.addEnum("id", "ID", "bigint", 0, "long", Types.BIGINT, "", EditorType.INPUT.value); | |||||
public static DataTypeBean CODE = instance.addEnum("code", "编码", "varchar", 32, "string", Types.VARCHAR, "", EditorType.INPUT.value); | |||||
public static DataTypeBean NAME = instance.addEnum("name", "名称", "varchar", 100, "string", Types.VARCHAR, "", EditorType.INPUT.value); | |||||
public static DataTypeBean REMARK = instance.addEnum("remark", "备注", "varchar", 255, "string", Types.VARCHAR, "", EditorType.INPUT.value); | |||||
public static DataTypeBean TEXT = instance.addEnum("text", "大文本", "text", 0, "string", Types.CLOB, "", EditorType.TEXT.value); | |||||
public static DataTypeBean INT = instance.addEnum("int", "整型", "int", 0, "int", Types.INTEGER, "0", EditorType.NUMBER.value); | |||||
public static DataTypeBean SHORT = instance.addEnum("short", "短整型", "smallint", 0, "short", Types.SMALLINT, "0", EditorType.NUMBER.value); | |||||
public static DataTypeBean BOOL = instance.addEnum("bool", "布尔型", "tinyint", 0, "boolean", Types.TINYINT, "0", EditorType.COMBO.value); | |||||
public static DataTypeBean CURRENCY = instance.addEnum("currency", "金额型", "bigint", 0, "long", Types.BIGINT, "0", EditorType.NUMBER.value); | |||||
public static DataTypeBean DATE = instance.addEnum("date", "日期型", "bigint", 0, "long", Types.BIGINT, "0", EditorType.DATE.value); | |||||
public static DataTypeBean TIME = instance.addEnum("time", "时间型", "bigint", 0, "long", Types.BIGINT, "0", EditorType.TIME.value); | |||||
public static DataTypeBean DATETIME = instance.addEnum("datetime", "日期时间型", "bigint", 0, "long", Types.BIGINT, "0", EditorType.DATETIME.value); | |||||
@Override | @Override | ||||
protected DataTypeBean buildBean(String value, String name) { | protected DataTypeBean buildBean(String value, String name) { | ||||
return null; | return null; | ||||
} | } | ||||
public DataTypeBean addEnum(String value, String name, String sqlType, int dataLength, String javaType, String defaultValue, String editor) { | |||||
final DataTypeBean bean = new DataTypeBean(value, name, sqlType, dataLength, javaType, defaultValue, editor); | |||||
public DataTypeBean addEnum(String value, String name, String sqlType, int dataLength, String javaType, int type, String defaultValue, String editor) { | |||||
final DataTypeBean bean = new DataTypeBean(value, name, sqlType, dataLength, javaType, type, defaultValue, editor); | |||||
mapAll.put(value, bean); | mapAll.put(value, bean); | ||||
return bean; | return bean; | ||||
} | } | ||||
@@ -141,6 +145,15 @@ public interface SwEnum { | |||||
if (value == null) return null; | if (value == null) return null; | ||||
return super.getByValue(value.toLowerCase()); | return super.getByValue(value.toLowerCase()); | ||||
} | } | ||||
//根据数据库查询的metadata适配类型 | |||||
public static DataTypeBean getBySqlType(int sqlType, int precision, int scale) { | |||||
for (DataTypeBean bean: instance.mapAll.values()) { | |||||
if (bean.type != sqlType) continue; | |||||
if (bean.dataLength == 0 || bean.dataLength == precision) return bean; | |||||
} | |||||
return REMARK; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
@@ -18,7 +18,6 @@ import java.util.*; | |||||
* 默认实体实现 | * 默认实体实现 | ||||
*/ | */ | ||||
public class DefaultComboHandler<T extends DefaultEntity> extends DefaultListHandler<T> { | public class DefaultComboHandler<T extends DefaultEntity> extends DefaultListHandler<T> { | ||||
protected String tableName; | |||||
public DefaultComboHandler(String tableName) { | public DefaultComboHandler(String tableName) { | ||||
super(tableName); | super(tableName); | ||||
@@ -43,7 +42,7 @@ public class DefaultComboHandler<T extends DefaultEntity> extends DefaultListHan | |||||
buildFilterCondition(text, sqlFilter, args); | buildFilterCondition(text, sqlFilter, args); | ||||
if (sqlFilter.length() == 0) throw new SwException("没有待搜索的字段!"); | if (sqlFilter.length() == 0) throw new SwException("没有待搜索的字段!"); | ||||
String sql = "select ar.* from (" + sqlPara.sql + ") ar where " + sqlFilter.substring(5); | |||||
String sql = "select ar.* from (" + sqlPara.sql + ") ar where " + sqlFilter.substring(4); | |||||
if (StringUtils.isNotEmpty(sort)) { | if (StringUtils.isNotEmpty(sort)) { | ||||
sql += " order by ar." + sort + " " + order; | sql += " order by ar." + sort + " " + order; | ||||
if (StringUtils.isNotEmpty(getPkFieldName())) { | if (StringUtils.isNotEmpty(getPkFieldName())) { | ||||
@@ -64,7 +63,7 @@ public class DefaultComboHandler<T extends DefaultEntity> extends DefaultListHan | |||||
text = text + "%";//combo不支持全模糊 | text = text + "%";//combo不支持全模糊 | ||||
for (String field: fields) { | for (String field: fields) { | ||||
sqlFilter.append(" and " + field + " like ?"); | |||||
sqlFilter.append(" or " + field + " like ?"); | |||||
args.add(text); | args.add(text); | ||||
} | } | ||||
@@ -10,16 +10,16 @@ import java.util.List; | |||||
public class SwListData { | public class SwListData { | ||||
public static final SwListData EMPTY = new SwListData(new ArrayList<>(), 0); | public static final SwListData EMPTY = new SwListData(new ArrayList<>(), 0); | ||||
private final List<SwMap> rows; | |||||
private final List rows; | |||||
// 总数, -1 表示需要异步获取数量, >=0 表示总数 | // 总数, -1 表示需要异步获取数量, >=0 表示总数 | ||||
private int total; | |||||
private int total = 0; | |||||
private SwListData(List<SwMap> rows, int total) { | |||||
private SwListData(List rows, int total) { | |||||
this.rows = rows; | this.rows = rows; | ||||
this.total = total; | this.total = total; | ||||
} | } | ||||
public static SwListData create(List<SwMap> list, int rows) { | |||||
public static SwListData create(List list, int rows) { | |||||
if (list == null) { | if (list == null) { | ||||
return SwListData.EMPTY; | return SwListData.EMPTY; | ||||
} | } | ||||
@@ -0,0 +1,18 @@ | |||||
package cc.smtweb.framework.core.util; | |||||
/** | |||||
* Created by Akmm at 2022/5/20 16:57 | |||||
* sql工具类 | |||||
*/ | |||||
public class SqlUtil { | |||||
/** | |||||
* 将sql中的表名替换成schema.table的格式 | |||||
* @param sql | |||||
* @return | |||||
*/ | |||||
public static String replaceTable(String sql) { | |||||
return sql; | |||||
} | |||||
} |
@@ -1,11 +1,23 @@ | |||||
package cc.smtweb.system.bpm.web.design.form; | package cc.smtweb.system.bpm.web.design.form; | ||||
import cc.smtweb.framework.core.common.R; | import cc.smtweb.framework.core.common.R; | ||||
import cc.smtweb.framework.core.common.SwEnum; | |||||
import cc.smtweb.framework.core.common.SwMap; | import cc.smtweb.framework.core.common.SwMap; | ||||
import cc.smtweb.framework.core.annotation.SwBody; | import cc.smtweb.framework.core.annotation.SwBody; | ||||
import cc.smtweb.framework.core.annotation.SwService; | import cc.smtweb.framework.core.annotation.SwService; | ||||
import cc.smtweb.framework.core.db.DbEngine; | |||||
import cc.smtweb.framework.core.mvc.service.*; | import cc.smtweb.framework.core.mvc.service.*; | ||||
import cc.smtweb.framework.core.session.UserSession; | import cc.smtweb.framework.core.session.UserSession; | ||||
import cc.smtweb.framework.core.util.SqlUtil; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.springframework.dao.DataAccessException; | |||||
import org.springframework.jdbc.core.ResultSetExtractor; | |||||
import java.sql.ResultSet; | |||||
import java.sql.ResultSetMetaData; | |||||
import java.sql.SQLException; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
/** | /** | ||||
* Created by Akmm at 2022/3/22 9:12 | * Created by Akmm at 2022/3/22 9:12 | ||||
@@ -87,4 +99,32 @@ public class ModelFormService extends AbstractCompService { | |||||
return R.error("操作失败!", e); | return R.error("操作失败!", e); | ||||
} | } | ||||
} | } | ||||
//获取自定义sql的字段信息,去库里查 | |||||
public R loadSqlFields(@SwBody SwMap params, UserSession us) { | |||||
try { | |||||
String sql = params.readString("sql"); | |||||
if (StringUtils.isEmpty(sql)) return R.error("没有传入的sql!"); | |||||
sql = sql.trim().toLowerCase(); | |||||
if (!sql.startsWith("select ")) return R.error("非查询类sql,禁止执行!"); | |||||
if (sql.contains(";")) return R.error("sql内禁止出现分号!"); | |||||
sql = SqlUtil.replaceTable(sql); | |||||
List<SwMap> ret = DbEngine.getInstance().query(sql + " where 1=0", rs -> { | |||||
List<SwMap> fields = new ArrayList<>(); | |||||
ResultSetMetaData metaData = rs.getMetaData(); | |||||
for (int i = 1, count = metaData.getColumnCount(); i <= count; i++) { | |||||
SwMap col = new SwMap(2); | |||||
col.put("name", metaData.getColumnLabel(i)); | |||||
SwEnum.DataTypeBean dtb = SwEnum.DataType.getBySqlType(metaData.getColumnType(i), metaData.getPrecision(i), metaData.getScale(i)); | |||||
col.put("dataType", dtb.value); | |||||
fields.add(col); | |||||
} | |||||
return fields; | |||||
}); | |||||
return R.success(ret); | |||||
} catch (Exception e) { | |||||
return R.error("操作失败!", e); | |||||
} | |||||
} | |||||
} | } |
@@ -1,9 +1,14 @@ | |||||
package cc.smtweb.system.bpm.web.design.table; | package cc.smtweb.system.bpm.web.design.table; | ||||
import cc.smtweb.framework.core.annotation.SwBody; | |||||
import cc.smtweb.framework.core.annotation.SwService; | import cc.smtweb.framework.core.annotation.SwService; | ||||
import cc.smtweb.framework.core.common.R; | |||||
import cc.smtweb.framework.core.common.SwMap; | |||||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||||
import cc.smtweb.framework.core.db.vo.ModelCatalog; | import cc.smtweb.framework.core.db.vo.ModelCatalog; | ||||
import cc.smtweb.framework.core.db.vo.ModelTable; | import cc.smtweb.framework.core.db.vo.ModelTable; | ||||
import cc.smtweb.framework.core.mvc.service.*; | import cc.smtweb.framework.core.mvc.service.*; | ||||
import cc.smtweb.framework.core.session.UserSession; | |||||
/** | /** | ||||
* Created by Akmm at 2022/3/22 9:12 | * Created by Akmm at 2022/3/22 9:12 | ||||
@@ -27,4 +32,17 @@ public class ModelTableService extends AbstractCompService { | |||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
//获取表的字段列表 | |||||
public R loadFields(@SwBody SwMap params, UserSession us) { | |||||
try { | |||||
final long tableId = params.readLong("tableId"); | |||||
ModelTable table = ModelTableCache.getInstance().get(tableId); | |||||
if (table == null) return R.error("没有找到对应的表定义信息【" + tableId + "】!"); | |||||
return R.success(SwListData.create(table.getFields(), 0)); | |||||
} catch (Exception e) { | |||||
return R.error("操作失败!", e); | |||||
} | |||||
} | |||||
} | } |