This commit is contained in:
sunkaixuan
2018-02-08 18:27:14 +08:00
parent 0b8ac9a200
commit 1b8effc987
5 changed files with 115 additions and 83 deletions

View File

@@ -8,40 +8,53 @@ namespace SqlSugar
public abstract partial class DbMaintenanceProvider : IDbMaintenance
{
#region DML
public virtual List<DbTableInfo> GetViewInfoList()
public virtual List<DbTableInfo> GetViewInfoList(bool isCache = true)
{
string cacheKey = "DbMaintenanceProvider.GetViewInfoList";
cacheKey = GetCacheKey(cacheKey);
var result = GetListOrCache<DbTableInfo>(cacheKey, this.GetViewInfoListSql);
var result = new List<DbTableInfo>();
if (isCache)
result = GetListOrCache<DbTableInfo>(cacheKey, this.GetViewInfoListSql);
else
result = this.Context.Ado.SqlQuery<DbTableInfo>(this.GetViewInfoListSql);
foreach (var item in result)
{
item.DbObjectType = DbObjectType.View;
}
return result;
}
public virtual List<DbTableInfo> GetTableInfoList()
public virtual List<DbTableInfo> GetTableInfoList(bool isCache = true)
{
string cacheKey = "DbMaintenanceProvider.GetTableInfoList";
cacheKey = GetCacheKey(cacheKey);
var result = GetListOrCache<DbTableInfo>(cacheKey, this.GetTableInfoListSql);
var result = new List<DbTableInfo>();
if (isCache)
result = GetListOrCache<DbTableInfo>(cacheKey, this.GetTableInfoListSql);
else
result = this.Context.Ado.SqlQuery<DbTableInfo>(this.GetTableInfoListSql);
foreach (var item in result)
{
item.DbObjectType = DbObjectType.Table;
}
return result;
}
public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
{
if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>();
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
cacheKey = GetCacheKey(cacheKey);
return GetListOrCache<DbColumnInfo>(cacheKey, string.Format(this.GetColumnInfosByTableNameSql, tableName));
var sql = string.Format(this.GetColumnInfosByTableNameSql, tableName);
if (isCache)
return GetListOrCache<DbColumnInfo>(cacheKey, sql);
else
return this.Context.Ado.SqlQuery<DbColumnInfo>(sql);
}
public virtual List<string> GetIsIdentities(string tableName)
{
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
cacheKey = GetCacheKey(cacheKey);
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,() =>
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
return result.Select(it => it.DbColumnName).ToList();
@@ -51,7 +64,7 @@ namespace SqlSugar
{
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
cacheKey = GetCacheKey(cacheKey);
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,() =>
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
return result.Select(it => it.DbColumnName).ToList();
@@ -60,10 +73,10 @@ namespace SqlSugar
#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));
}

View File

@@ -9,15 +9,15 @@ namespace SqlSugar
SqlSugarClient Context { get; set; }
#region DML
List<DbTableInfo> GetViewInfoList();
List<DbTableInfo> GetTableInfoList();
List<DbColumnInfo> GetColumnInfosByTableName(string tableName);
List<DbTableInfo> GetViewInfoList(bool isCache=true);
List<DbTableInfo> GetTableInfoList(bool isCache=true);
List<DbColumnInfo> GetColumnInfosByTableName(string tableName,bool isCache=true);
List<string> GetIsIdentities(string tableName);
List<string> 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);

View File

@@ -172,12 +172,22 @@ namespace SqlSugar
#endregion
#region Methods
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
public override List<DbColumnInfo> 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,
() =>
{
return GetColumnInfosByTableName(tableName);
});
}
private List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
{
string sql = "select * from " + tableName + " WHERE 1=2 ";
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
@@ -206,9 +216,8 @@ namespace SqlSugar
}
return result;
}
});
}
private List<string> GetPrimaryKeyByTableNames(string tableName)
{
string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();

View File

@@ -164,12 +164,23 @@ namespace SqlSugar
#endregion
#region Methods
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
public override List<DbColumnInfo> 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,
() =>
{
return GetColumnsByTableName(tableName);
});
}
private List<DbColumnInfo> GetColumnsByTableName(string tableName)
{
string sql = "select * from " + tableName + " limit 0,1";
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
@@ -197,8 +208,6 @@ namespace SqlSugar
}
return result;
}
});
}
public override bool CreateTable(string tableName, List<DbColumnInfo> 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);