2025-04-26 19:46:11 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Data.Common;
|
2025-05-01 14:01:27 +08:00
|
|
|
|
using System.Data;
|
2025-04-26 19:46:11 +08:00
|
|
|
|
using MongoDB.Bson;
|
2025-05-01 14:01:27 +08:00
|
|
|
|
using MongoDB.Driver;
|
2025-05-01 14:09:30 +08:00
|
|
|
|
using System.Linq;
|
2025-05-01 14:33:38 +08:00
|
|
|
|
using MongoDB.Bson.Serialization;
|
|
|
|
|
using System.Collections.Generic;
|
2025-05-02 11:39:41 +08:00
|
|
|
|
using System.Xml.Linq;
|
2025-05-01 14:01:27 +08:00
|
|
|
|
|
2025-04-26 19:46:11 +08:00
|
|
|
|
namespace MongoDb.Ado.data
|
|
|
|
|
{
|
|
|
|
|
public class MongoDbCommand : DbCommand
|
|
|
|
|
{
|
|
|
|
|
private string _commandText;
|
|
|
|
|
private MongoDbConnection _connection;
|
|
|
|
|
private int _commandTimeout;
|
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
public MongoDbCommand() { }
|
2025-04-26 19:46:11 +08:00
|
|
|
|
|
|
|
|
|
public MongoDbCommand(string commandText, MongoDbConnection connection)
|
|
|
|
|
{
|
|
|
|
|
_commandText = commandText;
|
|
|
|
|
_connection = connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string CommandText
|
|
|
|
|
{
|
|
|
|
|
get => _commandText;
|
|
|
|
|
set => _commandText = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int CommandTimeout
|
|
|
|
|
{
|
|
|
|
|
get => _commandTimeout;
|
|
|
|
|
set => _commandTimeout = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override CommandType CommandType { get; set; } = CommandType.Text;
|
|
|
|
|
|
|
|
|
|
protected override DbConnection DbConnection
|
|
|
|
|
{
|
|
|
|
|
get => _connection;
|
|
|
|
|
set => _connection = (MongoDbConnection)value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
protected override DbParameterCollection DbParameterCollection => throw new NotSupportedException("暂不支持参数。");
|
2025-04-26 19:46:11 +08:00
|
|
|
|
|
|
|
|
|
protected override DbTransaction DbTransaction { get; set; }
|
|
|
|
|
|
|
|
|
|
public override bool DesignTimeVisible { get; set; }
|
|
|
|
|
|
|
|
|
|
public override UpdateRowSource UpdatedRowSource { get; set; }
|
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
public override void Cancel() { }
|
2025-04-26 19:46:11 +08:00
|
|
|
|
|
|
|
|
|
public override int ExecuteNonQuery()
|
|
|
|
|
{
|
2025-05-01 14:01:27 +08:00
|
|
|
|
var (operation, collectionName, json) = ParseCommand(_commandText);
|
2025-05-02 15:42:18 +08:00
|
|
|
|
var collection = GetCollection(collectionName);
|
|
|
|
|
return ExecuteHandlerFactory.Handler(operation, json, collection);
|
2025-04-26 19:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ExecuteScalar()
|
2025-05-02 15:25:47 +08:00
|
|
|
|
{
|
|
|
|
|
var (operation, collectionName, json) = ParseCommand(_commandText);
|
2025-05-01 14:01:27 +08:00
|
|
|
|
var collection = GetCollection(collectionName);
|
2025-05-02 15:34:22 +08:00
|
|
|
|
return new ExecuteScalarHandler().Handle(operation,collection, json);
|
2025-04-26 19:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
|
|
|
|
{
|
2025-05-01 14:01:27 +08:00
|
|
|
|
var (operation, collectionName, json) = ParseCommand(_commandText);
|
|
|
|
|
var collection = GetCollection(collectionName);
|
2025-05-02 11:39:41 +08:00
|
|
|
|
return new DbDataReaderFactory().Handle(operation, collection, json);
|
2025-04-26 19:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
public override void Prepare() { }
|
|
|
|
|
|
|
|
|
|
protected override DbParameter CreateDbParameter()
|
2025-04-26 19:46:11 +08:00
|
|
|
|
{
|
2025-05-01 14:01:27 +08:00
|
|
|
|
throw new NotImplementedException();
|
2025-04-26 19:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
private IMongoCollection<BsonDocument> GetCollection(string name)
|
2025-04-26 19:46:11 +08:00
|
|
|
|
{
|
|
|
|
|
if (_connection == null || _connection.State != ConnectionState.Open)
|
2025-05-01 14:01:27 +08:00
|
|
|
|
throw new InvalidOperationException("连接尚未打开。");
|
2025-04-26 19:46:11 +08:00
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
return _connection.GetDatabase().GetCollection<BsonDocument>(name);
|
2025-04-26 19:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:01:27 +08:00
|
|
|
|
// 示例:find users {age:{$gt:20}} 或 insert users {"name":"Tom"}
|
|
|
|
|
private (string op, string collection, string json) ParseCommand(string cmd)
|
2025-04-26 19:46:11 +08:00
|
|
|
|
{
|
2025-05-01 14:01:27 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(cmd))
|
|
|
|
|
throw new InvalidOperationException("CommandText 不能为空。");
|
|
|
|
|
|
|
|
|
|
var parts = cmd.Trim().Split(new[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (parts.Length < 2)
|
|
|
|
|
throw new InvalidOperationException("命令格式错误,应为:操作 集合名 JSON过滤");
|
|
|
|
|
|
|
|
|
|
string op = parts[0].ToLowerInvariant();
|
|
|
|
|
string collection = parts[1];
|
|
|
|
|
string json = parts.Length >= 3 ? parts[2] : "";
|
|
|
|
|
|
|
|
|
|
return (op, collection, json);
|
2025-04-26 19:46:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-01 14:01:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|