Files
SqlSugar/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs

293 lines
14 KiB
C#
Raw Normal View History

2017-01-07 21:54:51 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2017-06-05 20:49:02 +08:00
2017-01-07 21:54:51 +08:00
namespace SqlSugar
{
public abstract partial class DbMaintenanceProvider : IDbMaintenance
{
2017-06-05 20:49:02 +08:00
#region DML
2018-02-08 18:27:14 +08:00
public virtual List<DbTableInfo> GetViewInfoList(bool isCache = true)
2017-01-07 21:54:51 +08:00
{
2017-07-19 00:31:27 +08:00
string cacheKey = "DbMaintenanceProvider.GetViewInfoList";
cacheKey = GetCacheKey(cacheKey);
2018-02-08 18:27:14 +08:00
var result = new List<DbTableInfo>();
if (isCache)
result = GetListOrCache<DbTableInfo>(cacheKey, this.GetViewInfoListSql);
else
result = this.Context.Ado.SqlQuery<DbTableInfo>(this.GetViewInfoListSql);
2017-05-29 00:55:48 +08:00
foreach (var item in result)
{
item.DbObjectType = DbObjectType.View;
}
return result;
2017-01-07 21:54:51 +08:00
}
2018-02-08 18:27:14 +08:00
public virtual List<DbTableInfo> GetTableInfoList(bool isCache = true)
2017-01-07 21:54:51 +08:00
{
2017-07-19 00:31:27 +08:00
string cacheKey = "DbMaintenanceProvider.GetTableInfoList";
cacheKey = GetCacheKey(cacheKey);
2018-02-08 18:27:14 +08:00
var result = new List<DbTableInfo>();
if (isCache)
result = GetListOrCache<DbTableInfo>(cacheKey, this.GetTableInfoListSql);
else
result = this.Context.Ado.SqlQuery<DbTableInfo>(this.GetTableInfoListSql);
2017-05-29 00:55:48 +08:00
foreach (var item in result)
{
item.DbObjectType = DbObjectType.Table;
}
return result;
2017-01-07 21:54:51 +08:00
}
2018-02-08 18:27:14 +08:00
public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
2017-01-07 21:54:51 +08:00
{
2017-05-25 12:39:51 +08:00
if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>();
2017-07-19 00:31:27 +08:00
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
cacheKey = GetCacheKey(cacheKey);
2018-02-08 18:27:14 +08:00
var sql = string.Format(this.GetColumnInfosByTableNameSql, tableName);
if (isCache)
return GetListOrCache<DbColumnInfo>(cacheKey, sql);
else
return this.Context.Ado.SqlQuery<DbColumnInfo>(sql);
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-07-19 00:31:27 +08:00
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
cacheKey = GetCacheKey(cacheKey);
2018-02-08 18:27:14 +08:00
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
return result.Select(it => it.DbColumnName).ToList();
});
2017-05-01 23:00:05 +08:00
}
public virtual List<string> GetPrimaries(string tableName)
{
2017-07-19 00:31:27 +08:00
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
cacheKey = GetCacheKey(cacheKey);
2018-02-08 18:27:14 +08:00
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
return result.Select(it => it.DbColumnName).ToList();
});
2017-04-28 01:20:30 +08:00
}
2017-06-05 20:49:02 +08:00
#endregion
2017-01-07 21:54:51 +08:00
2017-06-07 13:33:24 +08:00
#region Check
2018-02-08 18:27:14 +08:00
public virtual bool IsAnyTable(string tableName, bool isCache = true)
2017-06-07 13:33:24 +08:00
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
2018-02-08 18:27:14 +08:00
var tables = GetTableInfoList(isCache);
2017-06-12 14:02:58 +08:00
if (tables == null) return false;
else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
2017-06-07 13:33:24 +08:00
}
2017-06-17 23:08:50 +08:00
public virtual bool IsAnyColumn(string tableName, string columnName)
2017-06-07 13:33:24 +08:00
{
2018-04-04 13:45:54 +08:00
columnName = this.SqlBuilder.GetNoTranslationColumnName(columnName);
tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
2017-06-12 14:02:58 +08:00
var isAny = IsAnyTable(tableName);
Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
var columns = GetColumnInfosByTableName(tableName);
if (columns.IsNullOrEmpty()) return false;
return columns.Any(it => it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
2017-06-07 13:33:24 +08:00
}
2017-06-17 23:08:50 +08:00
public virtual bool IsPrimaryKey(string tableName, string columnName)
2017-06-07 13:33:24 +08:00
{
2018-04-04 13:45:54 +08:00
columnName = this.SqlBuilder.GetNoTranslationColumnName(columnName);
2017-06-12 14:02:58 +08:00
var isAny = IsAnyTable(tableName);
Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
var columns = GetColumnInfosByTableName(tableName);
if (columns.IsNullOrEmpty()) return false;
return columns.Any(it => it.IsPrimarykey = true && it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
2017-06-07 13:33:24 +08:00
}
2017-06-17 23:08:50 +08:00
public virtual bool IsIdentity(string tableName, string columnName)
2017-06-07 13:33:24 +08:00
{
2018-04-04 13:45:54 +08:00
columnName = this.SqlBuilder.GetNoTranslationColumnName(columnName);
2017-06-12 14:02:58 +08:00
var isAny = IsAnyTable(tableName);
Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
var columns = GetColumnInfosByTableName(tableName);
if (columns.IsNullOrEmpty()) return false;
return columns.Any(it => it.IsIdentity = true && it.DbColumnName.Equals(columnName, StringComparison.CurrentCultureIgnoreCase));
2017-06-07 13:33:24 +08:00
}
2017-06-18 05:29:42 +08:00
public virtual bool IsAnyConstraint(string constraintName)
{
return this.Context.Ado.GetInt("select object_id('" + constraintName + "')") > 0;
}
public virtual bool IsAnySystemTablePermissions()
{
2017-07-18 10:36:43 +08:00
this.Context.Ado.CheckConnection();
string sql = this.CheckSystemTablePermissionsSql;
try
{
2017-07-09 15:30:17 +08:00
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
this.Context.Ado.IsEnableLogEvent = false;
this.Context.Ado.ExecuteCommand(sql);
2017-07-09 15:30:17 +08:00
this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
return true;
}
catch
{
return false;
}
}
2017-06-07 13:33:24 +08:00
#endregion
2017-06-05 20:49:02 +08:00
#region DDL
2017-06-17 23:08:50 +08:00
public virtual bool AddPrimaryKey(string tableName, string columnName)
2017-01-07 21:54:51 +08:00
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
2017-07-19 00:31:27 +08:00
columnName = this.SqlBuilder.GetTranslationTableName(columnName);
string sql = string.Format(this.AddPrimaryKeySql, tableName, string.Format("PK_{0}_{1}", this.SqlBuilder.GetNoTranslationColumnName(tableName), this.SqlBuilder.GetNoTranslationColumnName(columnName)), columnName);
2017-06-17 23:08:50 +08:00
this.Context.Ado.ExecuteCommand(sql);
return true;
}
2017-06-17 23:50:28 +08:00
public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
2017-06-17 23:08:50 +08:00
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string sql = GetAddColumnSql(tableName, columnInfo);
2017-06-17 21:15:58 +08:00
this.Context.Ado.ExecuteCommand(sql);
return true;
2017-01-07 21:54:51 +08:00
}
2017-06-18 04:15:09 +08:00
public virtual bool UpdateColumn(string tableName, DbColumnInfo column)
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
2017-06-18 04:15:09 +08:00
string sql = GetUpdateColumnSql(tableName, column);
2017-06-17 23:50:28 +08:00
this.Context.Ado.ExecuteCommand(sql);
return true;
}
2018-02-08 17:44:23 +08:00
public abstract bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true);
2017-06-17 23:08:50 +08:00
public virtual bool DropTable(string tableName)
2017-06-17 21:15:58 +08:00
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
2017-07-19 00:31:27 +08:00
this.Context.Ado.ExecuteCommand(string.Format(this.DropTableSql, tableName));
2017-06-17 20:28:15 +08:00
return true;
}
2017-06-17 23:08:50 +08:00
public virtual bool DropColumn(string tableName, string columnName)
{
2017-06-25 19:16:10 +08:00
columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
2017-06-17 23:08:50 +08:00
this.Context.Ado.ExecuteCommand(string.Format(this.DropColumnToTableSql, tableName, columnName));
return true;
}
public virtual bool DropConstraint(string tableName, string constraintName)
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string sql = string.Format(this.DropConstraintSql, tableName, constraintName);
2017-06-17 23:08:50 +08:00
this.Context.Ado.ExecuteCommand(sql);
2017-06-17 21:45:17 +08:00
return true;
}
2017-01-07 21:54:51 +08:00
public virtual bool TruncateTable(string tableName)
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
this.Context.Ado.ExecuteCommand(string.Format(this.TruncateTableSql, tableName));
2017-01-07 21:54:51 +08:00
return true;
}
2017-06-17 23:08:50 +08:00
public virtual bool BackupDataBase(string databaseName, string fullFileName)
2017-06-05 20:49:02 +08:00
{
2017-06-17 21:15:58 +08:00
var directory = FileHelper.GetDirectoryFromFilePath(fullFileName);
if (!FileHelper.IsExistDirectory(directory))
{
2017-06-17 20:28:15 +08:00
FileHelper.CreateDirectory(directory);
}
this.Context.Ado.ExecuteCommand(string.Format(this.BackupDataBaseSql, databaseName, fullFileName));
return true;
2017-06-07 13:33:24 +08:00
}
2017-06-18 04:15:09 +08:00
public virtual bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue)
2017-06-17 23:08:50 +08:00
{
2017-06-25 19:16:10 +08:00
oldTableName = this.SqlBuilder.GetTranslationTableName(oldTableName);
newTableName = this.SqlBuilder.GetTranslationTableName(newTableName);
2017-07-19 00:31:27 +08:00
string sql = string.Format(this.BackupTableSql, newTableName, oldTableName, maxBackupDataRows);
2017-06-17 23:08:50 +08:00
this.Context.Ado.ExecuteCommand(sql);
return true;
}
2017-06-18 04:15:09 +08:00
public virtual bool RenameColumn(string tableName, string oldColumnName, string newColumnName)
{
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
oldColumnName = this.SqlBuilder.GetTranslationColumnName(oldColumnName);
newColumnName = this.SqlBuilder.GetTranslationColumnName(newColumnName);
2017-06-18 04:15:09 +08:00
string sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName);
this.Context.Ado.ExecuteCommand(sql);
return true;
}
2017-06-05 20:49:02 +08:00
#endregion
2017-01-07 21:54:51 +08:00
#region Private
private List<T> GetListOrCache<T>(string cacheKey, string sql)
{
2017-09-28 16:21:46 +08:00
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
2017-09-28 15:50:14 +08:00
() =>
2017-01-07 21:54:51 +08:00
{
2017-05-19 11:20:07 +08:00
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
this.Context.Ado.IsEnableLogEvent = false;
2018-01-31 16:41:30 +08:00
var result = this.Context.Ado.SqlQuery<T>(sql);
2017-05-19 11:20:07 +08:00
this.Context.Ado.IsEnableLogEvent = isEnableLogEvent;
2018-01-31 16:41:30 +08:00
return result;
2017-01-07 21:54:51 +08:00
});
}
2017-06-24 02:15:35 +08:00
protected virtual string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
2017-06-17 21:45:17 +08:00
{
List<string> columnArray = new List<string>();
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
foreach (var item in columns)
{
2017-07-19 00:31:27 +08:00
string columnName = this.SqlBuilder.GetTranslationTableName(item.DbColumnName);
2017-06-17 21:45:17 +08:00
string dataType = item.DataType;
2017-08-26 01:57:24 +08:00
string dataSize = GetSize(item);
2017-06-17 21:45:17 +08:00
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
2017-06-18 05:46:02 +08:00
string primaryKey = null;
2017-06-17 21:45:17 +08:00
string identity = item.IsIdentity ? this.CreateTableIdentity : null;
2017-07-19 00:31:27 +08:00
string addItem = string.Format(this.CreateTableColumn, columnName, dataType, dataSize, nullType, primaryKey, identity);
2017-06-17 21:45:17 +08:00
columnArray.Add(addItem);
}
2017-07-19 00:31:27 +08:00
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
2017-06-17 21:45:17 +08:00
return tableString;
}
2017-06-24 02:15:35 +08:00
protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo)
2017-06-17 21:45:17 +08:00
{
2017-07-19 00:31:27 +08:00
string columnName = this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName);
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
2017-06-17 21:45:17 +08:00
string dataType = columnInfo.DataType;
2017-08-26 01:57:24 +08:00
string dataSize = GetSize(columnInfo);
2017-06-17 21:45:17 +08:00
string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull;
2017-06-18 05:29:42 +08:00
string primaryKey = null;
string identity = null;
2017-06-17 21:45:17 +08:00
string result = string.Format(this.AddColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
return result;
}
2017-06-24 02:15:35 +08:00
protected virtual string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo)
2017-06-17 23:50:28 +08:00
{
2017-07-19 00:31:27 +08:00
string columnName = this.SqlBuilder.GetTranslationTableName(columnInfo.DbColumnName);
2017-06-25 19:16:10 +08:00
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
2017-06-17 23:50:28 +08:00
string dataType = columnInfo.DataType;
2017-08-26 01:57:24 +08:00
string dataSize = GetSize(columnInfo);
2017-06-17 23:50:28 +08:00
string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull;
2017-06-18 04:15:09 +08:00
string primaryKey = null;
string identity = null;
2017-06-17 23:50:28 +08:00
string result = string.Format(this.AlterColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
return result;
}
2017-07-19 00:31:27 +08:00
protected virtual string GetCacheKey(string cacheKey)
{
2017-08-26 01:57:24 +08:00
return this.Context.CurrentConnectionConfig.DbType + "." + this.Context.Ado.Connection.Database + "." + cacheKey;
2017-07-19 00:31:27 +08:00
}
2017-08-26 02:05:23 +08:00
protected virtual string GetSize(DbColumnInfo item)
{
string dataSize = null;
var isMax = item.Length > 4000 || item.Length == -1;
if (isMax)
{
dataSize = item.Length > 0 ? string.Format("({0})", "max") : null;
}
else if (item.Length > 0 && item.DecimalDigits == 0)
{
dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
}
else if (item.Length > 0 && item.DecimalDigits > 0)
{
dataSize = item.Length > 0 ? string.Format("({0},{1})", item.Length, item.DecimalDigits) : null;
}
return dataSize;
}
2017-01-07 21:54:51 +08:00
#endregion
}
}