From eb6a041b0fc85a83153fc6f10aeafe738f2dba04 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 26 Apr 2025 15:50:22 +0800 Subject: [PATCH] Add MongoDb Csproj --- .../MongoDb.Ado.data/MongoDb.Ado.data.csproj | 11 ++ .../MongoDb.Ado.data/MongoDbConnection.cs | 109 ++++++++++++++++++ Src/Asp.NetCore2/MongoDbTest/AdoTest.cs | 28 +++++ .../MongoDbTest/MongoDbTest.csproj | 14 +++ Src/Asp.NetCore2/MongoDbTest/Program.cs | 4 + Src/Asp.NetCore2/SqlSugarCore.sln | 32 +++++ 6 files changed, 198 insertions(+) create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/MongoDb.Ado.data.csproj create mode 100644 Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbConnection.cs create mode 100644 Src/Asp.NetCore2/MongoDbTest/AdoTest.cs create mode 100644 Src/Asp.NetCore2/MongoDbTest/MongoDbTest.csproj create mode 100644 Src/Asp.NetCore2/MongoDbTest/Program.cs diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDb.Ado.data.csproj b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDb.Ado.data.csproj new file mode 100644 index 000000000..340978a1c --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDb.Ado.data.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.1 + + + + + + + diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbConnection.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbConnection.cs new file mode 100644 index 000000000..9955caa7a --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbConnection.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace MongoDb.Ado.data +{ + public class MongoDbConnection : DbConnection + { + private readonly string _originalConnectionString; + private MongoClient _client; + private IMongoDatabase _database; + private ConnectionState _state = ConnectionState.Closed; + private string _databaseName; + + public override string ConnectionString + { + get => _originalConnectionString; + set => throw new NotSupportedException("Setting ConnectionString after construction is not supported."); + } + + public override string Database => _databaseName; + + public override string DataSource => _client?.Settings?.Server?.ToString() ?? ""; + + public override string ServerVersion => "MongoDB_" + (_client?.Cluster?.Description?.ClusterId.ToString() ?? "Unknown"); + + public override ConnectionState State => _state; + + public MongoDbConnection(string connectionString) + { + _originalConnectionString = connectionString; + ParseAndConnect(connectionString); + } + + private void ParseAndConnect(string connStr) + { + if (connStr.TrimStart().StartsWith("mongodb://", StringComparison.OrdinalIgnoreCase)) + { + var mongoUrl = new MongoUrl(connStr); + _client = new MongoClient(mongoUrl); + _databaseName = mongoUrl.DatabaseName ?? "test"; + _database = _client.GetDatabase(_databaseName); + return; + } + + var dict = ParsePgStyleConnectionString(connStr); + var host = dict.GetValueOrDefault("Host", "localhost"); + var port = dict.GetValueOrDefault("Port", "27017"); + _databaseName = dict.GetValueOrDefault("Database", "test"); + var username = dict.GetValueOrDefault("Username", ""); + var password = dict.GetValueOrDefault("Password", ""); + + var mongoConnStr = string.IsNullOrEmpty(username) + ? $"mongodb://{host}:{port}" + : $"mongodb://{Uri.EscapeDataString(username)}:{Uri.EscapeDataString(password)}@{host}:{port}/{_databaseName}"; + + var mongoUrlParsed = new MongoUrl(mongoConnStr); + _client = new MongoClient(mongoUrlParsed); + _database = _client.GetDatabase(_databaseName); + } + + private Dictionary ParsePgStyleConnectionString(string connStr) + { + var builder = new DbConnectionStringBuilder { ConnectionString = connStr }; + return builder.Cast>() + .ToDictionary(kv => kv.Key, kv => kv.Value.ToString(), StringComparer.OrdinalIgnoreCase); + } + + public override void Open() + { + // MongoClient 实际上在操作集合时才连接,我们只改变状态 + _state = ConnectionState.Open; + } + + public override void Close() + { + _state = ConnectionState.Closed; + } + + protected override DbCommand CreateDbCommand() + { + throw new NotSupportedException("MongoDB does not support SQL-style DbCommand."); + } + + public override void ChangeDatabase(string databaseName) + { + if (string.IsNullOrEmpty(databaseName)) + throw new ArgumentException("Database name cannot be null or empty.", nameof(databaseName)); + + _database = _client.GetDatabase(databaseName); + _databaseName = databaseName; + } + + protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) + { + throw new NotSupportedException("MongoDB does not support transactions via DbTransaction."); + } + + public IMongoDatabase GetDatabase() => _database; + + public MongoClient GetClient() => _client; + + public override string ToString() => _originalConnectionString; + } +} diff --git a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs new file mode 100644 index 000000000..510025ab1 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs @@ -0,0 +1,28 @@ +using MongoDb.Ado.data; +using MongoDB.Bson; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MongoDbTest +{ + public class AdoTest + { + public static void Init() + { + //开发中 + var client = new MongoClient(""); + var database = client.GetDatabase("test"); + // 获取当前数据库中的所有集合 + var collections = database.ListCollections(); + + + MongoDbConnection connection = new MongoDbConnection(""); + connection.Open(); + connection.Close(); + } + } +} diff --git a/Src/Asp.NetCore2/MongoDbTest/MongoDbTest.csproj b/Src/Asp.NetCore2/MongoDbTest/MongoDbTest.csproj new file mode 100644 index 000000000..42a3a2f01 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/MongoDbTest.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Src/Asp.NetCore2/MongoDbTest/Program.cs b/Src/Asp.NetCore2/MongoDbTest/Program.cs new file mode 100644 index 000000000..2a9dbd707 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/Program.cs @@ -0,0 +1,4 @@ +using MongoDbTest; +//开发中 +AdoTest.Init(); +Console.WriteLine("执行完成"); diff --git a/Src/Asp.NetCore2/SqlSugarCore.sln b/Src/Asp.NetCore2/SqlSugarCore.sln index b17c1833f..a5bb48a8f 100644 --- a/Src/Asp.NetCore2/SqlSugarCore.sln +++ b/Src/Asp.NetCore2/SqlSugarCore.sln @@ -98,6 +98,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar.DuckDBCore", "SqlS EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DuckDBTest", "DuckDBTest\DuckDBTest.csproj", "{F8DBA187-9A40-4AE5-972F-230835FEAEE4}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MongoDb", "MongoDb", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDb.Ado.data", "MongoDb.Ado.data\MongoDb.Ado.data.csproj", "{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDbTest", "MongoDbTest\MongoDbTest.csproj", "{C6547EB1-B5AE-426A-A04D-3E2685AD7114}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -636,6 +642,30 @@ Global {F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|ARM32.Build.0 = Release|Any CPU {F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|x86.ActiveCfg = Release|Any CPU {F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|x86.Build.0 = Release|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|ARM32.ActiveCfg = Debug|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|ARM32.Build.0 = Debug|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|x86.ActiveCfg = Debug|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|x86.Build.0 = Debug|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|Any CPU.Build.0 = Release|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|ARM32.ActiveCfg = Release|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|ARM32.Build.0 = Release|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|x86.ActiveCfg = Release|Any CPU + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|x86.Build.0 = Release|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|ARM32.ActiveCfg = Debug|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|ARM32.Build.0 = Debug|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|x86.Build.0 = Debug|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|Any CPU.Build.0 = Release|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|ARM32.ActiveCfg = Release|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|ARM32.Build.0 = Release|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|x86.ActiveCfg = Release|Any CPU + {C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -655,6 +685,8 @@ Global {F706204F-2FC4-5112-646F-28D498E56AF4} = {88992AAF-146B-4253-9AD7-493E8F415B57} {15CB3CFF-E99D-4A79-8F6F-D9175CC2F7CB} = {88992AAF-146B-4253-9AD7-493E8F415B57} {7AF1C5DF-AEA1-47E2-92CA-621330F68860} = {88992AAF-146B-4253-9AD7-493E8F415B57} + {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {88992AAF-146B-4253-9AD7-493E8F415B57} + {9D7D900A-01ED-4E7C-A97A-1D3325F67AA2} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {230A85B9-54F1-41B1-B1DA-80086581B2B4}