Browse Source

1、cosmos查询新增 用属性投影方式查询,减少数据返回量

李思淳 5 năm trước cách đây
mục cha
commit
47788c0410

+ 16 - 8
TEAMModelOS.SDK/Module/AzureCosmosDBV3/AzureCosmosDBV3Repository.cs

@@ -14,6 +14,7 @@ using System.Text.Json;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using TEAMModelOS.SDK.Context.Attributes.Azure;
 using TEAMModelOS.SDK.Context.Attributes.Azure;
 using TEAMModelOS.SDK.Context.Exception;
 using TEAMModelOS.SDK.Context.Exception;
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.SDK.Helper.Common.ReflectorExtensions;
 using TEAMModelOS.SDK.Helper.Common.ReflectorExtensions;
 using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
 using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
 
 
@@ -331,10 +332,14 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         //    return response.Resource;
         //    return response.Resource;
         //}
         //}
 
 
-        public async Task<List<T>> FindAll<T>() where T : ID
+        public async Task<List<T>> FindAll<T>(List<string> propertys = null) where T : ID
         {
         {
             Container container = await InitializeCollection<T>();
             Container container = await InitializeCollection<T>();
-            return await ResultsFromFeedIterator(container.GetItemQueryIterator<T>());
+            StringBuilder sql;
+            sql = SQLHelperParametric.GetSQLSelect(propertys);
+            CosmosDbQuery cosmosDbQuery = new CosmosDbQuery {QueryText = sql.ToString() };
+            FeedIterator<T> query = container.GetItemQueryIterator<T>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition);
+            return await ResultsFromFeedIterator(query);
 
 
         }
         }
         private async Task<List<T>> ResultsFromFeedIterator<T>(FeedIterator<T> query, int? maxItemCount = null)
         private async Task<List<T>> ResultsFromFeedIterator<T>(FeedIterator<T> query, int? maxItemCount = null)
@@ -375,7 +380,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
 
 
             return results;
             return results;
         }
         }
-        public async Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null)
+        public async Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null, List<string> propertys = null)
         {
         {
             if (DocumentCollectionDict.TryGetValue(CollectionName, out Container container))
             if (DocumentCollectionDict.TryGetValue(CollectionName, out Container container))
             {
             {
@@ -386,7 +391,8 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                 //    QueryText = sql.ToString()
                 //    QueryText = sql.ToString()
 
 
                 //};
                 //};
-                StringBuilder sql = new StringBuilder("select value(c) from c");
+                StringBuilder sql;
+                sql = SQLHelperParametric.GetSQLSelect(propertys);
                 CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
                 CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
                 QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(itemsPerPage, maxItemCount));
                 QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(itemsPerPage, maxItemCount));
                 FeedIterator<dynamic> query = container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
                 FeedIterator<dynamic> query = container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
@@ -422,18 +428,20 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             }
             }
         }
         }
 
 
-        public async Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null) where T : ID
+        public async Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null, List<string> propertys = null) where T : ID
         {
         {
-            return await FindByDict<T>(dict, itemsPerPage, maxItemCount, partitionKey);
+            return await FindByDict<T>(dict, itemsPerPage, maxItemCount, partitionKey, propertys);
         }
         }
-        public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null) where T : ID
+        public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null,List<string> propertys = null) where T : ID
         {
         {
+            StringBuilder sql;
+            sql = SQLHelperParametric.GetSQLSelect(propertys);
 
 
-            StringBuilder sql = new StringBuilder("select value(c) from c");
             CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
             CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
             QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(itemsPerPage, maxItemCount));
             QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(itemsPerPage, maxItemCount));
             return await ResultsFromQueryAndOptions<T>(cosmosDbQuery, queryRequestOptions);
             return await ResultsFromQueryAndOptions<T>(cosmosDbQuery, queryRequestOptions);
         }
         }
+        
 
 
         private async Task<List<T>> ResultsFromQueryAndOptions<T>(CosmosDbQuery cosmosDbQuery, QueryRequestOptions queryOptions, int? maxItemCount = null)
         private async Task<List<T>> ResultsFromQueryAndOptions<T>(CosmosDbQuery cosmosDbQuery, QueryRequestOptions queryOptions, int? maxItemCount = null)
         {
         {

+ 4 - 4
TEAMModelOS.SDK/Module/AzureCosmosDBV3/IAzureCosmosDBV3Repository.cs

@@ -38,7 +38,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         Task<T> FindById<T>(string id) where T : ID;
         Task<T> FindById<T>(string id) where T : ID;
         Task<T> FindByIdPk<T>(string id, string pk) where T : ID;
         Task<T> FindByIdPk<T>(string id, string pk) where T : ID;
         // Task<string> ReplaceObject<T>(T entity, string key, string partitionKey) where T : ID;
         // Task<string> ReplaceObject<T>(T entity, string key, string partitionKey) where T : ID;
-        Task<List<T>> FindAll<T>() where T : ID;
+        Task<List<T>> FindAll<T>(List<string> propertys = null) where T : ID;
         /// <summary>
         /// <summary>
         /// QueryText = @"SELECT *
         /// QueryText = @"SELECT *
         //            FROM    c 
         //            FROM    c 
@@ -73,9 +73,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         /// <param name="query"></param>
         /// <param name="query"></param>
         /// <returns></returns>
         /// <returns></returns>
         Task<List<T>> FindLinq<T>(Expression<Func<T, bool>> query = null, Expression<Func<T, object>> order = null, bool isDesc = false, int itemsPerPage = -1, int? maxItemCount = null) where T : ID;
         Task<List<T>> FindLinq<T>(Expression<Func<T, bool>> query = null, Expression<Func<T, object>> order = null, bool isDesc = false, int itemsPerPage = -1, int? maxItemCount = null) where T : ID;
-        Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null) where T : ID;
-        Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null) where T : ID;
-        Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null);
+        Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null, List<string> propertys = null) where T : ID;
+        Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null, List<string> propertys = null) where T : ID;
+        Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null, List<string> propertys = null);
         Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null);
         Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null);
         Task InitializeDatabase();
         Task InitializeDatabase();
 
 

+ 32 - 0
TEAMModelOS.SDK/Module/AzureCosmosDBV3/SQLHelperParametric.cs

@@ -7,6 +7,7 @@ using System.Text;
 using System.Text.Json;
 using System.Text.Json;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
 using TEAMModelOS.SDK.Context.Exception;
 using TEAMModelOS.SDK.Context.Exception;
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 
 
 namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
 namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
 {
 {
@@ -31,6 +32,37 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             sql.Replace(".having ", "['having'] ");
             sql.Replace(".having ", "['having'] ");
         }
         }
 
 
+        public static StringBuilder GetSQLSelect(List<string> propertys)
+        {
+            StringBuilder sql;
+            if (propertys.IsNotEmpty())
+            {
+                string tmpSQL = " SELECT ";
+                int i = 0;
+                foreach (string item in propertys)
+                {
+                    if (i == 0)
+                    {
+                        tmpSQL += " c." + item + " ";
+                    }
+                    else
+                    {
+                        tmpSQL += " ,c." + item + " ";
+                    }
+                    i++;
+                }
+                tmpSQL += "from c";
+
+                sql = new StringBuilder(tmpSQL);
+            }
+            else
+            {
+                sql = new StringBuilder("select value(c) from c");
+            }
+
+            return sql;
+        }
+
         public static CosmosDbQuery GetSQL(Dictionary<string, object> dict, StringBuilder sql)
         public static CosmosDbQuery GetSQL(Dictionary<string, object> dict, StringBuilder sql)
         {
         {