using System.Text;
|
|
namespace HotelPms.Share.Data.Script;
|
|
public class DataBaseCreator
|
{
|
/// <summary>
|
/// パーティション作成用SQL
|
/// 数字型(10件) + 日付型(10件)
|
/// </summary>
|
/// <param name="dbPath"></param>
|
/// <param name="dbName"></param>
|
/// <param name="beginDate"></param>
|
/// <returns></returns>
|
public static string CreatePartitionScript(string dbPath, string dbName, DateTime beginDate)
|
{
|
if(!dbPath.EndsWith(Path.DirectorySeparatorChar)){ dbPath += Path.DirectorySeparatorChar; }
|
StringBuilder sql = new StringBuilder();
|
|
//数字型
|
for (int i = 0; i < 10; i++)
|
{
|
sql.AppendLine($"ALTER DATABASE {dbName} ADD FILEGROUP {dbName}_IntFileGroup{(i + 1).ToString().PadLeft(2, '0')}");
|
}
|
|
for (int i = 0; i < 10; i++)
|
{
|
string no = (i + 1).ToString().PadLeft(2, '0');
|
sql.AppendLine($@"ALTER DATABASE {dbName} ADD FILE (NAME=N'{dbName}_IntFileGroup{no}',FILENAME=N'{dbPath}{dbName}_IntFileGroup{no}.ndf',size=10Mb,filegrowth=5mb) TO FILEGROUP {dbName}_IntFileGroup{no}");
|
}
|
|
sql.Append("CREATE PARTITION FUNCTION [IntPartitionFun](int) AS RANGE RIGHT FOR VALUES (1000000");
|
for (int i = 2; i <= 9; i++)
|
{
|
sql.Append($", {i}000000");
|
}
|
sql.AppendLine(")");
|
|
sql.Append($"CREATE PARTITION SCHEME [IntPartitionSchema] AS PARTITION [IntPartitionFun] TO ([{dbName}_IntFileGroup01]");
|
for (int i = 2; i <= 10; i++)
|
{
|
sql.Append($", [{dbName}_IntFileGroup{i.ToString().PadLeft(2, '0')}]");
|
}
|
sql.AppendLine(")");
|
sql.AppendLine("");
|
|
//日付型
|
for (int i = 0; i < 10; i++)
|
{
|
sql.AppendLine($"ALTER DATABASE {dbName} ADD FILEGROUP {dbName}_DateFileGroup{(i + 1).ToString().PadLeft(2, '0')}");
|
}
|
|
for (int i = 0; i < 10; i++)
|
{
|
string no = (i + 1).ToString().PadLeft(2, '0');
|
sql.AppendLine($@"ALTER DATABASE {dbName} ADD FILE (NAME=N'{dbName}_DateFileGroup{no}',FILENAME=N'{dbPath}{dbName}_DateFileGroup{no}.ndf',size=10Mb,filegrowth=5mb) TO FILEGROUP {dbName}_DateFileGroup{no}");
|
}
|
|
int curYear = beginDate.Year;
|
sql.Append($"CREATE PARTITION FUNCTION [DatePartitionFun](Date) AS RANGE RIGHT FOR VALUES ('{curYear}/01/01'");
|
for (int i = 1; i <= 8; i++)
|
{
|
sql.Append($", '{curYear + i}/01/01'");
|
}
|
sql.AppendLine(")");
|
|
sql.Append($"CREATE PARTITION SCHEME [DatePartitionSchema] AS PARTITION [DatePartitionFun] TO ([{dbName}_DateFileGroup01]");
|
for (int i = 2; i <= 10; i++)
|
{
|
sql.Append($", [{dbName}_DateFileGroup{i.ToString().PadLeft(2, '0')}]");
|
}
|
sql.AppendLine(")");
|
|
return sql.ToString();
|
}
|
}
|