|
@@ -0,0 +1,280 @@
|
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
|
+using HaBookCms.AzureStorage.Options;
|
|
|
|
+using Microsoft.WindowsAzure.Storage;
|
|
|
|
+using Microsoft.WindowsAzure.Storage.Table;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Concurrent;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using HaBookCms.AzureStorage.Interfaces;
|
|
|
|
+using HaBookCms.Jwt.Model;
|
|
|
|
+
|
|
|
|
+namespace HaBookCms.AzureStorage.Implements
|
|
|
|
+{
|
|
|
|
+ public class AzureTableDBRepository : IAzureTableDBRepository
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ public string _connectionString { get; set; }
|
|
|
|
+ public CloudTable table;
|
|
|
|
+ //public AzureTableDBRepository(IOptions<DatabaseOptions> optionsAccessor)
|
|
|
|
+ //{
|
|
|
|
+ // _connectionString = optionsAccessor.Value.ConnectionString;
|
|
|
|
+ //}
|
|
|
|
+
|
|
|
|
+ public AzureTableDBRepository()
|
|
|
|
+ {
|
|
|
|
+ _connectionString = BaseConfigModel.Configuration["AppSettings:Azure:TableStorageConnection"];
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ private async Task InitializeTable<T>()
|
|
|
|
+ {
|
|
|
|
+ if (table == null)
|
|
|
|
+ {
|
|
|
|
+ Type t = typeof(T);
|
|
|
|
+ string StorageConnectionString = _connectionString;
|
|
|
|
+ var storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
|
|
|
|
+ CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
|
|
|
|
+ table = tableClient.GetTableReference(t.Name);
|
|
|
|
+ await table.CreateIfNotExistsAsync();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> FindAll<T>() where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ return await QueryList<T>(exQuery);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async Task<List<T>> QueryList<T>(TableQuery<T> exQuery) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableContinuationToken continuationToken = null;
|
|
|
|
+ List<T> entitys = new List<T>();
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ var result = await table.ExecuteQuerySegmentedAsync(exQuery, continuationToken);
|
|
|
|
+ if (result.Results.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ entitys.AddRange(result.ToList());
|
|
|
|
+ }
|
|
|
|
+ continuationToken = result.ContinuationToken;
|
|
|
|
+ } while (continuationToken != null);
|
|
|
|
+ return entitys;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async Task<T> QueryObject<T>(TableQuery<T> exQuery) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableContinuationToken continuationToken = null;
|
|
|
|
+ T entity = new T();
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ var result = await table.ExecuteQuerySegmentedAsync(exQuery, continuationToken);
|
|
|
|
+ if (result.Results.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ entity = result.ToList().Single();
|
|
|
|
+ }
|
|
|
|
+ continuationToken = result.ContinuationToken;
|
|
|
|
+ } while (continuationToken != null);
|
|
|
|
+ return entity;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<int> Count<T>() where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ TableContinuationToken continuationToken = null;
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ List<T> entitys = new List<T>();
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ var result = await table.ExecuteQuerySegmentedAsync(exQuery, continuationToken);
|
|
|
|
+ if (result.Results.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ entitys.AddRange(result.ToList());
|
|
|
|
+ }
|
|
|
|
+ continuationToken = result.ContinuationToken;
|
|
|
|
+ } while (continuationToken != null);
|
|
|
|
+ return entitys.Count;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<T> FindById<T>(string id) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ if (string.IsNullOrEmpty(id))
|
|
|
|
+ {
|
|
|
|
+ exQuery.Where(TableQuery.GenerateFilterCondition("Id", QueryComparisons.Equal, id));
|
|
|
|
+ }
|
|
|
|
+ return await QueryObject<T>(exQuery);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> FindListByDict<T>(Dictionary<string, object> dict) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ if (null != dict && dict.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ var keys = dict.Keys;
|
|
|
|
+ foreach (string key in keys)
|
|
|
|
+ {
|
|
|
|
+ if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
|
+ {
|
|
|
|
+ exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
|
|
|
|
+ dict[key].ToString()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return await QueryList<T>(exQuery);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> FindListByKey<T>(string key, string value) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(value))
|
|
|
|
+ {
|
|
|
|
+ exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, value));
|
|
|
|
+ }
|
|
|
|
+ return await QueryList<T>(exQuery);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<T> FindOneByDict<T>(IDictionary<string, object> dict) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ if (null != dict && dict.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ var keys = dict.Keys;
|
|
|
|
+ foreach (string key in keys)
|
|
|
|
+ {
|
|
|
|
+ if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
|
+ {
|
|
|
|
+ exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return await QueryObject<T>(exQuery);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<T> FindOneByKey<T>(string key, string value) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(value))
|
|
|
|
+ {
|
|
|
|
+ exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
|
|
|
|
+ value));
|
|
|
|
+ }
|
|
|
|
+ return await QueryObject<T>(exQuery);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> GetEntities<T>(IDictionary<string, object> dict) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ var exQuery = new TableQuery<T>();
|
|
|
|
+ if (null != dict && dict.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ var keys = dict.Keys;
|
|
|
|
+ foreach (string key in keys)
|
|
|
|
+ {
|
|
|
|
+ if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
|
+ {
|
|
|
|
+ exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
|
|
|
|
+ dict[key].ToString()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return await QueryList<T>(exQuery);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> SaveAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ IList<TableResult> result = null;
|
|
|
|
+ Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
|
+ async range =>
|
|
|
|
+ {
|
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
|
+ for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
|
+ batchOperation.Insert(entitys[i]);
|
|
|
|
+ result = await table.ExecuteBatchAsync(batchOperation);
|
|
|
|
+ });
|
|
|
|
+ return (List<T>)result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> UpdateAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ IList<TableResult> result = null;
|
|
|
|
+ Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
|
+ async range =>
|
|
|
|
+ {
|
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
|
+ for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
|
+ batchOperation.Replace(entitys[i]);
|
|
|
|
+ result = await table.ExecuteBatchAsync(batchOperation);
|
|
|
|
+ });
|
|
|
|
+ return (List<T>)result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> DeleteAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ IList<TableResult> result = null;
|
|
|
|
+ Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
|
+ async range =>
|
|
|
|
+ {
|
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
|
+ for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
|
+ batchOperation.Delete(entitys[i]);
|
|
|
|
+ result = await table.ExecuteBatchAsync(batchOperation);
|
|
|
|
+ });
|
|
|
|
+ return (List<T>)result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<T> Save<T>(TableEntity entity) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ TableOperation operation = TableOperation.Insert(entity);
|
|
|
|
+ TableResult result = await table.ExecuteAsync(operation);
|
|
|
|
+ return (T)result.Result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<T> Update<T>(TableEntity entity) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ TableOperation operation = TableOperation.Replace(entity);
|
|
|
|
+ TableResult result = await table.ExecuteAsync(operation);
|
|
|
|
+ return (T)result.Result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<T> Delete<T>(TableEntity entity) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ await InitializeTable<T>();
|
|
|
|
+ TableOperation operation = TableOperation.Delete(entity);
|
|
|
|
+ TableResult result = await table.ExecuteAsync(operation);
|
|
|
|
+ return (T)result.Result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> FindListByDictAndLike<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ throw new NotImplementedException();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> FindListByDictAndLikeAndNotEQ<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict, Dictionary<string, object> notEQDict) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ throw new NotImplementedException();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<T>> FindListByDictAndLikeAndStartWith<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict, Dictionary<string, object> startDict) where T : TableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ throw new NotImplementedException();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|