mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 23:13:42 +08:00
Update Mongodb
This commit is contained in:
parent
739961e618
commit
c377d1e0ae
@ -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<object>().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();
|
||||||
|
}
|
||||||
|
}
|
@ -45,7 +45,8 @@ namespace MongoDb.Ado.data
|
|||||||
set => _connection = (MongoDbConnection)value;
|
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; }
|
protected override DbTransaction DbTransaction { get; set; }
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using MongoDb.Ado.data;
|
using MongoDb.Ado.data;
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
using MongoDbTest.DBHelper;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
49
Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs
Normal file
49
Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs
Normal file
@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Helper class for database operations
|
||||||
|
/// 数据库操作的辅助类
|
||||||
|
/// </summary>
|
||||||
|
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";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a new SqlSugarClient instance with specific configurations
|
||||||
|
/// 获取具有特定配置的新 SqlSugarClient 实例
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>SqlSugarClient instance</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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";
|
|
||||||
}
|
|
||||||
}
|
|
24
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs
Normal file
24
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs
Normal file
@ -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<OrderItem> Items { get; set; }
|
||||||
|
}
|
||||||
|
}
|
18
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs
Normal file
18
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs
Normal file
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
20
Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs
Normal file
20
Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using MongoDbTest;
|
using MongoDbTest;
|
||||||
//开发中
|
//开发中
|
||||||
|
OrmTest.Init();
|
||||||
AdoTest.Init();
|
AdoTest.Init();
|
||||||
ExpTest.Init();
|
ExpTest.Init();
|
||||||
Console.WriteLine("执行完成");
|
Console.WriteLine("执行完成");
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MongoDb.Ado.data;
|
using Microsoft.Data.SqlClient;
|
||||||
|
using MongoDb.Ado.data;
|
||||||
using SqlSugar.MongoDbCore;
|
using SqlSugar.MongoDbCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -99,7 +100,9 @@ namespace SqlSugar.MongoDb
|
|||||||
|
|
||||||
public override DbCommand GetCommand(string sql, SugarParameter[] pars)
|
public override DbCommand GetCommand(string sql, SugarParameter[] pars)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
MongoDbCommand mongoDbCommand = new MongoDbCommand(sql,(MongoDbConnection)this.Connection);
|
||||||
|
CheckConnection();
|
||||||
|
return mongoDbCommand;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace SqlSugar.MongoDb
|
namespace SqlSugar.MongoDb
|
||||||
{
|
{
|
||||||
@ -38,77 +40,41 @@ namespace SqlSugar.MongoDb
|
|||||||
};
|
};
|
||||||
public override string ToSqlString()
|
public override string ToSqlString()
|
||||||
{
|
{
|
||||||
if (IsNoInsertNull)
|
var sql= BuildInsertMany(this.DbColumnInfoList, this.EntityInfo.DbTableName);
|
||||||
{
|
return sql;
|
||||||
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
|
|
||||||
}
|
}
|
||||||
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
|
||||||
var isSingle = groupList.Count() == 1;
|
public static string BuildInsertMany(List<DbColumnInfo> columns, string tableName)
|
||||||
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();
|
var grouped = columns.GroupBy(c => c.TableId);
|
||||||
return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
|
|
||||||
|
var jsonObjects = new List<string>();
|
||||||
|
|
||||||
|
foreach (var group in grouped)
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
foreach (var col in group)
|
||||||
|
{
|
||||||
|
dict[col.DbColumnName] = col.Value;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
string json = JsonSerializer.Serialize(dict, new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
StringBuilder batchInsetrSql = new StringBuilder();
|
WriteIndented = false
|
||||||
int pageSize = 200;
|
});
|
||||||
int pageIndex = 1;
|
|
||||||
if (IsNoPage&&IsReturnPkList)
|
jsonObjects.Add(json);
|
||||||
{
|
|
||||||
pageSize = groupList.Count;
|
|
||||||
}
|
|
||||||
int totalRecord = groupList.Count;
|
|
||||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
|
||||||
while (pageCount >= pageIndex)
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
public object FormatValue(object value, string name, int i, DbColumnInfo columnInfo)
|
||||||
|
@ -563,6 +563,9 @@ namespace SqlSugar
|
|||||||
case DbType.DuckDB:
|
case DbType.DuckDB:
|
||||||
InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.DuckDBCore";
|
InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.DuckDBCore";
|
||||||
break;
|
break;
|
||||||
|
case DbType.MongoDb:
|
||||||
|
InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.MongoDbCore";
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Exception("ConnectionConfig.DbType is null");
|
throw new Exception("ConnectionConfig.DbType is null");
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ namespace SqlSugar
|
|||||||
DB2,
|
DB2,
|
||||||
GaussDBNative,
|
GaussDBNative,
|
||||||
DuckDB,
|
DuckDB,
|
||||||
|
MongoDb,
|
||||||
Custom =900
|
Custom =900
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user