Update mongodb

This commit is contained in:
sunkaixuan
2025-05-02 16:30:22 +08:00
parent dc0e1af9f8
commit 6d854a492e
15 changed files with 396 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class DbDataReaderFactoryAsync
{
public async Task<DbDataReader> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json)
{
var doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonValue>(json);
IQueryHandlerAsync queryHandler = null;
if (operation == "find")
{
queryHandler = new QueryFindHandlerAsync();
}
else if (operation == "aggregate")
{
queryHandler = new QueryAggregateHandlerAsync();
}
else
{
await ExecuteHandlerFactoryAsync.HandlerAsync(operation,json, collection);
return new DataTable().CreateDataReader();
}
return await queryHandler.HandlerAsync(collection, doc);
}
}
}

View File

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

View File

@@ -0,0 +1,28 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class QueryAggregateHandlerAsync : IQueryHandlerAsync
{
public async Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc)
{
// 解析 JSON 字符串为 BsonArray
var pipeline = doc.AsBsonArray; ;
// 构建聚合管道
var aggregateFluent = collection.Aggregate<BsonDocument>(pipeline.Select(stage => new BsonDocument(stage.AsBsonDocument)).ToArray());
// 执行聚合查询并返回 DbDataReader
var cursor =await aggregateFluent.ToListAsync();
return new MongoDbBsonDocumentDataReader(cursor);
}
}
}

View File

@@ -0,0 +1,44 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class QueryFindHandlerAsync : IQueryHandlerAsync
{
public async Task<DbDataReader> HandlerAsync(IMongoCollection<BsonDocument> collection, BsonValue doc)
{
BsonDocument filter;
BsonDocument projection = null;
if (doc.IsBsonArray)
{
var array = doc.AsBsonArray;
filter = array.Count > 0 ? array[0].AsBsonDocument : new BsonDocument();
if (array.Count > 1)
projection = array[1].AsBsonDocument;
}
else if (doc.IsBsonDocument)
{
filter = doc.AsBsonDocument;
}
else
{
throw new ArgumentException("Invalid JSON format for MongoDB find operation.");
}
var findFluent = collection.Find(filter);
if (projection != null)
findFluent = findFluent.Project<BsonDocument>(projection);
var cursor =await findFluent.ToListAsync();
return new MongoDbBsonDocumentDataReader(cursor); // 你要确保这个类支持逐行读取 BsonDocument
}
}
}

View File

@@ -0,0 +1,20 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class DeleteHandlerAsync : IMongoOperationHandlerAsync
{
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);
return (int)result.DeletedCount;
}
}
}

View File

@@ -0,0 +1,33 @@
using MongoDB.Bson.Serialization;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class DeleteManyHandlerAsync : IMongoOperationHandlerAsync
{
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);
int total = 0;
foreach (var doc in documents)
{
var filter = doc["filter"].AsBsonDocument;
var result =await collection.DeleteManyAsync(filter);
total += (int)result.DeletedCount;
}
return total;
}
private List<BsonDocument> ParseJsonArray(string json)
{
if (json.TrimStart().StartsWith("["))
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
return new List<BsonDocument> { BsonDocument.Parse(json) };
}
}
}

View File

@@ -0,0 +1,36 @@
using MongoDb.Ado.data;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class ExecuteHandlerFactoryAsync
{
public readonly static Dictionary<string, IMongoOperationHandlerAsync> Items = new Dictionary<string, IMongoOperationHandlerAsync>(StringComparer.OrdinalIgnoreCase)
{
{ "insert", new InsertHandlerAsync() },
{ "insertmany", new InsertManyHandlerAsync() },
{ "update", new UpdateHandlerAsync() },
{ "updatemany", new UpdateManyHandlerAsync() },
{ "delete", new DeleteHandlerAsync() },
{ "deletemany", new DeleteManyHandlerAsync() },
{ "find", new NonFindHandlerAsync() }
};
public static Task<int> HandlerAsync(string operation, string json, IMongoCollection<BsonDocument> collection)
{
var handlers = ExecuteHandlerFactoryAsync.Items;
if (!handlers.TryGetValue(operation, out var handler))
throw new NotSupportedException($"不支持的操作类型: {operation}");
return handler.HandleAsync(collection, json);
}
}
}

View File

@@ -0,0 +1,14 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public interface IMongoOperationHandlerAsync
{
Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json);
}
}

View File

@@ -0,0 +1,19 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class InsertHandlerAsync : IMongoOperationHandlerAsync
{
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var doc = BsonDocument.Parse(json);
await collection.InsertOneAsync(doc);
return 1;
}
}
}

View File

@@ -0,0 +1,27 @@
using MongoDB.Bson.Serialization;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class InsertManyHandlerAsync : IMongoOperationHandlerAsync
{
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);
await collection.InsertManyAsync(documents);
return documents.Count;
}
private List<BsonDocument> ParseJsonArray(string json)
{
if (json.TrimStart().StartsWith("["))
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
return new List<BsonDocument> { BsonDocument.Parse(json) };
}
}
}

View File

@@ -0,0 +1,24 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class NonFindHandlerAsync : IMongoOperationHandlerAsync
{
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
using (var dr = await new DbDataReaderFactoryAsync().HandleAsync("", collection, json))
{
if (dr.Read())
{
}
}
return 0; // 查询不改变数据库
}
}
}

View File

@@ -0,0 +1,21 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class UpdateHandlerAsync : IMongoOperationHandlerAsync
{
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);
return (int)result.ModifiedCount;
}
}
}

View File

@@ -0,0 +1,35 @@
using MongoDB.Bson.Serialization;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class UpdateManyHandlerAsync : IMongoOperationHandlerAsync
{
public async Task<int> HandleAsync(IMongoCollection<BsonDocument> collection, string json)
{
var documents = ParseJsonArray(json);
int total = 0;
foreach (var doc in documents)
{
var filter = doc["filter"].AsBsonDocument;
var update = doc["update"].AsBsonDocument;
var result =await collection.UpdateManyAsync(filter, update);
total += (int)result.ModifiedCount;
}
return total;
}
private List<BsonDocument> ParseJsonArray(string json)
{
if (json.TrimStart().StartsWith("["))
return BsonSerializer.Deserialize<List<BsonDocument>>(json);
return new List<BsonDocument> { BsonDocument.Parse(json) };
}
}
}

View File

@@ -0,0 +1,25 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
public class ExecuteScalarHandlerAsync
{
public async Task<object> HandleAsync(string operation, IMongoCollection<BsonDocument> collection, string json)
{
using (var dbReader = await new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json))
{
if (dbReader.Read())
{
return dbReader.GetValue(0);
}
return null; //
}
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Linq;
using MongoDB.Bson.Serialization;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Threading.Tasks;
namespace MongoDb.Ado.data
{
@@ -73,6 +74,26 @@ namespace MongoDb.Ado.data
return new DbDataReaderFactory().Handle(operation, collection, json);
}
public new Task<int> ExecuteNonQueryAsync()
{
var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName);
return ExecuteHandlerFactoryAsync.HandlerAsync(operation, json, collection);
}
public new Task<object> ExecuteScalarAsync()
{
var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName);
return new ExecuteScalarHandlerAsync().HandleAsync(operation, collection, json);
}
protected Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior)
{
var (operation, collectionName, json) = ParseCommand(_commandText);
var collection = GetCollection(collectionName);
return new DbDataReaderFactoryAsync().HandleAsync(operation, collection, json);
}
public override void Prepare() { }
protected override DbParameter CreateDbParameter()