mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	- 增加 ISelect.ToDataTable 系列方法;
- 增加 无参数化命令执行,便于调试;
This commit is contained in:
		@@ -81,7 +81,9 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
					++colidx;
 | 
			
		||||
				}
 | 
			
		||||
			sb.Append(") VALUES");
 | 
			
		||||
			_params = new DbParameter[colidx * _source.Count];
 | 
			
		||||
			//_params = new DbParameter[colidx * _source.Count];
 | 
			
		||||
			_params = new DbParameter[0];
 | 
			
		||||
			//_params = new DbParameter[colidx * 5]; //批量添加第5行起,不使用参数化
 | 
			
		||||
			var didx = 0;
 | 
			
		||||
			foreach (var d in _source) {
 | 
			
		||||
				if (didx > 0) sb.Append(", ");
 | 
			
		||||
@@ -90,14 +92,18 @@ 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.QuoteWriteParamter(col.CsType, $"{_commonUtils.QuoteParamterName(col.CsName)}{didx}"));
 | 
			
		||||
						object val = null;
 | 
			
		||||
						if (_table.Properties.TryGetValue(col.CsName, out var tryp)) {
 | 
			
		||||
							val = tryp.GetValue(d);
 | 
			
		||||
							if (col.Attribute.IsPrimary && (col.CsType == typeof(Guid) || col.CsType == typeof(Guid?))
 | 
			
		||||
								&& (val == null || (Guid)val == Guid.Empty)) tryp.SetValue(d, val = FreeUtil.NewMongodbId());
 | 
			
		||||
						}
 | 
			
		||||
						_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, val);
 | 
			
		||||
						//if (didx >= 5)
 | 
			
		||||
							sb.Append(_commonUtils.GetNoneParamaterSqlValue(col.CsType, val));
 | 
			
		||||
						//else {
 | 
			
		||||
						//	sb.Append(_commonUtils.QuoteWriteParamter(col.CsType, $"{_commonUtils.QuoteParamterName(col.CsName)}{didx}"));
 | 
			
		||||
						//	_params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}{didx}", col.CsType, val);
 | 
			
		||||
						//}
 | 
			
		||||
						++colidx2;
 | 
			
		||||
					}
 | 
			
		||||
				sb.Append(")");
 | 
			
		||||
 
 | 
			
		||||
@@ -152,6 +152,21 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
		}
 | 
			
		||||
		public TSelect Take(int limit) => this.Limit(limit) as TSelect;
 | 
			
		||||
 | 
			
		||||
		public DataTable ToDataTable(string field = null) {
 | 
			
		||||
			var sql = this.ToSql(field);
 | 
			
		||||
			if (_cache.seconds > 0 && string.IsNullOrEmpty(_cache.key)) _cache.key = sql;
 | 
			
		||||
 | 
			
		||||
			return _orm.Cache.Shell(_cache.key, _cache.seconds, () =>
 | 
			
		||||
				_orm.Ado.ExecuteDataTable(_transaction, CommandType.Text, sql, _params.ToArray()));
 | 
			
		||||
		}
 | 
			
		||||
		public Task<DataTable> ToDataTableAsync(string field = null) {
 | 
			
		||||
			var sql = this.ToSql(field);
 | 
			
		||||
			if (_cache.seconds > 0 && string.IsNullOrEmpty(_cache.key)) _cache.key = sql;
 | 
			
		||||
 | 
			
		||||
			return _orm.Cache.ShellAsync(_cache.key, _cache.seconds, () =>
 | 
			
		||||
				_orm.Ado.ExecuteDataTableAsync(_transaction, CommandType.Text, sql, _params.ToArray()));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public List<TTuple> ToList<TTuple>(string field) {
 | 
			
		||||
			var sql = this.ToSql(field);
 | 
			
		||||
			if (_cache.seconds > 0 && string.IsNullOrEmpty(_cache.key)) _cache.key = sql;
 | 
			
		||||
@@ -472,6 +487,9 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
			return this.ToSql(af.field);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		protected DataTable InternalToDataTable(Expression select) => _orm.Ado.ExecuteDataTable(_transaction, CommandType.Text, this.InternalToSql<int>(select), _params.ToArray());
 | 
			
		||||
		protected Task<DataTable> InternalToDataTableAsync(Expression select) => _orm.Ado.ExecuteDataTableAsync(_transaction, CommandType.Text, this.InternalToSql<int>(select), _params.ToArray());
 | 
			
		||||
 | 
			
		||||
		protected TReturn InternalToAggregate<TReturn>(Expression select) {
 | 
			
		||||
			var map = new ReadAnonymousTypeInfo();
 | 
			
		||||
			var field = new StringBuilder();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -51,6 +52,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.Where(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
using FreeSql.Internal.Model;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Text;
 | 
			
		||||
@@ -99,6 +100,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		public Task<List<TReturn>> ToListAsync<TReturn>(Expression<Func<T1, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		public DataTable ToDataTable<TReturn>(Expression<Func<T1, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		public Task<DataTable> ToDataTableAsync<TReturn>(Expression<Func<T1, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		public string ToSql<TReturn>(Expression<Func<T1, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		public TReturn ToAggregate<TReturn>(Expression<Func<ISelectGroupingAggregate<T1>, TReturn>> select) => this.InternalToAggregate<TReturn>(select?.Body);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -44,6 +45,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3>.ToSql<TReturn>(Expression<Func<T1, T2, T3, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3> ISelect<T1, T2, T3>.Where(Expression<Func<T1, T2, T3, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -45,6 +46,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3, T4>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4> ISelect<T1, T2, T3, T4>.Where(Expression<Func<T1, T2, T3, T4, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -46,6 +47,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4, T5>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4, T5>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, T5, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3, T4, T5>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4, T5>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, T5, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4, T5> ISelect<T1, T2, T3, T4, T5>.Where(Expression<Func<T1, T2, T3, T4, T5, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -47,6 +48,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4, T5, T6>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4, T5, T6>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3, T4, T5, T6>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4, T5, T6>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4, T5, T6> ISelect<T1, T2, T3, T4, T5, T6>.Where(Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -48,6 +49,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4, T5, T6, T7>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4, T5, T6, T7>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3, T4, T5, T6, T7>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4, T5, T6, T7>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4, T5, T6, T7> ISelect<T1, T2, T3, T4, T5, T6, T7>.Where(Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -49,6 +50,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		Task<DataTable> ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.ToDataTableAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TReturn>> select) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4, T5, T6, T7, T8> ISelect<T1, T2, T3, T4, T5, T6, T7, T8>.Where(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -50,6 +51,10 @@ namespace FreeSql.Internal.CommonProvider {
 | 
			
		||||
 | 
			
		||||
		Task<List<TReturn>> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.ToListAsync<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TReturn>> select) => this.InternalToListAsync<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		DataTable ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.ToDataTable<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TReturn>> select) => this.InternalToDataTable(select?.Body);
 | 
			
		||||
 | 
			
		||||
		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) => this.InternalToDataTableAsync(select?.Body);
 | 
			
		||||
 | 
			
		||||
		string ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.ToSql<TReturn>(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TReturn>> select) => this.InternalToSql<TReturn>(select?.Body);
 | 
			
		||||
 | 
			
		||||
		ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.Where(Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, bool>> exp) => this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@ using System.Text;
 | 
			
		||||
namespace FreeSql.Internal {
 | 
			
		||||
	internal abstract class CommonUtils {
 | 
			
		||||
 | 
			
		||||
		internal abstract string GetNoneParamaterSqlValue(Type type, object value);
 | 
			
		||||
		internal abstract DbParameter[] GetDbParamtersByObject(string sql, object obj);
 | 
			
		||||
		internal abstract DbParameter AppendParamter(List<DbParameter> _params, string parameterName, Type type, object value);
 | 
			
		||||
		internal abstract string FormatSql(string sql, params object[] args);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user