diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/EmptyDbParameterCollection.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/EmptyDbParameterCollection.cs new file mode 100644 index 000000000..bfa4a27b2 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/EmptyDbParameterCollection.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Text; + +namespace MongoDb.Ado.data +{ + // 将类的访问修饰符从 private 更改为 internal,以解决 CS1527 错误 + internal class EmptyDbParameterCollection : DbParameterCollection + { + public override int Add(object value) => 0; + public override void AddRange(Array values) { } + public override void Clear() { } + public override bool Contains(string value) => false; + public override bool Contains(object value) => false; + public override void CopyTo(Array array, int index) { } + public override int Count => 0; + public override System.Collections.IEnumerator GetEnumerator() => Array.Empty().GetEnumerator(); + public override int IndexOf(string parameterName) => -1; + public override int IndexOf(object value) => -1; + public override void Insert(int index, object value) { } + public override bool IsFixedSize => true; + public override bool IsReadOnly => true; + public override bool IsSynchronized => false; + public override void Remove(object value) { } + public override void RemoveAt(string parameterName) { } + public override void RemoveAt(int index) { } + + protected override DbParameter GetParameter(int index) + { + throw new NotImplementedException(); + } + + protected override DbParameter GetParameter(string parameterName) + { + return null; + } + + protected override void SetParameter(int index, DbParameter value) + { + } + + protected override void SetParameter(string parameterName, DbParameter value) + { + } + + public override object SyncRoot => new object(); + } +} diff --git a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs index b1337b062..2847ad947 100644 --- a/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs +++ b/Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbCommand.cs @@ -45,7 +45,8 @@ namespace MongoDb.Ado.data set => _connection = (MongoDbConnection)value; } - protected override DbParameterCollection DbParameterCollection => throw new NotSupportedException("暂不支持参数。"); + protected override DbParameterCollection DbParameterCollection + { get { return new EmptyDbParameterCollection(); } } protected override DbTransaction DbTransaction { get; set; } diff --git a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs index f76abc721..2ce35e069 100644 --- a/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs +++ b/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs @@ -1,6 +1,7 @@ using MongoDb.Ado.data; using MongoDB.Bson; using MongoDB.Driver; +using MongoDbTest.DBHelper; using System; using System.Collections.Generic; using System.Data; diff --git a/Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs b/Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs new file mode 100644 index 000000000..8e1fa5bdb --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs @@ -0,0 +1,49 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MongoDbTest.DBHelper +{ + /// + /// Helper class for database operations + /// 数据库操作的辅助类 + /// + public class DbHelper + { + public static string ConnectionString = "mongodb://mongouser:Huangxin%40123@lb-pvnmcqnd-81l6ojfdw3u4en92.clb.sh-tencentclb.com:27018/SqlSugarDb?replicaSet=cmgo-7d07e4w1_0&authSource=admin"; + public static string SqlSugarConnectionString = "host=lb-pvnmcqnd-81l6ojfdw3u4en92.clb.sh-tencentclb.com;Port=27018;Database=SqlSugarDb;Username=mongouser;Password=Huangxin@123;replicaSet=cmgo-7d07e4w1_0&authSource=admin"; + + /// + /// Get a new SqlSugarClient instance with specific configurations + /// 获取具有特定配置的新 SqlSugarClient 实例 + /// + /// SqlSugarClient instance + public static SqlSugarClient GetNewDb() + { + //注册DLL防止找不到DLL + InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] { + typeof(SqlSugar.MongoDb.MongoDbProvider).Assembly }; + + //创建DB + var db = new SqlSugarClient(new ConnectionConfig() + { + IsAutoCloseConnection = true, + DbType = DbType.MongoDb, + ConnectionString = SqlSugarConnectionString, + LanguageType = LanguageType.Default//Set language + + }, + it => + { + it.Aop.OnLogExecuting = (sql, para) => + { + Console.WriteLine(UtilMethods.GetNativeSql(sql, para)); + }; + }); + return db; + } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/MongoDbTest/DbHelper.cs b/Src/Asp.NetCore2/MongoDbTest/DbHelper.cs deleted file mode 100644 index 1bd92f8b2..000000000 --- a/Src/Asp.NetCore2/MongoDbTest/DbHelper.cs +++ /dev/null @@ -1,15 +0,0 @@ -using MongoDB.Driver.Core.Configuration; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MongoDbTest -{ - public class DbHelper - { - public static string ConnectionString= "mongodb://mongouser:Huangxin%40123@localhost:27018/SqlSugarDb?replicaSet=cmgo-7d07e4w1_0&authSource=admin"; - public static string SqlSugarConnectionString = "host=localhost;Port=27018;Database=SqlSugarDb;Username=mongouser;Password=Huangxin@123;replicaSet=cmgo-7d07e4w1_0&authSource=admin"; - } -} diff --git a/Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs b/Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs new file mode 100644 index 000000000..fde560dab --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs @@ -0,0 +1,24 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MongoDbTest +{ + + public class OrderInfo + { + [SugarColumn(IsPrimaryKey = true, IsIdentity = true,ColumnName ="_Id")] + public int Id { get; set; } + + public string Name { get; set; } + public decimal Price { get; set; } + [SugarColumn(IsNullable = true)] + public DateTime CreateTime { get; set; } + [SugarColumn(IsNullable =true)] + public int CustomId { get; set; } + [SugarColumn(IsIgnore = true)] + public List Items { get; set; } + } +} diff --git a/Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs b/Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs new file mode 100644 index 000000000..604c306ed --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MongoDbTest +{ + [SqlSugar.SugarTable("OrderDetail")] + public class OrderItem + { + [SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true, ColumnName = "_Id")] + public int Id { get; set; } + public int OrderId { get; set; } + public decimal? Price { get; set; } + [SqlSugar.SugarColumn(IsNullable = true)] + public DateTime? CreateTime { get; set; } + } +} diff --git a/Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs b/Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs new file mode 100644 index 000000000..f5e4ca011 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs @@ -0,0 +1,20 @@ +using MongoDbTest.DBHelper; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MongoDbTest +{ + public class OrmTest + { + public static void Init() + { + var db = DbHelper.GetNewDb(); + db.Insertable(new OrderInfo() { CreateTime = DateTime.Now, Name = "a", Price = 1 }) + .ExecuteCommand(); + } + } +} diff --git a/Src/Asp.NetCore2/MongoDbTest/Program.cs b/Src/Asp.NetCore2/MongoDbTest/Program.cs index 43a22adb3..0b859d33f 100644 --- a/Src/Asp.NetCore2/MongoDbTest/Program.cs +++ b/Src/Asp.NetCore2/MongoDbTest/Program.cs @@ -1,5 +1,6 @@ using MongoDbTest; //开发中 +OrmTest.Init(); AdoTest.Init(); ExpTest.Init(); Console.WriteLine("执行完成"); diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/DuckDBProvider.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/DuckDBProvider.cs index 7de849f9d..6be579cca 100644 --- a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/DuckDBProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/DuckDBProvider.cs @@ -1,4 +1,5 @@ -using MongoDb.Ado.data; +using Microsoft.Data.SqlClient; +using MongoDb.Ado.data; using SqlSugar.MongoDbCore; using System; using System.Collections.Generic; @@ -99,7 +100,9 @@ namespace SqlSugar.MongoDb public override DbCommand GetCommand(string sql, SugarParameter[] pars) { - throw new NotImplementedException(); + MongoDbCommand mongoDbCommand = new MongoDbCommand(sql,(MongoDbConnection)this.Connection); + CheckConnection(); + return mongoDbCommand; } } } diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs index eb6dcf816..d0b1b9459 100644 --- a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; namespace SqlSugar.MongoDb { @@ -38,77 +40,41 @@ namespace SqlSugar.MongoDb }; public override string ToSqlString() { - if (IsNoInsertNull) + var sql= BuildInsertMany(this.DbColumnInfoList, this.EntityInfo.DbTableName); + return sql; + } + + public static string BuildInsertMany(List columns, string tableName) + { + // 分组 + var grouped = columns.GroupBy(c => c.TableId); + + var jsonObjects = new List(); + + foreach (var group in grouped) { - DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList(); - } - var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList(); - var isSingle = groupList.Count() == 1; - string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName))); - if (isSingle) - { - string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it =>base.GetDbColumn(it, Builder.SqlParameterKeyWord + it.DbColumnName))); - ActionMinDate(); - return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString); - } - else - { - StringBuilder batchInsetrSql = new StringBuilder(); - int pageSize = 200; - int pageIndex = 1; - if (IsNoPage&&IsReturnPkList) + var dict = new Dictionary(); + + foreach (var col in group) { - pageSize = groupList.Count; + dict[col.DbColumnName] = col.Value; } - int totalRecord = groupList.Count; - int pageCount = (totalRecord + pageSize - 1) / pageSize; - while (pageCount >= pageIndex) + + string json = JsonSerializer.Serialize(dict, new JsonSerializerOptions { - batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString); - int i = 0; - foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()) - { - var isFirst = i == 0; - if (isFirst) - { - batchInsetrSql.Append(SqlTemplateBatchUnion); - } - batchInsetrSql.Append("\r\n ( " + string.Join(",", columns.Select(it => - { - if (it.InsertServerTime || it.InsertSql.HasValue()||it.SqlParameterDbType is Type|| it?.PropertyType?.Name=="DateOnly" || it?.PropertyType?.Name == "TimeOnly") - { - return GetDbColumn(it, null); - } - object value = null; - if (it.Value is DateTime) - { - value = ((DateTime)it.Value).ToString("O"); - } - else if (it.Value is DateTimeOffset) - { - return FormatDateTimeOffset(it.Value); - } - else if (it.IsArray&&it.Value!=null) - { - return FormatValue(it.Value,it.PropertyName,i,it); - } - else - { - value = it.Value; - } - if (value == null||value==DBNull.Value) - { - return string.Format(SqlTemplateBatchSelect, "NULL"); - } - return string.Format(SqlTemplateBatchSelect, "'" + value.ObjToStringNoTrim().ToSqlFilter() + "'"); - })) + "),"); - ++i; - } - pageIndex++; - batchInsetrSql.Remove(batchInsetrSql.Length - 1,1).Append("\r\n;\r\n"); - } - return batchInsetrSql.ToString(); + WriteIndented = false + }); + + jsonObjects.Add(json); } + + var sb = new StringBuilder(); + sb.Append($"insertMany {tableName} "); + sb.Append("["); + sb.Append(string.Join(", ", jsonObjects)); + sb.Append("]"); + + return sb.ToString(); } public object FormatValue(object value, string name, int i, DbColumnInfo columnInfo) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs index 87e19f13a..69e91e1eb 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs @@ -563,6 +563,9 @@ namespace SqlSugar case DbType.DuckDB: InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.DuckDBCore"; break; + case DbType.MongoDb: + InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.MongoDbCore"; + break; default: throw new Exception("ConnectionConfig.DbType is null"); } diff --git a/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs b/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs index 06f90f0e1..13704cd3d 100644 --- a/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs +++ b/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs @@ -39,6 +39,7 @@ namespace SqlSugar DB2, GaussDBNative, DuckDB, + MongoDb, Custom =900 } }