You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

737 lines
33 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. namespace SqlSugar
  8. {
  9. ///<summary>
  10. /// ** description:Create datathis.access object
  11. /// ** author:sunkaixuan
  12. /// ** date:2017/1/2
  13. /// ** email:610262374@qq.com
  14. /// </summary>
  15. public partial class SqlSugarClient : IDisposable
  16. {
  17. #region Constructor
  18. public SqlSugarClient(ConnectionConfig config)
  19. {
  20. this.Context = this;
  21. this.CurrentConnectionConfig = config;
  22. this.ContextID = Guid.NewGuid();
  23. Check.ArgumentNullException(config, "config is null");
  24. switch (config.DbType)
  25. {
  26. case DbType.MySql:
  27. DependencyManagement.TryMySqlData();
  28. break;
  29. case DbType.SqlServer:
  30. break;
  31. case DbType.Sqlite:
  32. DependencyManagement.TrySqlite();
  33. break;
  34. case DbType.Oracle:
  35. DependencyManagement.TryOracle();
  36. break;
  37. case DbType.PostgreSQL:
  38. throw new Exception("Development 50%");
  39. default:
  40. throw new Exception("ConnectionConfig.DbType is null");
  41. }
  42. }
  43. #endregion Constructor
  44. #region ADO Methods
  45. /// <summary>
  46. ///Datathis.operation
  47. /// </summary>
  48. public virtual IAdo Ado
  49. {
  50. get
  51. {
  52. if (this.ContextAdo == null)
  53. {
  54. var result = InstanceFactory.GetAdo(this.Context.CurrentConnectionConfig);
  55. this.ContextAdo = result;
  56. result.Context = this.Context;
  57. return result;
  58. }
  59. return this.Context._Ado;
  60. }
  61. }
  62. #endregion ADO Methods
  63. #region Aop Log Methods
  64. public virtual AopProvider Aop { get { return new AopProvider(this.Context); } }
  65. #endregion Aop Log Methods
  66. #region Util Methods
  67. [Obsolete("Use SqlSugarClient.Utilities")]
  68. public virtual IContextMethods RewritableMethods
  69. {
  70. get { return this.Context.Utilities; }
  71. set { this.Context.Utilities = value; }
  72. }
  73. public virtual IContextMethods Utilities
  74. {
  75. get
  76. {
  77. if (ContextRewritableMethods == null)
  78. {
  79. ContextRewritableMethods = new ContextMethods
  80. {
  81. Context = this.Context
  82. };
  83. }
  84. return ContextRewritableMethods;
  85. }
  86. set { ContextRewritableMethods = value; }
  87. }
  88. #endregion Util Methods
  89. #region Queryable
  90. /// <summary>
  91. /// Get datebase time
  92. /// </summary>
  93. /// <returns></returns>
  94. public DateTime GetDate()
  95. {
  96. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
  97. return this.Ado.GetDateTime(sqlBuilder.FullSqlDateNow);
  98. }
  99. /// <summary>
  100. /// Lambda Query operation
  101. /// </summary>
  102. public virtual ISugarQueryable<T> Queryable<T>() where T : class, new()
  103. {
  104. InitMppingInfo<T>();
  105. var result = this.CreateQueryable<T>();
  106. return result;
  107. }
  108. /// <summary>
  109. /// Lambda Query operation
  110. /// </summary>
  111. /// <param name="shortName">todo: describe shortName parameter on Queryable</param>
  112. public virtual ISugarQueryable<T> Queryable<T>(string shortName) where T : class, new()
  113. {
  114. var queryable = Queryable<T>();
  115. queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
  116. return queryable;
  117. }
  118. /// <summary>
  119. /// Lambda Query operation
  120. /// </summary>
  121. /// <param name="tableName">todo: describe tableName parameter on Queryable</param>
  122. /// <param name="shortName">todo: describe shortName parameter on Queryable</param>
  123. public virtual ISugarQueryable<ExpandoObject> Queryable(string tableName, string shortName)
  124. {
  125. var queryable = Queryable<ExpandoObject>();
  126. queryable.SqlBuilder.QueryBuilder.EntityName = tableName;
  127. queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
  128. return queryable;
  129. }
  130. public virtual ISugarQueryable<T, T2> Queryable<T, T2>(Expression<Func<T, T2, object[]>> joinExpression) where T : class, new()
  131. {
  132. InitMppingInfo<T, T2>();
  133. var types = new Type[] { typeof(T2) };
  134. var queryable = InstanceFactory.GetQueryable<T, T2>(this.CurrentConnectionConfig);
  135. this.CreateQueryJoin(joinExpression, types, queryable);
  136. return queryable;
  137. }
  138. public virtual ISugarQueryable<T, T2, T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression) where T : class, new()
  139. {
  140. InitMppingInfo<T, T2, T3>();
  141. var types = new Type[] { typeof(T2), typeof(T3) };
  142. var queryable = InstanceFactory.GetQueryable<T, T2, T3>(this.CurrentConnectionConfig);
  143. this.CreateQueryJoin(joinExpression, types, queryable);
  144. return queryable;
  145. }
  146. public virtual ISugarQueryable<T, T2, T3, T4> Queryable<T, T2, T3, T4>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression) where T : class, new()
  147. {
  148. InitMppingInfo<T, T2, T3, T4>();
  149. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4) };
  150. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4>(this.CurrentConnectionConfig);
  151. this.CreateQueryJoin(joinExpression, types, queryable);
  152. return queryable;
  153. }
  154. public virtual ISugarQueryable<T, T2, T3, T4, T5> Queryable<T, T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, T5, object[]>> joinExpression) where T : class, new()
  155. {
  156. InitMppingInfo<T, T2, T3, T4, T5>();
  157. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5) };
  158. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5>(this.CurrentConnectionConfig);
  159. this.CreateQueryJoin(joinExpression, types, queryable);
  160. return queryable;
  161. }
  162. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6> Queryable<T, T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T4, T5, T6, object[]>> joinExpression) where T : class, new()
  163. {
  164. InitMppingInfo<T, T2, T3, T4, T5, T6>();
  165. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6) };
  166. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6>(this.CurrentConnectionConfig);
  167. this.CreateQueryJoin(joinExpression, types, queryable);
  168. return queryable;
  169. }
  170. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7> Queryable<T, T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T3, T4, T5, T6, T7, object[]>> joinExpression) where T : class, new()
  171. {
  172. InitMppingInfo<T, T2, T3, T4, T5, T6, T7>();
  173. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) };
  174. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7>(this.CurrentConnectionConfig);
  175. this.CreateQueryJoin(joinExpression, types, queryable);
  176. return queryable;
  177. }
  178. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> Queryable<T, T2, T3, T4, T5, T6, T7, T8>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, object[]>> joinExpression) where T : class, new()
  179. {
  180. InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8>();
  181. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) };
  182. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(this.CurrentConnectionConfig);
  183. this.CreateQueryJoin(joinExpression, types, queryable);
  184. return queryable;
  185. }
  186. #region 9-12
  187. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, object[]>> joinExpression) where T : class, new()
  188. {
  189. InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9>();
  190. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9) };
  191. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(this.CurrentConnectionConfig);
  192. this.CreateQueryJoin(joinExpression, types, queryable);
  193. return queryable;
  194. }
  195. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, object[]>> joinExpression) where T : class, new()
  196. {
  197. InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>();
  198. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10) };
  199. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this.CurrentConnectionConfig);
  200. this.CreateQueryJoin(joinExpression, types, queryable);
  201. return queryable;
  202. }
  203. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, object[]>> joinExpression) where T : class, new()
  204. {
  205. InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>();
  206. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11) };
  207. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this.CurrentConnectionConfig);
  208. this.CreateQueryJoin(joinExpression, types, queryable);
  209. return queryable;
  210. }
  211. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, object[]>> joinExpression) where T : class, new()
  212. {
  213. InitMppingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>();
  214. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12) };
  215. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(this.CurrentConnectionConfig);
  216. this.CreateQueryJoin(joinExpression, types, queryable);
  217. return queryable;
  218. }
  219. #endregion 9-12
  220. public virtual ISugarQueryable<T, T2> Queryable<T, T2>(Expression<Func<T, T2, bool>> joinExpression) where T : class, new()
  221. {
  222. InitMppingInfo<T, T2>();
  223. var types = new Type[] { typeof(T2) };
  224. var queryable = InstanceFactory.GetQueryable<T, T2>(this.CurrentConnectionConfig);
  225. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  226. queryable.Where(joinExpression);
  227. return queryable;
  228. }
  229. public virtual ISugarQueryable<T, T2, T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, bool>> joinExpression) where T : class, new()
  230. {
  231. InitMppingInfo<T, T2, T3>();
  232. var types = new Type[] { typeof(T2), typeof(T3) };
  233. var queryable = InstanceFactory.GetQueryable<T, T2, T3>(this.CurrentConnectionConfig);
  234. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  235. queryable.Where(joinExpression);
  236. return queryable;
  237. }
  238. public virtual ISugarQueryable<T, T2, T3, T4> Queryable<T, T2, T3, T4>(Expression<Func<T, T2, T3, T4, bool>> joinExpression) where T : class, new()
  239. {
  240. InitMppingInfo<T, T2, T3, T4>();
  241. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4) };
  242. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4>(this.CurrentConnectionConfig);
  243. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  244. queryable.Where(joinExpression);
  245. return queryable;
  246. }
  247. public virtual ISugarQueryable<T, T2, T3, T4, T5> Queryable<T, T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, T5, bool>> joinExpression) where T : class, new()
  248. {
  249. InitMppingInfo<T, T2, T3, T4, T5>();
  250. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5) };
  251. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5>(this.CurrentConnectionConfig);
  252. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  253. queryable.Where(joinExpression);
  254. return queryable;
  255. }
  256. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6> Queryable<T, T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T4, T5, T6, bool>> joinExpression) where T : class, new()
  257. {
  258. InitMppingInfo<T, T2, T3, T4, T5, T6>();
  259. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6) };
  260. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6>(this.CurrentConnectionConfig);
  261. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  262. queryable.Where(joinExpression);
  263. return queryable;
  264. }
  265. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7> Queryable<T, T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T3, T4, T5, T6, T7, bool>> joinExpression) where T : class, new()
  266. {
  267. InitMppingInfo<T, T2, T3, T4, T5, T6>();
  268. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) };
  269. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7>(this.CurrentConnectionConfig);
  270. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  271. queryable.Where(joinExpression);
  272. return queryable;
  273. }
  274. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> Queryable<T, T2, T3, T4, T5, T6, T7, T8>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, bool>> joinExpression) where T : class, new()
  275. {
  276. InitMppingInfo<T, T2, T3, T4, T5, T6, T8>();
  277. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) };
  278. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(this.CurrentConnectionConfig);
  279. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  280. queryable.Where(joinExpression);
  281. return queryable;
  282. }
  283. #region 9-12
  284. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, bool>> joinExpression) where T : class, new()
  285. {
  286. InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9>();
  287. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9) };
  288. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(this.CurrentConnectionConfig);
  289. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  290. queryable.Where(joinExpression);
  291. return queryable;
  292. }
  293. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> joinExpression) where T : class, new()
  294. {
  295. InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10>();
  296. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10) };
  297. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this.CurrentConnectionConfig);
  298. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  299. queryable.Where(joinExpression);
  300. return queryable;
  301. }
  302. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, bool>> joinExpression) where T : class, new()
  303. {
  304. InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10, T11>();
  305. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11) };
  306. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this.CurrentConnectionConfig);
  307. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  308. queryable.Where(joinExpression);
  309. return queryable;
  310. }
  311. public virtual ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Queryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, bool>> joinExpression) where T : class, new()
  312. {
  313. InitMppingInfo<T, T2, T3, T4, T5, T6, T8, T9, T10, T11, T12>();
  314. var types = new Type[] { typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12) };
  315. var queryable = InstanceFactory.GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(this.CurrentConnectionConfig);
  316. this.CreateEasyQueryJoin(joinExpression, types, queryable);
  317. queryable.Where(joinExpression);
  318. return queryable;
  319. }
  320. public virtual ISugarQueryable<T, T2> Queryable<T, T2>(
  321. ISugarQueryable<T> joinQueryable1, ISugarQueryable<T2> joinQueryable2, Expression<Func<T, T2, bool>> joinExpression) where T : class, new() where T2 : class, new()
  322. {
  323. return Queryable(joinQueryable1, joinQueryable2, JoinType.Inner, joinExpression);
  324. }
  325. public virtual ISugarQueryable<T, T2> Queryable<T, T2>(
  326. ISugarQueryable<T> joinQueryable1, ISugarQueryable<T2> joinQueryable2, JoinType joinType, Expression<Func<T, T2, bool>> joinExpression) where T : class, new() where T2 : class, new()
  327. {
  328. Check.Exception(joinQueryable1.QueryBuilder.Take != null || joinQueryable1.QueryBuilder.Skip != null || joinQueryable1.QueryBuilder.OrderByValue.HasValue(), "joinQueryable1 Cannot have 'Skip' 'ToPageList' 'Take' Or 'OrderBy'");
  329. Check.Exception(joinQueryable2.QueryBuilder.Take != null || joinQueryable2.QueryBuilder.Skip != null || joinQueryable2.QueryBuilder.OrderByValue.HasValue(), "joinQueryable2 Cannot have 'Skip' 'ToPageList' 'Take' Or 'OrderBy'");
  330. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
  331. sqlBuilder.Context = this.Context;
  332. InitMppingInfo<T, T2>();
  333. var types = new Type[] { typeof(T2) };
  334. var queryable = InstanceFactory.GetQueryable<T, T2>(this.CurrentConnectionConfig);
  335. queryable.Context = this.Context;
  336. queryable.SqlBuilder = sqlBuilder;
  337. queryable.QueryBuilder = InstanceFactory.GetQueryBuilder(this.CurrentConnectionConfig);
  338. queryable.QueryBuilder.JoinQueryInfos = new List<JoinQueryInfo>();
  339. queryable.QueryBuilder.Builder = sqlBuilder;
  340. queryable.QueryBuilder.Context = this.Context;
  341. queryable.QueryBuilder.EntityType = typeof(T);
  342. queryable.QueryBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
  343. //master
  344. var shortName1 = joinExpression.Parameters[0].Name;
  345. var sqlObj1 = joinQueryable1.ToSql();
  346. var sql1 = sqlObj1.Key;
  347. UtilMethods.RepairReplicationParameters(ref sql1, sqlObj1.Value.ToArray(), 0);
  348. queryable.QueryBuilder.EntityName = sqlBuilder.GetPackTable(sql1, shortName1); ;
  349. queryable.QueryBuilder.Parameters.AddRange(sqlObj1.Value);
  350. //join table 1
  351. var shortName2 = joinExpression.Parameters[1].Name;
  352. var sqlObj2 = joinQueryable2.ToSql();
  353. var sql2 = sqlObj2.Key;
  354. UtilMethods.RepairReplicationParameters(ref sql2, sqlObj2.Value.ToArray(), 1);
  355. queryable.QueryBuilder.Parameters.AddRange(sqlObj2.Value);
  356. var exp = queryable.QueryBuilder.GetExpressionValue(joinExpression, ResolveExpressType.WhereMultiple);
  357. queryable.QueryBuilder.JoinQueryInfos.Add(new JoinQueryInfo { JoinIndex = 0, JoinType = joinType, JoinWhere = exp.GetResultString(), TableName = sqlBuilder.GetPackTable(sql2, shortName2) });
  358. return queryable;
  359. }
  360. #endregion 9-12
  361. public virtual ISugarQueryable<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
  362. {
  363. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
  364. Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
  365. var i = 1;
  366. var allItems = new List<KeyValuePair<string, List<SugarParameter>>>();
  367. foreach (var item in queryables)
  368. {
  369. var sqlObj = item.ToSql();
  370. var sql = sqlObj.Key;
  371. UtilMethods.RepairReplicationParameters(ref sql, sqlObj.Value.ToArray(), i);
  372. if (sqlObj.Value.HasValue())
  373. allItems.Add(new KeyValuePair<string, List<SugarParameter>>(sql, sqlObj.Value));
  374. else
  375. allItems.Add(new KeyValuePair<string, List<SugarParameter>>(sql, new List<SugarParameter>()));
  376. i++;
  377. }
  378. var allSql = sqlBuilder.GetUnionAllSql(allItems.Select(it => it.Key).ToList());
  379. var allParameters = allItems.SelectMany(it => it.Value).ToArray();
  380. var resulut = this.Context.Queryable<ExpandoObject>().AS(UtilMethods.GetPackTable(allSql, "unionTable")).With(SqlWith.Null);
  381. resulut.AddParameters(allParameters);
  382. return resulut.Select<T>(sqlBuilder.SqlSelectAll);
  383. }
  384. public virtual ISugarQueryable<T> UnionAll<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
  385. {
  386. Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
  387. return UnionAll(queryables.ToArray());
  388. }
  389. #endregion Queryable
  390. #region SqlQueryable
  391. public ISugarQueryable<T> SqlQueryable<T>(string sql) where T : class, new()
  392. {
  393. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
  394. return this.Context.Queryable<T>().AS(sqlBuilder.GetPackTable(sql, sqlBuilder.GetDefaultShortName())).With(SqlWith.Null).Select(sqlBuilder.GetDefaultShortName() + ".*");
  395. }
  396. #endregion SqlQueryable
  397. #region Insertable
  398. public virtual IInsertable<T> Insertable<T>(T[] insertObjs) where T : class, new()
  399. {
  400. InitMppingInfo<T>();
  401. var result = this.CreateInsertable(insertObjs);
  402. return result;
  403. }
  404. public virtual IInsertable<T> Insertable<T>(List<T> insertObjs) where T : class, new()
  405. {
  406. Check.ArgumentNullException(insertObjs, "Insertable.insertObjs can't be null");
  407. return this.Context.Insertable(insertObjs.ToArray());
  408. }
  409. public virtual IInsertable<T> Insertable<T>(T insertObj) where T : class, new()
  410. {
  411. return this.Context.Insertable(new T[] { insertObj });
  412. }
  413. public virtual IInsertable<T> Insertable<T>(Dictionary<string, object> columnDictionary) where T : class, new()
  414. {
  415. InitMppingInfo<T>();
  416. Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Insertable.columnDictionary can't be null");
  417. var insertObject = this.Context.Utilities.DeserializeObject<T>(this.Context.Utilities.SerializeObject(columnDictionary));
  418. var columns = columnDictionary.Select(it => it.Key).ToList();
  419. return this.Context.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ;
  420. }
  421. public virtual IInsertable<T> Insertable<T>(dynamic insertDynamicObject) where T : class, new()
  422. {
  423. InitMppingInfo<T>();
  424. if (insertDynamicObject is T)
  425. {
  426. return this.Context.Insertable((T)insertDynamicObject);
  427. }
  428. else
  429. {
  430. var columns = ((object)insertDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList();
  431. Check.Exception(columns.IsNullOrEmpty(), "Insertable.updateDynamicObject can't be null");
  432. T insertObject = this.Context.Utilities.DeserializeObject<T>(this.Context.Utilities.SerializeObject(insertDynamicObject));
  433. return this.Context.Insertable(insertObject).InsertColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase)));
  434. }
  435. }
  436. #endregion Insertable
  437. #region Deleteable
  438. public virtual IDeleteable<T> Deleteable<T>() where T : class, new()
  439. {
  440. InitMppingInfo<T>();
  441. var result = this.CreateDeleteable<T>();
  442. return result;
  443. }
  444. public virtual IDeleteable<T> Deleteable<T>(Expression<Func<T, bool>> expression) where T : class, new()
  445. {
  446. InitMppingInfo<T>();
  447. return this.Context.Deleteable<T>().Where(expression);
  448. }
  449. public virtual IDeleteable<T> Deleteable<T>(dynamic primaryKeyValue) where T : class, new()
  450. {
  451. InitMppingInfo<T>();
  452. return this.Context.Deleteable<T>().In(primaryKeyValue);
  453. }
  454. public virtual IDeleteable<T> Deleteable<T>(dynamic[] primaryKeyValues) where T : class, new()
  455. {
  456. InitMppingInfo<T>();
  457. return this.Context.Deleteable<T>().In(primaryKeyValues);
  458. }
  459. public virtual IDeleteable<T> Deleteable<T>(List<dynamic> pkValue) where T : class, new()
  460. {
  461. InitMppingInfo<T>();
  462. return this.Context.Deleteable<T>().In(pkValue);
  463. }
  464. public virtual IDeleteable<T> Deleteable<T>(T deleteObj) where T : class, new()
  465. {
  466. InitMppingInfo<T>();
  467. return this.Context.Deleteable<T>().Where(deleteObj);
  468. }
  469. public virtual IDeleteable<T> Deleteable<T>(List<T> deleteObjs) where T : class, new()
  470. {
  471. InitMppingInfo<T>();
  472. return this.Context.Deleteable<T>().Where(deleteObjs);
  473. }
  474. #endregion Deleteable
  475. #region Updateable
  476. public virtual IUpdateable<T> Updateable<T>(T[] UpdateObjs) where T : class, new()
  477. {
  478. InitMppingInfo<T>();
  479. var result = this.CreateUpdateable(UpdateObjs);
  480. return result;
  481. }
  482. public virtual IUpdateable<T> Updateable<T>(List<T> UpdateObjs) where T : class, new()
  483. {
  484. Check.ArgumentNullException(UpdateObjs, "Updateable.UpdateObjs can't be null");
  485. return Updateable(UpdateObjs.ToArray());
  486. }
  487. public virtual IUpdateable<T> Updateable<T>(T UpdateObj) where T : class, new()
  488. {
  489. return this.Context.Updateable(new T[] { UpdateObj });
  490. }
  491. public virtual IUpdateable<T> Updateable<T>() where T : class, new()
  492. {
  493. return this.Context.Updateable(new T[] { new T() });
  494. }
  495. public virtual IUpdateable<T> Updateable<T>(Dictionary<string, object> columnDictionary) where T : class, new()
  496. {
  497. InitMppingInfo<T>();
  498. Check.Exception(columnDictionary == null || columnDictionary.Count == 0, "Updateable.columnDictionary can't be null");
  499. var updateObject = this.Context.Utilities.DeserializeObject<T>(this.Context.Utilities.SerializeObject(columnDictionary));
  500. var columns = columnDictionary.Select(it => it.Key).ToList();
  501. return this.Context.Updateable(updateObject).UpdateColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase)));
  502. }
  503. public virtual IUpdateable<T> Updateable<T>(dynamic updateDynamicObject) where T : class, new()
  504. {
  505. InitMppingInfo<T>();
  506. if (updateDynamicObject is T)
  507. {
  508. return this.Context.Updateable((T)updateDynamicObject);
  509. }
  510. else
  511. {
  512. var columns = ((object)updateDynamicObject).GetType().GetProperties().Select(it => it.Name).ToList();
  513. Check.Exception(columns.IsNullOrEmpty(), "Updateable.updateDynamicObject can't be null");
  514. T updateObject = this.Context.Utilities.DeserializeObject<T>(this.Context.Utilities.SerializeObject(updateDynamicObject));
  515. return this.Context.Updateable(updateObject).UpdateColumns(it => columns.Any(c => it.Equals(c, StringComparison.CurrentCultureIgnoreCase))); ;
  516. }
  517. }
  518. #endregion Updateable
  519. #region DbFirst
  520. public virtual IDbFirst DbFirst
  521. {
  522. get
  523. {
  524. var dbFirst = InstanceFactory.GetDbFirst(this.Context.CurrentConnectionConfig);
  525. dbFirst.Context = this.Context;
  526. dbFirst.Init();
  527. return dbFirst;
  528. }
  529. }
  530. #endregion DbFirst
  531. #region CodeFirst
  532. public virtual ICodeFirst CodeFirst
  533. {
  534. get
  535. {
  536. var codeFirst = InstanceFactory.GetCodeFirst(this.Context.CurrentConnectionConfig);
  537. codeFirst.Context = this.Context;
  538. return codeFirst;
  539. }
  540. }
  541. #endregion CodeFirst
  542. #region Db Maintenance
  543. public virtual IDbMaintenance DbMaintenance
  544. {
  545. get
  546. {
  547. if (this.Context._DbMaintenance == null)
  548. {
  549. var maintenance = InstanceFactory.GetDbMaintenance(this.Context.CurrentConnectionConfig);
  550. this.Context._DbMaintenance = maintenance;
  551. maintenance.Context = this.Context;
  552. }
  553. return this.Context._DbMaintenance;
  554. }
  555. }
  556. #endregion Db Maintenance
  557. #region Entity Maintenance
  558. [Obsolete("Use SqlSugarClient.EntityMaintenance")]
  559. public virtual EntityMaintenance EntityProvider
  560. {
  561. get { return this.Context.EntityMaintenance; }
  562. set { this.Context.EntityMaintenance = value; }
  563. }
  564. public virtual EntityMaintenance EntityMaintenance
  565. {
  566. get
  567. {
  568. if (this.Context._EntityProvider == null)
  569. {
  570. this.Context._EntityProvider = new EntityMaintenance
  571. {
  572. Context = this.Context
  573. };
  574. }
  575. return this.Context._EntityProvider;
  576. }
  577. set { this.Context._EntityProvider = value; }
  578. }
  579. #endregion Entity Maintenance
  580. #region Gobal Filter
  581. public virtual QueryFilterProvider QueryFilter
  582. {
  583. get
  584. {
  585. if (this.Context._QueryFilterProvider == null)
  586. {
  587. this.Context._QueryFilterProvider = new QueryFilterProvider
  588. {
  589. Context = this.Context
  590. };
  591. }
  592. return this.Context._QueryFilterProvider;
  593. }
  594. set { this.Context._QueryFilterProvider = value; }
  595. }
  596. #endregion Gobal Filter
  597. #region SimpleClient
  598. [Obsolete("Use SqlSugarClient.GetSimpleClient() Or SqlSugarClient.GetSimpleClient<T>() ")]
  599. public virtual SimpleClient<T> GetSimpleClient<T>() where T : class, new()
  600. {
  601. return new SimpleClient<T>(this.Context);
  602. }
  603. public virtual SimpleClient GetSimpleClient()
  604. {
  605. if (this.Context._SimpleClient == null)
  606. this.Context._SimpleClient = new SimpleClient(this.Context);
  607. return this.Context._SimpleClient;
  608. }
  609. #endregion SimpleClient
  610. #region Dispose OR Close
  611. public virtual void Close()
  612. {
  613. if (this.Context.Ado != null)
  614. this.Context.Ado.Close();
  615. }
  616. public virtual void Open()
  617. {
  618. if (this.Context.Ado != null)
  619. this.Context.Ado.Open();
  620. }
  621. public virtual void Dispose()
  622. {
  623. if (this.Context.Ado != null)
  624. this.Context.Ado.Dispose();
  625. }
  626. #endregion Dispose OR Close
  627. }
  628. }