切换数据库时打印sql,增加获取主键的接口
This commit is contained in:
@@ -42,9 +42,7 @@ public static class ConfigureSqlSugarExtensions
|
|||||||
|
|
||||||
// // 设置超时时间
|
// // 设置超时时间
|
||||||
// db.Ado.CommandTimeOut = 30;
|
// db.Ado.CommandTimeOut = 30;
|
||||||
|
// db.Aop.OnLogExecuted = (sql, pars) =>
|
||||||
// // 打印SQL语句
|
|
||||||
// db.Aop.OnLogExecuting = (sql, pars) =>
|
|
||||||
// {
|
// {
|
||||||
// var oldColor = Console.ForegroundColor;
|
// var oldColor = Console.ForegroundColor;
|
||||||
// Console.ForegroundColor = ConsoleColor.Green;
|
// Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using JNPF.Common.Models.VisualDev;
|
|||||||
using JNPF.Common.Security;
|
using JNPF.Common.Security;
|
||||||
using JNPF.DependencyInjection;
|
using JNPF.DependencyInjection;
|
||||||
using JNPF.FriendlyException;
|
using JNPF.FriendlyException;
|
||||||
|
using JNPF.Logging;
|
||||||
using JNPF.Systems.Entitys.Dto.Database;
|
using JNPF.Systems.Entitys.Dto.Database;
|
||||||
using JNPF.Systems.Entitys.Model.DataBase;
|
using JNPF.Systems.Entitys.Model.DataBase;
|
||||||
using JNPF.Systems.Entitys.System;
|
using JNPF.Systems.Entitys.System;
|
||||||
@@ -74,6 +75,29 @@ public class DataBaseManager : IDataBaseManager, ITransient
|
|||||||
InitKeyType = InitKeyType.Attribute,
|
InitKeyType = InitKeyType.Attribute,
|
||||||
IsAutoCloseConnection = true
|
IsAutoCloseConnection = true
|
||||||
});
|
});
|
||||||
|
var db = _sqlSugarClient.GetConnectionScope(link.Id);
|
||||||
|
|
||||||
|
// 设置超时时间
|
||||||
|
db.Ado.CommandTimeOut = 30;
|
||||||
|
db.Aop.OnLogExecuted = (sql, pars) =>
|
||||||
|
{
|
||||||
|
var oldColor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
var finalSql = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars);
|
||||||
|
Console.WriteLine($"【{DateTime.Now.ToString("HH:mm:ss.fff")}——SQL执行完成】{db.Ado.SqlExecutionTime.TotalMilliseconds} ms");
|
||||||
|
Console.WriteLine(finalSql);
|
||||||
|
Console.ForegroundColor = oldColor;
|
||||||
|
if (db.Ado.SqlExecutionTime.TotalMilliseconds > 3000)
|
||||||
|
{
|
||||||
|
Log.Warning($"慢查询: {db.Ado.SqlExecutionTime.TotalMilliseconds}ms, SQL: " + finalSql);
|
||||||
|
}
|
||||||
|
Console.WriteLine();
|
||||||
|
};
|
||||||
|
db.Aop.OnError = (ex) =>
|
||||||
|
{
|
||||||
|
Log.Error(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
|
||||||
|
};
|
||||||
|
|
||||||
_sqlSugarClient.ChangeDatabase(link.Id);
|
_sqlSugarClient.ChangeDatabase(link.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,6 +415,13 @@ WHERE pcolumn.table_name='{0}' ORDER BY ordinal_position";
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<string> GetPrimaries(DbLinkEntity? link, string tableName)
|
||||||
|
{
|
||||||
|
if (link != null && _sqlSugarClient.CurrentConnectionConfig.ConfigId != link.Id) _sqlSugarClient = ChangeDataBase(link);
|
||||||
|
|
||||||
|
return _sqlSugarClient.DbMaintenance.GetPrimaries(tableName);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取表数据.
|
/// 获取表数据.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -137,6 +137,14 @@ public interface IDataBaseManager
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
List<DbTableFieldModel> GetFieldList(DbLinkEntity? link, string? tableName);
|
List<DbTableFieldModel> GetFieldList(DbLinkEntity? link, string? tableName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取表的主键
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="link"></param>
|
||||||
|
/// <param name="tableName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
List<string> GetPrimaries(DbLinkEntity? link, string tableName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取表数据.
|
/// 获取表数据.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -72,7 +72,6 @@ where TEntity : class, new()
|
|||||||
base.Context.Ado.CommandTimeOut = 30;
|
base.Context.Ado.CommandTimeOut = 30;
|
||||||
base.Context.Aop.OnLogExecuted = (sql, pars) =>
|
base.Context.Aop.OnLogExecuted = (sql, pars) =>
|
||||||
{
|
{
|
||||||
// 在控制台输出sql语句
|
|
||||||
var oldColor = Console.ForegroundColor;
|
var oldColor = Console.ForegroundColor;
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
var finalSql = UtilMethods.GetSqlString(Context.CurrentConnectionConfig.DbType, sql, pars);
|
var finalSql = UtilMethods.GetSqlString(Context.CurrentConnectionConfig.DbType, sql, pars);
|
||||||
|
|||||||
@@ -1732,10 +1732,13 @@ public class RunService : IRunService, ITransient
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private string GetPrimary(DbLinkEntity link, string MainTableName)
|
private string GetPrimary(DbLinkEntity link, string MainTableName)
|
||||||
{
|
{
|
||||||
List<DbTableFieldModel>? tableList = _databaseService.GetFieldList(link, MainTableName); // 获取主表所有列
|
var keys = _databaseService.GetPrimaries(link, MainTableName);
|
||||||
DbTableFieldModel? mainPrimary = tableList.Find(t => t.primaryKey); // 主表主键
|
if (keys.Count < 1) throw Oops.Oh(ErrorCode.D1402); // 主表未设置主键
|
||||||
if (mainPrimary == null || mainPrimary.IsNullOrEmpty()) throw Oops.Oh(ErrorCode.D1402); // 主表未设置主键
|
return keys.First();
|
||||||
return mainPrimary.field;
|
//List<DbTableFieldModel>? tableList = _databaseService.GetFieldList(link, MainTableName); // 获取主表所有列
|
||||||
|
//DbTableFieldModel? mainPrimary = tableList.Find(t => t.primaryKey); // 主表主键
|
||||||
|
//if (mainPrimary == null || mainPrimary.IsNullOrEmpty()) throw Oops.Oh(ErrorCode.D1402); // 主表未设置主键
|
||||||
|
//return mainPrimary.field;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user