2017-01-07 21:54:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
namespace SqlSugar
|
|
|
|
|
|
{
|
|
|
|
|
|
public abstract partial class DbMaintenanceProvider : IDbMaintenance
|
|
|
|
|
|
{
|
|
|
|
|
|
protected abstract string GetViewInfoListSql { get; }
|
2017-05-17 00:59:08 +08:00
|
|
|
|
protected abstract string GetTableInfoListSql { get; }
|
|
|
|
|
|
protected abstract string GetColumnInfosByTableNameSql { get; }
|
|
|
|
|
|
protected abstract string AddColumnToTableSql { get; }
|
|
|
|
|
|
protected abstract string BackupDataBaseSql { get; }
|
|
|
|
|
|
protected abstract string CreateTableSql { get; }
|
|
|
|
|
|
protected abstract string TruncateTableSql { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public SqlSugarClient Context { get; set; }
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public List<DbTableInfo> GetViewInfoList()
|
|
|
|
|
|
{
|
2017-05-17 01:31:24 +08:00
|
|
|
|
string key = "DbMaintenanceProvider.GetViewInfoList";
|
2017-05-29 00:55:48 +08:00
|
|
|
|
var result= GetListOrCache<DbTableInfo>(key, this.GetViewInfoListSql);
|
|
|
|
|
|
foreach (var item in result)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.DbObjectType = DbObjectType.View;
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
public List<DbTableInfo> GetTableInfoList()
|
|
|
|
|
|
{
|
2017-05-17 01:31:24 +08:00
|
|
|
|
string key = "DbMaintenanceProvider.GetTableInfoList";
|
2017-05-29 00:55:48 +08:00
|
|
|
|
var result= GetListOrCache<DbTableInfo>(key, this.GetTableInfoListSql);
|
|
|
|
|
|
foreach (var item in result)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.DbObjectType = DbObjectType.Table;
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
|
|
|
|
|
|
{
|
2017-05-25 12:39:51 +08:00
|
|
|
|
if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>();
|
2017-05-17 01:31:24 +08:00
|
|
|
|
string key = "DbMaintenanceProvider.GetColumnInfosByTableName." + tableName.ToLower();
|
2017-05-29 16:44:20 +08:00
|
|
|
|
return GetListOrCache<DbColumnInfo>(key,string.Format(this.GetColumnInfosByTableNameSql,tableName));
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-01 23:00:05 +08:00
|
|
|
|
public virtual List<string> GetIsIdentities(string tableName)
|
2017-04-28 01:20:30 +08:00
|
|
|
|
{
|
2017-05-01 23:00:05 +08:00
|
|
|
|
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
|
2017-05-21 01:40:06 +08:00
|
|
|
|
return result.Select(it => it.DbColumnName).ToList();
|
2017-05-01 23:00:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual List<string> GetPrimaries(string tableName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
|
2017-05-21 01:40:06 +08:00
|
|
|
|
return result.Select(it => it.DbColumnName).ToList();
|
2017-04-28 01:20:30 +08:00
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
|
|
|
|
|
public bool AddColumnToTable(string tableName, DbColumnInfo column)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
public bool BackupDataBase()
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool CreateTable(string tableName, List<DbColumnInfo> columns)
|
|
|
|
|
|
{
|
2017-05-19 11:20:07 +08:00
|
|
|
|
this.Context.Ado.ExecuteCommand(this.CreateTableSql);
|
2017-01-07 21:54:51 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual bool TruncateTable(string tableName)
|
|
|
|
|
|
{
|
2017-05-19 11:20:07 +08:00
|
|
|
|
this.Context.Ado.ExecuteCommand(this.TruncateTableSql);
|
2017-01-07 21:54:51 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Private
|
|
|
|
|
|
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
|
|
|
|
|
{
|
2017-05-28 18:13:44 +08:00
|
|
|
|
return this.Context.RewritableMethods.GetCacheInstance<List<T>>().Func(cacheKey,
|
2017-01-07 21:54:51 +08:00
|
|
|
|
(cm, key) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return cm[cacheKey];
|
|
|
|
|
|
|
|
|
|
|
|
}, (cm, key) =>
|
|
|
|
|
|
{
|
2017-05-19 11:20:07 +08:00
|
|
|
|
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
|
|
|
|
|
|
this.Context.Ado.IsEnableLogEvent = false;
|
2017-05-29 01:56:23 +08:00
|
|
|
|
var reval = this.Context.Ado.SqlQuery<T>(sql);
|
2017-05-19 11:20:07 +08:00
|
|
|
|
this.Context.Ado.IsEnableLogEvent = isEnableLogEvent;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
return reval;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|