diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index a77ef7d1..c2a242d3 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -113,7 +113,7 @@ namespace base_entity public B B { get; set; } } - [Table(Name = "as_table_log_{yyyyMM}", AsTable = "createtime=2022-1-1(1 month)")] + [Table(Name = "as_table_log_{yyyyMM}", AsTable = "createtime=2022-1-1 11(1 month)")] class AsTableLog { public Guid id { get; set; } @@ -572,6 +572,11 @@ namespace base_entity #endregion fsql.UseJsonMap(); + var atimpl = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog)) + .AsTableImpl; + + atimpl.GetTableNameByColumnValue(DateTime.Parse("2023-7-1"), autoExpand: true); + var dywhere = new DynamicFilterInfo { Field = "AspNetRoless.Name", Operator = DynamicFilterOperator.Equal, Value = "Admin" }; var method = typeof(ISelect).GetMethod("WhereDynamicFilter"); var users4 = fsql.Select().IncludeByPropertyName("AspNetUserRoless", then => then.WhereDynamicFilter(dywhere)).ToList(); @@ -1086,10 +1091,10 @@ namespace base_entity .ToList(); - var atimpl = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog)) + var atimpl2 = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog)) .AsTableImpl; - atimpl.GetTableNameByColumnValue(DateTime.Parse("2023-7-1"), autoExpand: true); + atimpl2.GetTableNameByColumnValue(DateTime.Parse("2023-7-1"), autoExpand: true); fsql.Select() diff --git a/FreeSql/DataAnnotations/TableAttribute.cs b/FreeSql/DataAnnotations/TableAttribute.cs index bcaf6b70..5d64cb7a 100644 --- a/FreeSql/DataAnnotations/TableAttribute.cs +++ b/FreeSql/DataAnnotations/TableAttribute.cs @@ -45,7 +45,7 @@ namespace FreeSql.DataAnnotations { if (string.IsNullOrEmpty(AsTable) == false) { - var atm = Regex.Match(AsTable, @"([\w_\d]+)\s*=\s*(\d\d\d\d)\s*\-\s*(\d\d?)\s*\-\s*(\d\d?)\s*\((\d+)\s*(year|month|day|hour)\)", RegexOptions.IgnoreCase); + var atm = Regex.Match(AsTable, @"([\w_\d]+)\s*=\s*(\d\d\d\d)\s*\-\s*(\d\d?)\s*\-\s*(\d\d?)\s*( [\d:]+)?\((\d+)\s*(year|month|day|hour)\)", RegexOptions.IgnoreCase); if (atm.Success == false) throw new Exception(CoreStrings.AsTable_PropertyName_FormatError(AsTable)); @@ -56,16 +56,25 @@ namespace FreeSql.DataAnnotations tb.AsTableColumn = null; throw new Exception(CoreStrings.AsTable_PropertyName_NotDateTime(atm.Groups[1].Value)); } - int.TryParse(atm.Groups[5].Value, out var atm5); - string atm6 = atm.Groups[6].Value.ToLower(); - tb.AsTableImpl = new DateTimeAsTableImpl(Name, DateTime.Parse($"{atm.Groups[2].Value}-{atm.Groups[3].Value}-{atm.Groups[4].Value}"), dt => + var beginTime = $"{atm.Groups[2].Value}-{atm.Groups[3].Value}-{atm.Groups[4].Value}"; + var atm5 = atm.Groups[5].Value; + if (string.IsNullOrWhiteSpace(atm5) == false) { - switch (atm6) + var hhmmss = atm5.Split(':').Select(a => int.TryParse(a.Trim(), out var tryint) ? tryint : 0).ToArray(); + if (hhmmss.Length == 1) beginTime = $"{beginTime} {hhmmss[0]}:0:0"; + else if (hhmmss.Length == 2) beginTime = $"{beginTime} {hhmmss[0]}:{hhmmss[1]}:0"; + else if (hhmmss.Length == 3) beginTime = $"{beginTime} {hhmmss[0]}:{hhmmss[1]}:{hhmmss[2]}"; + } + int.TryParse(atm.Groups[6].Value, out var atm6); + string atm7 = atm.Groups[7].Value.ToLower(); + tb.AsTableImpl = new DateTimeAsTableImpl(Name, DateTime.Parse(beginTime), dt => + { + switch (atm7) { - case "year": return dt.AddYears(atm5); - case "month": return dt.AddMonths(atm5); - case "day": return dt.AddDays(atm5); - case "hour": return dt.AddHours(atm5); + case "year": return dt.AddYears(atm6); + case "month": return dt.AddMonths(atm6); + case "day": return dt.AddDays(atm6); + case "hour": return dt.AddHours(atm6); } throw new NotImplementedException(CoreStrings.Functions_AsTable_NotImplemented(AsTable)); }); diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index ec33a9bf..21395735 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -3335,6 +3335,177 @@ + + + 测试数据库是否连接正确,本方法执行如下命令: + MySql/SqlServer/PostgreSQL/达梦/人大金仓/神通: SELECT 1 + Oracle: SELECT 1 FROM dual + + 命令超时设置(秒) + + true: 成功, false: 失败 + + + + 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】 + + + + + + + + + + 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 查询 + + + + + + + + + 查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 查询 + + + + + + + + + 查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 查询 + + + + + + + + + 查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 在【主库】执行 + + + + + + + + + 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 在【主库】执行 + + + + + + + + + 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + + + 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + + + 执行SQL返回对象集合,Query<User, Address>("select * from user where age > @age; select * from address", new { age = 25 }) + 提示:parms 参数还可以传 Dictionary<string, object> + + + + + + + + 可自定义解析表达式 @@ -4325,6 +4496,12 @@ 超时 + + + 获取资源 + + + 使用完毕后,归还资源 @@ -4400,6 +4577,12 @@ 资源对象 + + + 从对象池获取对象成功的时候触发,通过该方法统计或初始化对象 + + 资源对象 + 归还对象给对象池的时候触发