public String evalFieldType(MappingField mf) { if (mf.getCustomDbType() != null) return mf.getCustomDbType(); switch (mf.getColumnType()) { case CHAR: return "CHAR(" + mf.getWidth() + ")"; case BOOLEAN: return "BOOLEAN"; case VARCHAR: return "VARCHAR(" + mf.getWidth() + ")"; case TEXT: return "TEXT"; case BINARY: return "BLOB"; case TIMESTAMP: return "TIMESTAMP"; case DATETIME: return "DATETIME"; case DATE: return "DATE"; case TIME: return "TIME"; case INT: // 用户自定义了宽度 if (mf.getWidth() > 0) return "INT(" + mf.getWidth() + ")"; // 用数据库的默认宽度 return "INT"; case FLOAT: // 用户自定义了精度 if (mf.getWidth() > 0 && mf.getPrecision() > 0) { return "NUMERIC(" + mf.getWidth() + "," + mf.getPrecision() + ")"; } // 用默认精度 if (mf.getTypeMirror().isDouble()) return "NUMERIC(15,10)"; return "FLOAT"; case PSQL_ARRAY: return "ARRAY"; case PSQL_JSON: case MYSQL_JSON: return "JSON"; // TODO 这里不用加 default 么 } throw Lang.makeThrow( "Unsupport colType '%s' of field '%s' in '%s' ", mf.getColumnType(), mf.getName(), mf.getEntity().getType().getName()); }
@SuppressWarnings({"unchecked", "rawtypes"}) public static boolean filterFields( Object obj, FieldMatcher matcher, Dao dao, Callback2<MappingField, Object> callback) { if (obj == null) return false; obj = Lang.first(obj); if (obj == null) { return false; } if (obj.getClass() == Class.class) { throw Lang.impossible(); } if (obj instanceof String || obj instanceof Number || obj instanceof Boolean) { throw Lang.impossible(); } Entity en = dao.getEntity(obj.getClass()); if (en == null) { throw Lang.impossible(); } List<MappingField> mfs = en.getMappingFields(); if (matcher != null) { Iterator<MappingField> it = mfs.iterator(); while (it.hasNext()) { MappingField mf = it.next(); if (!matcher.match(mf.getName())) it.remove(); } } boolean flag = false; for (MappingField mf : mfs) { if (matcher.isIgnoreId() && mf.isId()) continue; if (matcher.isIgnoreName() && mf.isName()) continue; if (matcher.isIgnorePk() && mf.isCompositePk()) continue; Object val = mf.getValue(obj); if (val == null) { if (matcher.isIgnoreNull()) continue; } if (val instanceof Number && ((Number) val).doubleValue() == 0.0) { if (matcher.isIgnoreZero()) continue; } if (val instanceof Date) { if (matcher.isIgnoreDate()) continue; } callback.invoke(mf, val); flag = true; } return flag; }
/** 根据Pojo生成数据字典,zdoc格式 */ public static StringBuilder dataDict(DataSource ds, String... packages) { StringBuilder sb = new StringBuilder(); List<Class<?>> ks = new ArrayList<Class<?>>(); for (String packageName : packages) { ks.addAll(Scans.me().scanPackage(packageName)); } Iterator<Class<?>> it = ks.iterator(); while (it.hasNext()) { Class<?> klass = it.next(); if (klass.getAnnotation(Table.class) == null) it.remove(); } // log.infof("Found %d table class", ks.size()); JdbcExpert exp = Jdbcs.getExpert(ds); NutDao dao = new NutDao(ds); Method evalFieldType; try { evalFieldType = exp.getClass().getDeclaredMethod("evalFieldType", MappingField.class); } catch (Throwable e) { throw Lang.wrapThrow(e); } evalFieldType.setAccessible(true); Entity<?> entity = null; String line = "-------------------------------------------------------------------\n"; sb.append("#title:数据字典\n"); sb.append("#author:wendal\n"); sb.append("#index:0,1\n").append(line); for (Class<?> klass : ks) { sb.append(line); entity = dao.getEntity(klass); sb.append("表名 ").append(entity.getTableName()).append("\n\n"); if (!Strings.isBlank(entity.getTableComment())) sb.append("表注释: ").append(entity.getTableComment()); sb.append("\t").append("Java类名 ").append(klass.getName()).append("\n\n"); sb.append("\t||序号||列名||数据类型||主键||非空||默认值||java属性名||java类型||注释||\n"); int index = 1; for (MappingField field : entity.getMappingFields()) { String dataType; try { dataType = (String) evalFieldType.invoke(exp, field); } catch (Throwable e) { throw Lang.wrapThrow(e); // 不可能发生的 } sb.append("\t||") .append(index++) .append("||") .append(field.getColumnName()) .append("||") .append(dataType) .append("||") .append(field.isPk()) .append("||") .append(field.isNotNull()) .append("||") .append(field.getDefaultValue(null) == null ? " " : field.getDefaultValue(null)) .append("||") .append(field.getName()) .append("||") .append(field.getTypeClass().getName()) .append("||") .append(field.getColumnComment() == null ? " " : field.getColumnComment()) .append("||\n"); } } return sb; }