|
| 1 | +using FreeSql.Internal.CommonProvider; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Data; |
| 5 | +using System.Data.Common; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace FreeSql.Internal.Model |
| 11 | +{ |
| 12 | + public class AdoCommandFluent |
| 13 | + { |
| 14 | + internal protected AdoProvider Ado { get; protected set; } |
| 15 | + internal protected DbConnection Connection { get; protected set; } |
| 16 | + internal protected DbTransaction Transaction { get; protected set; } |
| 17 | + internal protected CommandType CmdType { get; protected set; } = System.Data.CommandType.Text; |
| 18 | + internal protected string CmdText { get; protected set; } |
| 19 | + internal protected int CmdTimeout { get; protected set; } |
| 20 | + internal protected List<DbParameter> CmdParameters { get; } = new List<DbParameter>(); |
| 21 | + |
| 22 | + public AdoCommandFluent(AdoProvider ado, string commandText, object parms) |
| 23 | + { |
| 24 | + this.Ado = ado; |
| 25 | + this.CmdText = commandText; |
| 26 | + this.CmdParameters.AddRange(this.Ado.GetDbParamtersByObject(parms)); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// 使用指定 DbConnection 连接执行 |
| 31 | + /// </summary> |
| 32 | + /// <param name="conn"></param> |
| 33 | + /// <returns></returns> |
| 34 | + public AdoCommandFluent WithConnection(DbConnection conn) |
| 35 | + { |
| 36 | + this.Transaction = null; |
| 37 | + this.Connection = conn; |
| 38 | + return this; |
| 39 | + } |
| 40 | + /// <summary> |
| 41 | + /// 使用指定 DbTransaction 事务执行 |
| 42 | + /// </summary> |
| 43 | + /// <param name="tran"></param> |
| 44 | + /// <returns></returns> |
| 45 | + public AdoCommandFluent WithTransaction(DbTransaction tran) |
| 46 | + { |
| 47 | + this.Transaction = tran; |
| 48 | + if (tran != null) this.Connection = tran.Connection; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// 增加参数化对象 |
| 54 | + /// </summary> |
| 55 | + /// <param name="parameterName">参数名</param> |
| 56 | + /// <param name="value">参数值</param> |
| 57 | + /// <param name="modify">修改本次创建好的参数化对象,比如将 parameterName 参数修改为 Output 类型</param> |
| 58 | + /// <returns></returns> |
| 59 | + public AdoCommandFluent WithParameter(string parameterName, object value, Action<DbParameter> modify = null) |
| 60 | + { |
| 61 | + var param = this.Ado.GetDbParamtersByObject(new Dictionary<string, object> { [parameterName] = value }).FirstOrDefault(); |
| 62 | + modify?.Invoke(param); |
| 63 | + this.CmdParameters.Add(param); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// 设置执行的命令类型,SQL文本、或存储过程 |
| 69 | + /// </summary> |
| 70 | + /// <param name="commandType"></param> |
| 71 | + /// <returns></returns> |
| 72 | + public AdoCommandFluent CommandType(CommandType commandType) |
| 73 | + { |
| 74 | + this.CmdType = commandType; |
| 75 | + return this; |
| 76 | + } |
| 77 | + /// <summary> |
| 78 | + /// 设置命令执行超时(秒) |
| 79 | + /// </summary> |
| 80 | + /// <param name="commandTimeout"></param> |
| 81 | + /// <returns></returns> |
| 82 | + public AdoCommandFluent CommandTimeout(int commandTimeout) |
| 83 | + { |
| 84 | + this.CmdTimeout = commandTimeout; |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public int ExecuteNonQuery() => this.Ado.ExecuteNonQuery(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 89 | + public object ExecuteScalar() => this.Ado.ExecuteScalar(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 90 | + public DataTable ExecuteDataTable() => this.Ado.ExecuteDataTable(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 91 | + public DataSet ExecuteDataSet() => this.Ado.ExecuteDataSet(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 92 | + public object[][] ExecuteArray() => this.Ado.ExecuteArray(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 93 | + public List<T> Query<T>() => this.Ado.Query<T>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 94 | + public NativeTuple<List<T1>, List<T2>> Query<T1, T2>() => this.Ado.Query<T1, T2>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 95 | + public NativeTuple<List<T1>, List<T2>, List<T3>> Query<T1, T2, T3>() => this.Ado.Query<T1, T2, T3>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 96 | + public NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>> Query<T1, T2, T3, T4>() => this.Ado.Query<T1, T2, T3, T4>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 97 | + public NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>, List<T5>> Query<T1, T2, T3, T4, T5>() => this.Ado.Query<T1, T2, T3, T4, T5>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 98 | + |
| 99 | +#if net40 |
| 100 | +#else |
| 101 | + public Task<int> ExecuteNonQueryAsync() => this.Ado.ExecuteNonQueryAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 102 | + public Task<object> ExecuteScalarAsync() => this.Ado.ExecuteScalarAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 103 | + public Task<DataTable> ExecuteDataTableAsync() => this.Ado.ExecuteDataTableAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 104 | + public Task<DataSet> ExecuteDataSetAsync() => this.Ado.ExecuteDataSetAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 105 | + public Task<object[][]> ExecuteArrayAsync() => this.Ado.ExecuteArrayAsync(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 106 | + public Task<List<T>> QueryAsync<T>() => this.Ado.QueryAsync<T>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 107 | + public Task<NativeTuple<List<T1>, List<T2>>> QueryAsync<T1, T2>() => this.Ado.QueryAsync<T1, T2>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 108 | + public Task<NativeTuple<List<T1>, List<T2>, List<T3>>> QueryAsync<T1, T2, T3>() => this.Ado.QueryAsync<T1, T2, T3>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 109 | + public Task<NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>>> QueryAsync<T1, T2, T3, T4>() => this.Ado.QueryAsync<T1, T2, T3, T4>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 110 | + public Task<NativeTuple<List<T1>, List<T2>, List<T3>, List<T4>, List<T5>>> QueryAsync<T1, T2, T3, T4, T5>() => this.Ado.QueryAsync<T1, T2, T3, T4, T5>(this.Connection, this.Transaction, this.CmdType, this.CmdText, this.CmdTimeout, this.CmdParameters.ToArray()); |
| 111 | +#endif |
| 112 | + } |
| 113 | +} |
0 commit comments