Skip to content

Commit 639d30f

Browse files
committed
- 优化 varchar/nvarchar 的 NoneParameter 处理;#519
1 parent b701ad8 commit 639d30f

File tree

30 files changed

+218
-50
lines changed

30 files changed

+218
-50
lines changed

FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using FreeSql.DataAnnotations;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Text;
7+
using System.Threading;
8+
using Xunit;
9+
10+
namespace FreeSql.Tests.Issues
11+
{
12+
public class _518
13+
{
14+
[Fact]
15+
public void SelectTest()
16+
{
17+
IFreeSql free = g.sqlserver;
18+
19+
//创建测试数据
20+
using (var db = free.CreateDbContext())
21+
{
22+
db.Set<TestEntity518>().Remove(t => true); //清空旧数据
23+
db.SaveChanges();
24+
25+
//插入三条测试数据
26+
db.Add(new TestEntity518() { ID = "A", Name = "张三", Age = 18 });
27+
db.Add(new TestEntity518() { ID = "B", Name = "李四", Age = 19 });
28+
db.Add(new TestEntity518() { ID = "C", Name = "王五", Age = 20 });
29+
db.SaveChanges();
30+
}
31+
32+
//开始测试
33+
using (var db = free.CreateDbContext())
34+
{
35+
var entities = db.Set<TestEntity518>().Where(t => true).ToDictionary(t => t.ID);
36+
37+
entities["A"].Age = 25;
38+
db.Update(entities["A"]);
39+
40+
entities["B"].Age = 26;
41+
db.Update(entities["B"]);
42+
43+
entities["C"].Age = 27;
44+
//entities["C"].Name = "王五5"; //注释掉这一行就不会报错
45+
db.Update(entities["C"]);
46+
47+
db.Add(new TestEntity518() { ID = "D", Name = "马六", Age = 30 });
48+
49+
db.SaveChanges();
50+
}
51+
}
52+
class TestEntity518
53+
{
54+
public string ID { get; set; }
55+
public string Name { get; set; }
56+
public int Age { get; set; }
57+
}
58+
}
59+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using FreeSql.DataAnnotations;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Text;
7+
using System.Threading;
8+
using Xunit;
9+
10+
namespace FreeSql.Tests.Issues
11+
{
12+
public class _519
13+
{
14+
[Fact]
15+
public void SelectTest()
16+
{
17+
IFreeSql fsql = g.sqlserver;
18+
19+
fsql.Delete<ST_Stock519>().Where("1=1").ExecuteAffrows();
20+
fsql.Insert(new[]
21+
{
22+
new ST_Stock519 { StoreHouse = "001", Works = "101", MaterialCode = "201", BatchCode = "301", CreatedTime = DateTime.Now },
23+
new ST_Stock519 { StoreHouse = "002", Works = "102", MaterialCode = "202", BatchCode = "302", CreatedTime = DateTime.Now },
24+
new ST_Stock519 { StoreHouse = "003", Works = "103", MaterialCode = "203", BatchCode = "303", CreatedTime = DateTime.Now }
25+
}).ExecuteAffrows();
26+
27+
var list = fsql.Select<ST_Stock519>().ToList();
28+
var sql1 = fsql.Insert(list).NoneParameter().ToSql();
29+
var sql2 = fsql.Update<ST_Stock519>().SetSource(list).NoneParameter().ToSql();
30+
var sql3 = fsql.InsertOrUpdate<ST_Stock519>().SetSource(list).ToSql();
31+
}
32+
class ST_Stock519
33+
{
34+
/// <summary>
35+
/// 库位
36+
/// </summary>
37+
[Column(IsPrimary = true, DbType = "varchar(50)")]
38+
public string StoreHouse { get; set; } = string.Empty;
39+
40+
/// <summary>
41+
/// 工厂
42+
/// </summary>
43+
[Column(IsPrimary = true, DbType = "varchar(50)")]
44+
public string Works { get; set; } = string.Empty;
45+
/// <summary>
46+
/// 物料号
47+
/// </summary>
48+
[Column(IsPrimary = true, DbType = "varchar(50)")]
49+
public string MaterialCode { get; set; } = string.Empty;
50+
/// <summary>
51+
/// 条码号
52+
/// </summary>
53+
[Column(IsPrimary = true, DbType = "varchar(50)")]
54+
public string BatchCode { get; set; } = string.Empty;
55+
56+
/// <summary>
57+
/// 创建时间
58+
/// </summary>
59+
public DateTime? CreatedTime { get; set; }
60+
61+
/// <summary>
62+
/// 创建人
63+
/// </summary>
64+
public string CreatorID { get; set; } = string.Empty;
65+
66+
/// <summary>
67+
/// 创建人名称
68+
/// </summary>
69+
public string CreatorName { get; set; } = string.Empty;
70+
}
71+
}
72+
}

FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void WriteSourceSelectUnionAll(List<T1> source, StringBuilder sb, List<Db
166166
else
167167
{
168168
object val = col.GetDbValue(d);
169-
sb.Append(_commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col.Attribute.MapType, val));
169+
sb.Append(_commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col, col.Attribute.MapType, val));
170170
}
171171
if (didx == 0) sb.Append(" as ").Append(col.Attribute.Name);
172172
++colidx2;

FreeSql/Internal/CommonProvider/InsertProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ public string ToSqlValuesOrSelectUnionAllExtension102(bool isValues, Action<obje
571571
object val = col.GetDbValue(d);
572572
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
573573
if (_noneParameter)
574-
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col.Attribute.MapType, val));
574+
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
575575
else
576576
{
577577
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));

FreeSql/Internal/CommonProvider/UpdateProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ protected void SetPriv(ColumnInfo col, object value)
427427
_set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
428428
if (_noneParameter)
429429
{
430-
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, "u", col.Attribute.MapType, val));
430+
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, "u", col, col.Attribute.MapType, val));
431431
}
432432
else
433433
{
@@ -588,7 +588,7 @@ protected string WhereCaseSource(string CsName, Func<string, string> thenValue)
588588
var sb = new StringBuilder();
589589

590590
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
591-
sb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, col.GetDbValue(_source.First()))));
591+
sb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, col.GetDbValue(_source.First()))));
592592

593593
return sb.ToString();
594594

@@ -612,7 +612,7 @@ protected string WhereCaseSource(string CsName, Func<string, string> thenValue)
612612
ToSqlWhen(cwsb, _table.Primarys, d);
613613
cwsb.Append(" THEN ");
614614
var val = col.GetDbValue(d);
615-
cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, val)));
615+
cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val)));
616616
if (val == null || val == DBNull.Value) nulls++;
617617
}
618618
cwsb.Append(" END");
@@ -685,7 +685,7 @@ public string ToSql()
685685
{
686686
var val = col.GetDbValue(_source.First());
687687
if (_noneParameter)
688-
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, val));
688+
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val));
689689
else
690690
{
691691
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
@@ -730,7 +730,7 @@ public string ToSql()
730730
cwsb.Append(" THEN ");
731731
var val = col.GetDbValue(d);
732732
if (_noneParameter)
733-
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, val));
733+
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val));
734734
else
735735
{
736736
cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));

FreeSql/Internal/CommonUtils.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace FreeSql.Internal
2323
public abstract class CommonUtils
2424
{
2525

26-
public abstract string GetNoneParamaterSqlValue(List<DbParameter> specialParams, string specialParamFlag, Type type, object value);
26+
public abstract string GetNoneParamaterSqlValue(List<DbParameter> specialParams, string specialParamFlag, ColumnInfo col, Type type, object value);
2727
public abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value);
2828
public abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
2929
public abstract string FormatSql(string sql, params object[] args);
@@ -267,7 +267,7 @@ public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
267267
var pk1 = primarys.FirstOrDefault();
268268
if (primarys.Length == 1 && (type == pk1.CsType || type.IsNumberType() && pk1.CsType.IsNumberType()))
269269
{
270-
return $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {this.FormatSql("{0}", Utils.GetDataReaderValue(pk1.Attribute.MapType, dywhere))}";
270+
return $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, Utils.GetDataReaderValue(pk1.Attribute.MapType, dywhere))}";
271271
}
272272
else if (primarys.Length > 0 && (type == table.Type || type.BaseType == table.Type))
273273
{
@@ -276,15 +276,15 @@ public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
276276
foreach (var pk in primarys)
277277
{
278278
if (pkidx > 0) sb.Append(" AND ");
279-
sb.Append(aliasAndDot).Append(this.QuoteSqlName(pk.Attribute.Name));
280-
sb.Append(this.FormatSql(" = {0}", pk.GetDbValue(dywhere)));
279+
sb.Append(aliasAndDot).Append(this.QuoteSqlName(pk.Attribute.Name)).Append(" = ");
280+
sb.Append(GetNoneParamaterSqlValue(null, null, pk, pk.Attribute.MapType, pk.GetDbValue(dywhere)));
281281
++pkidx;
282282
}
283283
return sb.ToString();
284284
}
285285
else if (primarys.Length == 1 && type == typeof(string))
286286
{
287-
return $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {this.FormatSql("{0}", Utils.GetDataReaderValue(pk1.Attribute.MapType, dywhere))}";
287+
return $"{aliasAndDot}{this.QuoteSqlName(pk1.Attribute.Name)} = {GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, Utils.GetDataReaderValue(pk1.Attribute.MapType, dywhere))}";
288288
}
289289
else if (primarys.Length == 1 && dywhere is IEnumerable)
290290
{
@@ -303,8 +303,8 @@ public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
303303
var itype = i.GetType();
304304
isEntityType = (itype == table.Type || itype.BaseType == table.Type);
305305
}
306-
if (isEntityType) sb.Append(this.FormatSql("{0}", primarys[0].GetDbValue(i)));
307-
else sb.Append(this.FormatSql("{0}", Utils.GetDataReaderValue(pk1.Attribute.MapType, i)));
306+
if (isEntityType) sb.Append(GetNoneParamaterSqlValue(null, null, primarys[0], primarys[0].Attribute.MapType, primarys[0].GetDbValue(i)));
307+
else sb.Append(GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, Utils.GetDataReaderValue(pk1.Attribute.MapType, i)));
308308
++ieidx;
309309
}
310310
if (isAny == false) return "";
@@ -338,8 +338,8 @@ public string WhereObject(TableInfo table, string aliasAndDot, object dywhere)
338338
if (trycol == null) continue;
339339

340340
if (psidx > 0) sb.Append(" AND ");
341-
sb.Append(aliasAndDot).Append(this.QuoteSqlName(trycol.Attribute.Name));
342-
sb.Append(this.FormatSql(" = {0}", Utils.GetDataReaderValue(trycol.Attribute.MapType, p.GetValue(dywhere, null))));
341+
sb.Append(aliasAndDot).Append(this.QuoteSqlName(trycol.Attribute.Name)).Append(" = ");
342+
sb.Append(GetNoneParamaterSqlValue(null, null, trycol, trycol.Attribute.MapType, Utils.GetDataReaderValue(trycol.Attribute.MapType, p.GetValue(dywhere, null))));
343343
++psidx;
344344
}
345345
if (psidx == 0) return "";
@@ -360,8 +360,8 @@ public string WhereItems<TEntity>(TableInfo table, string aliasAndDot, IEnumerab
360360
sbin.Append(aliasAndDot).Append(this.QuoteSqlName(pk1.Attribute.Name));
361361
var indt = its.Select(a => pk1.GetDbValue(a)).Where(a => a != null).ToArray();
362362
if (indt.Any() == false) return null;
363-
if (indt.Length == 1) sbin.Append(" = ").Append(this.FormatSql("{0}", indt.First()));
364-
else sbin.Append(" IN (").Append(string.Join(",", indt.Select(a => this.FormatSql("{0}", a)))).Append(")");
363+
if (indt.Length == 1) sbin.Append(" = ").Append(GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, indt.First()));
364+
else sbin.Append(" IN (").Append(string.Join(",", indt.Select(a => GetNoneParamaterSqlValue(null, null, pk1, pk1.Attribute.MapType, a)))).Append(")");
365365
return sbin.ToString();
366366
}
367367
var dicpk = its.Length > 5 ? new Dictionary<string, bool>() : null;
@@ -371,7 +371,7 @@ public string WhereItems<TEntity>(TableInfo table, string aliasAndDot, IEnumerab
371371
{
372372
var filter = "";
373373
foreach (var pk in table.Primarys)
374-
filter += $" AND {aliasAndDot}{this.QuoteSqlName(pk.Attribute.Name)} = {this.FormatSql("{0}", pk.GetDbValue(item))}";
374+
filter += $" AND {aliasAndDot}{this.QuoteSqlName(pk.Attribute.Name)} = {GetNoneParamaterSqlValue(null, null, pk, pk.Attribute.MapType, pk.GetDbValue(item))}";
375375
if (string.IsNullOrEmpty(filter)) continue;
376376
if (sb != null)
377377
{

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common)
187187
}
188188
try
189189
{
190-
col.DbDefaultValue = common.GetNoneParamaterSqlValue(new List<DbParameter>(), "init", colattr.MapType, defaultValue);
190+
col.DbDefaultValue = common.GetNoneParamaterSqlValue(new List<DbParameter>(), "init", col, colattr.MapType, defaultValue);
191191
}
192192
catch
193193
{

Providers/FreeSql.Provider.Dameng/Curd/DamengInsert.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override string ToSql()
6969
object val = col.GetDbValue(d);
7070
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
7171
if (_noneParameter)
72-
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col.Attribute.MapType, val));
72+
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col, col.Attribute.MapType, val));
7373
else
7474
{
7575
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));

Providers/FreeSql.Provider.Dameng/DamengUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public override string TrimQuoteSqlName(string name)
9393
public override string QuoteWriteParamter(Type type, string paramterName) => paramterName;
9494
public override string QuoteReadColumn(Type type, Type mapType, string columnName) => columnName;
9595

96-
public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams, string specialParamFlag, Type type, object value)
96+
public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams, string specialParamFlag, ColumnInfo col, Type type, object value)
9797
{
9898
if (value == null) return "NULL";
9999
if (type.IsNumberType()) return string.Format(CultureInfo.InvariantCulture, "{0}", value);

0 commit comments

Comments
 (0)