Skip to content
Permalink
Browse files

- 增加 实体特性 [Column(ServerTime = DateTimeKind.Utc)] 使用数据库时间执行插入数据;

- 修复 ToList(a => new Dto { .. }) 在使用 GroupBy 之后报错的 bug;
- 修复 注释迁移到数据库,在 asp.net 4.7 无效的问题;
  • Loading branch information
28810 28810
28810 authored and 28810 committed Nov 25, 2019
1 parent 27acd4d commit 01b31c095b816aa71c6a93479abc81a2ee4af56f
Showing with 332 additions and 146 deletions.
  1. +1 −0 Examples/base_entity/Program.cs
  2. +7 −0 FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/MySqlCodeFirstTest.cs
  3. +7 −0 FreeSql.Tests/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
  4. +9 −0 FreeSql.Tests/FreeSql.Tests/Oracle/OracleCodeFirstTest.cs
  5. +7 −0 FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLCodeFirstTest.cs
  6. +10 −1 FreeSql.Tests/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs
  7. +9 −0 FreeSql.Tests/FreeSql.Tests/Sqlite/SqliteCodeFirstTest.cs
  8. +3 −3 FreeSql.Tests/FreeSql.Tests/UnitTest1.cs
  9. +35 −0 FreeSql.Tests/FreeSql.Tests/UnitTest2.cs
  10. +13 −5 FreeSql/DataAnnotations/ColumnAttribute.cs
  11. +11 −0 FreeSql/DataAnnotations/ColumnFluent.cs
  12. +1 −1 FreeSql/Extensions/FreeSqlGlobalExtensions.cs
  13. +12 −5 FreeSql/FreeSql.xml
  14. +1 −1 FreeSql/Internal/CommonExpression.cs
  15. +10 −5 FreeSql/Internal/CommonProvider/InsertProvider.cs
  16. +15 −5 FreeSql/Internal/CommonUtils.cs
  17. +2 −0 FreeSql/Internal/Model/ColumnInfo.cs
  18. +34 −8 FreeSql/Internal/UtilsExpressionTree.cs
  19. +16 −12 Providers/FreeSql.Provider.MySql/MySqlCodeFirst.cs
  20. +2 −2 Providers/FreeSql.Provider.MySql/MySqlExpression.cs
  21. +2 −0 Providers/FreeSql.Provider.MySql/MySqlUtils.cs
  22. +2 −0 Providers/FreeSql.Provider.MySqlConnector/MySqlConnectorUtils.cs
  23. +2 −0 Providers/FreeSql.Provider.Odbc/Default/OdbcUtils.cs
  24. +16 −12 Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlCodeFirst.cs
  25. +2 −2 Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlExpression.cs
  26. +2 −0 Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlUtils.cs
  27. +10 −5 Providers/FreeSql.Provider.Odbc/Oracle/Curd/OdbcOracleInsert.cs
  28. +5 −6 Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleCodeFirst.cs
  29. +2 −2 Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleExpression.cs
  30. +2 −0 Providers/FreeSql.Provider.Odbc/Oracle/OdbcOracleUtils.cs
  31. +7 −3 Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs
  32. +2 −2 Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLExpression.cs
  33. +2 −0 Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLUtils.cs
  34. +13 −19 Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerCodeFirst.cs
  35. +2 −2 Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerExpression.cs
  36. +2 −0 Providers/FreeSql.Provider.Odbc/SqlServer/OdbcSqlServerUtils.cs
  37. +10 −5 Providers/FreeSql.Provider.Oracle/Curd/OracleInsert.cs
  38. +4 −4 Providers/FreeSql.Provider.Oracle/OracleCodeFirst.cs
  39. +2 −2 Providers/FreeSql.Provider.Oracle/OracleExpression.cs
  40. +2 −0 Providers/FreeSql.Provider.Oracle/OracleUtils.cs
  41. +7 −3 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs
  42. +2 −2 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs
  43. +2 −0 Providers/FreeSql.Provider.PostgreSQL/PostgreSQLUtils.cs
  44. +13 −19 Providers/FreeSql.Provider.SqlServer/SqlServerCodeFirst.cs
  45. +2 −2 Providers/FreeSql.Provider.SqlServer/SqlServerExpression.cs
  46. +2 −0 Providers/FreeSql.Provider.SqlServer/SqlServerUtils.cs
  47. +4 −6 Providers/FreeSql.Provider.Sqlite/SqliteCodeFirst.cs
  48. +2 −2 Providers/FreeSql.Provider.Sqlite/SqliteExpression.cs
  49. +2 −0 Providers/FreeSql.Provider.Sqlite/SqliteUtils.cs
@@ -39,6 +39,7 @@ static void Main(string[] args)
.UseNoneCommandParameter(true)
.UseConnectionString(FreeSql.DataType.Sqlite, "data source=test.db;max pool size=5")
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=2")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3")
.Build();
BaseEntity.Initialization(fsql);
#endregion
@@ -36,6 +36,7 @@ class

public string 标题 { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
}

@@ -458,7 +459,10 @@ class TableAllType
public float testFieldFloat { get; set; }
public decimal testFieldDecimal { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }

public byte[] testFieldBytes { get; set; }
public string testFieldString { get; set; }
public Guid testFieldGuid { get; set; }
@@ -476,7 +480,10 @@ class TableAllType
public float? testFieldFloatNullable { get; set; }
public decimal? testFieldDecimalNullable { get; set; }
public TimeSpan? testFieldTimeSpanNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }

public Guid? testFieldGuidNullable { get; set; }

public MygisPoint testFieldPoint { get; set; }
@@ -36,6 +36,7 @@ class

public string 标题 { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
}

@@ -464,7 +465,10 @@ class TableAllType
public float testFieldFloat { get; set; }
public decimal testFieldDecimal { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }

public byte[] testFieldBytes { get; set; }
public string testFieldString { get; set; }
public Guid testFieldGuid { get; set; }
@@ -482,7 +486,10 @@ class TableAllType
public float? testFieldFloatNullable { get; set; }
public decimal? testFieldDecimalNullable { get; set; }
public TimeSpan? testFieldTimeSpanNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }

public Guid? testFieldGuidNullable { get; set; }

public MygisPoint testFieldPoint { get; set; }
@@ -36,6 +36,7 @@ class

public string 标题 { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
}

@@ -222,8 +223,12 @@ class TableAllType
public float Float { get; set; }
public decimal Decimal { get; set; }
public TimeSpan TimeSpan { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime DateTime { get; set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTime DateTimeOffSet { get; set; }

public byte[] Bytes { get; set; }
public string String { get; set; }
public Guid Guid { get; set; }
@@ -241,8 +246,12 @@ class TableAllType
public float? FloatNullable { get; set; }
public decimal? DecimalNullable { get; set; }
public TimeSpan? TimeSpanNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? DateTimeNullable { get; set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTime? DateTimeOffSetNullable { get; set; }

public Guid? GuidNullable { get; set; }

public TableAllTypeEnumType1 Enum1 { get; set; }
@@ -43,6 +43,7 @@ class

public string 标题 { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
}

@@ -367,7 +368,10 @@ class TableAllType
public float testFieldFloat { get; set; }
public decimal testFieldDecimal { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }

public byte[] testFieldBytes { get; set; }
public string testFieldString { get; set; }
public Guid testFieldGuid { get; set; }
@@ -397,7 +401,10 @@ class TableAllType
public float? testFieldFloatNullable { get; set; }
public decimal? testFieldDecimalNullable { get; set; }
public TimeSpan? testFieldTimeSpanNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }

public Guid? testFieldGuidNullable { get; set; }
public NpgsqlPoint? testFieldNpgsqlPointNullable { get; set; }
public NpgsqlLine? testFieldNpgsqlLineNullable { get; set; }
@@ -37,6 +37,7 @@ class

public string 标题 { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
}

@@ -159,7 +160,7 @@ public void CurdAllField()
var sqlTestUpdate = g.sqlserver.Update<TableAllType>().SetSource(item3NP).NoneParameter().ToSql();

var item3 = insert.AppendData(item2).ExecuteInserted();
var newitem2 = select.Where(a => a.Id == item2.Id).ToOne();
var newitem2 = select.Where(a => a.Id == item3NP[0].Id).ToOne();

var items = select.ToList();
}
@@ -344,8 +345,12 @@ class TableAllType
public float testFieldFloat { get; set; }
public decimal testFieldDecimal { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTimeOffset testFieldDateTimeOffset { get; set; }

public byte[] testFieldBytes { get; set; }
public string testFieldString { get; set; }
public Guid testFieldGuid { get; set; }
@@ -363,8 +368,12 @@ class TableAllType
public float? testFieldFloatNullable { get; set; }
public decimal? testFieldDecimalNullable { get; set; }
public TimeSpan? testFieldTimeSpanNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTimeOffset? testFieldDateTimeNullableOffset { get; set; }

public Guid? testFieldGuidNullable { get; set; }

public TableAllTypeEnumType1 testFieldEnum1 { get; set; }
@@ -36,6 +36,7 @@ class

public string 标题 { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
}

@@ -262,8 +263,12 @@ class TableAllType
public float Float { get; set; }
public decimal Decimal { get; set; }
public TimeSpan TimeSpan { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime DateTime { get; set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTime DateTimeOffSet { get; set; }

public byte[] Bytes { get; set; }
public string String { get; set; }
public Guid Guid { get; set; }
@@ -281,8 +286,12 @@ class TableAllType
public float? FloatNullable { get; set; }
public decimal? DecimalNullable { get; set; }
public TimeSpan? TimeSpanNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? DateTimeNullable { get; set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTime? DateTimeOffSetNullable { get; set; }

public Guid? GuidNullable { get; set; }

public TableAllTypeEnumType1 Enum1 { get; set; }
@@ -670,10 +670,10 @@ .ToList< TestDto>(a => new TestDto()
.From<Templates>((a, b) => a.InnerJoin(aa => aa.TemplatesId
== b.Id2))
.GroupBy((a, b) => b.Code)
.ToSql(a => new
.ToSql(a => new NewsArticleDto
{
a.Key,
sss = a.Sum(a.Value.Item1.OptionsEntity04)
ArticleTitle = a.Key,
ChannelId = a.Sum(a.Value.Item1.OptionsEntity04)
});

var testgrpsql2 = g.sqlite.Select<TaskBuild>()
@@ -183,9 +183,44 @@ public class TestMySqlStringIsNullable
public string varchar_notnull { get; set; }
}

public class TestIgnoreDefaultValue {
public Guid Id { get; set; }

[Column(IsIgnore = true)]
public double? quantity { get; set; } = 100f;

public DateTime ct1 { get; set; }
public DateTime? ct2 { get; set; }
}

[Fact]
public void Test02()
{
var serverTime = g.pgsql.Select<TestIgnoreDefaultValue>().Limit(1).First(a => DateTime.UtcNow);
var timeOffset = DateTime.UtcNow.Subtract(serverTime); //减去数据库时间

g.pgsql.Aop.AuditValue += new EventHandler<Aop.AuditValueEventArgs>((_, e) =>
{
if (e.Column.Attribute.MapType.NullableTypeOrThis() == typeof(DateTime))
{
if (e.Value == null || (DateTime)e.Value == default(DateTime))
{
e.Value = DateTime.Now.Subtract(timeOffset);
return;
}
}
});


g.pgsql.Delete<TestIgnoreDefaultValue>().Where("1=1").ExecuteAffrows();
g.pgsql.GetRepository<TestIgnoreDefaultValue>().Insert(new TestIgnoreDefaultValue[]
{
new TestIgnoreDefaultValue(),
new TestIgnoreDefaultValue(),
new TestIgnoreDefaultValue()
});
var testttt = g.pgsql.Select<TestIgnoreDefaultValue>().Limit(10).ToList();

var slsksd = g.mysql.Update<UserLike>().SetSource(new UserLike { Id = Guid.NewGuid(), CreateUserId = 1000, SubjectId = Guid.NewGuid() })
.UpdateColumns(a => new
{
@@ -43,11 +43,6 @@ public class ColumnAttribute : Attribute
/// </summary>
public bool IsVersion { get => _IsVersion ?? false; set => _IsVersion = value; }

/// <summary>
/// 数据库默认值
/// </summary>
public object DbDefautValue { get; internal set; }

/// <summary>
/// 类型映射,除了可做基本的类型映射外,特别介绍的功能:<para></para>
/// 1、将 enum 属性映射成 typeof(string)<para></para>
@@ -76,5 +71,18 @@ public class ColumnAttribute : Attribute
/// 该字段是否可以更新,默认值true,指定为false更新时该字段会被忽略
/// </summary>
public bool CanUpdate { get => _CanUpdate ?? true; set => _CanUpdate = value; }

internal DateTimeKind? _ServerTime;
/// <summary>
/// 标记属性为数据库服务器时间(utc/local),在插入的时候使用类似 getdate() 执行
/// </summary>
public DateTimeKind ServerTime
{
get => _ServerTime ?? DateTimeKind.Local;
set
{
_ServerTime = value == DateTimeKind.Unspecified ? DateTimeKind.Local : value;
}
}
}
}
@@ -124,5 +124,16 @@ public ColumnFluent CanUpdate(bool value)
_column.CanUpdate = value;
return this;
}

/// <summary>
/// 标记属性为数据库服务器时间(utc/local),在插入的时候使用类似 getdate() 执行
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public ColumnFluent ServerTime(DateTimeKind value)
{
_column.ServerTime = value;
return this;
}
}
}
@@ -39,7 +39,7 @@ public static partial class FreeSqlGlobalExtensions
});
public static bool IsIntegerType(this Type that) => that == null ? false : (dicIsNumberType.Value.TryGetValue(that, out var tryval) ? tryval : false);
public static bool IsNumberType(this Type that) => that == null ? false : dicIsNumberType.Value.ContainsKey(that);
public static bool IsNullableType(this Type that) => that?.FullName.StartsWith("System.Nullable`1[") == true;
public static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true;
public static bool IsAnonymousType(this Type that) => that?.FullName.StartsWith("<>f__AnonymousType") == true;
public static bool IsArrayOrList(this Type that) => that == null ? false : (that.IsArray || typeof(IList).IsAssignableFrom(that));
public static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;

Some generated files are not rendered by default. Learn more.

@@ -110,7 +110,7 @@ public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder fiel
parent.Consturctor = initExp.NewExpression.Type.GetConstructors()[0];
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;

if (initExp.NewExpression.Type != _tables.FirstOrDefault()?.Table.Type)
if (_tables != null && _tables.Any() && initExp.NewExpression.Type != _tables.FirstOrDefault().Table.Type)
{
//dto 映射
var dtoProps = initExp.NewExpression.Type.GetPropertiesDictIgnoreCase().Values;
@@ -445,13 +445,18 @@ public string ToSqlValuesOrSelectUnionAll(bool isValues = true)
if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;

if (colidx2 > 0) sb.Append(", ");
object val = col.GetMapValue(d);
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, col.Attribute.MapType, val));
if (string.IsNullOrEmpty(col.DbInsertValue) == false)
sb.Append(col.DbInsertValue);
else
{
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
object val = col.GetMapValue(d);
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, col.Attribute.MapType, val);
}
}
++colidx2;
}

0 comments on commit 01b31c0

Please sign in to comment.
You can’t perform that action at this time.