mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-29 09:54:44 +08:00
-
This commit is contained in:
@@ -8,62 +8,75 @@ namespace SqlSugar
|
|||||||
public abstract partial class DbMaintenanceProvider : IDbMaintenance
|
public abstract partial class DbMaintenanceProvider : IDbMaintenance
|
||||||
{
|
{
|
||||||
#region DML
|
#region DML
|
||||||
public virtual List<DbTableInfo> GetViewInfoList()
|
public virtual List<DbTableInfo> GetViewInfoList(bool isCache = true)
|
||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetViewInfoList";
|
string cacheKey = "DbMaintenanceProvider.GetViewInfoList";
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
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)
|
foreach (var item in result)
|
||||||
{
|
{
|
||||||
item.DbObjectType = DbObjectType.View;
|
item.DbObjectType = DbObjectType.View;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public virtual List<DbTableInfo> GetTableInfoList()
|
public virtual List<DbTableInfo> GetTableInfoList(bool isCache = true)
|
||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetTableInfoList";
|
string cacheKey = "DbMaintenanceProvider.GetTableInfoList";
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
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)
|
foreach (var item in result)
|
||||||
{
|
{
|
||||||
item.DbObjectType = DbObjectType.Table;
|
item.DbObjectType = DbObjectType.Table;
|
||||||
}
|
}
|
||||||
return result;
|
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>();
|
if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>();
|
||||||
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
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)
|
public virtual List<string> GetIsIdentities(string tableName)
|
||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
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();
|
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsIdentity).ToList();
|
||||||
return result.Select(it => it.DbColumnName).ToList();
|
return result.Select(it => it.DbColumnName).ToList();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
public virtual List<string> GetPrimaries(string tableName)
|
public virtual List<string> GetPrimaries(string tableName)
|
||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
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();
|
var result = GetColumnInfosByTableName(tableName).Where(it => it.IsPrimarykey).ToList();
|
||||||
return result.Select(it => it.DbColumnName).ToList();
|
return result.Select(it => it.DbColumnName).ToList();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Check
|
#region Check
|
||||||
public virtual bool IsAnyTable(string tableName)
|
public virtual bool IsAnyTable(string tableName, bool isCache = true)
|
||||||
{
|
{
|
||||||
tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
|
tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
|
||||||
var tables = GetTableInfoList();
|
var tables = GetTableInfoList(isCache);
|
||||||
if (tables == null) return false;
|
if (tables == null) return false;
|
||||||
else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
|
else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,15 @@ namespace SqlSugar
|
|||||||
SqlSugarClient Context { get; set; }
|
SqlSugarClient Context { get; set; }
|
||||||
|
|
||||||
#region DML
|
#region DML
|
||||||
List<DbTableInfo> GetViewInfoList();
|
List<DbTableInfo> GetViewInfoList(bool isCache=true);
|
||||||
List<DbTableInfo> GetTableInfoList();
|
List<DbTableInfo> GetTableInfoList(bool isCache=true);
|
||||||
List<DbColumnInfo> GetColumnInfosByTableName(string tableName);
|
List<DbColumnInfo> GetColumnInfosByTableName(string tableName,bool isCache=true);
|
||||||
List<string> GetIsIdentities(string tableName);
|
List<string> GetIsIdentities(string tableName);
|
||||||
List<string> GetPrimaries(string tableName);
|
List<string> GetPrimaries(string tableName);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Check
|
#region Check
|
||||||
bool IsAnyTable(string tableName);
|
bool IsAnyTable(string tableName, bool isCache = true);
|
||||||
bool IsAnyColumn(string tableName, string column);
|
bool IsAnyColumn(string tableName, string column);
|
||||||
bool IsPrimaryKey(string tableName, string column);
|
bool IsPrimaryKey(string tableName, string column);
|
||||||
bool IsIdentity(string tableName, string column);
|
bool IsIdentity(string tableName, string column);
|
||||||
|
|||||||
@@ -172,43 +172,52 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#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();
|
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
|
if (!isCache)
|
||||||
|
return GetColumnInfosByTableName(tableName);
|
||||||
|
else
|
||||||
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
||||||
() =>
|
() =>
|
||||||
{
|
{
|
||||||
string sql = "select * from " + tableName + " WHERE 1=2 ";
|
return GetColumnInfosByTableName(tableName);
|
||||||
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<DbColumnInfo> result = new List<DbColumnInfo>();
|
|
||||||
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<DbColumnInfo> 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<DbColumnInfo> result = new List<DbColumnInfo>();
|
||||||
|
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<string> GetPrimaryKeyByTableNames(string tableName)
|
private List<string> GetPrimaryKeyByTableNames(string tableName)
|
||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetPrimaryKeyByTableNames." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
|
|||||||
@@ -164,41 +164,50 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#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();
|
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
cacheKey = GetCacheKey(cacheKey);
|
cacheKey = GetCacheKey(cacheKey);
|
||||||
|
if (!isCache)
|
||||||
|
{
|
||||||
|
return GetColumnInfosByTableName(tableName);
|
||||||
|
}
|
||||||
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
||||||
() =>
|
() =>
|
||||||
{
|
{
|
||||||
string sql = "select * from " + tableName + " limit 0,1";
|
return GetColumnsByTableName(tableName);
|
||||||
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<DbColumnInfo> result = new List<DbColumnInfo>();
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<DbColumnInfo> 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<DbColumnInfo> result = new List<DbColumnInfo>();
|
||||||
|
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<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
|
public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
|
||||||
@@ -219,8 +228,9 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
string sql = GetCreateTableSql(tableName, columns);
|
string sql = GetCreateTableSql(tableName, columns);
|
||||||
if (!isCreatePrimaryKey) {
|
if (!isCreatePrimaryKey)
|
||||||
sql = sql.Replace("PRIMARY KEY AUTOINCREMENT","").Replace("PRIMARY KEY", "");
|
{
|
||||||
|
sql = sql.Replace("PRIMARY KEY AUTOINCREMENT", "").Replace("PRIMARY KEY", "");
|
||||||
}
|
}
|
||||||
this.Context.Ado.ExecuteCommand(sql);
|
this.Context.Ado.ExecuteCommand(sql);
|
||||||
return true;
|
return true;
|
||||||
@@ -239,7 +249,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
||||||
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
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 identity = item.IsIdentity ? this.CreateTableIdentity : null;
|
||||||
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
|
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
|
||||||
columnArray.Add(addItem);
|
columnArray.Add(addItem);
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user