From 08ef441a8dc1ce78e8f838a76a1d44aa6f709330 Mon Sep 17 00:00:00 2001 From: Victor Qu Date: Tue, 26 Nov 2024 10:34:19 +0800 Subject: [PATCH] fix missing --- ProjectCommon.targets | 2 +- src/SV.Db.Analyzers/CodeGenerator.cs | 1 + src/SV.Db.Sloth/DbEntityInfo.cs | 11 ++++- .../Controllers/WeatherForecastController.cs | 43 +++++++++++++++++++ test/RestfulSample/Program.cs | 9 +++- 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ProjectCommon.targets b/ProjectCommon.targets index 2bf4de0..3329e4c 100644 --- a/ProjectCommon.targets +++ b/ProjectCommon.targets @@ -1,7 +1,7 @@ $(VersionSuffix) - 0.0.2.13 + 0.0.2.14 $(Version) $(Version) $(Version)$(VersionSuffix) diff --git a/src/SV.Db.Analyzers/CodeGenerator.cs b/src/SV.Db.Analyzers/CodeGenerator.cs index 23a0503..4a5cc9a 100644 --- a/src/SV.Db.Analyzers/CodeGenerator.cs +++ b/src/SV.Db.Analyzers/CodeGenerator.cs @@ -107,6 +107,7 @@ private SourceState TransformFunc(GeneratorSyntaxContext ctx, CancellationToken case "Query": case "QueryAsync": case "ExecuteInsertAsync": + case "ExecuteUpdateAsync": hasResultType = true; break; diff --git a/src/SV.Db.Sloth/DbEntityInfo.cs b/src/SV.Db.Sloth/DbEntityInfo.cs index ba1ee73..45a8d47 100644 --- a/src/SV.Db.Sloth/DbEntityInfo.cs +++ b/src/SV.Db.Sloth/DbEntityInfo.cs @@ -175,7 +175,16 @@ public static DbEntityInfo Get() var structFields = ups.Where(i => { var f = fields.First(x => x.Name.Equals(i.Key, StringComparison.OrdinalIgnoreCase)); - return f.DeclaringType.IsValueType && Nullable.GetUnderlyingType(f.DeclaringType) is null; + Type t = null; + if (f is PropertyInfo p) + { + t = p.PropertyType; + } + else if (f is FieldInfo field) + { + t = field.FieldType; + } + return t != null && t.IsValueType && Nullable.GetUnderlyingType(t) is null; }).Select(i => new KeyValuePair(i.Key, i.Value.Field)).ToArray(); var checkUps = ups.Select(i => { diff --git a/test/RestfulSample/Controllers/WeatherForecastController.cs b/test/RestfulSample/Controllers/WeatherForecastController.cs index 92c1937..5931c08 100644 --- a/test/RestfulSample/Controllers/WeatherForecastController.cs +++ b/test/RestfulSample/Controllers/WeatherForecastController.cs @@ -3,7 +3,9 @@ using SV.Db.Sloth; using SV.Db.Sloth.Attributes; using SV.Db.Sloth.Swagger; +using System.ComponentModel.DataAnnotations; using System.Data; +using System.Text.Json; namespace RestfulSample.Controllers { @@ -87,6 +89,21 @@ public async Task QueryUserInfo() { return await this.QueryByParamsAsync(); } + + [HttpPost("account/profile")] + public async Task UpdateAccountProfile([FromBody, Required] AccountProfile accountProfile) + { + return await factory.ExecuteUpdateAsync(accountProfile); + //return await factory.ExecuteInsertAsync(accountProfile); + //var has = (await factory.From().Where(i => i.AccountId == accountProfile.AccountId && i.Key == accountProfile.Key).Select(nameof(AccountProfile.AccountId)).Limit(1).ExecuteQueryAsync()).Rows?.Count > 0; + //if (has) + //{ + // return await factory.ExecuteUpdateAsync(accountProfile); + //} + //else + //{ + //} + } } [Db(StaticInfo.Demo)] @@ -148,4 +165,30 @@ public class UserInfo [Select("a.InDate"), OrderBy, Where, Column(Name = "InDate", Type = DbType.Int64), Update] public long InDate { get; set; } } + + [Db(StaticInfo.Demo)] + [Table(""" + select {Fields} + FROM account_profile a + {where} + """, UpdateTable = "account_profile")] + public class AccountProfile + { + [Select("a.AccountId"), OrderBy, Where, Column(Name = "AccountId", Type = DbType.Int32), Update(PrimaryKey = true)] + public int AccountId { get; set; } + + [Select("a.Key"), OrderBy, Where, Column(Name = "Key"), Update(PrimaryKey = true)] + public string Key { get; set; } + + [Select("a.Value"), OrderBy, Where, Column(Name = "Value", Type = DbType.String, IsJson = true, CustomConvertToDbMethod = "RestfulSample.Controllers.AccountProfile.ToJsonString"), Update] + public object Value { get; set; } + + [Select("a.LastEditDate"), OrderBy, Where, Column(Name = "LastEditDate", Type = DbType.Int64), Update] + public long LastEditDate { get; set; } + + public static string? ToJsonString(object? v) + { + return v == null ? null : JsonSerializer.Serialize(v); + } + } } \ No newline at end of file diff --git a/test/RestfulSample/Program.cs b/test/RestfulSample/Program.cs index b760f14..716b909 100644 --- a/test/RestfulSample/Program.cs +++ b/test/RestfulSample/Program.cs @@ -27,7 +27,14 @@ CREATE TABLE Weather ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, value text - ) + ); + + CREATE TABLE account_profile ( + AccountId INTEGER, + Key TEXT, + Value text, + LastEditDate INTEGER + ); """); a.ExecuteNonQuery(""" INSERT INTO Weather