using Microsoft.Extensions.Logging; using SqlSugar; using System; using System.Linq; namespace ReZero.SuperAPI { /// /// Get rezero db /// public class ZeroDb { public static ISqlSugarClient Db =>new SqlSugarClient(UtilMethods.CopyConfig( App.PreStartupDb!.CurrentConnectionConfig)); } /// /// Represents the application's main entry point and provides access to essential services and resources. /// internal class App { /// /// Gets or sets the application's service provider, allowing access to registered services. /// internal static ApplicationServiceProvider? ServiceProvider { get; set; } /// /// Represents a database connection object used before service startup. /// internal static ISqlSugarClient? PreStartupDb { get; set; } /// /// Gets the instance of the SqlSugar client for database operations. /// /// /// This property provides convenient access to the configured SqlSugar client for database operations. /// internal static ISqlSugarClient Db { get => ServiceProvider!.GetService().SugarClient; } /// /// Obtain the database operation object based on the database ID /// /// /// internal static SqlSugarClient? GetDbById(long dbId) { var rootDb = App.Db; var zeroDatabaseInfo = rootDb.Queryable().Where(it => it.Id == dbId).First(); SqlSugarClient? db = null; if (zeroDatabaseInfo != null) db = GetSqlSugarClientByDatabaseInfo(zeroDatabaseInfo); return db; } /// /// Obtain the database operation object based on the table ID /// /// /// internal static SqlSugarClient? GetDbTableId(long tableId) { var rootDb = App.Db; var dbId = CacheManager.Instance.GetList().Where(it => it.Id == tableId).First()?.DataBaseId; var zeroDatabaseInfo =CacheManager.Instance.GetList().Where(it => it.Id == dbId).First(); zeroDatabaseInfo = rootDb.Utilities.TranslateCopy(zeroDatabaseInfo); SqlSugarClient? db = null; if (zeroDatabaseInfo != null) db = GetSqlSugarClientByDatabaseInfo(zeroDatabaseInfo); return db; } /// /// Obtain the database operation object based on the ZeroDatabaseInfo /// /// /// private static SqlSugarClient GetSqlSugarClientByDatabaseInfo(ZeroDatabaseInfo zeroDatabaseInfo) { return new SqlSugarClient(new ConnectionConfig() { ConfigId=int.MaxValue, ConnectionString = zeroDatabaseInfo.Connection, DbType = zeroDatabaseInfo.DbType, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute, MoreSettings = new ConnMoreSettings { SqlServerCodeFirstNvarchar = true, SqliteCodeFirstEnableDropColumn = true, EnableCodeFirstUpdatePrecision = true, IsAutoToUpper=false, PgSqlIsAutoToLower=false, PgSqlIsAutoToLowerCodeFirst=false, EnableOracleIdentity=true } }, db => { db.Aop.OnLogExecuting = (s, p) => { ReZero.DependencyInjection.DependencyResolver.GetLogger().LogInformation(UtilMethods.GetNativeSql(s, p)); }; }); } /// /// Gets the language used by the SuperAPI module. /// /// The language. internal static Language Language { get { return SuperAPIModule._apiOptions!.UiOptions!.UiLanguage; } } } }