diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index deba51989..6a2be323f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -8,62 +8,75 @@ namespace SqlSugar public abstract partial class DbMaintenanceProvider : IDbMaintenance { #region DML - public virtual List GetViewInfoList() + public virtual List GetViewInfoList(bool isCache = true) { string cacheKey = "DbMaintenanceProvider.GetViewInfoList"; cacheKey = GetCacheKey(cacheKey); - var result = GetListOrCache(cacheKey, this.GetViewInfoListSql); + var result = new List(); + if (isCache) + result = GetListOrCache(cacheKey, this.GetViewInfoListSql); + else + result = this.Context.Ado.SqlQuery(this.GetViewInfoListSql); foreach (var item in result) { item.DbObjectType = DbObjectType.View; } return result; } - public virtual List GetTableInfoList() + public virtual List GetTableInfoList(bool isCache = true) { string cacheKey = "DbMaintenanceProvider.GetTableInfoList"; cacheKey = GetCacheKey(cacheKey); - var result = GetListOrCache(cacheKey, this.GetTableInfoListSql); + var result = new List(); + if (isCache) + result = GetListOrCache(cacheKey, this.GetTableInfoListSql); + else + result = this.Context.Ado.SqlQuery(this.GetTableInfoListSql); foreach (var item in result) { item.DbObjectType = DbObjectType.Table; } return result; } - public virtual List GetColumnInfosByTableName(string tableName) + public virtual List GetColumnInfosByTableName(string tableName, bool isCache = true) { if (string.IsNullOrEmpty(tableName)) return new List(); string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return GetListOrCache(cacheKey, string.Format(this.GetColumnInfosByTableNameSql, tableName)); + var sql = string.Format(this.GetColumnInfosByTableNameSql, tableName); + if (isCache) + return GetListOrCache(cacheKey, sql); + else + return this.Context.Ado.SqlQuery(sql); + } public virtual List GetIsIdentities(string tableName) { string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,() => - { - var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList(); - return result.Select(it => it.DbColumnName).ToList(); - }); + return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => + { + var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList(); + return result.Select(it => it.DbColumnName).ToList(); + }); } public virtual List GetPrimaries(string tableName) { string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); - return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,() => - { - var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList(); - return result.Select(it => it.DbColumnName).ToList(); - }); + return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => + { + var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList(); + return result.Select(it => it.DbColumnName).ToList(); + }); } #endregion #region Check - public virtual bool IsAnyTable(string tableName) + public virtual bool IsAnyTable(string tableName, bool isCache = true) { tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName); - var tables = GetTableInfoList(); + var tables = GetTableInfoList(isCache); if (tables == null) return false; else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase)); } diff --git a/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs index 8307f952c..cb55982be 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDbMaintenance.cs @@ -9,15 +9,15 @@ namespace SqlSugar SqlSugarClient Context { get; set; } #region DML - List GetViewInfoList(); - List GetTableInfoList(); - List GetColumnInfosByTableName(string tableName); + List GetViewInfoList(bool isCache=true); + List GetTableInfoList(bool isCache=true); + List GetColumnInfosByTableName(string tableName,bool isCache=true); List GetIsIdentities(string tableName); List GetPrimaries(string tableName); #endregion #region Check - bool IsAnyTable(string tableName); + bool IsAnyTable(string tableName, bool isCache = true); bool IsAnyColumn(string tableName, string column); bool IsPrimaryKey(string tableName, string column); bool IsIdentity(string tableName, string column); diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs index 6d9df406e..c125eb326 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/DbMaintenance/OracleDbMaintenance.cs @@ -172,43 +172,52 @@ namespace SqlSugar #endregion #region Methods - public override List GetColumnInfosByTableName(string tableName) + public override List GetColumnInfosByTableName(string tableName,bool isCache=true) { string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); + if (!isCache) + return GetColumnInfosByTableName(tableName); + else return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () => { - string sql = "select * from " + tableName + " WHERE 1=2 "; - var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent; - this.Context.Ado.IsEnableLogEvent = false; - using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql)) - { - this.Context.Ado.IsEnableLogEvent = oldIsEnableLog; - List result = new List(); - var schemaTable = reader.GetSchemaTable(); - foreach (DataRow row in schemaTable.Rows) - { - DbColumnInfo column = new DbColumnInfo() - { - TableName = tableName, - DataType = row["DataType"].ToString().Replace("System.", "").Trim(), - IsNullable = (bool)row["AllowDBNull"], - //IsIdentity = (bool)row["IsAutoIncrement"], - ColumnDescription = GetFieldComment(tableName, row["ColumnName"].ToString()), - DbColumnName = row["ColumnName"].ToString(), - //DefaultValue = row["defaultValue"].ToString(), - IsPrimarykey = GetPrimaryKeyByTableNames(tableName).Any(it => it.Equals(row["ColumnName"].ToString(), StringComparison.CurrentCultureIgnoreCase)), - Length = row["ColumnSize"].ObjToInt(), - Scale = row["numericscale"].ObjToInt() - }; - result.Add(column); - } - return result; - } + return GetColumnInfosByTableName(tableName); }); } + + private List GetColumnInfosByTableName(string tableName) + { + string sql = "select * from " + tableName + " WHERE 1=2 "; + var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent; + this.Context.Ado.IsEnableLogEvent = false; + using (DbDataReader reader = (DbDataReader)this.Context.Ado.GetDataReader(sql)) + { + this.Context.Ado.IsEnableLogEvent = oldIsEnableLog; + List result = new List(); + var schemaTable = reader.GetSchemaTable(); + foreach (DataRow row in schemaTable.Rows) + { + DbColumnInfo column = new DbColumnInfo() + { + TableName = tableName, + DataType = row["DataType"].ToString().Replace("System.", "").Trim(), + IsNullable = (bool)row["AllowDBNull"], + //IsIdentity = (bool)row["IsAutoIncrement"], + ColumnDescription = GetFieldComment(tableName, row["ColumnName"].ToString()), + DbColumnName = row["ColumnName"].ToString(), + //DefaultValue = row["defaultValue"].ToString(), + IsPrimarykey = GetPrimaryKeyByTableNames(tableName).Any(it => it.Equals(row["ColumnName"].ToString(), StringComparison.CurrentCultureIgnoreCase)), + Length = row["ColumnSize"].ObjToInt(), + Scale = row["numericscale"].ObjToInt() + }; + result.Add(column); + } + return result; + } + } + private List GetPrimaryKeyByTableNames(string tableName) { string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs index beb31612f..859c519fd 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs @@ -164,41 +164,50 @@ namespace SqlSugar #endregion #region Methods - public override List GetColumnInfosByTableName(string tableName) + public override List GetColumnInfosByTableName(string tableName, bool isCache = true) { string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower(); cacheKey = GetCacheKey(cacheKey); + if (!isCache) + { + return GetColumnInfosByTableName(tableName); + } return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, - () => - { - string sql = "select * from " + tableName + " limit 0,1"; - var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent; - this.Context.Ado.IsEnableLogEvent = false; - using (DbDataReader reader = (SQLiteDataReader)this.Context.Ado.GetDataReader(sql)) - { - this.Context.Ado.IsEnableLogEvent = oldIsEnableLog; - List result = new List(); - var schemaTable = reader.GetSchemaTable(); - foreach (DataRow row in schemaTable.Rows) - { - DbColumnInfo column = new DbColumnInfo() - { - TableName = tableName, - DataType = row["DataTypeName"].ToString().Trim(), - IsNullable = (bool)row["AllowDBNull"], - IsIdentity = (bool)row["IsAutoIncrement"], - ColumnDescription = null, - DbColumnName = row["ColumnName"].ToString(), - DefaultValue = row["defaultValue"].ToString(), - IsPrimarykey = (bool)row["IsKey"], - Length = Convert.ToInt32(row["ColumnSize"]) - }; - result.Add(column); - } - return result; - } + () => + { + return GetColumnsByTableName(tableName); - }); + }); + } + + private List GetColumnsByTableName(string tableName) + { + string sql = "select * from " + tableName + " limit 0,1"; + var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent; + this.Context.Ado.IsEnableLogEvent = false; + using (DbDataReader reader = (SQLiteDataReader)this.Context.Ado.GetDataReader(sql)) + { + this.Context.Ado.IsEnableLogEvent = oldIsEnableLog; + List result = new List(); + var schemaTable = reader.GetSchemaTable(); + foreach (DataRow row in schemaTable.Rows) + { + DbColumnInfo column = new DbColumnInfo() + { + TableName = tableName, + DataType = row["DataTypeName"].ToString().Trim(), + IsNullable = (bool)row["AllowDBNull"], + IsIdentity = (bool)row["IsAutoIncrement"], + ColumnDescription = null, + DbColumnName = row["ColumnName"].ToString(), + DefaultValue = row["defaultValue"].ToString(), + IsPrimarykey = (bool)row["IsKey"], + Length = Convert.ToInt32(row["ColumnSize"]) + }; + result.Add(column); + } + return result; + } } public override bool CreateTable(string tableName, List columns, bool isCreatePrimaryKey = true) @@ -219,8 +228,9 @@ namespace SqlSugar } } string sql = GetCreateTableSql(tableName, columns); - if (!isCreatePrimaryKey) { - sql = sql.Replace("PRIMARY KEY AUTOINCREMENT","").Replace("PRIMARY KEY", ""); + if (!isCreatePrimaryKey) + { + sql = sql.Replace("PRIMARY KEY AUTOINCREMENT", "").Replace("PRIMARY KEY", ""); } this.Context.Ado.ExecuteCommand(sql); return true; @@ -239,7 +249,7 @@ namespace SqlSugar } string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null; string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull; - string primaryKey = item.IsPrimarykey?this.CreateTablePirmaryKey:null; + string primaryKey = item.IsPrimarykey ? this.CreateTablePirmaryKey : null; string identity = item.IsIdentity ? this.CreateTableIdentity : null; string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity); columnArray.Add(addItem); diff --git a/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite b/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite index 3219c0c59..7fe81f78b 100644 Binary files a/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite and b/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite differ