- 调整内部参数化处理逻辑,为以后 Where 条件参数化做准备;

This commit is contained in:
28810 2019-11-22 05:58:17 +08:00
parent 71dbd75a72
commit 12be7f0051
32 changed files with 213 additions and 97 deletions

View File

@ -168,17 +168,24 @@ namespace FreeSql.Tests
public class TestMySqlStringIsNullable public class TestMySqlStringIsNullable
{ {
public Guid id { get; set; } public Guid id { get; set; }
public string varchar { get; set; } public string nvarchar { get; set; }
[Column(IsNullable = true)] [Column(IsNullable = true)]
public string varchar_null { get; set; } public string nvarchar_null { get; set; }
[Column(IsNullable = false)] [Column(IsNullable = false)]
public string nvarchar_notnull { get; set; }
[Column(DbType = "varchar(100)")]
public string varchar { get; set; }
[Column(IsNullable = true, DbType = "varchar(100)")]
public string varchar_null { get; set; }
[Column(IsNullable = false, DbType = "varchar(100)")]
public string varchar_notnull { get; set; } public string varchar_notnull { get; set; }
} }
[Fact] [Fact]
public void Test02() public void Test02()
{ {
g.mysql.Select<TestMySqlStringIsNullable>(); var testparmSelect = g.sqlserver.Select<TestMySqlStringIsNullable>().Where(a => a.nvarchar == "11" && a.nvarchar_notnull == "22" && a.nvarchar_null == "33" && a.varchar == "11" && a.varchar_notnull == "22" && a.varchar_null == "33");
var slsksd = g.mysql.Update<UserLike>().SetSource(new UserLike { Id = Guid.NewGuid(), CreateUserId = 1000, SubjectId = Guid.NewGuid() }) var slsksd = g.mysql.Update<UserLike>().SetSource(new UserLike { Id = Guid.NewGuid(), CreateUserId = 1000, SubjectId = Guid.NewGuid() })
.UpdateColumns(a => new .UpdateColumns(a => new

View File

@ -37,6 +37,7 @@ public class g
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3") .UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3")
//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=192.168.164.129;uid=sa;pwd=123456;Initial Catalog=ds_shop;Pooling=true;Max Pool Size=3") //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=192.168.164.129;uid=sa;pwd=123456;Initial Catalog=ds_shop;Pooling=true;Max Pool Size=3")
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
//.UseNoneCommandParameter(true)
.UseMonitorCommand( .UseMonitorCommand(
cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象在执行前 cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象在执行前
(cmd, traceLog) => Console.WriteLine(traceLog)) (cmd, traceLog) => Console.WriteLine(traceLog))

View File

@ -329,23 +329,23 @@ namespace FreeSql.Internal
{ ExpressionType.Modulo, "%" }, { ExpressionType.Modulo, "%" },
{ ExpressionType.Equal, "=" }, { ExpressionType.Equal, "=" },
}; };
public string ExpressionWhereLambdaNoneForeignObject(List<SelectTableInfo> _tables, TableInfo table, List<SelectColumnInfo> _selectColumnMap, Expression exp, Func<Expression[], string> getSelectGroupingMapString) public string ExpressionWhereLambdaNoneForeignObject(List<SelectTableInfo> _tables, TableInfo table, List<SelectColumnInfo> _selectColumnMap, Expression exp, Func<Expression[], string> getSelectGroupingMapString, List<DbParameter> dbParams)
{ {
var sql = ExpressionLambdaToSql(exp, new ExpTSC { _tables = _tables, _selectColumnMap = _selectColumnMap, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, currentTable = table }); var sql = ExpressionLambdaToSql(exp, new ExpTSC { _tables = _tables, _selectColumnMap = _selectColumnMap, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, currentTable = table, dbParams = dbParams });
var isBool = exp.Type.NullableTypeOrThis() == typeof(bool); var isBool = exp.Type.NullableTypeOrThis() == typeof(bool);
if (exp.NodeType == ExpressionType.MemberAccess && isBool && sql.Contains(" IS ") == false && sql.Contains(" = ") == false) if (exp.NodeType == ExpressionType.MemberAccess && isBool && sql.Contains(" IS ") == false && sql.Contains(" = ") == false)
return $"{sql} = {formatSql(true, null, null)}"; return $"{sql} = {formatSql(true, null, null, null)}";
if (isBool) if (isBool)
return GetBoolString(sql); return GetBoolString(sql);
return sql; return sql;
} }
public string ExpressionWhereLambda(List<SelectTableInfo> _tables, Expression exp, Func<Expression[], string> getSelectGroupingMapString, List<LambdaExpression> whereCascadeExpression) public string ExpressionWhereLambda(List<SelectTableInfo> _tables, Expression exp, Func<Expression[], string> getSelectGroupingMapString, List<LambdaExpression> whereCascadeExpression, List<DbParameter> dbParams)
{ {
var sql = ExpressionLambdaToSql(exp, new ExpTSC { _tables = _tables, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereCascadeExpression = whereCascadeExpression }); var sql = ExpressionLambdaToSql(exp, new ExpTSC { _tables = _tables, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereCascadeExpression = whereCascadeExpression, dbParams = dbParams });
var isBool = exp.Type.NullableTypeOrThis() == typeof(bool); var isBool = exp.Type.NullableTypeOrThis() == typeof(bool);
if (exp.NodeType == ExpressionType.MemberAccess && isBool && sql.Contains(" IS ") == false && sql.Contains(" = ") == false) if (exp.NodeType == ExpressionType.MemberAccess && isBool && sql.Contains(" IS ") == false && sql.Contains(" = ") == false)
return $"{sql} = {formatSql(true, null, null)}"; return $"{sql} = {formatSql(true, null, null, null)}";
if (isBool) if (isBool)
return GetBoolString(sql); return GetBoolString(sql);
return sql; return sql;
@ -357,7 +357,7 @@ namespace FreeSql.Internal
var sql = ExpressionLambdaToSql(exp, new ExpTSC { _tables = _tables, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = tbtype, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereCascadeExpression = whereCascadeExpression }); var sql = ExpressionLambdaToSql(exp, new ExpTSC { _tables = _tables, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = tbtype, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereCascadeExpression = whereCascadeExpression });
var isBool = exp.Type.NullableTypeOrThis() == typeof(bool); var isBool = exp.Type.NullableTypeOrThis() == typeof(bool);
if (exp.NodeType == ExpressionType.MemberAccess && isBool && sql.Contains(" IS ") == false && sql.Contains(" = ") == false) if (exp.NodeType == ExpressionType.MemberAccess && isBool && sql.Contains(" IS ") == false && sql.Contains(" = ") == false)
sql = $"{sql} = {formatSql(true, null, null)}"; sql = $"{sql} = {formatSql(true, null, null, null)}";
if (isBool) if (isBool)
sql = GetBoolString(sql); sql = GetBoolString(sql);
@ -435,7 +435,7 @@ namespace FreeSql.Internal
{ {
var enumType = leftMapColumn.CsType.NullableTypeOrThis(); var enumType = leftMapColumn.CsType.NullableTypeOrThis();
if (enumType.IsEnum) if (enumType.IsEnum)
right = formatSql(Enum.Parse(enumType, right.StartsWith("N'") ? right.Substring(1).Trim('\'') : right.Trim('\'')), leftMapColumn.Attribute.MapType, leftMapColumn); right = formatSql(Enum.Parse(enumType, right.StartsWith("N'") ? right.Substring(1).Trim('\'') : right.Trim('\'')), leftMapColumn.Attribute.MapType, leftMapColumn, tsc.dbParams);
} }
if (leftMapColumn == null) if (leftMapColumn == null)
{ {
@ -449,7 +449,7 @@ namespace FreeSql.Internal
{ {
var enumType = rightMapColumn.CsType.NullableTypeOrThis(); var enumType = rightMapColumn.CsType.NullableTypeOrThis();
if (enumType.IsEnum) if (enumType.IsEnum)
left = formatSql(Enum.Parse(enumType, left.StartsWith("N'") ? left.Substring(1).Trim('\'') : left.Trim('\'')), rightMapColumn.Attribute.MapType, rightMapColumn); left = formatSql(Enum.Parse(enumType, left.StartsWith("N'") ? left.Substring(1).Trim('\'') : left.Trim('\'')), rightMapColumn.Attribute.MapType, rightMapColumn, tsc.dbParams);
} }
} }
} }
@ -457,8 +457,8 @@ namespace FreeSql.Internal
{ {
if (oper == "=") if (oper == "=")
{ {
var trueVal = formatSql(true, null, null); var trueVal = formatSql(true, null, null, null);
var falseVal = formatSql(false, null, null); var falseVal = formatSql(false, null, null, null);
if (left == trueVal) return right; if (left == trueVal) return right;
else if (left == falseVal) return $"not({right})"; else if (left == falseVal) return $"not({right})";
else if (right == trueVal) return left; else if (right == trueVal) return left;
@ -466,8 +466,8 @@ namespace FreeSql.Internal
} }
else if (oper == "<>") else if (oper == "<>")
{ {
var trueVal = formatSql(true, null, null); var trueVal = formatSql(true, null, null, null);
var falseVal = formatSql(false, null, null); var falseVal = formatSql(false, null, null, null);
if (left == trueVal) return $"not({right})"; if (left == trueVal) return $"not({right})";
else if (left == falseVal) return right; else if (left == falseVal) return right;
else if (right == trueVal) return $"not({left})"; else if (right == trueVal) return $"not({left})";
@ -489,9 +489,9 @@ namespace FreeSql.Internal
break; break;
case "AND": case "AND":
case "OR": case "OR":
if (leftMapColumn != null) left = $"{left} = {formatSql(true, null, null)}"; if (leftMapColumn != null) left = $"{left} = {formatSql(true, null, null, null)}";
else left = GetBoolString(left); else left = GetBoolString(left);
if (rightMapColumn != null) right = $"{right} = {formatSql(true, null, null)}"; if (rightMapColumn != null) right = $"{right} = {formatSql(true, null, null, null)}";
else right = GetBoolString(right); else right = GetBoolString(right);
break; break;
} }
@ -520,7 +520,7 @@ namespace FreeSql.Internal
if (notBody.Contains(" IS NOT NULL")) return notBody.Replace(" IS NOT NULL", " IS NULL"); if (notBody.Contains(" IS NOT NULL")) return notBody.Replace(" IS NOT NULL", " IS NULL");
if (notBody.Contains("=")) return notBody.Replace("=", "!="); if (notBody.Contains("=")) return notBody.Replace("=", "!=");
if (notBody.Contains("!=")) return notBody.Replace("!=", "="); if (notBody.Contains("!=")) return notBody.Replace("!=", "=");
return $"{notBody} = {formatSql(false, null, null)}"; return $"{notBody} = {formatSql(false, null, null, null)}";
} }
return $"not({ExpressionLambdaToSql(notExp, tsc)})"; return $"not({ExpressionLambdaToSql(notExp, tsc)})";
case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, tsc); case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, tsc);
@ -532,7 +532,7 @@ namespace FreeSql.Internal
return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, tsc); return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, tsc);
case ExpressionType.Negate: case ExpressionType.Negate:
case ExpressionType.NegateChecked: return "-" + ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, tsc); case ExpressionType.NegateChecked: return "-" + ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, tsc);
case ExpressionType.Constant: return formatSql((exp as ConstantExpression)?.Value, tsc.mapType, tsc.mapColumnTmp); case ExpressionType.Constant: return formatSql((exp as ConstantExpression)?.Value, tsc.mapType, tsc.mapColumnTmp, tsc.dbParams);
case ExpressionType.Conditional: case ExpressionType.Conditional:
var condExp = exp as ConditionalExpression; var condExp = exp as ConditionalExpression;
return $"case when {ExpressionLambdaToSql(condExp.Test, tsc)} then {ExpressionLambdaToSql(condExp.IfTrue, tsc)} else {ExpressionLambdaToSql(condExp.IfFalse, tsc)} end"; return $"case when {ExpressionLambdaToSql(condExp.Test, tsc)} then {ExpressionLambdaToSql(condExp.IfTrue, tsc)} else {ExpressionLambdaToSql(condExp.IfFalse, tsc)} end";
@ -890,7 +890,7 @@ namespace FreeSql.Internal
//} //}
other3Exp = ExpressionLambdaToSqlOther(exp3, tsc); other3Exp = ExpressionLambdaToSqlOther(exp3, tsc);
if (string.IsNullOrEmpty(other3Exp) == false) return other3Exp; if (string.IsNullOrEmpty(other3Exp) == false) return other3Exp;
if (exp3.IsParameter() == false) return formatSql(Expression.Lambda(exp3).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp); if (exp3.IsParameter() == false) return formatSql(Expression.Lambda(exp3).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp, tsc.dbParams);
throw new Exception($"未实现函数表达式 {exp3} 解析"); throw new Exception($"未实现函数表达式 {exp3} 解析");
case ExpressionType.Parameter: case ExpressionType.Parameter:
case ExpressionType.MemberAccess: case ExpressionType.MemberAccess:
@ -950,7 +950,7 @@ namespace FreeSql.Internal
} }
break; break;
} }
if (expStack.First().NodeType != ExpressionType.Parameter) return formatSql(Expression.Lambda(exp).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp); if (expStack.First().NodeType != ExpressionType.Parameter) return formatSql(Expression.Lambda(exp).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp, tsc.dbParams);
if (callExp != null) return ExpressionLambdaToSql(callExp, tsc); if (callExp != null) return ExpressionLambdaToSql(callExp, tsc);
if (tsc.getSelectGroupingMapString != null && expStack.First().Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`")) if (tsc.getSelectGroupingMapString != null && expStack.First().Type.FullName.StartsWith("FreeSql.ISelectGroupingAggregate`"))
{ {
@ -1161,7 +1161,7 @@ namespace FreeSql.Internal
} }
if (dicExpressionOperator.TryGetValue(expBinary.NodeType, out var tryoper) == false) if (dicExpressionOperator.TryGetValue(expBinary.NodeType, out var tryoper) == false)
{ {
if (exp.IsParameter() == false) return formatSql(Expression.Lambda(exp).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp); if (exp.IsParameter() == false) return formatSql(Expression.Lambda(exp).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp, tsc.dbParams);
return ""; return "";
} }
return ExpressionBinary(tryoper, expBinary.Left, expBinary.Right, tsc); return ExpressionBinary(tryoper, expBinary.Left, expBinary.Right, tsc);
@ -1195,6 +1195,7 @@ namespace FreeSql.Internal
public ColumnInfo mapColumnTmp { get; set; } public ColumnInfo mapColumnTmp { get; set; }
public TableInfo currentTable { get; set; } public TableInfo currentTable { get; set; }
public List<LambdaExpression> whereCascadeExpression { get; set; } public List<LambdaExpression> whereCascadeExpression { get; set; }
public List<DbParameter> dbParams { get; set; }
public string alias001 { get; set; } //单表字段的表别名 public string alias001 { get; set; } //单表字段的表别名
public ExpTSC SetMapColumnTmp(ColumnInfo col) public ExpTSC SetMapColumnTmp(ColumnInfo col)
@ -1229,8 +1230,12 @@ namespace FreeSql.Internal
isQuoteName = this.isQuoteName, isQuoteName = this.isQuoteName,
isDisableDiyParse = this.isDisableDiyParse, isDisableDiyParse = this.isDisableDiyParse,
style = this.style, style = this.style,
//mapType = this.mapType,
//mapTypeTmp = this.mapTypeTmp,
//mapColumnTmp = this.mapColumnTmp,
currentTable = this.currentTable, currentTable = this.currentTable,
whereCascadeExpression = this.whereCascadeExpression, whereCascadeExpression = this.whereCascadeExpression,
dbParams = this.dbParams,
alias001 = this.alias001 alias001 = this.alias001
}; };
} }
@ -1245,8 +1250,12 @@ namespace FreeSql.Internal
isQuoteName = this.isQuoteName, isQuoteName = this.isQuoteName,
isDisableDiyParse = true, isDisableDiyParse = true,
style = this.style, style = this.style,
mapType = this.mapType,
mapTypeTmp = this.mapTypeTmp,
mapColumnTmp = this.mapColumnTmp,
currentTable = this.currentTable, currentTable = this.currentTable,
whereCascadeExpression = this.whereCascadeExpression, whereCascadeExpression = this.whereCascadeExpression,
dbParams = this.dbParams,
alias001 = this.alias001 alias001 = this.alias001
}; };
} }
@ -1309,6 +1318,17 @@ namespace FreeSql.Internal
} }
} }
public string formatSql(object obj, Type mapType, ColumnInfo mapColumn) => string.Concat(_ado.AddslashesProcessParam(obj, mapType, mapColumn)); public string formatSql(object obj, Type mapType, ColumnInfo mapColumn, List<DbParameter> dbParams)
{
//参数化设置,日后优化
//if (dbParams != null && mapColumn != null)
//{
// var paramName = $"exp_{dbParams.Count}";
// var parm = _common.AppendParamter(dbParams, paramName, mapColumn, mapType ?? mapColumn.Attribute.MapType, mapType == null ? obj : Utils.GetDataReaderValue(mapType, obj));
// _common.SetParameterSize(parm, mapColumn.Attribute.DbType, mapColumn);
// return _common.QuoteParamterName(paramName);
//}
return string.Concat(_ado.AddslashesProcessParam(obj, mapType, mapColumn));
}
} }
} }

View File

@ -22,6 +22,7 @@ namespace FreeSql.Internal.CommonProvider
protected int _whereTimes = 0; protected int _whereTimes = 0;
protected List<GlobalFilter.Item> _whereGlobalFilter; protected List<GlobalFilter.Item> _whereGlobalFilter;
protected List<DbParameter> _params = new List<DbParameter>(); protected List<DbParameter> _params = new List<DbParameter>();
protected bool _noneParameter;
protected DbTransaction _transaction; protected DbTransaction _transaction;
protected DbConnection _connection; protected DbConnection _connection;
@ -31,6 +32,7 @@ namespace FreeSql.Internal.CommonProvider
_commonUtils = commonUtils; _commonUtils = commonUtils;
_commonExpression = commonExpression; _commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1)); _table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
this.Where(_commonUtils.WhereObject(_table, "", dywhere)); this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>(); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters(); _whereGlobalFilter = _orm.GlobalFilter.GetFilters();
@ -85,7 +87,7 @@ namespace FreeSql.Internal.CommonProvider
} }
public abstract List<T1> ExecuteDeleted(); public abstract List<T1> ExecuteDeleted();
public IDelete<T1> Where(Expression<Func<T1, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp?.Body, null)); public IDelete<T1> Where(Expression<Func<T1, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp?.Body, null, _noneParameter ? _params : null));
public IDelete<T1> Where(string sql, object parms = null) public IDelete<T1> Where(string sql, object parms = null)
{ {
if (string.IsNullOrEmpty(sql)) return this; if (string.IsNullOrEmpty(sql)) return this;

View File

@ -451,7 +451,7 @@ namespace FreeSql.Internal.CommonProvider
else else
{ {
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"))); 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); _params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
} }
++colidx2; ++colidx2;
} }

View File

@ -35,6 +35,7 @@ namespace FreeSql.Internal.CommonProvider
protected Action<object> _trackToList; protected Action<object> _trackToList;
protected List<Action<object>> _includeToList = new List<Action<object>>(); protected List<Action<object>> _includeToList = new List<Action<object>>();
protected bool _distinct; protected bool _distinct;
protected bool _noneParameter;
protected Expression _selectExpression; protected Expression _selectExpression;
protected List<LambdaExpression> _whereCascadeExpression = new List<LambdaExpression>(); protected List<LambdaExpression> _whereCascadeExpression = new List<LambdaExpression>();
protected List<GlobalFilter.Item> _whereGlobalFilter; protected List<GlobalFilter.Item> _whereGlobalFilter;
@ -105,6 +106,7 @@ namespace FreeSql.Internal.CommonProvider
toType.GetField("_trackToList", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._trackToList); toType.GetField("_trackToList", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._trackToList);
toType.GetField("_includeToList", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._includeToList); toType.GetField("_includeToList", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._includeToList);
toType.GetField("_distinct", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._distinct); toType.GetField("_distinct", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._distinct);
toType.GetField("_noneParameter", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._noneParameter);
toType.GetField("_selectExpression", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._selectExpression); toType.GetField("_selectExpression", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._selectExpression);
toType.GetField("_whereCascadeExpression", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._whereCascadeExpression); toType.GetField("_whereCascadeExpression", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._whereCascadeExpression);
toType.GetField("_whereGlobalFilter", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._whereGlobalFilter); toType.GetField("_whereGlobalFilter", BindingFlags.Instance | BindingFlags.NonPublic)?.SetValue(to, from._whereGlobalFilter);
@ -116,6 +118,7 @@ namespace FreeSql.Internal.CommonProvider
_commonUtils = commonUtils; _commonUtils = commonUtils;
_commonExpression = commonExpression; _commonExpression = commonExpression;
_tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T1)), Alias = "a", On = null, Type = SelectTableInfoType.From }); _tables.Add(new SelectTableInfo { Table = _commonUtils.GetTableByEntity(typeof(T1)), Alias = "a", On = null, Type = SelectTableInfoType.From });
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
this.Where(_commonUtils.WhereObject(_tables.First().Table, "a.", dywhere)); this.Where(_commonUtils.WhereObject(_tables.First().Table, "a.", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>(); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
} }
@ -1051,7 +1054,7 @@ namespace FreeSql.Internal.CommonProvider
return this.ToListMapReader<TReturn>((map, field.Length > 0 ? field.Remove(0, 2).ToString() : null)).FirstOrDefault(); return this.ToListMapReader<TReturn>((map, field.Length > 0 ? field.Remove(0, 2).ToString() : null)).FirstOrDefault();
} }
protected TSelect InternalWhere(Expression exp) => exp == null ? this as TSelect : this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp, null, _whereCascadeExpression)); protected TSelect InternalWhere(Expression exp) => exp == null ? this as TSelect : this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp, null, _whereCascadeExpression, _noneParameter ? _params : null));
#endregion #endregion
#if net40 #if net40

View File

@ -153,21 +153,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> exp) ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> exp)
{ {
if (condition == false || exp == null) return this.Where(null); if (condition == false || exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)) : this; return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)) : this;
} }
bool ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> exp) bool ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -226,7 +226,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -348,7 +348,7 @@ namespace FreeSql.Internal.CommonProvider
if (tb == null) throw new Exception("Include 参数类型错误"); if (tb == null) throw new Exception("Include 参数类型错误");
_isIncluded = true; _isIncluded = true;
_commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(expBody, tb.Properties[tb.ColumnsByCs.First().Value.CsName]), null, null); _commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(expBody, tb.Properties[tb.ColumnsByCs.First().Value.CsName]), null, null, null);
return this; return this;
} }
@ -422,7 +422,7 @@ namespace FreeSql.Internal.CommonProvider
if (tbNav == null) throw new Exception($"类型 {typeof(TNavigate).FullName} 错误,不能使用 IncludeMany"); if (tbNav == null) throw new Exception($"类型 {typeof(TNavigate).FullName} 错误,不能使用 IncludeMany");
if (collMem.Expression.NodeType != ExpressionType.Parameter) if (collMem.Expression.NodeType != ExpressionType.Parameter)
_commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(collMem.Expression, tb.Properties[tb.ColumnsByCs.First().Value.CsName]), null, null); _commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(collMem.Expression, tb.Properties[tb.ColumnsByCs.First().Value.CsName]), null, null, null);
TableRef tbref = null; TableRef tbref = null;
var tbrefOneToManyColumns = new List<List<MemberExpression>>(); //临时 OneToMany 三个表关联,第三个表需要前两个表确定 var tbrefOneToManyColumns = new List<List<MemberExpression>>(); //临时 OneToMany 三个表关联,第三个表需要前两个表确定

View File

@ -130,21 +130,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2> ISelect<T1, T2>.WhereIf(bool condition, Expression<Func<T1, T2, bool>> exp) ISelect<T1, T2> ISelect<T1, T2>.WhereIf(bool condition, Expression<Func<T1, T2, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2>.Any(Expression<Func<T1, T2, bool>> exp) bool ISelect<T1, T2>.Any(Expression<Func<T1, T2, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -203,7 +203,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -133,21 +133,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3> ISelect<T1, T2, T3>.WhereIf(bool condition, Expression<Func<T1, T2, T3, bool>> exp) ISelect<T1, T2, T3> ISelect<T1, T2, T3>.WhereIf(bool condition, Expression<Func<T1, T2, T3, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3>.Any(Expression<Func<T1, T2, T3, bool>> exp) bool ISelect<T1, T2, T3>.Any(Expression<Func<T1, T2, T3, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -206,7 +206,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -136,21 +136,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4> ISelect<T1, T2, T3, T4>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, bool>> exp) ISelect<T1, T2, T3, T4> ISelect<T1, T2, T3, T4>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3, T4>.Any(Expression<Func<T1, T2, T3, T4, bool>> exp) bool ISelect<T1, T2, T3, T4>.Any(Expression<Func<T1, T2, T3, T4, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -209,7 +209,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -139,21 +139,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4, T5> ISelect<T1, T2, T3, T4, T5>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, bool>> exp) ISelect<T1, T2, T3, T4, T5> ISelect<T1, T2, T3, T4, T5>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3, T4, T5>.Any(Expression<Func<T1, T2, T3, T4, T5, bool>> exp) bool ISelect<T1, T2, T3, T4, T5>.Any(Expression<Func<T1, T2, T3, T4, T5, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -212,7 +212,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -142,21 +142,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4, T5, T6> ISelect<T1, T2, T3, T4, T5, T6>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp) ISelect<T1, T2, T3, T4, T5, T6> ISelect<T1, T2, T3, T4, T5, T6>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3, T4, T5, T6>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp) bool ISelect<T1, T2, T3, T4, T5, T6>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -215,7 +215,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -145,21 +145,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4, T5, T6, T7> ISelect<T1, T2, T3, T4, T5, T6, T7>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp) ISelect<T1, T2, T3, T4, T5, T6, T7> ISelect<T1, T2, T3, T4, T5, T6, T7>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3, T4, T5, T6, T7>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp) bool ISelect<T1, T2, T3, T4, T5, T6, T7>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -218,7 +218,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -149,21 +149,21 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4, T5, T6, T7, T8> ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, bool>> exp) ISelect<T1, T2, T3, T4, T5, T6, T7, T8> ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, bool>> exp) bool ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -222,7 +222,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
#endif #endif
} }

View File

@ -147,20 +147,20 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.Where(null); if (exp == null) return this.Where(null);
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, bool>> exp) ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, bool>> exp)
{ {
if (condition == false || exp == null) return this; if (condition == false || exp == null) return this;
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null));
} }
bool ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, bool>> exp) bool ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, bool>> exp)
{ {
if (exp == null) return this.Any(); if (exp == null) return this.Any();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).Any(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).Any();
} }
#if net40 #if net40
@ -169,7 +169,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
if (exp == null) return this.AnyAsync(); if (exp == null) return this.AnyAsync();
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a]; for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression)).AnyAsync(); return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null, _whereCascadeExpression, _noneParameter ? _params : null)).AnyAsync();
} }
Task<DataTable> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TReturn>> select) Task<DataTable> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TReturn>> select)

View File

@ -84,7 +84,7 @@ namespace FreeSql.Internal.CommonProvider
public ISelectGrouping<TKey, TValue> Having(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, bool>> exp) public ISelectGrouping<TKey, TValue> Having(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, bool>> exp)
{ {
var sql = _comonExp.ExpressionWhereLambda(null, exp, getSelectGroupingMapString, null); var sql = _comonExp.ExpressionWhereLambda(null, exp, getSelectGroupingMapString, null, null);
var method = _select.GetType().GetMethod("Having", new[] { typeof(string), typeof(object) }); var method = _select.GetType().GetMethod("Having", new[] { typeof(string), typeof(object) });
method.Invoke(_select, new object[] { sql, null }); method.Invoke(_select, new object[] { sql, null });
return this; return this;
@ -92,7 +92,7 @@ namespace FreeSql.Internal.CommonProvider
public ISelectGrouping<TKey, TValue> OrderBy<TMember>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TMember>> column) public ISelectGrouping<TKey, TValue> OrderBy<TMember>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TMember>> column)
{ {
var sql = _comonExp.ExpressionWhereLambda(null, column, getSelectGroupingMapString, null); var sql = _comonExp.ExpressionWhereLambda(null, column, getSelectGroupingMapString, null, null);
var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) }); var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) });
method.Invoke(_select, new object[] { sql, null }); method.Invoke(_select, new object[] { sql, null });
return this; return this;
@ -100,7 +100,7 @@ namespace FreeSql.Internal.CommonProvider
public ISelectGrouping<TKey, TValue> OrderByDescending<TMember>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TMember>> column) public ISelectGrouping<TKey, TValue> OrderByDescending<TMember>(Expression<Func<ISelectGroupingAggregate<TKey, TValue>, TMember>> column)
{ {
var sql = _comonExp.ExpressionWhereLambda(null, column, getSelectGroupingMapString, null); var sql = _comonExp.ExpressionWhereLambda(null, column, getSelectGroupingMapString, null, null);
var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) }); var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) });
method.Invoke(_select, new object[] { $"{sql} DESC", null }); method.Invoke(_select, new object[] { $"{sql} DESC", null });
return this; return this;

View File

@ -353,7 +353,7 @@ namespace FreeSql.Internal.CommonProvider
else else
{ {
_set.Append(_commonUtils.QuoteWriteParamter(col.Column.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}")); _set.Append(_commonUtils.QuoteWriteParamter(col.Column.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}"));
_commonUtils.AppendParamter(_params, null, col.Column.Attribute.MapType, paramVal); _commonUtils.AppendParamter(_params, null, col.Column, col.Column.Attribute.MapType, paramVal);
} }
//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value); //foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
return this; return this;
@ -365,7 +365,7 @@ namespace FreeSql.Internal.CommonProvider
switch (nodeType) switch (nodeType)
{ {
case ExpressionType.Equal: case ExpressionType.Equal:
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp, null)); _set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp, null, null));
return this; return this;
case ExpressionType.MemberInit: case ExpressionType.MemberInit:
var initExp = body as MemberInitExpression; var initExp = body as MemberInitExpression;
@ -401,7 +401,7 @@ namespace FreeSql.Internal.CommonProvider
if (body is BinaryExpression == false && if (body is BinaryExpression == false &&
nodeType != ExpressionType.Call) return this; nodeType != ExpressionType.Call) return this;
var cols = new List<SelectColumnInfo>(); var cols = new List<SelectColumnInfo>();
var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, cols, exp, null); var expt = _commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, cols, exp, null, null);
if (cols.Any() == false) return this; if (cols.Any() == false) return this;
foreach (var col in cols) foreach (var col in cols)
{ {
@ -424,7 +424,7 @@ namespace FreeSql.Internal.CommonProvider
return this; return this;
} }
public IUpdate<T1> Where(Expression<Func<T1, bool>> expression) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, expression?.Body, null)); public IUpdate<T1> Where(Expression<Func<T1, bool>> expression) => this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, expression?.Body, null, _noneParameter ? _params : null));
public IUpdate<T1> Where(string sql, object parms = null) public IUpdate<T1> Where(string sql, object parms = null)
{ {
if (string.IsNullOrEmpty(sql)) return this; if (string.IsNullOrEmpty(sql)) return this;
@ -564,7 +564,7 @@ namespace FreeSql.Internal.CommonProvider
else else
{ {
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"))); sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col.Attribute.MapType, val); _commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
} }
++colidx; ++colidx;
} }
@ -603,7 +603,7 @@ namespace FreeSql.Internal.CommonProvider
else else
{ {
cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"))); cwsb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
_commonUtils.AppendParamter(_paramsSource, null, col.Attribute.MapType, val); _commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
} }
if (val == null || val == DBNull.Value) nulls++; if (val == null || val == DBNull.Value) nulls++;
} }

View File

@ -13,6 +13,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
using System.Xml.XPath; using System.Xml.XPath;
@ -23,7 +24,7 @@ namespace FreeSql.Internal
{ {
public abstract string GetNoneParamaterSqlValue(List<DbParameter> specialParams, Type type, object value); public abstract string GetNoneParamaterSqlValue(List<DbParameter> specialParams, Type type, object value);
public abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value); public abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value);
public abstract DbParameter[] GetDbParamtersByObject(string sql, object obj); public abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
public abstract string FormatSql(string sql, params object[] args); public abstract string FormatSql(string sql, params object[] args);
public abstract string QuoteSqlName(string name); public abstract string QuoteSqlName(string name);
@ -47,6 +48,25 @@ namespace FreeSql.Internal
_orm = orm; _orm = orm;
} }
static Regex _regexSize = new Regex(@"\(([^\)]+)\)", RegexOptions.Compiled);
internal void SetParameterSize(DbParameter parm, string dbtypeFull, ColumnInfo col)
{
if (col == null) return;
if (string.IsNullOrEmpty(dbtypeFull)) return;
var m = _regexSize.Match(dbtypeFull);
if (m.Success == false) return;
var sizeStr = m.Groups[1].Value.Trim();
if (string.Compare(sizeStr, "max", true) == 0)
{
parm.Size = -1;
return;
}
var sizeArr = sizeStr.Split(',');
if (int.TryParse(sizeArr[0], out var size) == false) return;
if (sizeArr.Length > 1 && int.TryParse(sizeArr[1], out var size2)) size += size2;
parm.Size = size;
}
ConcurrentDictionary<Type, TableAttribute> dicConfigEntity = new ConcurrentDictionary<Type, TableAttribute>(); ConcurrentDictionary<Type, TableAttribute> dicConfigEntity = new ConcurrentDictionary<Type, TableAttribute>();
public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity) public ICodeFirst ConfigEntity<T>(Action<TableFluent<T>> entity)
{ {

View File

@ -18,14 +18,22 @@ namespace FreeSql.MySql
{ {
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
var ret = new MySqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new MySqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null) if (tp != null)
{ {
if ((MySqlDbType)tp.Value == MySqlDbType.Geometry) if (col != null && type == typeof(string))
{
if (col.Attribute.DbType.Contains("VARCHAR")) ret.MySqlDbType = MySqlDbType.VarChar;
else if (col.Attribute.DbType.Contains("CHAR")) ret.MySqlDbType = MySqlDbType.VarChar;
else if (col.Attribute.DbType.Contains("TEXT")) ret.MySqlDbType = MySqlDbType.Text;
else ret.MySqlDbType = MySqlDbType.VarChar;
}
else if ((MySqlDbType)tp.Value == MySqlDbType.Geometry)
{ {
ret.MySqlDbType = MySqlDbType.Text; ret.MySqlDbType = MySqlDbType.Text;
if (value != null) ret.Value = (value as MygisGeometry).AsText(); if (value != null) ret.Value = (value as MygisGeometry).AsText();

View File

@ -18,14 +18,22 @@ namespace FreeSql.MySql
{ {
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
var ret = new MySqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new MySqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null) if (tp != null)
{ {
if ((MySqlDbType)tp.Value == MySqlDbType.Geometry) if (col != null && type == typeof(string))
{
if (col.Attribute.DbType.Contains("VARCHAR")) ret.MySqlDbType = MySqlDbType.VarChar;
else if (col.Attribute.DbType.Contains("CHAR")) ret.MySqlDbType = MySqlDbType.VarChar;
else if (col.Attribute.DbType.Contains("TEXT")) ret.MySqlDbType = MySqlDbType.Text;
else ret.MySqlDbType = MySqlDbType.VarChar;
}
else if ((MySqlDbType)tp.Value == MySqlDbType.Geometry)
{ {
ret.MySqlDbType = MySqlDbType.Text; ret.MySqlDbType = MySqlDbType.Text;
if (value != null) ret.Value = (value as MygisGeometry).AsText(); if (value != null) ret.Value = (value as MygisGeometry).AsText();

View File

@ -1,4 +1,5 @@
using FreeSql.Internal; using FreeSql.Internal;
using FreeSql.Internal.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -16,9 +17,10 @@ namespace FreeSql.Odbc.Default
public OdbcUtils(IFreeSql orm) : base(orm) { } public OdbcUtils(IFreeSql orm) : base(orm) { }
public OdbcAdapter Adapter => _orm.GetOdbcAdapter(); public OdbcAdapter Adapter => _orm.GetOdbcAdapter();
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1); if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1);
var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;

View File

@ -18,9 +18,10 @@ namespace FreeSql.Odbc.MySql
{ {
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null) if (tp != null)

View File

@ -68,7 +68,7 @@ namespace FreeSql.Odbc.Oracle
else else
{ {
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"))); 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); _params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
} }
++colidx2; ++colidx2;
} }
@ -110,7 +110,7 @@ namespace FreeSql.Odbc.Oracle
return 0; return 0;
} }
var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name); var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name);
var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol.Attribute.MapType, 0); var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol, _identCol.Attribute.MapType, 0);
identParam.Direction = ParameterDirection.Output; identParam.Direction = ParameterDirection.Output;
sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}"; sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}";
var dbParms = _params.Concat(new[] { identParam }).ToArray(); var dbParms = _params.Concat(new[] { identParam }).ToArray();
@ -179,7 +179,7 @@ namespace FreeSql.Odbc.Oracle
return 0; return 0;
} }
var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name); var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name);
var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol.Attribute.MapType, 0); var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol, _identCol.Attribute.MapType, 0);
identParam.Direction = ParameterDirection.Output; identParam.Direction = ParameterDirection.Output;
sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}"; sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}";
var dbParms = _params.Concat(new[] { identParam }).ToArray(); var dbParms = _params.Concat(new[] { identParam }).ToArray();

View File

@ -16,9 +16,10 @@ namespace FreeSql.Odbc.Oracle
{ {
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
var dbtype = (OdbcType)_orm.CodeFirst.GetDbInfo(type)?.type; var dbtype = (OdbcType)_orm.CodeFirst.GetDbInfo(type)?.type;
switch (dbtype) switch (dbtype)
{ {

View File

@ -10,6 +10,7 @@ using System.Text;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Data.Odbc; using System.Data.Odbc;
using FreeSql.Internal.Model;
namespace FreeSql.Odbc.PostgreSQL namespace FreeSql.Odbc.PostgreSQL
{ {
@ -63,9 +64,10 @@ namespace FreeSql.Odbc.PostgreSQL
return value; return value;
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
if (value != null) value = getParamterValue(type, value); if (value != null) value = getParamterValue(type, value);
var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
//if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) { //if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) {

View File

@ -1,4 +1,5 @@
using FreeSql.Internal; using FreeSql.Internal;
using FreeSql.Internal.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -21,9 +22,10 @@ namespace FreeSql.Odbc.SqlServer
public bool IsSqlServer2005 => ServerVersion == 9; public bool IsSqlServer2005 => ServerVersion == 9;
public int ServerVersion = 0; public int ServerVersion = 0;
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1); if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1);
var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new OdbcParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;

View File

@ -70,7 +70,7 @@ namespace FreeSql.Oracle.Curd
else else
{ {
sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}"))); 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); _params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
} }
++colidx2; ++colidx2;
} }
@ -112,7 +112,7 @@ namespace FreeSql.Oracle.Curd
return 0; return 0;
} }
var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name); var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name);
var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol.Attribute.MapType, 0) as OracleParameter; var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol, _identCol.Attribute.MapType, 0) as OracleParameter;
identParam.Direction = ParameterDirection.Output; identParam.Direction = ParameterDirection.Output;
sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}"; sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}";
var dbParms = _params.Concat(new[] { identParam }).ToArray(); var dbParms = _params.Concat(new[] { identParam }).ToArray();
@ -180,7 +180,7 @@ namespace FreeSql.Oracle.Curd
return 0; return 0;
} }
var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name); var identColName = _commonUtils.QuoteSqlName(_identCol.Attribute.Name);
var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol.Attribute.MapType, 0) as OracleParameter; var identParam = _commonUtils.AppendParamter(null, $"{_identCol.CsName}99", _identCol, _identCol.Attribute.MapType, 0) as OracleParameter;
identParam.Direction = ParameterDirection.Output; identParam.Direction = ParameterDirection.Output;
sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}"; sql = $"{sql} RETURNING {identColName} INTO {identParam.ParameterName}";
var dbParms = _params.Concat(new[] { identParam }).ToArray(); var dbParms = _params.Concat(new[] { identParam }).ToArray();

View File

@ -16,9 +16,10 @@ namespace FreeSql.Oracle
{ {
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
var dbtype = (OracleDbType)_orm.CodeFirst.GetDbInfo(type)?.type; var dbtype = (OracleDbType)_orm.CodeFirst.GetDbInfo(type)?.type;
if (dbtype == OracleDbType.Boolean) if (dbtype == OracleDbType.Boolean)
{ {
@ -26,6 +27,14 @@ namespace FreeSql.Oracle
else value = (bool)value == true ? 1 : 0; else value = (bool)value == true ? 1 : 0;
dbtype = OracleDbType.Int16; dbtype = OracleDbType.Int16;
} }
else if (col != null && type == typeof(string))
{
if (col.Attribute.DbType.Contains("NVARCHAR2")) dbtype = OracleDbType.NVarchar2;
else if (col.Attribute.DbType.Contains("VARCHAR2")) dbtype = OracleDbType.Varchar2;
else if (col.Attribute.DbType.Contains("NCHAR")) dbtype = OracleDbType.NChar;
else if (col.Attribute.DbType.Contains("CHAR")) dbtype = OracleDbType.Char;
else dbtype = OracleDbType.NVarchar2;
}
var ret = new OracleParameter { ParameterName = QuoteParamterName(parameterName), OracleDbType = dbtype, Value = value }; var ret = new OracleParameter { ParameterName = QuoteParamterName(parameterName), OracleDbType = dbtype, Value = value };
_params?.Add(ret); _params?.Add(ret);
return ret; return ret;

View File

@ -13,6 +13,7 @@ using System.Net;
using System.Text; using System.Text;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using FreeSql.Internal.Model;
namespace FreeSql.PostgreSQL namespace FreeSql.PostgreSQL
{ {
@ -78,16 +79,28 @@ namespace FreeSql.PostgreSQL
return value; return value;
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
if (value != null) value = getParamterValue(type, value); if (value != null) value = getParamterValue(type, value);
var ret = new NpgsqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new NpgsqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
//if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) { //if (value.GetType().IsEnum || value.GetType().GenericTypeArguments.FirstOrDefault()?.IsEnum == true) {
// ret.DataTypeName = ""; // ret.DataTypeName = "";
//} else { //} else {
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null) ret.NpgsqlDbType = (NpgsqlDbType)tp.Value; if (tp != null)
{
if (col != null && type == typeof(string))
{
if (col.Attribute.DbType.Contains("VARCHAR")) ret.NpgsqlDbType = NpgsqlDbType.Varchar;
else if (col.Attribute.DbType.Contains("CHAR")) ret.NpgsqlDbType = NpgsqlDbType.Char;
else if (col.Attribute.DbType.Contains("TEXT")) ret.NpgsqlDbType = NpgsqlDbType.Text;
else ret.NpgsqlDbType = NpgsqlDbType.Varchar;
}
else
ret.NpgsqlDbType = (NpgsqlDbType)tp.Value;
}
//} //}
_params?.Add(ret); _params?.Add(ret);
return ret; return ret;
@ -137,7 +150,7 @@ namespace FreeSql.PostgreSQL
if (value == null) return "NULL"; if (value == null) return "NULL";
if (_dicIsAssignableFromPostgisGeometry.GetOrAdd(type, t2 => typeof(PostgisGeometry).IsAssignableFrom(type.IsArray ? type.GetElementType() : type))) if (_dicIsAssignableFromPostgisGeometry.GetOrAdd(type, t2 => typeof(PostgisGeometry).IsAssignableFrom(type.IsArray ? type.GetElementType() : type)))
{ {
var pam = AppendParamter(specialParams, null, type, value); var pam = AppendParamter(specialParams, null, null, type, value);
return pam.ParameterName; return pam.ParameterName;
} }
value = getParamterValue(type, value); value = getParamterValue(type, value);

View File

@ -1,4 +1,5 @@
using FreeSql.Internal; using FreeSql.Internal;
using FreeSql.Internal.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -21,13 +22,28 @@ namespace FreeSql.SqlServer
public bool IsSqlServer2005 => ServerVersion == 9; public bool IsSqlServer2005 => ServerVersion == 9;
public int ServerVersion = 0; public int ServerVersion = 0;
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1); if (value?.Equals(DateTime.MinValue) == true) value = new DateTime(1970, 1, 1);
var ret = new SqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value }; var ret = new SqlParameter { ParameterName = QuoteParamterName(parameterName), Value = value };
var tp = _orm.CodeFirst.GetDbInfo(type)?.type; var tp = _orm.CodeFirst.GetDbInfo(type)?.type;
if (tp != null) ret.SqlDbType = (SqlDbType)tp.Value; if (tp != null)
{
if (col != null && type == typeof(string))
{
if (col.Attribute.DbType.Contains("NVARCHAR")) ret.SqlDbType = SqlDbType.NVarChar;
else if (col.Attribute.DbType.Contains("VARCHAR")) ret.SqlDbType = SqlDbType.VarChar;
else if (col.Attribute.DbType.Contains("NCHAR")) ret.SqlDbType = SqlDbType.NChar;
else if (col.Attribute.DbType.Contains("CHAR")) ret.SqlDbType = SqlDbType.Char;
else if (col.Attribute.DbType.Contains("NTEXT")) ret.SqlDbType = SqlDbType.NText;
else if (col.Attribute.DbType.Contains("TEXT")) ret.SqlDbType = SqlDbType.Text;
else ret.SqlDbType = SqlDbType.VarChar;
}
else
ret.SqlDbType = (SqlDbType)tp.Value;
}
_params?.Add(ret); _params?.Add(ret);
return ret; return ret;
} }

View File

@ -16,9 +16,10 @@ namespace FreeSql.Sqlite
{ {
} }
public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value) public override DbParameter AppendParamter(List<DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value)
{ {
if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}"; if (string.IsNullOrEmpty(parameterName)) parameterName = $"p_{_params?.Count}";
if (type == null && col != null) type = col.Attribute.MapType ?? col.CsType;
var dbtype = (DbType)_orm.CodeFirst.GetDbInfo(type)?.type; var dbtype = (DbType)_orm.CodeFirst.GetDbInfo(type)?.type;
switch (dbtype) switch (dbtype)
{ {