Upddate mongodb

This commit is contained in:
sunkaixuan 2025-06-10 17:46:57 +08:00
parent 6afa9f7539
commit b41076f0b0
12 changed files with 34 additions and 13 deletions

View File

@ -7,6 +7,7 @@ using System.Data;
using System.Data.Common;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
@ -18,16 +19,16 @@ namespace MongoDb.Ado.data
{ "find", new QueryFindHandlerAsync() },
{ "aggregate", new QueryAggregateHandlerAsync() },
};
public async Task<DbDataReader> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json)
public async Task<DbDataReader> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json, CancellationToken cancellationToken)
{
MongoDbMethodUtils.ValidateOperation(operation);
var doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(json);
DbDataReaderFactoryAsync.Items.TryGetValue(operation, out var handler);
if (handler == null)
{
await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection);
await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken);
return new DataTable().CreateDataReader();
}
}
return await handler.HandlerAsync(collection, doc);
}

View File

@ -3,18 +3,20 @@ using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class DeleteHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);
var filter = doc["filter"].AsBsonDocument;
var result =await collection.DeleteOneAsync(filter);
var result =await collection.DeleteOneAsync(filter,token);
return (int)result.DeletedCount;
}
}

View File

@ -5,11 +5,13 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace MongoDb.Ado.data
{
public class DeleteManyHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
@ -18,7 +20,7 @@ namespace MongoDb.Ado.data
foreach (var doc in documents)
{
var filter = doc["filter"].AsBsonDocument;
var result =await collection.DeleteManyAsync(filter);
var result =await collection.DeleteManyAsync(filter,token);
total += (int)result.DeletedCount;
}
return total;

View File

@ -4,6 +4,7 @@ using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
@ -22,7 +23,7 @@ namespace MongoDb.Ado.data
};
public static Task<int> HandlerAsync(string operation, string json, IMongoCollection<BsonDocument> collection)
public static Task<int> HandlerAsync(string operation, string json, IMongoCollection<BsonDocument> collection,CancellationToken cancellationToken)
{
MongoDbMethodUtils.ValidateOperation(operation);
var handlers = ExecuteHandlerFactoryAsync.Items;
@ -30,6 +31,7 @@ namespace MongoDb.Ado.data
if (!handlers.TryGetValue(operation, out var handler))
throw new NotSupportedException($"不支持的操作类型: {operation}");
handler.operation = operation;
handler.token = cancellationToken;
return handler.HandleAsync(collection, json);
}

View File

@ -3,6 +3,7 @@ using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
@ -10,6 +11,8 @@ namespace MongoDb.Ado.data
public interface IMongoOperationHandlerAsync
{
string operation { get; set; }
CancellationToken token { get; set; }
Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json);
}
}

View File

@ -3,12 +3,14 @@ using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class InsertHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{

View File

@ -5,11 +5,13 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace MongoDb.Ado.data
{
public class InsertManyHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{

View File

@ -3,17 +3,19 @@ using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class NonFindHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
using (var dr = await new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json))
using (var dr = await new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json,token))
{
if (dr.Read())
{

View File

@ -3,19 +3,21 @@ using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class UpdateHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);
var filter = doc["filter"].AsBsonDocument;
var update = doc["update"].AsBsonDocument;
var result =await collection.UpdateOneAsync(filter, update);
var result =await collection.UpdateOneAsync(filter, update,null,token);
return (int)result.ModifiedCount;
}
}

View File

@ -5,11 +5,13 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace MongoDb.Ado.data
{
public class UpdateManyHandlerAsync : IMongoOperationHandlerAsync
{
public CancellationToken token { get; set; }
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{

View File

@ -4,15 +4,16 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class ExecuteScalarHandlerAsync
{
public async Task<object> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json)
public async Task<object> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json,CancellationToken cancellationToken)
{
using (var dbReader = await new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json))
using (var dbReader = await new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json,cancellationToken))
{
if (dbReader.Read())
{

View File

@ -83,19 +83,19 @@ namespace MongoDb.Ado.data
{
var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName);
return ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection);
return ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken);
}
public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName);
return new ExecuteScalarHandlerAsync().HandleAsync(operation, collection, json);
return new ExecuteScalarHandlerAsync().HandleAsync(operation, collection, json, cancellationToken);
}
protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior,CancellationToken cancellationToken)
{
var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName);
return new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json);
return new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json, cancellationToken);
}