ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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();
    }
}