SqlSugarAccessory.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public partial class SqlSugarProvider
  11. {
  12. #region Properties
  13. public SqlSugarProvider Context
  14. {
  15. get
  16. {
  17. _Context = this;
  18. return _Context;
  19. }
  20. set
  21. {
  22. _Context = value;
  23. }
  24. }
  25. public SqlSugarClient Root { get; set; }
  26. public ConnectionConfig CurrentConnectionConfig { get; set; }
  27. public Dictionary<string, object> TempItems { get { if (_TempItems == null) { _TempItems = new Dictionary<string, object>(); } return _TempItems; } set { _TempItems = value; } }
  28. public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } }
  29. public Guid ContextID { get; set; }
  30. public MappingTableList MappingTables { get; set; }
  31. public MappingColumnList MappingColumns { get; set; }
  32. public IgnoreColumnList IgnoreColumns { get; set; }
  33. public IgnoreColumnList IgnoreInsertColumns { get; set; }
  34. public SugarActionType SugarActionType { get; set; } = SugarActionType.UnKnown;
  35. public ConfigQuery ConfigQuery {
  36. get
  37. {
  38. if (_SqlConfigTable==null)
  39. {
  40. _SqlConfigTable = new ConfigQuery() { Context = this.Context };
  41. }
  42. return _SqlConfigTable;
  43. }
  44. set
  45. {
  46. _SqlConfigTable = value;
  47. }
  48. }
  49. #endregion
  50. #region Fields
  51. public Dictionary<string, object> _TempItems;
  52. public QueueList _Queues;
  53. protected ISqlBuilder _SqlBuilder;
  54. protected SqlSugarProvider _Context { get; set; }
  55. protected EntityMaintenance _EntityProvider;
  56. protected IAdo _Ado;
  57. protected ILambdaExpressions _LambdaExpressions;
  58. protected IContextMethods _RewritableMethods;
  59. protected IDbMaintenance _DbMaintenance;
  60. protected QueryFilterProvider _QueryFilterProvider;
  61. protected ConfigQuery _SqlConfigTable;
  62. //protected SimpleClient _SimpleClient;
  63. protected IAdo ContextAdo
  64. {
  65. get
  66. {
  67. return this._Ado;
  68. }
  69. set
  70. {
  71. this._Ado = value;
  72. }
  73. }
  74. protected IContextMethods ContextRewritableMethods
  75. {
  76. get
  77. {
  78. return this._RewritableMethods;
  79. }
  80. set
  81. {
  82. this._RewritableMethods = value;
  83. }
  84. }
  85. #endregion
  86. #region Init mappingInfo
  87. protected void InitMappingInfo<T, T2>()
  88. {
  89. InitMappingInfo<T>();
  90. InitMappingInfo<T2>();
  91. }
  92. protected void InitMappingInfo<T, T2, T3>()
  93. {
  94. InitMappingInfo<T, T2>();
  95. InitMappingInfo<T3>();
  96. }
  97. protected void InitMappingInfo<T, T2, T3, T4>()
  98. {
  99. InitMappingInfo<T, T2, T3>();
  100. InitMappingInfo<T4>();
  101. }
  102. protected void InitMappingInfo<T, T2, T3, T4, T5>()
  103. {
  104. InitMappingInfo<T, T2, T3, T4>();
  105. InitMappingInfo<T5>();
  106. }
  107. protected void InitMappingInfo<T, T2, T3, T4, T5, T6>()
  108. {
  109. InitMappingInfo<T, T2, T3, T4, T5>();
  110. InitMappingInfo<T6>();
  111. }
  112. protected void InitMappingInfo<T, T2, T3, T4, T5, T6, T7>()
  113. {
  114. InitMappingInfo<T, T2, T3, T4, T5, T6>();
  115. InitMappingInfo<T7>();
  116. }
  117. protected void InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8>()
  118. {
  119. InitMappingInfo<T, T2, T3, T4, T5, T6, T7>();
  120. InitMappingInfo<T8>();
  121. }
  122. #region 9-12
  123. protected void InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9>()
  124. {
  125. InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8>();
  126. InitMappingInfo<T9>();
  127. }
  128. protected void InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>()
  129. {
  130. InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9>();
  131. InitMappingInfo<T10>();
  132. }
  133. protected void InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>()
  134. {
  135. InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>();
  136. InitMappingInfo<T11>();
  137. }
  138. protected void InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>()
  139. {
  140. InitMappingInfo<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>();
  141. InitMappingInfo<T12>();
  142. }
  143. #endregion
  144. public void InitMappingInfo<T>()
  145. {
  146. InitMappingInfo(typeof(T));
  147. }
  148. public void InitMappingInfo(Type type)
  149. {
  150. string cacheKey = "Context.InitAttributeMappingTables" + type.FullName+this.Context?.CurrentConnectionConfig?.ConfigId;
  151. var entityInfo = this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate<EntityInfo>(cacheKey,
  152. () =>
  153. {
  154. var result = this.Context.EntityMaintenance.GetEntityInfoWithAttr(type);
  155. return result;
  156. });
  157. //var copyObj = CopyEntityInfo(entityInfo);
  158. InitMappingInfo(entityInfo);
  159. }
  160. public void InitMappingInfoNoCache(Type type)
  161. {
  162. var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(type);
  163. InitMappingInfo(entityInfo);
  164. }
  165. //private EntityInfo CopyEntityInfo(EntityInfo entityInfo)
  166. //{
  167. // EntityInfo result = new EntityInfo()
  168. // {
  169. // DbTableName = entityInfo.DbTableName,
  170. // EntityName = entityInfo.EntityName,
  171. // Type = entityInfo.Type
  172. // };
  173. // List<EntityColumnInfo> columns = new List<EntityColumnInfo>();
  174. // if (entityInfo.Columns.HasValue())
  175. // {
  176. // foreach (var item in entityInfo.Columns)
  177. // {
  178. // EntityColumnInfo column = new EntityColumnInfo()
  179. // {
  180. // ColumnDescription = item.ColumnDescription,
  181. // DataType = item.DataType,
  182. // DbColumnName = item.DbColumnName,
  183. // DbTableName = item.DbTableName,
  184. // DecimalDigits = item.DecimalDigits,
  185. // DefaultValue = item.DefaultValue,
  186. // EntityName = item.EntityName,
  187. // IsIdentity = item.IsIdentity,
  188. // IsIgnore = item.IsIgnore,
  189. // IsNullable = item.IsNullable,
  190. // IsOnlyIgnoreInsert = item.IsOnlyIgnoreInsert,
  191. // IsPrimarykey = item.IsPrimarykey,
  192. // Length = item.Length,
  193. // OldDbColumnName = item.OldDbColumnName,
  194. // OracleSequenceName = item.OracleSequenceName,
  195. // PropertyInfo = item.PropertyInfo,
  196. // PropertyName = item.PropertyName,
  197. // IsArray=item.IsArray,
  198. // IsJson=item.IsJson
  199. // };
  200. // columns.Add(item);
  201. // }
  202. // }
  203. // result.Columns = columns;
  204. // return result;
  205. //}
  206. private void InitMappingInfo(EntityInfo entityInfo)
  207. {
  208. if (this.MappingTables == null)
  209. this.MappingTables = new MappingTableList();
  210. if (this.MappingColumns == null)
  211. this.MappingColumns = new MappingColumnList();
  212. if (this.IgnoreColumns == null)
  213. this.IgnoreColumns = new IgnoreColumnList();
  214. if (this.IgnoreInsertColumns == null)
  215. this.IgnoreInsertColumns = new IgnoreColumnList();
  216. if (!this.MappingTables.Any(it => it.EntityName == entityInfo.EntityName))
  217. {
  218. if (entityInfo.DbTableName != entityInfo.EntityName && entityInfo.DbTableName.HasValue())
  219. {
  220. this.MappingTables.Add(entityInfo.EntityName, entityInfo.DbTableName);
  221. }
  222. }
  223. if (entityInfo.Columns.Any(it => it.EntityName == entityInfo.EntityName))
  224. {
  225. var mappingColumnInfos = this.MappingColumns.Where(it => it.EntityName == entityInfo.EntityName);
  226. foreach (var item in entityInfo.Columns.Where(it => it.IsIgnore == false))
  227. {
  228. if (!mappingColumnInfos.Any(it => it.PropertyName == item.PropertyName))
  229. if (item.PropertyName != item.DbColumnName && item.DbColumnName.HasValue())
  230. this.MappingColumns.Add(item.PropertyName, item.DbColumnName, item.EntityName);
  231. }
  232. var ignoreInfos = this.IgnoreColumns.Where(it => it.EntityName == entityInfo.EntityName);
  233. foreach (var item in entityInfo.Columns.Where(it => it.IsIgnore))
  234. {
  235. if (!ignoreInfos.Any(it => it.PropertyName == item.PropertyName))
  236. this.IgnoreColumns.Add(item.PropertyName, item.EntityName);
  237. }
  238. var ignoreInsertInfos = this.IgnoreInsertColumns.Where(it => it.EntityName == entityInfo.EntityName);
  239. foreach (var item in entityInfo.Columns.Where(it => it.IsOnlyIgnoreInsert))
  240. {
  241. if (!ignoreInsertInfos.Any(it => it.PropertyName == item.PropertyName))
  242. this.IgnoreInsertColumns.Add(item.PropertyName, item.EntityName);
  243. }
  244. }
  245. }
  246. #endregion
  247. #region Create Instance
  248. protected ISugarQueryable<T> CreateQueryable<T>()
  249. {
  250. ISugarQueryable<T> result = InstanceFactory.GetQueryable<T>(this.CurrentConnectionConfig);
  251. return CreateQueryable(result);
  252. }
  253. protected ISugarQueryable<T> CreateQueryable<T>(ISugarQueryable<T> result)
  254. {
  255. this.SugarActionType = SugarActionType.Query;
  256. Check.Exception(typeof(T).IsClass() == false || typeof(T).GetConstructors().Length == 0, "Queryable<{0}> Error ,{0} is invalid , need is a class,and can new().", typeof(T).Name);
  257. var sqlBuilder = InstanceFactory.GetSqlbuilder(CurrentConnectionConfig);
  258. result.Context = this.Context;
  259. result.SqlBuilder = sqlBuilder;
  260. result.SqlBuilder.QueryBuilder = InstanceFactory.GetQueryBuilder(CurrentConnectionConfig);
  261. result.SqlBuilder.QueryBuilder.Builder = sqlBuilder;
  262. result.SqlBuilder.Context = result.SqlBuilder.QueryBuilder.Context = this;
  263. result.SqlBuilder.QueryBuilder.EntityType = typeof(T);
  264. result.SqlBuilder.QueryBuilder.EntityName = typeof(T).Name;
  265. result.SqlBuilder.QueryBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(CurrentConnectionConfig);
  266. if (StaticConfig.CompleteQueryableFunc != null)
  267. {
  268. StaticConfig.CompleteQueryableFunc(result);
  269. }
  270. return result;
  271. }
  272. protected InsertableProvider<T> CreateInsertable<T>(T[] insertObjs) where T : class, new()
  273. {
  274. this.SugarActionType = SugarActionType.Insert;
  275. var result = InstanceFactory.GetInsertableProvider<T>(this.CurrentConnectionConfig);
  276. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.CurrentConnectionConfig); ;
  277. result.Context = this;
  278. result.EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>();
  279. result.SqlBuilder = sqlBuilder;
  280. result.InsertObjs = insertObjs;
  281. sqlBuilder.InsertBuilder = result.InsertBuilder = InstanceFactory.GetInsertBuilder(this.CurrentConnectionConfig);
  282. sqlBuilder.InsertBuilder.Builder = sqlBuilder;
  283. sqlBuilder.InsertBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
  284. sqlBuilder.Context = result.SqlBuilder.InsertBuilder.Context = this;
  285. result.Init();
  286. if (StaticConfig.CompleteInsertableFunc != null)
  287. {
  288. StaticConfig.CompleteInsertableFunc(result);
  289. }
  290. return result;
  291. }
  292. protected DeleteableProvider<T> CreateDeleteable<T>() where T : class, new()
  293. {
  294. this.SugarActionType = SugarActionType.Delete;
  295. var result = InstanceFactory.GetDeleteableProvider<T>(this.CurrentConnectionConfig);
  296. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.CurrentConnectionConfig); ;
  297. result.Context = this;
  298. result.SqlBuilder = sqlBuilder;
  299. sqlBuilder.DeleteBuilder = result.DeleteBuilder = InstanceFactory.GetDeleteBuilder(this.CurrentConnectionConfig);
  300. sqlBuilder.DeleteBuilder.Builder = sqlBuilder;
  301. sqlBuilder.DeleteBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
  302. sqlBuilder.Context = result.SqlBuilder.DeleteBuilder.Context = this;
  303. if (StaticConfig.CompleteDeleteableFunc != null)
  304. {
  305. StaticConfig.CompleteDeleteableFunc(result);
  306. }
  307. return result;
  308. }
  309. protected UpdateableProvider<T> CreateUpdateable<T>(T[] UpdateObjs) where T : class, new()
  310. {
  311. this.SugarActionType = SugarActionType.Update;
  312. var result = InstanceFactory.GetUpdateableProvider<T>(this.CurrentConnectionConfig);
  313. var sqlBuilder = InstanceFactory.GetSqlbuilder(this.CurrentConnectionConfig); ;
  314. result.Context = this;
  315. result.EntityInfo = this.Context.EntityMaintenance.GetEntityInfo<T>();
  316. result.SqlBuilder = sqlBuilder;
  317. result.UpdateObjs = UpdateObjs;
  318. sqlBuilder.UpdateBuilder = result.UpdateBuilder = InstanceFactory.GetUpdateBuilder(this.CurrentConnectionConfig);
  319. sqlBuilder.UpdateBuilder.Builder = sqlBuilder;
  320. sqlBuilder.UpdateBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(this.CurrentConnectionConfig);
  321. sqlBuilder.Context = result.SqlBuilder.UpdateBuilder.Context = this;
  322. result.Init();
  323. if (StaticConfig.CompleteUpdateableFunc != null)
  324. {
  325. StaticConfig.CompleteUpdateableFunc(result);
  326. }
  327. return result;
  328. }
  329. protected void CreateQueryJoin<T>(Expression joinExpression, Type[] types, ISugarQueryable<T> queryable)
  330. {
  331. this.SugarActionType = SugarActionType.Query;
  332. this.CreateQueryable<T>(queryable);
  333. string shortName = string.Empty;
  334. List<SugarParameter> paramters = new List<SugarParameter>();
  335. queryable.SqlBuilder.QueryBuilder.JoinQueryInfos = this.GetJoinInfos(queryable.SqlBuilder, joinExpression, ref paramters, ref shortName, types);
  336. if (queryable.SqlBuilder.QueryBuilder.JoinQueryInfos.Any())
  337. {
  338. queryable.SqlBuilder.QueryBuilder.JoinQueryInfos.Last().EntityType = types.Last();
  339. }
  340. queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
  341. queryable.SqlBuilder.QueryBuilder.JoinExpression = joinExpression;
  342. if (paramters != null)
  343. {
  344. queryable.SqlBuilder.QueryBuilder.Parameters.AddRange(paramters);
  345. }
  346. UtilMethods.AddDiscrimator(typeof(T), queryable, queryable.QueryBuilder.TableShortName + ".");
  347. }
  348. protected void CreateEasyQueryJoin<T>(Expression joinExpression, Type[] types, ISugarQueryable<T> queryable)
  349. {
  350. this.SugarActionType = SugarActionType.Query;
  351. this.CreateQueryable<T>(queryable);
  352. string shortName = string.Empty;
  353. queryable.SqlBuilder.QueryBuilder.EasyJoinInfos = this.GetEasyJoinInfo(joinExpression, ref shortName, queryable.SqlBuilder, types);
  354. queryable.SqlBuilder.QueryBuilder.TableShortName = shortName;
  355. queryable.SqlBuilder.QueryBuilder.JoinExpression = joinExpression;
  356. var isNoPgAuto = this.Context.CurrentConnectionConfig.MoreSettings?.PgSqlIsAutoToLower == false;
  357. if (isNoPgAuto)
  358. queryable.SqlBuilder.QueryBuilder.TableShortName = queryable.SqlBuilder.GetTranslationColumnName(shortName);
  359. }
  360. #endregion
  361. #region Private methods
  362. private void _ThenMapper<T>(IEnumerable<T> list, Action<T> action)
  363. {
  364. MapperContext<T> result = new MapperContext<T>();
  365. result.context = this.Context;
  366. if (result.context.TempItems == null)
  367. {
  368. result.context.TempItems = new Dictionary<string, object>();
  369. }
  370. var key = "Queryable_To_Context";
  371. result.context.TempItems.Add(key, result);
  372. result.list = list.ToList();
  373. foreach (var item in list)
  374. {
  375. action.Invoke(item);
  376. }
  377. result.context.TempItems.Remove(key);
  378. }
  379. private async Task _ThenMapperAsync<T>(IEnumerable<T> list, Func<T, Task> action)
  380. {
  381. MapperContext<T> result = new MapperContext<T>();
  382. result.context = this.Context;
  383. if (result.context.TempItems == null)
  384. {
  385. result.context.TempItems = new Dictionary<string, object>();
  386. }
  387. var key = "Queryable_To_Context";
  388. result.context.TempItems.Add(key, result);
  389. result.list = list.ToList();
  390. foreach (var item in list)
  391. {
  392. await action.Invoke(item);
  393. }
  394. result.context.TempItems.Remove(key);
  395. }
  396. internal string GetN()
  397. {
  398. var N = "N";
  399. if (_Context.CurrentConnectionConfig.MoreSettings != null && _Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar)
  400. {
  401. N = "";
  402. }
  403. return N;
  404. }
  405. public bool IsVarchar()
  406. {
  407. if (_Context.CurrentConnectionConfig.MoreSettings != null && _Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar)
  408. {
  409. return true;
  410. }
  411. return false;
  412. }
  413. private void CheckDbDependency(ConnectionConfig config)
  414. {
  415. switch (config.DbType)
  416. {
  417. case DbType.MySql:
  418. DependencyManagement.TryMySqlData();
  419. break;
  420. case DbType.SqlServer:
  421. break;
  422. case DbType.Sqlite:
  423. DependencyManagement.TrySqlite();
  424. break;
  425. case DbType.Oracle:
  426. DependencyManagement.TryOracle();
  427. break;
  428. case DbType.PostgreSQL:
  429. DependencyManagement.TryPostgreSQL();
  430. break;
  431. case DbType.OpenGauss:
  432. config.DbType = DbType.PostgreSQL;
  433. break;
  434. case DbType.HG:
  435. InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.HGCore";
  436. break;
  437. case DbType.Kdbndp:
  438. DependencyManagement.TryKdbndb();
  439. break;
  440. case DbType.Dm:
  441. DependencyManagement.TryDm();
  442. break;
  443. case DbType.Oscar:
  444. DependencyManagement.TryOscar();
  445. break;
  446. case DbType.MySqlConnector:
  447. InstanceFactory.CustomDllName = SugarCompatible.IsFramework?"SqlSugar.MySqlConnector": "SqlSugar.MySqlConnectorCore";
  448. if (SugarCompatible.IsFramework.ObjToBool() == false)
  449. {
  450. config.DbType= DbType.MySql;
  451. InstanceFactory.CustomDllName = null;
  452. }
  453. break;
  454. case DbType.Access:
  455. InstanceFactory.CustomDllName = SugarCompatible.IsFramework?"SqlSugar.Access": "SqlSugar.AccessCore";
  456. break;
  457. case DbType.Custom:
  458. Check.Exception(InstanceFactory.CustomDbName.IsNullOrEmpty() , "DbType.Custom: InstanceFactory.CustomDbName is not null ");
  459. Check.Exception(InstanceFactory.CustomNamespace.IsNullOrEmpty(), "DbType.Custom: InstanceFactory.CustomNamespace is not null ");
  460. Check.Exception(InstanceFactory.CustomDllName.IsNullOrEmpty(), "DbType.Custom: InstanceFactory.CustomDllName is not null ");
  461. break;
  462. case DbType.QuestDB:
  463. DependencyManagement.TryPostgreSQL();
  464. break;
  465. case DbType.ClickHouse:
  466. Check.Exception(SugarCompatible.IsFramework, "ClickHouse only support .net core");
  467. InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? "SqlSugar.ClickHouse" : "SqlSugar.ClickHouseCore";
  468. break;
  469. case DbType.GBase:
  470. Check.Exception(SugarCompatible.IsFramework, "GBase only support .net core");
  471. InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? "SqlSugar.GBase" : "SqlSugar.GBaseCore";
  472. break;
  473. case DbType.Odbc:
  474. InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? "SqlSugar.Odbc" : "SqlSugar.OdbcCore";
  475. break;
  476. case DbType.OceanBaseForOracle:
  477. Check.Exception(SugarCompatible.IsFramework, "OceanBaseForOracle only support .net core");
  478. InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? "SqlSugar.OceanBaseForOracle" : "SqlSugar.OceanBaseForOracleCore";
  479. break;
  480. case DbType.GaussDB:
  481. config.DbType = DbType.PostgreSQL;
  482. break;
  483. case DbType.Vastbase:
  484. config.DbType = DbType.PostgreSQL;
  485. if (this.TempItems == null)
  486. {
  487. this.TempItems = new Dictionary<string, object>();
  488. }
  489. this.TempItems.Add("DbType.Vastbase", "DbType.Vastbase");
  490. break;
  491. case DbType.OceanBase:
  492. config.DbType = DbType.MySql;
  493. break;
  494. case DbType.Tidb:
  495. config.DbType = DbType.MySql;
  496. break;
  497. case DbType.TDengine:
  498. Check.Exception(SugarCompatible.IsFramework, "GBase only support .net core");
  499. InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? "SqlSugar.TDengine" : "SqlSugar.TDengineCore";
  500. break;
  501. default:
  502. throw new Exception("ConnectionConfig.DbType is null");
  503. }
  504. }
  505. protected List<JoinQueryInfo> GetJoinInfos(ISqlBuilder sqlBuilder, Expression joinExpression, ref List<SugarParameter> parameters, ref string shortName, params Type[] entityTypeArray)
  506. {
  507. List<JoinQueryInfo> result = new List<JoinQueryInfo>();
  508. var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList();
  509. ILambdaExpressions expressionContext = sqlBuilder.QueryBuilder.LambdaExpressions;
  510. expressionContext.MappingColumns = this.MappingColumns;
  511. expressionContext.MappingTables = this.MappingTables;
  512. expressionContext.IsSingle = false;
  513. expressionContext.SugarContext = new ExpressionOutParameter() { Context=this.Context };
  514. if (this.Context.CurrentConnectionConfig.MoreSettings != null)
  515. {
  516. expressionContext.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
  517. }
  518. else
  519. {
  520. expressionContext.PgSqlIsAutoToLower = true;
  521. }
  522. if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null)
  523. expressionContext.SqlFuncServices = this.Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
  524. expressionContext.Resolve(joinExpression, ResolveExpressType.Join);
  525. int i = 0;
  526. var joinArray = MergeJoinArray(expressionContext.Result.GetResultArray());
  527. if (joinArray == null) return null;
  528. parameters = expressionContext.Parameters;
  529. foreach (var entityType in entityTypeArray)
  530. {
  531. var isFirst = i == 0; ++i;
  532. JoinQueryInfo joinInfo = new JoinQueryInfo();
  533. var hasMappingTable = expressionContext.MappingTables.HasValue();
  534. MappingTable mappingInfo = null;
  535. if (hasMappingTable)
  536. {
  537. mappingInfo = expressionContext.MappingTables.FirstOrDefault(it => it.EntityName.Equals(entityType.Name, StringComparison.CurrentCultureIgnoreCase));
  538. joinInfo.TableName = mappingInfo != null ? mappingInfo.DbTableName : entityType.Name;
  539. }
  540. else
  541. {
  542. joinInfo.TableName = entityType.Name;
  543. }
  544. var isNoPgAuto = this.Context.CurrentConnectionConfig.MoreSettings?.PgSqlIsAutoToLower == false;
  545. if (isFirst)
  546. {
  547. var firstItem = lambdaParameters.First();
  548. lambdaParameters.Remove(firstItem);
  549. shortName = firstItem.Name;
  550. if (isNoPgAuto)
  551. shortName = sqlBuilder.GetTranslationColumnName(shortName);
  552. }
  553. var joinString = joinArray[i * 2 - 2];
  554. joinInfo.ShortName = lambdaParameters[i - 1].Name;
  555. joinInfo.JoinType = (JoinType)Enum.Parse(typeof(JoinType), joinString);
  556. joinInfo.JoinWhere = joinArray[i * 2 - 1];
  557. joinInfo.JoinIndex = i;
  558. joinInfo.EntityType = entityType;
  559. if (isNoPgAuto)
  560. joinInfo.ShortName = sqlBuilder.GetTranslationColumnName(joinInfo.ShortName);
  561. result.Add((joinInfo));
  562. }
  563. expressionContext.Clear();
  564. return result;
  565. }
  566. private string[] MergeJoinArray(string[] joinArray)
  567. {
  568. List<string> result = new List<string>();
  569. string joinValue = null;
  570. int i = 0;
  571. if (joinArray == null) return null;
  572. foreach (var item in joinArray)
  573. {
  574. ++i;
  575. var isLast = joinArray.Length == i;
  576. var isJoinType = item.IsIn(JoinType.Full.ToString(),JoinType.Inner.ToString(), JoinType.Left.ToString(), JoinType.Right.ToString(),JoinType.Cross.ToString());
  577. if (isJoinType)
  578. {
  579. if (joinValue != null)
  580. result.Add(joinValue);
  581. joinValue = null;
  582. result.Add(item);
  583. }
  584. else
  585. {
  586. isJoinType = false;
  587. joinValue += joinValue == null ? item : ("," + item);
  588. }
  589. if (isLast)
  590. {
  591. result.Add(joinValue);
  592. }
  593. }
  594. return result.ToArray(); ;
  595. }
  596. protected Dictionary<string, string> GetEasyJoinInfo(Expression joinExpression, ref string shortName, ISqlBuilder builder, params Type[] entityTypeArray)
  597. {
  598. Dictionary<string, string> result = new Dictionary<string, string>();
  599. var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList();
  600. shortName = lambdaParameters.First().Name;
  601. var isNoPgAuto = this.Context.CurrentConnectionConfig.MoreSettings?.PgSqlIsAutoToLower == false;
  602. var index = 1;
  603. foreach (var item in entityTypeArray)
  604. {
  605. if (isNoPgAuto)
  606. {
  607. result.Add(UtilConstants.Space +builder.GetTranslationColumnName(lambdaParameters[index].Name), item.Name);
  608. }
  609. else
  610. {
  611. result.Add(UtilConstants.Space + lambdaParameters[index].Name, item.Name);
  612. }
  613. ++index;
  614. }
  615. return result;
  616. }
  617. #endregion
  618. }
  619. }