From 7127eb6e07213f8dc4e1f3eddcbc53d4f083ca0b Mon Sep 17 00:00:00 2001 From: Elivo Date: Mon, 27 Oct 2025 12:14:48 +0800 Subject: [PATCH] =?UTF-8?q?SqlClient=EF=BC=9A=E5=B1=9E=E6=80=A7=E7=9A=84?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E6=94=B9=E4=B8=BA=E5=9F=BA=E7=B1=BB=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=9C=A8=E5=89=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer.Source/Source/SqlClient.cs | 18 ++++++++++++++---- Apewer/RuntimeUtility.cs | 3 +++ Apewer/Source/TableStructure.cs | 29 ++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) 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