From e5452ad43fd1994ef5780c116b2907b27121d781 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Mon, 21 Dec 2020 21:03:43 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20Column=20=E7=89=B9?= =?UTF-8?q?=E6=80=A7=20RewriteSql=20=E9=87=8D=E8=AF=BB=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 9 ----- FreeSql/DataAnnotations/ColumnAttribute.cs | 17 +++++---- FreeSql/FreeSql.xml | 14 ++++++-- .../Internal/CommonProvider/InsertProvider.cs | 11 +++--- .../Internal/CommonProvider/UpdateProvider.cs | 35 ++++++++----------- FreeSql/Internal/CommonUtils.cs | 6 ++++ .../Curd/DamengInsert.cs | 11 +++--- .../Dameng/Curd/OdbcDamengInsert.cs | 11 +++--- .../Oracle/Curd/OdbcOracleInsert.cs | 11 +++--- .../Curd/OracleInsert.cs | 22 ++++++------ 10 files changed, 72 insertions(+), 75 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 27909b2e..02eb0609 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -512,14 +512,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - - diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs index 3b86febd..8ca27766 100644 --- a/FreeSql/DataAnnotations/ColumnAttribute.cs +++ b/FreeSql/DataAnnotations/ColumnAttribute.cs @@ -116,14 +116,17 @@ namespace FreeSql.DataAnnotations /// public int Scale { get => _Scale ?? 0; set => _Scale = value; } - ///// - ///// 写入格式化,比如 geography::STGeomFromText({0},4236) - ///// - //public string AuditWriteSql { get; set; } /// - /// 设置重读功能 - /// [Column(RereadSql = "{0}.STAsText()")] - /// 查询:SELECT a.[id], a.[geo].STAsText() FROM table a + /// 重写功能 + /// 比如:[Column(RewriteSql = "geography::STGeomFromText({0},4236)")] + /// 插入:INSERT INTO [table]([geo]) VALUES(geography::STGeomFromText('...',4236)) + /// 提示:更新也生效 + /// + public string RewriteSql { get; set; } + /// + /// 重读功能 + /// 比如:[Column(RereadSql = "{0}.STAsText()")] + /// 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a /// public string RereadSql { get; set; } } diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 08d01311..6459ce6a 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -115,11 +115,19 @@ decimal/numeric 类型的小数位长度 + + + 重写功能 + 比如:[Column(RewriteSql = "geography::STGeomFromText({0},4236)")] + 插入:INSERT INTO [table]([geo]) VALUES(geography::STGeomFromText('...',4236)) + 提示:更新也生效 + + - 设置重读功能 - [Column(RereadSql = "{0}.STAsText()")] - 查询:SELECT a.[id], a.[geo].STAsText() FROM table a + 重读功能 + 比如:[Column(RereadSql = "{0}.STAsText()")] + 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs index 04cd04b2..fa683afd 100644 --- a/FreeSql/Internal/CommonProvider/InsertProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs @@ -572,13 +572,12 @@ 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; } diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 619c20c0..2237c5be 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -434,15 +434,12 @@ 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 Set(Expression> column, TMember value) { @@ -698,13 +695,12 @@ 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"); diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index 5e9b68d5..306f6068 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -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); diff --git a/Providers/FreeSql.Provider.Dameng/Curd/DamengInsert.cs b/Providers/FreeSql.Provider.Dameng/Curd/DamengInsert.cs index 54d465ca..d6bd3792 100644 --- a/Providers/FreeSql.Provider.Dameng/Curd/DamengInsert.cs +++ b/Providers/FreeSql.Provider.Dameng/Curd/DamengInsert.cs @@ -73,13 +73,12 @@ 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; } diff --git a/Providers/FreeSql.Provider.Odbc/Dameng/Curd/OdbcDamengInsert.cs b/Providers/FreeSql.Provider.Odbc/Dameng/Curd/OdbcDamengInsert.cs index 96dce1fc..b0eb6fd6 100644 --- a/Providers/FreeSql.Provider.Odbc/Dameng/Curd/OdbcDamengInsert.cs +++ b/Providers/FreeSql.Provider.Odbc/Dameng/Curd/OdbcDamengInsert.cs @@ -69,13 +69,12 @@ 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; } diff --git a/Providers/FreeSql.Provider.Odbc/Oracle/Curd/OdbcOracleInsert.cs b/Providers/FreeSql.Provider.Odbc/Oracle/Curd/OdbcOracleInsert.cs index 87d33c14..66889d60 100644 --- a/Providers/FreeSql.Provider.Odbc/Oracle/Curd/OdbcOracleInsert.cs +++ b/Providers/FreeSql.Provider.Odbc/Oracle/Curd/OdbcOracleInsert.cs @@ -69,13 +69,12 @@ 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; } diff --git a/Providers/FreeSql.Provider.Oracle/Curd/OracleInsert.cs b/Providers/FreeSql.Provider.Oracle/Curd/OracleInsert.cs index f93d1852..f78e30dd 100644 --- a/Providers/FreeSql.Provider.Oracle/Curd/OracleInsert.cs +++ b/Providers/FreeSql.Provider.Oracle/Curd/OracleInsert.cs @@ -82,13 +82,12 @@ 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,13 +146,12 @@ 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; }