Update mongodb

This commit is contained in:
sunkaixuan
2025-07-09 12:06:43 +08:00
parent c9636a999c
commit 53c50aab06
12 changed files with 21 additions and 4 deletions

View File

@@ -27,7 +27,7 @@ namespace MongoDb.Ado.data
DbDataReaderFactoryAsync.Items.TryGetValue(operation, out var handler); DbDataReaderFactoryAsync.Items.TryGetValue(operation, out var handler);
if (handler == null) if (handler == null)
{ {
await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken); await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken,new HandlerContext());
return new DataTable().CreateDataReader(); return new DataTable().CreateDataReader();
} }
handler.token = cancellationToken; handler.token = cancellationToken;

View File

@@ -11,6 +11,7 @@ namespace MongoDb.Ado.data
{ {
public class BulkWriteHandlerAsync : IMongoOperationHandlerAsync public class BulkWriteHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)

View File

@@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{ {
public class DeleteHandlerAsync : IMongoOperationHandlerAsync public class DeleteHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)

View File

@@ -11,6 +11,7 @@ namespace MongoDb.Ado.data
{ {
public class DeleteManyHandlerAsync : IMongoOperationHandlerAsync public class DeleteManyHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)

View File

@@ -3,6 +3,7 @@ using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -24,7 +25,7 @@ namespace MongoDb.Ado.data
}; };
public static Task<int> HandlerAsync(string operation, string json, IMongoCollection<BsonDocument> collection,CancellationToken cancellationToken) public static Task<int> HandlerAsync(string operation, string json, IMongoCollection<BsonDocument> collection,CancellationToken cancellationToken, HandlerContext handlerContext)
{ {
MongoDbMethodUtils.ValidateOperation(operation); MongoDbMethodUtils.ValidateOperation(operation);
var handlers = ExecuteHandlerFactoryAsync.Items; var handlers = ExecuteHandlerFactoryAsync.Items;
@@ -33,6 +34,7 @@ namespace MongoDb.Ado.data
throw new NotSupportedException($"不支持的操作类型: {operation}"); throw new NotSupportedException($"不支持的操作类型: {operation}");
handler.operation = operation; handler.operation = operation;
handler.token = cancellationToken; handler.token = cancellationToken;
handler.context = handlerContext;
return handler.HandleAsync(collection, json); return handler.HandleAsync(collection, json);
} }

View File

@@ -12,6 +12,7 @@ namespace MongoDb.Ado.data
{ {
string operation { get; set; } string operation { get; set; }
CancellationToken token { get; set; } CancellationToken token { get; set; }
HandlerContext context { get; set; }
Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json); Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json);
} }

View File

@@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{ {
public class InsertHandlerAsync : IMongoOperationHandlerAsync public class InsertHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)

View File

@@ -6,17 +6,21 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Linq;
namespace MongoDb.Ado.data namespace MongoDb.Ado.data
{ {
public class InsertManyHandlerAsync : IMongoOperationHandlerAsync public class InsertManyHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{ {
var documents = ParseJsonArray(json); var documents = ParseJsonArray(json);
await collection.InsertManyAsync(documents,null,token); await collection.InsertManyAsync(documents,null,token);
var objectIds = documents.Select(it => it["_id"].AsObjectId.ToString()).ToArray();
context.ids = objectIds;
return documents.Count; return documents.Count;
} }

View File

@@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{ {
public class NonFindHandlerAsync : IMongoOperationHandlerAsync public class NonFindHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }

View File

@@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{ {
public class UpdateHandlerAsync : IMongoOperationHandlerAsync public class UpdateHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)

View File

@@ -11,6 +11,7 @@ namespace MongoDb.Ado.data
{ {
public class UpdateManyHandlerAsync : IMongoOperationHandlerAsync public class UpdateManyHandlerAsync : IMongoOperationHandlerAsync
{ {
public HandlerContext context { get; set; }
public CancellationToken token { get; set; } public CancellationToken token { get; set; }
public string operation { get; set; } public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json) public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)

View File

@@ -79,11 +79,14 @@ namespace MongoDb.Ado.data
return new DbDataReaderFactory().Handle(operation, collection, json); return new DbDataReaderFactory().Handle(operation, collection, json);
} }
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken) public async override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{ {
var (operation, collectionName, json) = ParseCommand(_commandText); var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName); var collection = GetCollection(collectionName);
return ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken); var context = new HandlerContext();
var result= await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken,context);
((MongoDbConnection)this.Connection).ObjectIds = context.ids;
return result;
} }
public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken) public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{ {