123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using HaBookCms.AzureStorage.AzureTable.Interfaces;
- using HaBookCms.AzureStorage.ServiceExtension;
- using Microsoft.WindowsAzure.Storage.Table;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace HaBookCms.AzureStorage.AzureTable.Implements
- {
- public class AzureTableDBRepository : IAzureTableDBRepository
- {
- private readonly CloudTableClient tableClient;
- private CloudTable table { get; set; }
- public AzureTableDBRepository(AzureStorageOptions options)
- {
- tableClient = TableClientSingleton.getInstance(options.ConnectionString).GetTableClient();
- }
- public AzureTableDBRepository()
- {
- }
- private async Task InitializeTable<T>()
- {
- Type t = typeof(T);
- if (table == null || !table.Name.Equals(t.Name))
- {
- 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();
- }
- }
- }
|