You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

200 lines
6.3 KiB

namespace SqlSugar
{
public class SqlServerDbMaintenance : DbMaintenanceProvider
{
#region DML
protected override string GetColumnInfosByTableNameSql
{
get
{
var sql = @"SELECT sysobjects.name AS TableName,
syscolumns.Id AS TableId,
syscolumns.name AS DbColumnName,
systypes.name AS DataType,
syscolumns.length AS [Length],
sys.extended_properties.[value] AS [ColumnDescription],
syscomments.text AS DefaultValue,
syscolumns.isnullable AS IsNullable,
columnproperty(syscolumns.id,syscolumns.name,'IsIdentity')as IsIdentity,
(CASE
WHEN EXISTS
(
select 1
from sysindexes i
join sysindexkeys k on i.id = k.id and i.indid = k.indid
join sysobjects o on i.id = o.id
join syscolumns c on i.id=c.id and k.colid = c.colid
where o.xtype = 'U'
and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)
and o.name=sysobjects.name and c.name=syscolumns.name
) THEN 1
ELSE 0
END) AS IsPrimaryKey
FROM syscolumns
INNER JOIN systypes ON syscolumns.xtype = systypes.xtype
LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id
LEFT OUTER JOIN sys.extended_properties ON (sys.extended_properties.minor_id = syscolumns.colid
AND sys.extended_properties.major_id = syscolumns.id)
LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
WHERE syscolumns.id IN
(SELECT id
FROM sysobjects
WHERE xtype IN('u',
'v') )
AND (systypes.name <> 'sysname')
AND sysobjects.name='{0}'
AND systypes.name<>'geometry'
AND systypes.name<>'geography'
ORDER BY syscolumns.colid";
return sql;
}
}
protected override string GetTableInfoListSql
{
get
{
return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
FROM sysobjects s
LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('U') ";
}
}
protected override string GetViewInfoListSql
{
get
{
return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
FROM sysobjects s
LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('V') ";
}
}
#endregion
#region DDL
protected override string AddPrimaryKeySql
{
get
{
return "ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY({2})";
}
}
protected override string AddColumnToTableSql
{
get
{
return "ALTER TABLE {0} ADD {1} {2}{3} {4} {5} {6}";
}
}
protected override string AlterColumnToTableSql
{
get
{
return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
}
}
protected override string BackupDataBaseSql
{
get
{
return @"USE master;BACKUP DATABASE {0} TO disk = '{1}'";
}
}
protected override string CreateTableSql
{
get
{
return "CREATE TABLE {0}(\r\n{1})";
}
}
protected override string CreateTableColumn
{
get
{
return "{0} {1}{2} {3} {4} {5}";
}
}
protected override string TruncateTableSql
{
get
{
return "TRUNCATE TABLE {0}";
}
}
protected override string BackupTableSql
{
get
{
return "SELECT TOP {0} * INTO {1} FROM {2}";
}
}
protected override string DropTableSql
{
get
{
return "DROP TABLE {0}";
}
}
protected override string DropColumnToTableSql
{
get
{
return "ALTER TABLE {0} DROP COLUMN {1}";
}
}
protected override string DropConstraintSql
{
get
{
return "ALTER TABLE {0} DROP CONSTRAINT {1}";
}
}
protected override string RenameColumnSql
{
get
{
return "exec sp_rename '{0}.{1}','{2}','column';";
}
}
#endregion
#region Check
protected override string CheckSystemTablePermissionsSql
{
get
{
return "select top 1 id from sysobjects";
}
}
#endregion
#region Scattered
protected override string CreateTableNull
{
get
{
return "NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "IDENTITY(1,1)";
}
}
#endregion
}
}