提交 977e16db 编写于 作者: D daijianping
......@@ -557,7 +557,14 @@ public class EntityBuilder : ClassBuilder
// 附加特性
if (dc.Properties.TryGetValue("Attribute", out var att))
WriteLine("[{0}]", att.Replace("{name}", dc.Name));
{
// 兼容支持新旧两种格式
var str = att.Replace("{name}", dc.Name);
if (str[0] != '[')
WriteLine("[{0}]", str);
else
WriteLine("{0}", str);//lps 2023-07-22 去掉两边的方括号,以便支持多个验证。例如:<Column Name="TestQuantity" DataType="Int32" Description="测试数量" Attribute="[Required(ErrorMessage = &quot;{0}必须填写&quot;)][Range(1, 100,ErrorMessage =&quot;超出范围&quot;)]" />
}
// 分类特性
if (dc.Properties.TryGetValue("Category", out att) && !att.IsNullOrEmpty())
......
......@@ -354,6 +354,11 @@ internal abstract partial class DbSession : DisposeBase, IDbSession, IAsyncDbSes
});
}
/// <summary>执行SQL查询,返回记录集</summary>
/// <param name="builder">查询生成器</param>
/// <returns>总记录数</returns>
public virtual DbTable Query(SelectBuilder builder) => Query(builder.ToString(), builder.Parameters.ToArray());
/// <summary>执行SQL查询,返回记录集</summary>
/// <param name="sql">SQL语句</param>
/// <param name="ps">命令参数</param>
......
......@@ -78,6 +78,11 @@ public interface IDbSession : IDisposable2
/// <returns>记录集</returns>
DataSet Query(DbCommand cmd);
/// <summary>执行SQL查询,返回记录集</summary>
/// <param name="builder">查询生成器</param>
/// <returns>总记录数</returns>
DbTable Query(SelectBuilder builder);
/// <summary>执行SQL查询,返回记录集</summary>
/// <param name="sql">SQL语句</param>
/// <param name="ps">命令参数</param>
......
using System;
using System.Data;
using System.Data;
using System.Data.Common;
using NewLife;
using NewLife.Log;
......
......@@ -78,7 +78,7 @@ partial class DAL
return QueryByCache(builder, startRowIndex, maximumRows, (sb, start, max) =>
{
sb = PageSplit(sb, start, max);
return Session.Query(sb.ToString(), sb.Parameters.ToArray());
return Session.Query(sb);
}, nameof(Query));
}
......
......@@ -489,8 +489,8 @@ internal class SqlServer : RemoteDb
// fix 2023.03.22
// LIKE 构建SQL语句 中 [ ] % 会循环转义 ,只转_比较合适
if (value.IndexOfAny(_likeKeys) >= 0)
value = value
.Replace("_", "[_]");
value = value
.Replace("_", "[_]");
return base.FormatLike(column, format, value);
}
#endregion
......@@ -504,6 +504,21 @@ internal class SqlServerSession : RemoteDbSession
#endregion
#region 查询
/// <summary>执行SQL查询,返回记录集</summary>
/// <param name="builder">查询生成器</param>
/// <returns>总记录数</returns>
public override DbTable Query(SelectBuilder builder)
{
if (Transaction != null)
{
builder = builder.Clone();
builder.Table += " WITH (UPDLOCK, HOLDLOCK)";
}
var sql = builder.ToString();
return Query(sql, builder.Parameters.ToArray());
}
protected override DbTable OnFill(DbDataReader dr)
{
var dt = new DbTable();
......
using System;
using System.Collections.Generic;
using System.Data;
using System.Data;
using System.Text.RegularExpressions;
using NewLife;
using NewLife.Collections;
......
......@@ -210,6 +210,8 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
if (!rt) return 0;
}
AutoFillSnowIDPrimaryKey();
// 自动分库分表
using var split = Meta.CreateShard(this as TEntity);
......@@ -371,6 +373,8 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
if (!rt) return Task.FromResult(0);
}
AutoFillSnowIDPrimaryKey();
return func();
}
......@@ -381,6 +385,25 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
/// <remarks>建议重写者调用基类的实现,因为基类根据数据字段的唯一索引进行数据验证。</remarks>
/// <param name="isNew">是否新数据</param>
public override void Valid(Boolean isNew)
{
var factory = Meta.Factory;
// 校验字符串长度,超长时抛出参数异常
foreach (var fi in factory.Fields)
{
if (fi.Type == typeof(String) && fi.Length > 0)
{
if (this[fi.Name] is String str && str.Length > fi.Length)
throw new ArgumentOutOfRangeException(fi.Name, $"{fi.DisplayName}长度限制{fi.Length}字符");
}
}
AutoFillSnowIDPrimaryKey();
}
/// <summary>
/// 雪花Id生成器。Int64主键非自增时,自动填充
/// </summary>
private void AutoFillSnowIDPrimaryKey()
{
var factory = Meta.Factory;
......@@ -394,16 +417,6 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
SetItem(pk.Name, factory.Snow.NewId());
}
}
// 校验字符串长度,超长时抛出参数异常
foreach (var fi in factory.Fields)
{
if (fi.Type == typeof(String) && fi.Length > 0)
{
if (this[fi.Name] is String str && str.Length > fi.Length)
throw new ArgumentOutOfRangeException(fi.Name, $"{fi.DisplayName}长度限制{fi.Length}字符");
}
}
}
/// <summary>根据指定键检查数据,返回数据是否已存在</summary>
......
......@@ -22,6 +22,11 @@ public partial class Log : Entity<Log>
// 关闭SQL日志
ThreadPool.UnsafeQueueUserWorkItem(s => { Meta.Session.Dal.Db.ShowSQL = false; }, null);
#endif
// 针对Mysql启用压缩表
var table = Meta.Table.DataTable;
table.Properties["ROW_FORMAT"] = "COMPRESSED";
table.Properties["KEY_BLOCK_SIZE"] = "4";
}
/// <summary>已重载。记录当前管理员</summary>
......
......@@ -434,6 +434,17 @@ public class MySqlTests
dal.SetTables(table);
Assert.Contains(dal.Tables, t => t.TableName == table.TableName);
// Log表自带压缩表能力
table = Log.Meta.Table.DataTable.Clone() as IDataTable;
table.DbType = DatabaseType.MySql;
Assert.Equal("COMPRESSED", table.Properties["ROW_FORMAT"]);
Assert.Equal("4", table.Properties["KEY_BLOCK_SIZE"]);
sql = meta.GetSchemaSQL(DDLSchema.CreateTable, table);
Assert.Contains(" ROW_FORMAT=COMPRESSED", sql);
Assert.Contains(" KEY_BLOCK_SIZE=4", sql);
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册