Browse Source

添加黑白名单过滤机制

James 3 years ago
parent
commit
80ca261a65

+ 43 - 6
src/main/java/com/jfinal/plugin/activerecord/generator/MetaBuilder.java

@@ -42,7 +42,10 @@ public class MetaBuilder {
 	
 	protected DataSource dataSource;
 	protected Dialect dialect = new MysqlDialect();
-	protected Set<String> excludedTables = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
+	
+	// 白名单 + 黑名单选择过滤的 tableName 集合,白名单优先于黑名单
+	protected Set<String> whitelist = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
+	protected Set<String> blacklist = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
 	
 	protected Predicate<String> tableSkip = null;
 	
@@ -77,14 +80,44 @@ public class MetaBuilder {
 		}
 	}
 	
-	public void addExcludedTable(String... excludedTables) {
-		if (excludedTables != null) {
-			for (String table : excludedTables) {
-				this.excludedTables.add(table.trim());
+	/**
+	 * 添加要生成的 tableName 到白名单
+	 */
+	public void addWhitelist(String... tableNames) {
+		if (tableNames != null) {
+			for (String table : tableNames) {
+				this.whitelist.add(table.trim());
+			}
+		}
+	}
+	
+	public void removeWhitelist(String tableName) {
+		if (tableName != null) {
+			this.whitelist.remove(tableName.trim());
+		}
+	}
+	
+	/**
+	 * 添加要排除的 tableName 到黑名单
+	 */
+	public void addBlacklist(String... tableNames) {
+		if (tableNames != null) {
+			for (String table : tableNames) {
+				this.blacklist.add(table.trim());
 			}
 		}
 	}
 	
+	public void removeBlacklist(String tableName) {
+		if (tableName != null) {
+			this.blacklist.remove(tableName.trim());
+		}
+	}
+	
+	public void addExcludedTable(String... excludedTables) {
+		addBlacklist(excludedTables);
+	}
+	
 	/**
 	 * 设置需要被移除的表名前缀,仅用于生成 modelName 与  baseModelName
 	 * 例如表名  "osc_account",移除前缀 "osc_" 后变为 "account"
@@ -222,10 +255,14 @@ public class MetaBuilder {
 		while (rs.next()) {
 			String tableName = rs.getString("TABLE_NAME");
 			
-			if (excludedTables.contains(tableName)) {
+			// 白名单优先
+			if (whitelist.contains(tableName)) {
+				;
+			} else if (blacklist.contains(tableName)) {
 				System.out.println("Skip table :" + tableName);
 				continue ;
 			}
+			
 			if (isSkipTable(tableName)) {
 				System.out.println("Skip table :" + tableName);
 				continue ;