|
@@ -108,7 +108,27 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
UriFactory.CreateDatabaseUri(Database), collectionDefinition, new RequestOptions { OfferThroughput = CollectionThroughput }
|
|
UriFactory.CreateDatabaseUri(Database), collectionDefinition, new RequestOptions { OfferThroughput = CollectionThroughput }
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
+ return CosmosCollection;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ private async Task<DocumentCollection> InitializeCollection(string CollectionName, string PartitionKey)
|
|
|
|
+ {
|
|
|
|
+ if (CosmosCollection == null || !CosmosCollection.Id.Equals(CollectionName))
|
|
|
|
+ {
|
|
|
|
+ DocumentCollection collectionDefinition = new DocumentCollection { Id = CollectionName };
|
|
|
|
+ collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });
|
|
|
|
+
|
|
|
|
+ // collectionDefinition.PartitionKey = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection<string>() };
|
|
|
|
+ if (!string.IsNullOrEmpty(PartitionKey))
|
|
|
|
+ {
|
|
|
|
+ collectionDefinition.PartitionKey.Paths.Add("/" + PartitionKey);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ // CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(Database), collectionDefinition);
|
|
|
|
+ CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(
|
|
|
|
+ UriFactory.CreateDatabaseUri(Database), collectionDefinition, new RequestOptions { OfferThroughput = CollectionThroughput }
|
|
|
|
+ );
|
|
|
|
+ }
|
|
return CosmosCollection;
|
|
return CosmosCollection;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -252,31 +272,31 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
public async Task<List<T>> FindSQL<T>(string sql)
|
|
public async Task<List<T>> FindSQL<T>(string sql)
|
|
{
|
|
{
|
|
Type t = typeof(T);
|
|
Type t = typeof(T);
|
|
- List<T> objs = new List<T>();
|
|
|
|
|
|
+ //List<T> objs = new List<T>();
|
|
await InitializeCollection<T>();
|
|
await InitializeCollection<T>();
|
|
var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql);
|
|
var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql);
|
|
- foreach (var item in query)
|
|
|
|
- {
|
|
|
|
- objs.Add(item);
|
|
|
|
- }
|
|
|
|
- return objs;
|
|
|
|
|
|
+ //foreach (var item in query)
|
|
|
|
+ //{
|
|
|
|
+ // objs.Add(item);
|
|
|
|
+ //}
|
|
|
|
+ return query.ToList<T>();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<List<T>> FindSQL<T>(string sql, bool IsPk)
|
|
public async Task<List<T>> FindSQL<T>(string sql, bool IsPk)
|
|
{
|
|
{
|
|
Type t = typeof(T);
|
|
Type t = typeof(T);
|
|
- List<T> objs = new List<T>();
|
|
|
|
- Boolean open = IsPk;
|
|
|
|
|
|
+ //List<T> objs = new List<T>();
|
|
|
|
+ // Boolean open = IsPk;
|
|
await InitializeCollection<T>();
|
|
await InitializeCollection<T>();
|
|
//查询条数 -1是全部
|
|
//查询条数 -1是全部
|
|
- FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = open };
|
|
|
|
|
|
+ FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = IsPk };
|
|
var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql, queryOptions);
|
|
var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql, queryOptions);
|
|
- foreach (var item in query)
|
|
|
|
- {
|
|
|
|
- objs.Add(item);
|
|
|
|
- }
|
|
|
|
- return objs;
|
|
|
|
|
|
+ //foreach (var item in query)
|
|
|
|
+ //{
|
|
|
|
+ // objs.Add(item);
|
|
|
|
+ //}
|
|
|
|
+ return query.ToList<T>();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -566,7 +586,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, bool IsPk)
|
|
public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, bool IsPk)
|
|
{
|
|
{
|
|
Type t = typeof(T);
|
|
Type t = typeof(T);
|
|
- List<T> objs = new List<T>();
|
|
|
|
|
|
+ // List<T> objs = new List<T>();
|
|
await InitializeCollection<T>();
|
|
await InitializeCollection<T>();
|
|
StringBuilder sql = new StringBuilder("select * from c where 1=1 ");
|
|
StringBuilder sql = new StringBuilder("select * from c where 1=1 ");
|
|
if (dict != null)
|
|
if (dict != null)
|
|
@@ -579,11 +599,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
//查询条数 -1是全部
|
|
//查询条数 -1是全部
|
|
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = IsPk };
|
|
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = IsPk };
|
|
var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql.ToString(), queryOptions);
|
|
var query = CosmosClient.CreateDocumentQuery<T>(UriFactory.CreateDocumentCollectionUri(Database, t.Name), sql.ToString(), queryOptions);
|
|
- foreach (var item in query)
|
|
|
|
- {
|
|
|
|
- objs.Add(item);
|
|
|
|
- }
|
|
|
|
- return objs;
|
|
|
|
|
|
+ //foreach (var item in query)
|
|
|
|
+ //{
|
|
|
|
+ // objs.Add(item);
|
|
|
|
+ //}
|
|
|
|
+ return query.ToList<T>();
|
|
}
|
|
}
|
|
|
|
|
|
private static string GenSql(object obj, string key)
|
|
private static string GenSql(object obj, string key)
|
|
@@ -602,5 +622,30 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
|
|
_ => null,
|
|
_ => null,
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public async Task<IQueryable<dynamic>> FindByDict(string CollectionName, string PartitionKey, Dictionary<string, object> dict) {
|
|
|
|
+
|
|
|
|
+ await InitializeCollection(CollectionName, PartitionKey);
|
|
|
|
+ StringBuilder sql = new StringBuilder("select * from "+ CollectionName + " c where 1=1 ");
|
|
|
|
+ if (dict != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (string key in dict.Keys)
|
|
|
|
+ {
|
|
|
|
+ sql.Append(GenSql(dict[key], key));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ FeedOptions queryOptions;
|
|
|
|
+ if (!string.IsNullOrEmpty(PartitionKey))
|
|
|
|
+ {
|
|
|
|
+ queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true };
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = false };
|
|
|
|
+ }
|
|
|
|
+ //查询条数 -1是全部
|
|
|
|
+
|
|
|
|
+ var query = CosmosClient.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(Database, CollectionName), sql.ToString(), queryOptions);
|
|
|
|
+ return query;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|