Update mongodb

This commit is contained in:
sunkaixuan 2025-06-10 17:51:49 +08:00
parent ea02771199
commit fffbe29151
4 changed files with 12 additions and 3 deletions

View File

@ -14,6 +14,7 @@ namespace MongoDb.Ado.data
{ {
public class DbDataReaderFactoryAsync public class DbDataReaderFactoryAsync
{ {
public CancellationToken token { get; set; }
public readonly static Dictionary<string, IQueryHandlerAsync> Items = new Dictionary<string, IQueryHandlerAsync>(StringComparer.OrdinalIgnoreCase) public readonly static Dictionary<string, IQueryHandlerAsync> Items = new Dictionary<string, IQueryHandlerAsync>(StringComparer.OrdinalIgnoreCase)
{ {
{ "find", new QueryFindHandlerAsync() }, { "find", new QueryFindHandlerAsync() },
@ -28,7 +29,8 @@ namespace MongoDb.Ado.data
{ {
await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken); await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection, cancellationToken);
return new DataTable().CreateDataReader(); return new DataTable().CreateDataReader();
} }
handler.token = cancellationToken;
return await handler.HandlerAsync(collection, doc); return await handler.HandlerAsync(collection, doc);
} }

View File

@ -1,12 +1,15 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using System.Data.Common; using System.Data.Common;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDb.Ado.data namespace MongoDb.Ado.data
{ {
public interface IQueryHandlerAsync public interface IQueryHandlerAsync
{ {
CancellationToken token { get; set; }
Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc); Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc);
} }
} }

View File

@ -6,12 +6,14 @@ using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDb.Ado.data namespace MongoDb.Ado.data
{ {
public class QueryAggregateHandlerAsync : IQueryHandlerAsync public class QueryAggregateHandlerAsync : IQueryHandlerAsync
{ {
public CancellationToken token { get; set; }
public async Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc) public async Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc)
{ {
// 解析 JSON 字符串为 BsonArray // 解析 JSON 字符串为 BsonArray
@ -21,7 +23,7 @@ namespace MongoDb.Ado.data
var aggregateFluent = collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray()); var aggregateFluent = collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray());
// 执行聚合查询并返回 DbDataReader // 执行聚合查询并返回 DbDataReader
var cursor =await aggregateFluent.ToListAsync(); var cursor =await aggregateFluent.ToListAsync(token);
return new MongoDbBsonDocumentDataReader(cursor); return new MongoDbBsonDocumentDataReader(cursor);
} }
} }

View File

@ -5,12 +5,14 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDb.Ado.data namespace MongoDb.Ado.data
{ {
public class QueryFindHandlerAsync : IQueryHandlerAsync public class QueryFindHandlerAsync : IQueryHandlerAsync
{ {
public CancellationToken token { get; set; }
public async Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc) public async Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc)
{ {
BsonDocument filter; BsonDocument filter;
@ -37,7 +39,7 @@ namespace MongoDb.Ado.data
if (projection != null) if (projection != null)
findFluent = findFluent.Project<BsonDocument>(projection); findFluent = findFluent.Project<BsonDocument>(projection);
var cursor =await findFluent.ToListAsync(); var cursor =await findFluent.ToListAsync(token);
return new MongoDbBsonDocumentDataReader(cursor); // 你要确保这个类支持逐行读取 BsonDocument return new MongoDbBsonDocumentDataReader(cursor); // 你要确保这个类支持逐行读取 BsonDocument
} }
} }