- 增加 Column 特性 RewriteSql 重读功能;

This commit is contained in:
2881099 2020-12-21 21:03:43 +08:00
parent 76e8b3efce
commit e5452ad43f
10 changed files with 72 additions and 75 deletions

View File

@ -512,14 +512,5 @@
<param name="that"></param>
<returns></returns>
</member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@ -116,14 +116,17 @@ namespace FreeSql.DataAnnotations
/// </summary>
public int Scale { get => _Scale ?? 0; set => _Scale = value; }
///// <summary>
///// 写入格式化,比如 geography::STGeomFromText({0},4236)
///// </summary>
//public string AuditWriteSql { get; set; }
/// <summary>
/// 设置重读功能<para></para>
/// [Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
/// 查询SELECT a.[id], a.[geo].STAsText() FROM table a
/// 重写功能<para></para>
/// 比如:[Column(RewriteSql = &quot;geography::STGeomFromText({0},4236)&quot;)]<para></para>
/// 插入INSERT INTO [table]([geo]) VALUES(geography::STGeomFromText('...',4236))<para></para>
/// 提示:更新也生效
/// </summary>
public string RewriteSql { get; set; }
/// <summary>
/// 重读功能<para></para>
/// 比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
/// 查询SELECT a.[id], a.[geo].STAsText() FROM [table] a
/// </summary>
public string RereadSql { get; set; }
}

View File

@ -115,11 +115,19 @@
decimal/numeric 类型的小数位长度
</summary>
</member>
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.RewriteSql">
<summary>
重写功能<para></para>
比如:[Column(RewriteSql = &quot;geography::STGeomFromText({0},4236)&quot;)]<para></para>
插入INSERT INTO [table]([geo]) VALUES(geography::STGeomFromText('...',4236))<para></para>
提示:更新也生效
</summary>
</member>
<member name="P:FreeSql.DataAnnotations.ColumnAttribute.RereadSql">
<summary>
设置重读功能<para></para>
[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
查询SELECT a.[id], a.[geo].STAsText() FROM table a
重读功能<para></para>
比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
查询SELECT a.[id], a.[geo].STAsText() FROM [table] a
</summary>
</member>
<member name="M:FreeSql.DataAnnotations.ColumnFluent.Name(System.String)">

View File

@ -572,14 +572,13 @@ namespace FreeSql.Internal.CommonProvider
{
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"));
sb.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
}
}
++colidx2;
}
if (isValues) sb.Append(')');

View File

@ -434,16 +434,13 @@ namespace FreeSql.Internal.CommonProvider
if (col.Attribute.MapType == col.CsType) val = value;
else val = Utils.GetDataReaderValue(col.Attribute.MapType, value);
_set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
if (_noneParameter)
{
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, "u", col, col.Attribute.MapType, val));
}
else
{
_set.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}"));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_params, "u", col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}");
_set.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_commonUtils.AppendParamter(_params, null, col, col.Attribute.MapType, val);
}
}
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value)
{
var cols = new List<SelectColumnInfo>();
@ -698,14 +695,13 @@ namespace FreeSql.Internal.CommonProvider
else
{
var val = col.GetDbValue(_source.First());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
_set.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
}
}
++colidx;
}
}
@ -743,13 +739,12 @@ namespace FreeSql.Internal.CommonProvider
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var val = col.GetDbValue(d);
if (_noneParameter)
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val));
else
{
cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
_set.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
}
if (val == null || val == DBNull.Value) nulls++;
}
cwsb.Append(" END");

View File

@ -54,6 +54,12 @@ namespace FreeSql.Internal
public abstract string NowUtc { get; }
public abstract string QuoteWriteParamter(Type type, string paramterName);
protected abstract string QuoteReadColumnAdapter(Type type, Type mapType, string columnName);
public string QuoteWriteColumn(ColumnInfo col, string sql)
{
if (string.IsNullOrWhiteSpace(col?.Attribute.RewriteSql) == false)
return string.Format(col.Attribute.RereadSql, sql);
return sql;
}
public string QuoteReadColumn(ColumnInfo col, Type type, Type mapType, string columnName)
{
var result = QuoteReadColumnAdapter(type, mapType, columnName);

View File

@ -73,14 +73,13 @@ namespace FreeSql.Dameng.Curd
{
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"));
sb.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
}
}
++colidx2;
}
sb.Append(")");

View File

@ -69,14 +69,13 @@ namespace FreeSql.Odbc.Dameng
{
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"));
sb.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
}
}
++colidx2;
}
sb.Append(")");

View File

@ -69,14 +69,13 @@ namespace FreeSql.Odbc.Oracle
{
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"));
sb.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
}
}
++colidx2;
}
sb.Append(")");

View File

@ -82,14 +82,13 @@ namespace FreeSql.Oracle.Curd
{
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"));
sb.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
}
}
if (didx == 0) sb.Append(" as ").Append(col.Attribute.Name);
++colidx2;
}
@ -147,14 +146,13 @@ namespace FreeSql.Oracle.Curd
{
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"));
sb.Append(_commonUtils.QuoteWriteColumn(col, colsql));
if (_noneParameter == false)
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
}
}
++colidx2;
}
sb.Append(")");