Skip to content

Commit

Permalink
test RereadSql/RewriteSql dotnetcore#614
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 21, 2020
1 parent c905278 commit 960da5a
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 8 deletions.
211 changes: 211 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,217 @@ namespace FreeSql.Tests.SqlServer
{
public class SqlServerCodeFirstTest
{
[Fact]
public void GeographyCrud()
{
var fsql = g.sqlserver;
fsql.Delete<ts_geocrud01>().Where("1=1").ExecuteAffrows();
var id1 = Guid.NewGuid();
var geo1 = "LINESTRING (-122.36 47.656, -122.343 47.656)";
Assert.Equal(1, fsql.Insert(new ts_geocrud01 { id = id1, geo = geo1 }).ExecuteAffrows());
var item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

//NoneParameter
fsql.Delete<ts_geocrud01>().Where("1=1").ExecuteAffrows();
id1 = Guid.NewGuid();
geo1 = "LINESTRING (-122.36 47.656, -122.343 47.656)";
Assert.Equal(1, fsql.Insert(new ts_geocrud01 { id = id1, geo = geo1 }).NoneParameter().ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

var item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
var item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
var item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
var item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
var item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//Update SetSource
geo1 = "COMPOUNDCURVE (CIRCULARSTRING (-122.358 47.653, -122.348 47.649, -122.348 47.658), CIRCULARSTRING (-122.348 47.658, -122.358 47.658, -122.358 47.653))";
item1.geo = geo1;
Assert.Equal(1, fsql.Update<ts_geocrud01>().SetSource(item1).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//Update SetSource NoneParameter
geo1 = "COMPOUNDCURVE (CIRCULARSTRING (-122.358 47.653, -122.348 47.649, -122.348 47.658), CIRCULARSTRING (-122.348 47.658, -122.358 47.658, -122.358 47.653))";
item1.geo = geo1;
Assert.Equal(1, fsql.Update<ts_geocrud01>().SetSource(item1).NoneParameter().ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//Update Set
geo1 = "POLYGON ((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))";
Assert.Equal(1, fsql.Update<ts_geocrud01>().Where(a => a.id == id1).Set(a => a.geo, geo1).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//Update Set NoneParameter
geo1 = "POLYGON ((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))";
Assert.Equal(1, fsql.Update<ts_geocrud01>().NoneParameter().Where(a => a.id == id1).Set(a => a.geo, geo1).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//Update Set Multi
geo1 = "LINESTRING (-122.36 47.656, -122.343 47.656)";
Assert.Equal(1, fsql.Update<ts_geocrud01>().Where(a => a.id == id1).Set(a => new
{
geo = geo1
}).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//Update Set Multi NoneParameter
geo1 = "LINESTRING (-122.36 47.656, -122.343 47.656)";
Assert.Equal(1, fsql.Update<ts_geocrud01>().NoneParameter().Where(a => a.id == id1).Set(a => new
{
geo = geo1
}).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First();
Assert.NotNull(item1);
Assert.Equal(id1, item1.id);
Assert.Equal(geo1, item1.geo);

item2 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => a.geo);
Assert.Equal(geo1, item2);
item3 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new { a.geo });
Assert.Equal(geo1, item3.geo);
item4 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocrud01 { geo = a.geo });
Assert.Equal(geo1, item4.geo);
item5 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).ToList<ts_geocurd01_dto1>().FirstOrDefault();
Assert.Equal(geo1, item5.geo);
item6 = fsql.Select<ts_geocrud01>().Where(a => a.id == id1).First(a => new ts_geocurd01_dto1 { geo = a.geo });
Assert.Equal(geo1, item6.geo);

//批量
fsql.Delete<ts_geocrud01>().Where("1=1").ExecuteAffrows();
id1 = Guid.NewGuid();
geo1 = "LINESTRING (-122.36 47.656, -122.343 47.656)";
var id2 = Guid.NewGuid();
var geo2 = "POLYGON ((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))";
Assert.Equal(2, fsql.Insert(new[] { new ts_geocrud01 { id = id1, geo = geo1 }, new ts_geocrud01 { id = id2, geo = geo2 } }).ExecuteAffrows());
var items = fsql.Select<ts_geocrud01>().ToList();
Assert.Equal(2, items.Count);

items[0].geo = "POLYGON EMPTY";
items[1].geo = "LINESTRING (0 0, 2 2, 1 0)";
Assert.Equal(2, fsql.Update<ts_geocrud01>().SetSource(items).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == items[0].id).First();
Assert.NotNull(item1);
Assert.Equal(items[0].id, item1.id);
Assert.Equal(items[0].geo, item1.geo);
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == items[1].id).First();
Assert.NotNull(item1);
Assert.Equal(items[1].id, item1.id);
Assert.Equal(items[1].geo, item1.geo);

//批量 NoneParameter
fsql.Delete<ts_geocrud01>().Where("1=1").ExecuteAffrows();
id1 = Guid.NewGuid();
geo1 = "LINESTRING (-122.36 47.656, -122.343 47.656)";
id2 = Guid.NewGuid();
geo2 = "POLYGON ((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))";
Assert.Equal(2, fsql.Insert(new[] { new ts_geocrud01 { id = id1, geo = geo1 }, new ts_geocrud01 { id = id2, geo = geo2 } }).NoneParameter().ExecuteAffrows());
items = fsql.Select<ts_geocrud01>().ToList();
Assert.Equal(2, items.Count);

items[0].geo = "POLYGON EMPTY";
items[1].geo = "LINESTRING (0 0, 2 2, 1 0)";
Assert.Equal(2, fsql.Update<ts_geocrud01>().NoneParameter().SetSource(items).ExecuteAffrows());
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == items[0].id).First();
Assert.NotNull(item1);
Assert.Equal(items[0].id, item1.id);
Assert.Equal(items[0].geo, item1.geo);
item1 = fsql.Select<ts_geocrud01>().Where(a => a.id == items[1].id).First();
Assert.NotNull(item1);
Assert.Equal(items[1].id, item1.id);
Assert.Equal(items[1].geo, item1.geo);
}
class ts_geocrud01
{
public Guid id { get; set; }
[Column(DbType = "geography", RewriteSql = "geography::STGeomFromText({0},4236)", RereadSql = "{0}.STAsText()")]
public string geo { get; set; }
}
class ts_geocurd01_dto1
{
public string geo { get; set; }
}

[Fact]
public void InsertUpdateParameter()
{
Expand Down
26 changes: 26 additions & 0 deletions FreeSql/DataAnnotations/ColumnFluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,31 @@ public ColumnFluent Precision(int precision, int scale = 0)
_column.Scale = scale;
return this;
}

/// <summary>
/// 重写功能<para></para>
/// 比如:[Column(RewriteSql = &quot;geography::STGeomFromText({0},4236)&quot;)]<para></para>
/// 插入:INSERT INTO [table]([geo]) VALUES(geography::STGeomFromText('...',4236))<para></para>
/// 提示:更新也生效
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ColumnFluent RewriteSql(string value)
{
_column.RewriteSql = value;
return this;
}
/// <summary>
/// 重读功能<para></para>
/// 比如:[Column(RereadSql = &quot;{0}.STAsText()&quot;)]<para></para>
/// 查询:SELECT a.[id], a.[geo].STAsText() FROM [table] a
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ColumnFluent RereadSql(string value)
{
_column.RereadSql = value;
return this;
}
}
}
19 changes: 19 additions & 0 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,21 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
return false;
}
if (parent.CsType == null) parent.CsType = exp.Type;
parent.DbField = ExpressionLambdaToSql(exp, getTSC());
field.Append(", ").Append(parent.DbField);
var pdbfield = parent.DbField = ExpressionLambdaToSql(exp, getTSC());
if (parent.MapType == null || _tables?.Any(a => a.Table.IsRereadSql) == true)
{
var findcol = SearchColumnByField(_tables, null, parent.DbField);
if (parent.MapType == null) parent.MapType = findcol?.Attribute.MapType ?? exp.Type;
if (findcol != null) pdbfield = _common.RereadColumn(findcol, pdbfield);
}
field.Append(", ").Append(pdbfield);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false)
{
var csname = GetFieldAsCsName(parent.CsName);
if (parent.DbField.EndsWith(csname, StringComparison.CurrentCultureIgnoreCase) == false) //DbField 和 CsName 相同的时候,不处理
field.Append(_common.FieldAsAlias(csname));
}
if (parent.MapType == null) parent.MapType = SearchColumnByField(_tables, null, parent.DbField)?.Attribute.MapType ?? exp.Type;
return false;
}
return false;
Expand Down Expand Up @@ -254,7 +259,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
else
{
child.DbField = $"{dtTb.Alias}.{_common.QuoteSqlName(trydtocol.Attribute.Name)}";
field.Append(", ").Append(child.DbField);
field.Append(", ").Append(_common.RereadColumn(trydtocol, child.DbField));
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
}
break;
Expand Down Expand Up @@ -336,7 +341,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
ReadAnonymousField(_tables, field, child, ref index, Expression.Property(dtTb.Parameter, dtTb.Table.Properties[trydtocol.CsName]), select, diymemexp, whereGlobalFilter, findIncludeMany, isAllDtoMap);
else
{
child.DbField = $"{dtTb.Alias}.{_common.QuoteSqlName(trydtocol.Attribute.Name)}";
child.DbField = _common.RereadColumn(trydtocol, $"{dtTb.Alias}.{_common.QuoteSqlName(trydtocol.Attribute.Name)}");
field.Append(", ").Append(child.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
}
Expand Down
4 changes: 2 additions & 2 deletions FreeSql/Internal/CommonProvider/UpdateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public string ToSql()

var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
_set.Append(_commonUtils.RewriteColumn(col, colsql));
sb.Append(_commonUtils.RewriteColumn(col, colsql));
if (_noneParameter == false)
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
}
Expand Down Expand Up @@ -742,7 +742,7 @@ public string ToSql()

var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
_commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
_set.Append(_commonUtils.RewriteColumn(col, colsql));
cwsb.Append(_commonUtils.RewriteColumn(col, colsql));
if (_noneParameter == false)
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
if (val == null || val == DBNull.Value) nulls++;
Expand Down
Loading

0 comments on commit 960da5a

Please sign in to comment.