Update mongodb

This commit is contained in:
sunkaixuan 2025-05-02 16:55:48 +08:00
parent 9552fd74a4
commit 9b394ced24
21 changed files with 70 additions and 31 deletions

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class MongoDbMethodUtils
{
public void ValidateOperation(string operation)
{
if (ExecuteHandlerFactory.Items.TryGetValue(operation, out var handler))
{
return ;
}
if (DbDataReaderFactory.Items.TryGetValue(operation, out var handlerQuery))
{
return;
}
throw new InvalidOperationException($"Operation '{operation}' is not supported.");
}
}
}

View File

@ -12,24 +12,21 @@ namespace MongoDb.Ado.data
{
public class DbDataReaderFactory
{
public readonly static Dictionary<string, IQueryHandler> Items = new Dictionary<string, IQueryHandler>(StringComparer.OrdinalIgnoreCase)
{
{ "find", new QueryFindHandler() },
{ "aggregate", new QueryFindHandler() },
};
public DbDataReader Handle(string operation, IMongoCollection<BsonDocument> collection, string json)
{
var doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(json);
IQueryHandler queryHandler = null;
if (operation == "find")
DbDataReaderFactory.Items.TryGetValue(operation, out var handler);
if (handler==null)
{
queryHandler = new QueryFindHandler();
}
else if (operation == "aggregate")
{
queryHandler = new QueryAggregateHandler();
}
else
{
ExecuteHandlerFactory.Handler(operation,json, collection);
ExecuteHandlerFactory.Handler(operation, json, collection);
return new DataTable().CreateDataReader();
}
return queryHandler.Handler(collection, doc);
return handler.Handler(collection, doc);
}
}

View File

@ -13,24 +13,21 @@ namespace MongoDb.Ado.data
{
public class DbDataReaderFactoryAsync
{
public async Task<DbDataReader> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json)
public readonly static Dictionary<string, IQueryHandlerAsync> Items = new Dictionary<string, IQueryHandlerAsync>(StringComparer.OrdinalIgnoreCase)
{
{ "find", new QueryFindHandlerAsync() },
{ "aggregate", new QueryFindHandlerAsync() },
};
public async Task<DbDataReader> Handle(string operation, IMongoCollection<BsonDocument> collection, string json)
{
var doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(json);
IQueryHandlerAsync queryHandler = null;
if (operation == "find")
DbDataReaderFactoryAsync.Items.TryGetValue(operation, out var handler);
if (handler == null)
{
queryHandler = new QueryFindHandlerAsync();
}
else if (operation == "aggregate")
{
queryHandler = new QueryAggregateHandlerAsync();
}
else
{
await ExecuteHandlerFactoryAsync.HandlerAsync(operation,json, collection);
await ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection);
return new DataTable().CreateDataReader();
}
return await queryHandler.HandlerAsync(collection, doc);
return await handler.HandlerAsync(collection, doc);
}
}

View File

@ -8,6 +8,7 @@ namespace MongoDb.Ado.data
{
public class DeleteHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);

View File

@ -9,6 +9,7 @@ namespace MongoDb.Ado.data
{
public class DeleteManyHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);

View File

@ -27,7 +27,7 @@ namespace MongoDb.Ado.data
if (!handlers.TryGetValue(operation, out var handler))
throw new NotSupportedException($"不支持的操作类型: {operation}");
handler.operation = operation;
return handler.Handle(collection, json);
}

View File

@ -8,6 +8,7 @@ namespace MongoDb.Ado.data
{
public interface IMongoOperationHandler
{
string operation { get; set; }
int Handle(IMongoCollection<BsonDocument> collection, string json);
}
}

View File

@ -8,6 +8,7 @@ namespace MongoDb.Ado.data
{
public class InsertHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);

View File

@ -9,6 +9,7 @@ namespace MongoDb.Ado.data
{
public class InsertManyHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);

View File

@ -8,10 +8,16 @@ namespace MongoDb.Ado.data
{
public class NonFindHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var filter = string.IsNullOrWhiteSpace(json) ? FilterDefinition<BsonDocument>.Empty : BsonDocument.Parse(json);
var result = collection.Find(filter).FirstOrDefault();
using (var dr = new DbDataReaderFactory().Handle(operation, collection, json))
{
if (dr.Read())
{
}
}
return 0; // 查询不改变数据库
}
}

View File

@ -8,6 +8,7 @@ namespace MongoDb.Ado.data
{
public class UpdateHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);

View File

@ -9,6 +9,7 @@ namespace MongoDb.Ado.data
{
public class UpdateManyHandler : IMongoOperationHandler
{
public string operation { get; set; }
public int Handle(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);

View File

@ -9,6 +9,7 @@ namespace MongoDb.Ado.data
{
public class DeleteHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);

View File

@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{
public class DeleteManyHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);

View File

@ -28,7 +28,7 @@ namespace MongoDb.Ado.data
if (!handlers.TryGetValue(operation, out var handler))
throw new NotSupportedException($"不支持的操作类型: {operation}");
handler.operation = operation;
return handler.HandleAsync(collection, json);
}

View File

@ -8,7 +8,8 @@ using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public interface IMongoOperationHandlerAsync
{
{
string operation { get; set; }
Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json);
}
}

View File

@ -9,6 +9,7 @@ namespace MongoDb.Ado.data
{
public class InsertHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);

View File

@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{
public class InsertManyHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);

View File

@ -9,9 +9,11 @@ namespace MongoDb.Ado.data
{
public class NonFindHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
using (var dr = await new DbDataReaderFactoryAsync().HandleAsync("", collection, json))
using (var dr = await new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json))
{
if (dr.Read())
{

View File

@ -9,6 +9,7 @@ namespace MongoDb.Ado.data
{
public class UpdateHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);

View File

@ -10,6 +10,7 @@ namespace MongoDb.Ado.data
{
public class UpdateManyHandlerAsync : IMongoOperationHandlerAsync
{
public string operation { get; set; }
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);