mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	【测试】三大数据库,添加所有类型数据null/非空,后查询正常
This commit is contained in:
		@@ -40,7 +40,7 @@ namespace FreeSql.Internal {
 | 
			
		||||
						parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
 | 
			
		||||
						for (var idx = 0; idx < map.Count; idx++) {
 | 
			
		||||
							var child = new ReadAnonymousTypeInfo { CsName = map[idx].Column.CsName, DbField = $"{map[idx].Table.Alias}.{_common.QuoteSqlName(map[idx].Column.Attribute.Name)}" };
 | 
			
		||||
							field.Append(", ").Append(child.DbField).Append(" as").Append(++index);
 | 
			
		||||
							field.Append(", ").Append(_common.QuoteReadColumn(map[idx].Column.CsType, child.DbField)).Append(" as").Append(++index);
 | 
			
		||||
							parent.Childs.Add(child);
 | 
			
		||||
						}
 | 
			
		||||
					} else {
 | 
			
		||||
@@ -192,7 +192,7 @@ namespace FreeSql.Internal {
 | 
			
		||||
					throw new Exception($"MySqlExpression 未现实函数表达式 {exp3} 解析");
 | 
			
		||||
				case ExpressionType.MemberAccess:
 | 
			
		||||
					var exp4 = exp as MemberExpression;
 | 
			
		||||
					if (exp4.Expression != null && exp4.Expression.Type.FullName.StartsWith("System.Nullable`1[")) return ExpressionLambdaToSql(exp4.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
 | 
			
		||||
					if (exp4.Expression != null && exp4.Expression.Type.IsArray == false && exp4.Expression.Type.FullName.StartsWith("System.Nullable`1[")) return ExpressionLambdaToSql(exp4.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
 | 
			
		||||
					var extRet = "";
 | 
			
		||||
					switch (exp4.Expression?.Type.FullName ?? exp4.Type.FullName) {
 | 
			
		||||
						case "System.String": extRet = ExpressionLambdaToSqlMemberAccessString(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); break;
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,7 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
			if (e == null) return;
 | 
			
		||||
			string log = $"{pool.Policy.Name}数据库出错(执行SQL)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\r\n{cmd.CommandText}\r\n";
 | 
			
		||||
			foreach (DbParameter parm in cmd.Parameters)
 | 
			
		||||
				log += parm.ParameterName.PadRight(20, ' ') + " = " + (parm.Value ?? "NULL") + "\r\n";
 | 
			
		||||
				log += parm.ParameterName.PadRight(20, ' ') + " = " + ((parm.Value ?? DBNull.Value) == DBNull.Value ? "NULL" : parm.Value) + "\r\n";
 | 
			
		||||
 | 
			
		||||
			log += e.Message;
 | 
			
		||||
			_log.LogError(log);
 | 
			
		||||
 
 | 
			
		||||
@@ -75,8 +75,8 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
				foreach (var col in _table.Columns.Values)
 | 
			
		||||
					if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name) == false) {
 | 
			
		||||
						if (colidx2 > 0) sb.Append(", ");
 | 
			
		||||
						sb.Append(_commonUtils.QuoteParamterName(col.CsName)).Append(didx);
 | 
			
		||||
						_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value);
 | 
			
		||||
						sb.Append(_commonUtils.QuoteWriteParamter(col.CsType, $"{_commonUtils.QuoteParamterName(col.CsName)}{didx}"));
 | 
			
		||||
						_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : null);
 | 
			
		||||
						++colidx2;
 | 
			
		||||
					}
 | 
			
		||||
				sb.Append(")");
 | 
			
		||||
 
 | 
			
		||||
@@ -187,7 +187,7 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
				if (tb.Table.ColumnsByCs.TryGetValue(p.Name, out var col)) { //普通字段
 | 
			
		||||
					if (index > 0) field.Append(", ");
 | 
			
		||||
					var quoteName = _commonUtils.QuoteSqlName(col.Attribute.Name);
 | 
			
		||||
					field.Append(tb.Alias).Append(".").Append(quoteName);
 | 
			
		||||
					field.Append(_commonUtils.QuoteReadColumn(col.CsType, $"{tb.Alias}.{quoteName}"));
 | 
			
		||||
					++index;
 | 
			
		||||
					if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index);
 | 
			
		||||
					else dicfield.Add(quoteName, true);
 | 
			
		||||
@@ -200,7 +200,7 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
					foreach (var col2 in tb2.Table.Columns.Values) {
 | 
			
		||||
						if (index > 0) field.Append(", ");
 | 
			
		||||
						var quoteName = _commonUtils.QuoteSqlName(col2.Attribute.Name);
 | 
			
		||||
						field.Append(tb2.Alias).Append(".").Append(quoteName);
 | 
			
		||||
						field.Append(_commonUtils.QuoteReadColumn(col.CsType, $"{tb2.Alias}.{quoteName}"));
 | 
			
		||||
						++index;
 | 
			
		||||
						if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index);
 | 
			
		||||
						else dicfield.Add(quoteName, true);
 | 
			
		||||
 
 | 
			
		||||
@@ -52,10 +52,12 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value) {
 | 
			
		||||
			var col = _commonExpression.ExpressionSelectColumn_MemberAccess(null, null, SelectTableInfoType.From, column?.Body, true);
 | 
			
		||||
			if (string.IsNullOrEmpty(col)) return this;
 | 
			
		||||
			_set.Append(", ").Append(col).Append(" = ").Append(_commonUtils.QuoteParamterName("p_")).Append(_params.Count);
 | 
			
		||||
			_commonUtils.AppendParamter(_params, null, value);
 | 
			
		||||
			var cols = new List<SelectColumnInfo>();
 | 
			
		||||
			_commonExpression.ExpressionSelectColumn_MemberAccess(null, cols, SelectTableInfoType.From, column?.Body, true);
 | 
			
		||||
			if (cols.Count != 1) return this;
 | 
			
		||||
			var col = cols.First();
 | 
			
		||||
			_set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Column.Attribute.Name)).Append(" = ").Append(_commonUtils.QuoteWriteParamter(col.Column.CsType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}"));
 | 
			
		||||
			_commonUtils.AppendParamter(_params, null, col.Column.CsType, value);
 | 
			
		||||
			//foreach (var t in _source) Utils.FillPropertyValue(t, tryf.CsName, value);
 | 
			
		||||
			return this;
 | 
			
		||||
		}
 | 
			
		||||
@@ -111,8 +113,8 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
				foreach (var col in _table.Columns.Values) {
 | 
			
		||||
					if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.CsName) == false) {
 | 
			
		||||
						if (colidx > 0) sb.Append(", ");
 | 
			
		||||
						sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(_commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
 | 
			
		||||
						_commonUtils.AppendParamter(_paramsSource, null, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(_source.First()) : DBNull.Value);
 | 
			
		||||
						sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(_commonUtils.QuoteWriteParamter(col.CsType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
 | 
			
		||||
						_commonUtils.AppendParamter(_paramsSource, null, col.CsType, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(_source.First()) : null);
 | 
			
		||||
						++colidx;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
@@ -151,8 +153,8 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
							//	++pkidx;
 | 
			
		||||
							//}
 | 
			
		||||
							//if (_table.Primarys.Length > 1) sb.Append(")");
 | 
			
		||||
							sb.Append(" THEN ").Append(_commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
 | 
			
		||||
							_commonUtils.AppendParamter(_paramsSource, null, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value);
 | 
			
		||||
							sb.Append(" THEN ").Append(_commonUtils.QuoteWriteParamter(col.CsType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}")));
 | 
			
		||||
							_commonUtils.AppendParamter(_paramsSource, null, col.CsType, _table.Properties.TryGetValue(col.CsName, out var tryp) ? tryp.GetValue(d) : DBNull.Value);
 | 
			
		||||
						}
 | 
			
		||||
						sb.Append(" END");
 | 
			
		||||
						++colidx;
 | 
			
		||||
 
 | 
			
		||||
@@ -11,12 +11,14 @@ namespace FreeSql.Internal {
 | 
			
		||||
	internal abstract class CommonUtils {
 | 
			
		||||
 | 
			
		||||
		internal abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
 | 
			
		||||
		internal abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, object value);
 | 
			
		||||
		internal abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value);
 | 
			
		||||
		internal abstract string FormatSql(string sql, params object[] args);
 | 
			
		||||
		internal abstract string QuoteSqlName(string name);
 | 
			
		||||
		internal abstract string QuoteParamterName(string name);
 | 
			
		||||
		internal abstract string IsNull(string sql, object value);
 | 
			
		||||
		internal abstract string StringConcat(string left, string right, Type leftType, Type rightType);
 | 
			
		||||
		internal abstract string QuoteWriteParamter(Type type, string paramterName);
 | 
			
		||||
		internal abstract string QuoteReadColumn(Type type, string columnName);
 | 
			
		||||
 | 
			
		||||
		internal ICodeFirst CodeFirst { get; set; }
 | 
			
		||||
		internal TableInfo GetTableByEntity(Type entity) => Utils.GetTableByEntity(entity, this);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using FreeSql.DataAnnotations;
 | 
			
		||||
using FreeSql.Internal.Model;
 | 
			
		||||
using Newtonsoft.Json.Linq;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Concurrent;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
@@ -14,7 +15,7 @@ namespace FreeSql.Internal {
 | 
			
		||||
 | 
			
		||||
		static ConcurrentDictionary<string, TableInfo> _cacheGetTableByEntity = new ConcurrentDictionary<string, TableInfo>();
 | 
			
		||||
		internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
 | 
			
		||||
			if (_cacheGetTableByEntity.TryGetValue(entity.FullName, out var trytb)) return trytb;
 | 
			
		||||
			if (_cacheGetTableByEntity.TryGetValue($"{common.QuoteSqlName("db")}{entity.FullName}", out var trytb)) return trytb; //区分数据库类型缓存
 | 
			
		||||
			if (common.CodeFirst.GetDbInfo(entity) != null) return null;
 | 
			
		||||
 | 
			
		||||
			var tbattr = entity.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
 | 
			
		||||
@@ -145,8 +146,35 @@ namespace FreeSql.Internal {
 | 
			
		||||
		}
 | 
			
		||||
		internal static object GetDataReaderValue(Type type, object value) {
 | 
			
		||||
			if (value == null || value == DBNull.Value) return null;
 | 
			
		||||
			if (type.FullName == "System.Byte[]") return value;
 | 
			
		||||
			if (type.IsArray) {
 | 
			
		||||
				var elementType = type.GetElementType();
 | 
			
		||||
				var valueArr = value as Array;
 | 
			
		||||
				var len = valueArr.GetLength(0);
 | 
			
		||||
				var ret = Array.CreateInstance(elementType, len);
 | 
			
		||||
				for (var a = 0; a < len; a++) {
 | 
			
		||||
					var item = valueArr.GetValue(a);
 | 
			
		||||
					ret.SetValue(GetDataReaderValue(elementType, item), a);
 | 
			
		||||
				}
 | 
			
		||||
				return ret;
 | 
			
		||||
			}
 | 
			
		||||
			if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First();
 | 
			
		||||
			if (type.IsEnum) return Enum.Parse(type, string.Concat(value));
 | 
			
		||||
			if (type.IsEnum) return Enum.Parse(type, string.Concat(value), true);
 | 
			
		||||
			switch(type.FullName) {
 | 
			
		||||
				case "System.Guid":
 | 
			
		||||
					if (value.GetType() != type) return Guid.TryParse(string.Concat(value), out var tryguid) ? tryguid : Guid.Empty;
 | 
			
		||||
					return value;
 | 
			
		||||
				case "MygisPoint": return MygisPoint.Parse(string.Concat(value)) as MygisPoint;
 | 
			
		||||
				case "MygisLineString": return MygisLineString.Parse(string.Concat(value)) as MygisLineString;
 | 
			
		||||
				case "MygisPolygon": return MygisPolygon.Parse(string.Concat(value)) as MygisPolygon;
 | 
			
		||||
				case "MygisMultiPoint": return MygisMultiPoint.Parse(string.Concat(value)) as MygisMultiPoint;
 | 
			
		||||
				case "MygisMultiLineString": return MygisMultiLineString.Parse(string.Concat(value)) as MygisMultiLineString;
 | 
			
		||||
				case "MygisMultiPolygon": return MygisMultiPolygon.Parse(string.Concat(value)) as MygisMultiPolygon;
 | 
			
		||||
				case "Newtonsoft.Json.Linq.JToken": return JToken.Parse(string.Concat(value));
 | 
			
		||||
				case "Newtonsoft.Json.Linq.JObject": return JObject.Parse(string.Concat(value));
 | 
			
		||||
				case "Newtonsoft.Json.Linq.JArray": return JArray.Parse(string.Concat(value));
 | 
			
		||||
				case "Npgsql.LegacyPostgis.PostgisGeometry": return value;
 | 
			
		||||
			}
 | 
			
		||||
			if (type != value.GetType()) return Convert.ChangeType(value, type);
 | 
			
		||||
			return value;
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user