- 优化 IInsert.InsertIdentity 可插入自增属性;

This commit is contained in:
28810 2019-07-29 16:35:36 +08:00
parent 5fc603a18b
commit 9b80f8cd53
5 changed files with 64 additions and 44 deletions

View File

@ -322,6 +322,7 @@ namespace FreeSql.Tests
[Fact] [Fact]
public void Test1() public void Test1()
{ {
//g.sqlite.Aop.ParseExpression += parseExp; //g.sqlite.Aop.ParseExpression += parseExp;
var sqddddl = g.sqlite.Select<TaskBuild>().ToSql(t => t.OptionsEntity04 == "1".TryTo<int>()); var sqddddl = g.sqlite.Select<TaskBuild>().ToSql(t => t.OptionsEntity04 == "1".TryTo<int>());

View File

@ -649,6 +649,12 @@
<param name="columns">lambda选择列</param> <param name="columns">lambda选择列</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.IInsert`1.InsertIdentity">
<summary>
指定可插入自增字段
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.IInsert`1.NoneParameter"> <member name="M:FreeSql.IInsert`1.NoneParameter">
<summary> <summary>
不使用参数化,可通过 IFreeSql.CodeFirst.IsNotCommandParameter 全局性设置 不使用参数化,可通过 IFreeSql.CodeFirst.IsNotCommandParameter 全局性设置

View File

@ -54,6 +54,12 @@ namespace FreeSql
/// <returns></returns> /// <returns></returns>
IInsert<T1> IgnoreColumns(Expression<Func<T1, object>> columns); IInsert<T1> IgnoreColumns(Expression<Func<T1, object>> columns);
/// <summary>
/// 指定可插入自增字段
/// </summary>
/// <returns></returns>
IInsert<T1> InsertIdentity();
/// <summary> /// <summary>
/// 不使用参数化,可通过 IFreeSql.CodeFirst.IsNotCommandParameter 全局性设置 /// 不使用参数化,可通过 IFreeSql.CodeFirst.IsNotCommandParameter 全局性设置
/// </summary> /// </summary>

View File

@ -21,7 +21,7 @@ namespace FreeSql.Internal.CommonProvider
protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase); protected Dictionary<string, bool> _ignore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
protected TableInfo _table; protected TableInfo _table;
protected Func<string, string> _tableRule; protected Func<string, string> _tableRule;
protected bool _noneParameter; protected bool _noneParameter, _insertIdentity;
protected DbParameter[] _params; protected DbParameter[] _params;
protected DbTransaction _transaction; protected DbTransaction _transaction;
protected DbConnection _connection; protected DbConnection _connection;
@ -38,6 +38,7 @@ namespace FreeSql.Internal.CommonProvider
protected void ClearData() protected void ClearData()
{ {
_insertIdentity = false;
_source.Clear(); _source.Clear();
_ignore.Clear(); _ignore.Clear();
_params = null; _params = null;
@ -56,6 +57,12 @@ namespace FreeSql.Internal.CommonProvider
return this; return this;
} }
public IInsert<T1> InsertIdentity()
{
_insertIdentity = true;
return this;
}
public IInsert<T1> NoneParameter() public IInsert<T1> NoneParameter()
{ {
_noneParameter = true; _noneParameter = true;
@ -526,12 +533,14 @@ namespace FreeSql.Internal.CommonProvider
sb.Append("INSERT INTO ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append("("); sb.Append("INSERT INTO ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append("(");
var colidx = 0; var colidx = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
{ if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (colidx > 0) sb.Append(", "); if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
++colidx; if (colidx > 0) sb.Append(", ");
} sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
++colidx;
}
sb.Append(") VALUES"); sb.Append(") VALUES");
_params = _noneParameter ? new DbParameter[0] : new DbParameter[colidx * _source.Count]; _params = _noneParameter ? new DbParameter[0] : new DbParameter[colidx * _source.Count];
var specialParams = new List<DbParameter>(); var specialParams = new List<DbParameter>();
@ -542,21 +551,23 @@ namespace FreeSql.Internal.CommonProvider
sb.Append("("); sb.Append("(");
var colidx2 = 0; var colidx2 = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else
{ {
if (colidx2 > 0) sb.Append(", "); sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
object val = col.GetMapValue(d); _params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col.Attribute.MapType, val);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col.Attribute.MapType, val);
}
++colidx2;
} }
++colidx2;
}
sb.Append(")"); sb.Append(")");
++didx; ++didx;
} }

View File

@ -41,17 +41,13 @@ namespace FreeSql.Oracle.Curd
var colidx = 0; var colidx = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (col.Attribute.IsIdentity == true) if (col.Attribute.IsIdentity) _identCol = col;
{ if (_ignore.ContainsKey(col.Attribute.Name)) continue;
_identCol = col; if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
continue;
} if (colidx > 0) sbtb.Append(", ");
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) sbtb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
{ ++colidx;
if (colidx > 0) sbtb.Append(", ");
sbtb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name));
++colidx;
}
} }
sbtb.Append(") "); sbtb.Append(") ");
@ -67,21 +63,21 @@ namespace FreeSql.Oracle.Curd
var colidx2 = 0; var colidx2 = 0;
foreach (var col in _table.Columns.Values) foreach (var col in _table.Columns.Values)
{ {
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) if (_ignore.ContainsKey(col.Attribute.Name)) continue;
if (col.Attribute.IsIdentity && _insertIdentity == false) continue;
if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else
{ {
if (colidx2 > 0) sb.Append(", "); sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
object val = col.GetMapValue(d); _params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col.Attribute.MapType, val);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(d, val = FreeUtil.NewMongodbId());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col.Attribute.MapType, val);
}
++colidx2;
} }
++colidx2;
} }
sb.Append(")"); sb.Append(")");
++didx; ++didx;