2025-05-04 15:59:46 +08:00
|
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
|
using MongoDb.Ado.data;
|
2025-07-02 10:33:09 +08:00
|
|
|
|
using MongoDB.Driver;
|
2025-05-11 14:19:01 +08:00
|
|
|
|
using SqlSugar.MongoDb;
|
2025-05-04 10:06:16 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Transactions;
|
|
|
|
|
|
|
|
|
|
namespace SqlSugar.MongoDb
|
|
|
|
|
{
|
|
|
|
|
public partial class MongoDbProvider : AdoProvider
|
|
|
|
|
{
|
2025-07-11 17:20:42 +08:00
|
|
|
|
IClientSessionHandle iClientSessionHandle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (this.Connection as MongoDbConnection).iClientSessionHandle;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
(this.Connection as MongoDbConnection).iClientSessionHandle = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-04 10:06:16 +08:00
|
|
|
|
public MongoDbProvider()
|
|
|
|
|
{
|
|
|
|
|
if (StaticConfig.AppContext_ConvertInfinityDateTime == false)
|
|
|
|
|
{
|
|
|
|
|
AppContext.SetSwitch("MongoDb.EnableLegacyTimestampBehavior", true);
|
|
|
|
|
AppContext.SetSwitch("MongoDb.DisableDateTimeInfinityConversions", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-05-05 15:01:07 +08:00
|
|
|
|
public override bool IsNoSql { get; set; } = true;
|
2025-07-02 10:33:09 +08:00
|
|
|
|
public override void BeginTran()
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle = ((MongoDbConnection)this.Connection).GetClient().StartSession();
|
|
|
|
|
iClientSessionHandle.StartTransaction();
|
|
|
|
|
}
|
|
|
|
|
public override void CommitTran()
|
|
|
|
|
{
|
2025-07-11 12:00:56 +08:00
|
|
|
|
if (iClientSessionHandle != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle.CommitTransaction();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle.Dispose();
|
|
|
|
|
iClientSessionHandle = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-02 10:33:09 +08:00
|
|
|
|
}
|
|
|
|
|
public override void RollbackTran()
|
|
|
|
|
{
|
2025-07-11 12:00:56 +08:00
|
|
|
|
if (iClientSessionHandle != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle.AbortTransaction();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle.Dispose();
|
|
|
|
|
iClientSessionHandle = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-02 10:33:09 +08:00
|
|
|
|
}
|
2025-07-02 10:38:08 +08:00
|
|
|
|
public override async Task BeginTranAsync()
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle = await ((MongoDbConnection)this.Connection).GetClient().StartSessionAsync();
|
|
|
|
|
iClientSessionHandle.StartTransaction();//StartTransaction has no asynchronous methods
|
|
|
|
|
}
|
|
|
|
|
public override async Task CommitTranAsync()
|
|
|
|
|
{
|
2025-07-11 12:00:56 +08:00
|
|
|
|
if (iClientSessionHandle != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await iClientSessionHandle.CommitTransactionAsync();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle.Dispose();
|
|
|
|
|
iClientSessionHandle = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-02 10:38:08 +08:00
|
|
|
|
}
|
|
|
|
|
public override async Task RollbackTranAsync()
|
|
|
|
|
{
|
2025-07-11 12:00:56 +08:00
|
|
|
|
if (iClientSessionHandle != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await iClientSessionHandle.AbortTransactionAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
iClientSessionHandle.Dispose();
|
|
|
|
|
iClientSessionHandle = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-02 10:38:08 +08:00
|
|
|
|
}
|
2025-05-04 10:06:16 +08:00
|
|
|
|
public override IDbConnection Connection
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (base._DbConnection == null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var MongoDbConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
|
|
|
|
|
base._DbConnection = new MongoDbConnection(MongoDbConnectionString);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return base._DbConnection;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
base._DbConnection = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void BeginTran(string transactionName)
|
|
|
|
|
{
|
|
|
|
|
base.BeginTran();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDataAdapter GetAdapter()
|
|
|
|
|
{
|
|
|
|
|
return new SqlSugarMongoDbDataAdapter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// if mysql return MySqlParameter[] pars
|
|
|
|
|
/// if sqlerver return SqlParameter[] pars ...
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parameters"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
|
|
|
|
|
{
|
|
|
|
|
return parameters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void UNumber(SugarParameter parameter)
|
|
|
|
|
{
|
|
|
|
|
if (parameter.DbType == System.Data.DbType.UInt16)
|
|
|
|
|
{
|
|
|
|
|
parameter.DbType = System.Data.DbType.Int16;
|
|
|
|
|
parameter.Value = Convert.ToInt16(parameter.Value);
|
|
|
|
|
}
|
|
|
|
|
else if (parameter.DbType == System.Data.DbType.UInt32)
|
|
|
|
|
{
|
|
|
|
|
parameter.DbType = System.Data.DbType.Int32;
|
|
|
|
|
parameter.Value = Convert.ToInt32(parameter.Value);
|
|
|
|
|
}
|
|
|
|
|
else if (parameter.DbType == System.Data.DbType.UInt64)
|
|
|
|
|
{
|
|
|
|
|
parameter.DbType = System.Data.DbType.Int64;
|
|
|
|
|
parameter.Value = Convert.ToInt64(parameter.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void BeginTran(System.Data.IsolationLevel iso, string transactionName)
|
|
|
|
|
{
|
|
|
|
|
base.BeginTran();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetCommandToAdapter(IDataAdapter adapter, DbCommand command)
|
|
|
|
|
{
|
2025-05-05 14:26:49 +08:00
|
|
|
|
((MongoDbDataAdapter)adapter).SelectCommand =(MongoDbCommand)command;
|
2025-05-04 10:06:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override DbCommand GetCommand(string sql, SugarParameter[] pars)
|
|
|
|
|
{
|
2025-05-04 15:59:46 +08:00
|
|
|
|
MongoDbCommand mongoDbCommand = new MongoDbCommand(sql,(MongoDbConnection)this.Connection);
|
|
|
|
|
CheckConnection();
|
|
|
|
|
return mongoDbCommand;
|
2025-05-04 10:06:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|