diff --git a/Apewer.Source/Source/SqlClient.cs b/Apewer.Source/Source/SqlClient.cs index 0b485f0..c300353 100644 --- a/Apewer.Source/Source/SqlClient.cs +++ b/Apewer.Source/Source/SqlClient.cs @@ -113,7 +113,9 @@ namespace Apewer.Source columns = lower.ToArray(); } - // 增加列。 + // 找出新列。 + var newColumns = new List(); + var newPrimaryKey = new List(); foreach (var column in structure.Columns) { // 检查 Independent 特性。 @@ -124,11 +126,19 @@ namespace Apewer.Source if (columns.Contains(lower)) continue; var type = Declaration(column); - if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。"); + if (string.IsNullOrEmpty(type)) return $"类型 {column.Type} 不受支持。"; + + newColumns.Add(column); + } - var sql = TextUtility.Merge("alter table [", table, "] add ", type, "; "); + // 执行:添加列。 + if (newColumns.IsEmpty()) return TextUtility.Empty; + foreach (var column in newColumns) + { + var type = Declaration(column); + var sql = $"alter table [{table}] add {type}; "; var execute = Execute(sql, null, false); - if (execute.Success == false) return execute.Message; + if (!execute.Success) return execute.Message; } return TextUtility.Empty; } diff --git a/Apewer/RuntimeUtility.cs b/Apewer/RuntimeUtility.cs index 48e39df..25ff02c 100644 --- a/Apewer/RuntimeUtility.cs +++ b/Apewer/RuntimeUtility.cs @@ -15,6 +15,9 @@ namespace Apewer public static class RuntimeUtility { + /// + public static Type ObjectType { get; } = typeof(object); + /// 使用默认比较器判断相等。 public static bool Equals(T a, T b) => EqualityComparer.Default.Equals(a, b); diff --git a/Apewer/Source/TableStructure.cs b/Apewer/Source/TableStructure.cs index b97e576..5e6797d 100644 --- a/Apewer/Source/TableStructure.cs +++ b/Apewer/Source/TableStructure.cs @@ -111,7 +111,7 @@ namespace Apewer.Source // 类型。 var isRecord = RuntimeUtility.IsInherits(type, typeof(Record)); - var properties = type.GetProperties(); + var properties = GetProperties(type); var total = properties.Length; // 解析 ColumnAttribute。 @@ -190,6 +190,33 @@ namespace Apewer.Source return ts; } + // 获取属性,基类的属性排在前面。 + static PropertyInfo[] GetProperties(Type type) + { + if (type == null) throw new ArgumentNullException(nameof(type)); + + // 需要获取属性的类型。 + var types = new List(); + { + var target = type; + while (true) + { + if (target.Equals(RuntimeUtility.ObjectType)) break; + types.Add(target); + target = target.BaseType; + } + } + + // 所有属性。 + var list = new List(); + for (var i = types.Count - 1; i >= 0; i--) + { + var properties = types[i].GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); + list.Add(properties); + } + return list.ToArray(); + } + #endregion #region TableAttribute