代码调整

This commit is contained in:
28810 2019-01-25 23:14:50 +08:00
parent 4cc6f9a3ff
commit a6b2b80d44
17 changed files with 46 additions and 49 deletions

View File

@ -17,6 +17,6 @@ namespace FreeSql.DataAnnotations {
/// </summary> /// </summary>
public string SelectFilter { get; set; } public string SelectFilter { get; set; }
internal ConcurrentDictionary<string, ColumnAttribute> _columns = new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase); internal ConcurrentDictionary<string, ColumnAttribute> _columns => new ConcurrentDictionary<string, ColumnAttribute>(StringComparer.CurrentCultureIgnoreCase);
} }
} }

View File

@ -26,6 +26,7 @@ public static class FreeSqlGlobalExtensions {
[typeof(decimal)] = true, [typeof(decimal?)] = true [typeof(decimal)] = true, [typeof(decimal?)] = true
}); });
public static bool IsNumberType(this Type that) => that == null ? false : dicIsNumberType.Value.ContainsKey(that); 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;
/// <summary> /// <summary>
/// 测量两个经纬度的距离,返回单位:米 /// 测量两个经纬度的距离,返回单位:米

View File

@ -299,7 +299,7 @@ namespace FreeSql.Internal {
throw new Exception($"未现实函数表达式 {exp3} 解析"); throw new Exception($"未现实函数表达式 {exp3} 解析");
case ExpressionType.MemberAccess: case ExpressionType.MemberAccess:
var exp4 = exp as MemberExpression; var exp4 = exp as MemberExpression;
if (exp4.Expression != null && exp4.Expression.Type.IsArray == false && exp4.Expression.Type.FullName.StartsWith("System.Nullable`1[")) return ExpressionLambdaToSql(exp4.Expression, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName); if (exp4.Expression != null && exp4.Expression.Type.IsArray == false && exp4.Expression.Type.IsNullableType()) return ExpressionLambdaToSql(exp4.Expression, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
var extRet = ""; var extRet = "";
var memberType = exp4.Expression?.Type ?? exp4.Type; var memberType = exp4.Expression?.Type ?? exp4.Type;
switch (memberType.FullName) { switch (memberType.FullName) {

View File

@ -249,7 +249,7 @@ namespace FreeSql.Internal.CommonProvider {
public Func<DbDataReader, T1> Read { get; set; } public Func<DbDataReader, T1> Read { get; set; }
} }
protected GetAllFieldExpressionTreeInfo GetAllFieldExpressionTree() { protected GetAllFieldExpressionTreeInfo GetAllFieldExpressionTree() {
return _dicGetAllFieldExpressionTree.GetOrAdd(string.Join("+", _tables.Select(a => $"{a.Table.DbName}-{a.Alias}-{a.Type}")), s => { return _dicGetAllFieldExpressionTree.GetOrAdd(string.Join("+", _tables.Select(a => $"{_commonUtils.DbName}-{a.Table.DbName}-{a.Alias}-{a.Type}")), s => {
var tb1 = _tables.First().Table; var tb1 = _tables.First().Table;
var type = tb1.TypeLazy ?? tb1.Type; var type = tb1.TypeLazy ?? tb1.Type;
var props = tb1.Properties; var props = tb1.Properties;
@ -313,7 +313,7 @@ namespace FreeSql.Internal.CommonProvider {
); );
else { else {
var proptypeGeneric = prop.PropertyType; var proptypeGeneric = prop.PropertyType;
if (proptypeGeneric.FullName.StartsWith("System.Nullable`1[")) proptypeGeneric = proptypeGeneric.GenericTypeArguments.First(); if (proptypeGeneric.IsNullableType()) proptypeGeneric = proptypeGeneric.GenericTypeArguments.First();
if (proptypeGeneric.IsEnum || if (proptypeGeneric.IsEnum ||
Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(Utils.RowInfo.Constructor, Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(Utils.RowInfo.Constructor,
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp)), Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp)),

View File

@ -85,11 +85,9 @@ namespace FreeSql.Internal {
}); });
colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type)); colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type));
if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue; if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue;
if (colattr.IsNullable == false && colattr.DbDefautValue == null) { if (colattr.IsNullable == false && colattr.DbDefautValue == null)
var consturctorType = p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType; colattr.DbDefautValue = Activator.CreateInstance(p.PropertyType.IsNullableType() ? p.PropertyType.GenericTypeArguments.FirstOrDefault() : p.PropertyType);
colattr.DbDefautValue = Activator.CreateInstance(consturctorType); if (colattr.IsIdentity == true && p.PropertyType.IsNumberType() == false)
}
if (colattr.IsIdentity == true && (p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType)?.IsNumberType() == false)
colattr.IsIdentity = false; colattr.IsIdentity = false;
var col = new ColumnInfo { var col = new ColumnInfo {
@ -538,7 +536,7 @@ namespace FreeSql.Internal {
), new[] { typeExp, indexesExp, rowExp, dataIndexExp }).Compile(); ), new[] { typeExp, indexesExp, rowExp, dataIndexExp }).Compile();
var typeGeneric = type; var typeGeneric = type;
if (typeGeneric.FullName.StartsWith("System.Nullable`1[")) typeGeneric = type.GenericTypeArguments.First(); if (typeGeneric.IsNullableType()) typeGeneric = type.GenericTypeArguments.First();
if (typeGeneric.IsEnum || if (typeGeneric.IsEnum ||
dicExecuteArrayRowReadClassOrTuple.ContainsKey(typeGeneric)) dicExecuteArrayRowReadClassOrTuple.ContainsKey(typeGeneric))
return Expression.Lambda<Func<Type, int[], DbDataReader, int, RowInfo>>( return Expression.Lambda<Func<Type, int[], DbDataReader, int, RowInfo>>(
@ -567,7 +565,7 @@ namespace FreeSql.Internal {
); );
else { else {
var fieldtypeGeneric = field.FieldType; var fieldtypeGeneric = field.FieldType;
if (fieldtypeGeneric.FullName.StartsWith("System.Nullable`1[")) fieldtypeGeneric = fieldtypeGeneric.GenericTypeArguments.First(); if (fieldtypeGeneric.IsNullableType()) fieldtypeGeneric = fieldtypeGeneric.GenericTypeArguments.First();
if (fieldtypeGeneric.IsEnum || if (fieldtypeGeneric.IsEnum ||
dicExecuteArrayRowReadClassOrTuple.ContainsKey(fieldtypeGeneric)) read2ExpAssign = Expression.New(RowInfo.Constructor, dicExecuteArrayRowReadClassOrTuple.ContainsKey(fieldtypeGeneric)) read2ExpAssign = Expression.New(RowInfo.Constructor,
GetDataReaderValueBlockExpression(field.FieldType, Expression.Call(rowExp, MethodDataReaderGetValue, dataIndexExp)), GetDataReaderValueBlockExpression(field.FieldType, Expression.Call(rowExp, MethodDataReaderGetValue, dataIndexExp)),
@ -653,7 +651,7 @@ namespace FreeSql.Internal {
); );
else { else {
var proptypeGeneric = ctorParm.ParameterType; var proptypeGeneric = ctorParm.ParameterType;
if (proptypeGeneric.FullName.StartsWith("System.Nullable`1[")) proptypeGeneric = proptypeGeneric.GenericTypeArguments.First(); if (proptypeGeneric.IsNullableType()) proptypeGeneric = proptypeGeneric.GenericTypeArguments.First();
if (proptypeGeneric.IsEnum || if (proptypeGeneric.IsEnum ||
dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(RowInfo.Constructor, dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(RowInfo.Constructor,
GetDataReaderValueBlockExpression(ctorParm.ParameterType, Expression.Call(rowExp, MethodDataReaderGetValue, dataIndexExp)), GetDataReaderValueBlockExpression(ctorParm.ParameterType, Expression.Call(rowExp, MethodDataReaderGetValue, dataIndexExp)),
@ -701,7 +699,7 @@ namespace FreeSql.Internal {
); );
else { else {
var proptypeGeneric = prop.PropertyType; var proptypeGeneric = prop.PropertyType;
if (proptypeGeneric.FullName.StartsWith("System.Nullable`1[")) proptypeGeneric = proptypeGeneric.GenericTypeArguments.First(); if (proptypeGeneric.IsNullableType()) proptypeGeneric = proptypeGeneric.GenericTypeArguments.First();
if (proptypeGeneric.IsEnum || if (proptypeGeneric.IsEnum ||
dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(RowInfo.Constructor, dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(RowInfo.Constructor,
GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(rowExp, MethodDataReaderGetValue, tryidxExp)), GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(rowExp, MethodDataReaderGetValue, tryidxExp)),
@ -825,7 +823,7 @@ namespace FreeSql.Internal {
) )
); );
} }
if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First(); if (type.IsNullableType()) type = type.GenericTypeArguments.First();
if (type.IsEnum) return Expression.Return(returnTarget, Expression.Call(MethodEnumParse, Expression.Constant(type, typeof(Type)), Expression.Call(MethodToString, valueExp), Expression.Constant(true, typeof(bool)))); if (type.IsEnum) return Expression.Return(returnTarget, Expression.Call(MethodEnumParse, Expression.Constant(type, typeof(Type)), Expression.Call(MethodToString, valueExp), Expression.Constant(true, typeof(bool))));
switch(type.FullName) { switch(type.FullName) {
case "System.Guid": return Expression.IfThenElse( case "System.Guid": return Expression.IfThenElse(
@ -927,7 +925,7 @@ namespace FreeSql.Internal {
// ), parmExp).Compile(); // ), parmExp).Compile();
// } // }
// if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First(); // if (type.IsNullableType()) type = type.GenericTypeArguments.First();
// if (type.IsEnum) return Expression.Lambda<Func<object, object>>( // if (type.IsEnum) return Expression.Lambda<Func<object, object>>(
// Expression.Call( // Expression.Call(
// MethodEnumParse, // MethodEnumParse,
@ -1020,7 +1018,7 @@ namespace FreeSql.Internal {
} }
return ret; return ret;
} }
if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First(); if (type.IsNullableType()) type = type.GenericTypeArguments.First();
if (type.IsEnum) return Enum.Parse(type, string.Concat(value), true); if (type.IsEnum) return Enum.Parse(type, string.Concat(value), true);
switch (type.FullName) { switch (type.FullName) {
case "System.Guid": case "System.Guid":

View File

@ -70,10 +70,8 @@ namespace FreeSql.Internal {
}); });
colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type)); colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type));
if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue; if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue;
if (colattr.IsNullable == false && colattr.DbDefautValue == null) { if (colattr.IsNullable == false && colattr.DbDefautValue == null)
var consturctorType = p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType; colattr.DbDefautValue = Activator.CreateInstance(p.PropertyType.IsNullableType() ? p.PropertyType.GenericTypeArguments.FirstOrDefault() : p.PropertyType);
colattr.DbDefautValue = Activator.CreateInstance(consturctorType);
}
var col = new ColumnInfo { var col = new ColumnInfo {
Table = trytb, Table = trytb,
@ -177,7 +175,7 @@ namespace FreeSql.Internal {
internal static (object value, int dataIndex) ExecuteArrayRowReadClassOrTuple(Type type, Dictionary<string, int> names, object[] row, int dataIndex = 0) { internal static (object value, int dataIndex) ExecuteArrayRowReadClassOrTuple(Type type, Dictionary<string, int> names, object[] row, int dataIndex = 0) {
if (type.IsArray) return (GetDataReaderValue(type, row[dataIndex]), dataIndex + 1); if (type.IsArray) return (GetDataReaderValue(type, row[dataIndex]), dataIndex + 1);
var typeGeneric = type; var typeGeneric = type;
if (typeGeneric.FullName.StartsWith("System.Nullable`1[")) typeGeneric = type.GenericTypeArguments.First(); if (typeGeneric.IsNullableType()) typeGeneric = type.GenericTypeArguments.First();
if (typeGeneric.IsEnum || if (typeGeneric.IsEnum ||
dicExecuteArrayRowReadClassOrTuple.ContainsKey(typeGeneric)) dicExecuteArrayRowReadClassOrTuple.ContainsKey(typeGeneric))
return (GetDataReaderValue(type, row[dataIndex]), dataIndex + 1); return (GetDataReaderValue(type, row[dataIndex]), dataIndex + 1);
@ -251,7 +249,7 @@ namespace FreeSql.Internal {
} }
return ret; return ret;
} }
if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First(); if (type.IsNullableType()) type = type.GenericTypeArguments.First();
if (type.IsEnum) return Enum.Parse(type, string.Concat(value), true); if (type.IsEnum) return Enum.Parse(type, string.Concat(value), true);
switch(type.FullName) { switch(type.FullName) {
case "System.Guid": case "System.Guid":

View File

@ -65,7 +65,7 @@ namespace FreeSql.MySql {
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue)); if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue));
if (type.IsArray) return null; if (type.IsArray) return null;
var enumType = type.IsEnum ? type : null; var enumType = type.IsEnum ? type : null;
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First(); if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
if (enumType != null) { if (enumType != null) {
var names = string.Join(",", Enum.GetNames(enumType).Select(a => _commonUtils.FormatSql("{0}", a))); var names = string.Join(",", Enum.GetNames(enumType).Select(a => _commonUtils.FormatSql("{0}", a)));
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?

View File

@ -246,10 +246,10 @@ namespace FreeSql.MySql {
case "AddTicks": return $"date_add({left}, interval ({args1})/10 microsecond)"; case "AddTicks": return $"date_add({left}, interval ({args1})/10 microsecond)";
case "AddYears": return $"date_add({left}, interval ({args1}) year)"; case "AddYears": return $"date_add({left}, interval ({args1}) year)";
case "Subtract": case "Subtract":
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") switch((exp.Arguments[0].Type.IsNullableType() ? exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault() : exp.Arguments[0].Type).FullName) {
return $"timestampdiff(microsecond, {args1}, {left})"; case "System.DateTime": return $"timestampdiff(microsecond, {args1}, {left})";
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") case "System.TimeSpan": return $"date_sub({left}, interval ({args1}) microsecond)";
return $"date_sub({left}, interval ({args1}) microsecond)"; }
break; break;
case "Equals": return $"({left} = {getExp(exp.Arguments[0])})"; case "Equals": return $"({left} = {getExp(exp.Arguments[0])})";
case "CompareTo": return $"(({left}) - ({getExp(exp.Arguments[0])}))"; case "CompareTo": return $"(({left}) - ({getExp(exp.Arguments[0])}))";

View File

@ -59,7 +59,7 @@ namespace FreeSql.Oracle {
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue)); if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue));
if (type.IsArray) return null; if (type.IsArray) return null;
var enumType = type.IsEnum ? type : null; var enumType = type.IsEnum ? type : null;
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First(); if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
if (enumType != null) { if (enumType != null) {
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
(OracleDbType.Int32, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) : (OracleDbType.Int32, "number", $"number(16){(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) :

View File

@ -247,10 +247,10 @@ namespace FreeSql.Oracle {
case "AddTicks": return $"({left}+({args1})/864000000000)"; case "AddTicks": return $"({left}+({args1})/864000000000)";
case "AddYears": return $"add_months({left},({args1})*12)"; case "AddYears": return $"add_months({left},({args1})*12)";
case "Subtract": case "Subtract":
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") switch ((exp.Arguments[0].Type.IsNullableType() ? exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault() : exp.Arguments[0].Type).FullName) {
return $"({args1}-{left})"; case "System.DateTime": return $"({args1}-{left})";
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") case "System.TimeSpan": return $"({left}-{args1})";
return $"({left}-{args1})"; }
break; break;
case "Equals": return $"({left} = {getExp(exp.Arguments[0])})"; case "Equals": return $"({left} = {getExp(exp.Arguments[0])})";
case "CompareTo": return $"extract(day from ({left}-({getExp(exp.Arguments[0])})))"; case "CompareTo": return $"extract(day from ({left}-({getExp(exp.Arguments[0])})))";

View File

@ -104,7 +104,7 @@ namespace FreeSql.PostgreSQL {
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (NpgsqlDbType, string, string, bool?, object)?((trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue)); if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (NpgsqlDbType, string, string, bool?, object)?((trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue));
if (type.IsArray) return null; if (type.IsArray) return null;
var enumType = type.IsEnum ? type : null; var enumType = type.IsEnum ? type : null;
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First(); if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
if (enumType != null) { if (enumType != null) {
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
(NpgsqlDbType.Bigint, "int8", $"int8{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) : (NpgsqlDbType.Bigint, "int8", $"int8{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) :

View File

@ -343,10 +343,10 @@ namespace FreeSql.PostgreSQL {
case "AddTicks": return $"(({left})::timestamp+(({args1})/10||' microseconds')::interval)"; case "AddTicks": return $"(({left})::timestamp+(({args1})/10||' microseconds')::interval)";
case "AddYears": return $"(({left})::timestamp+(({args1})||' year')::interval)"; case "AddYears": return $"(({left})::timestamp+(({args1})||' year')::interval)";
case "Subtract": case "Subtract":
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") switch ((exp.Arguments[0].Type.IsNullableType() ? exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault() : exp.Arguments[0].Type).FullName) {
return $"(extract(epoch from ({left})::timestamp-({args1})::timestamp)*1000000)"; case "System.DateTime": return $"(extract(epoch from ({left})::timestamp-({args1})::timestamp)*1000000)";
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") case "System.TimeSpan": return $"(({left})::timestamp-(({args1})||' microseconds')::interval)";
return $"(({left})::timestamp-(({args1})||' microseconds')::interval)"; }
break; break;
case "Equals": return $"({left} = ({getExp(exp.Arguments[0])})::timestamp)"; case "Equals": return $"({left} = ({getExp(exp.Arguments[0])})::timestamp)";
case "CompareTo": return $"extract(epoch from ({left})::timestamp-({getExp(exp.Arguments[0])})::timestamp)"; case "CompareTo": return $"extract(epoch from ({left})::timestamp-({getExp(exp.Arguments[0])})::timestamp)";

View File

@ -53,13 +53,13 @@ namespace FreeSql.PostgreSQL {
var elementType = type.GetElementType(); var elementType = type.GetElementType();
Type enumType = null; Type enumType = null;
if (elementType.IsEnum) enumType = elementType; if (elementType.IsEnum) enumType = elementType;
else if (elementType.FullName.StartsWith("System.Nullable`1[") && elementType.GenericTypeArguments.First().IsEnum) enumType = elementType.GenericTypeArguments.First(); else if (elementType.IsNullableType() && elementType.GenericTypeArguments.First().IsEnum) enumType = elementType.GenericTypeArguments.First();
if (enumType != null) return enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? if (enumType != null) return enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
getParamterArrayValue(typeof(long), value, elementType.IsEnum ? null : Enum.GetValues(enumType).GetValue(0)) : getParamterArrayValue(typeof(long), value, elementType.IsEnum ? null : Enum.GetValues(enumType).GetValue(0)) :
getParamterArrayValue(typeof(int), value, elementType.IsEnum ? null : Enum.GetValues(enumType).GetValue(0)); getParamterArrayValue(typeof(int), value, elementType.IsEnum ? null : Enum.GetValues(enumType).GetValue(0));
return dicGetParamterValue.TryGetValue(type.FullName, out var trydicarr) ? trydicarr(value) : value; return dicGetParamterValue.TryGetValue(type.FullName, out var trydicarr) ? trydicarr(value) : value;
} }
if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First(); if (type.IsNullableType()) type = type.GenericTypeArguments.First();
if (type.IsEnum) return (int)value; if (type.IsEnum) return (int)value;
if (dicGetParamterValue.TryGetValue(type.FullName, out var trydic)) return trydic(value); if (dicGetParamterValue.TryGetValue(type.FullName, out var trydic)) return trydic(value);
return value; return value;

View File

@ -58,7 +58,7 @@ namespace FreeSql.SqlServer {
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue)); if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue));
if (type.IsArray) return null; if (type.IsArray) return null;
var enumType = type.IsEnum ? type : null; var enumType = type.IsEnum ? type : null;
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First(); if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
if (enumType != null) { if (enumType != null) {
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
(SqlDbType.BigInt, "bigint", $"bigint{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) : (SqlDbType.BigInt, "bigint", $"bigint{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) :

View File

@ -228,10 +228,10 @@ namespace FreeSql.SqlServer {
case "AddTicks": return $"dateadd(second, ({args1})/10000000, {left})"; case "AddTicks": return $"dateadd(second, ({args1})/10000000, {left})";
case "AddYears": return $"dateadd(year, {args1}, {left})"; case "AddYears": return $"dateadd(year, {args1}, {left})";
case "Subtract": case "Subtract":
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") switch ((exp.Arguments[0].Type.IsNullableType() ? exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault() : exp.Arguments[0].Type).FullName) {
return $"datediff(second, {args1}, {left})"; case "System.DateTime": return $"datediff(second, {args1}, {left})";
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") case "System.TimeSpan": return $"dateadd(second, {args1}*-1, {left})";
return $"dateadd(second, {args1}*-1, {left})"; }
break; break;
case "Equals": return $"({left} = {getExp(exp.Arguments[0])})"; case "Equals": return $"({left} = {getExp(exp.Arguments[0])})";
case "CompareTo": return $"(({left}) - ({getExp(exp.Arguments[0])}))"; case "CompareTo": return $"(({left}) - ({getExp(exp.Arguments[0])}))";

View File

@ -57,7 +57,7 @@ namespace FreeSql.Sqlite {
if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue)); if (_dicCsToDb.TryGetValue(type.FullName, out var trydc)) return new (int, string, string, bool?, object)?(((int)trydc.type, trydc.dbtype, trydc.dbtypeFull, trydc.isnullable, trydc.defaultValue));
if (type.IsArray) return null; if (type.IsArray) return null;
var enumType = type.IsEnum ? type : null; var enumType = type.IsEnum ? type : null;
if (enumType == null && type.FullName.StartsWith("System.Nullable`1[") && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First(); if (enumType == null && type.IsNullableType() && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments.First().IsEnum) enumType = type.GenericTypeArguments.First();
if (enumType != null) { if (enumType != null) {
var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ? var newItem = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Any() ?
(DbType.Int64, "bigint", $"bigint{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) : (DbType.Int64, "bigint", $"bigint{(type.IsEnum ? " NOT NULL" : "")}", false, type.IsEnum ? false : true, Enum.GetValues(enumType).GetValue(0)) :

View File

@ -251,10 +251,10 @@ namespace FreeSql.Sqlite {
case "AddTicks": return $"datetime({left},(({args1})/10000000)||' seconds')"; case "AddTicks": return $"datetime({left},(({args1})/10000000)||' seconds')";
case "AddYears": return $"datetime({left},({args1})||' years')"; case "AddYears": return $"datetime({left},({args1})||' years')";
case "Subtract": case "Subtract":
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") switch ((exp.Arguments[0].Type.IsNullableType() ? exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault() : exp.Arguments[0].Type).FullName) {
return $"(strftime('%s',{left})-strftime('%s',{args1}))"; case "System.DateTime": return $"(strftime('%s',{left})-strftime('%s',{args1}))";
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") case "System.TimeSpan": return $"datetime({left},(-{args1})||' seconds')";
return $"datetime({left},(-{args1})||' seconds')"; }
break; break;
case "Equals": return $"({left} = {getExp(exp.Arguments[0])})"; case "Equals": return $"({left} = {getExp(exp.Arguments[0])})";
case "CompareTo": return $"(strftime('%s',{left})-strftime('%s',{args1}))"; case "CompareTo": return $"(strftime('%s',{left})-strftime('%s',{args1}))";