ホテル管理システム
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using HotelPms.Share.Data;
using HotelPms.Share.IO;
using HotelPms.Share.Util;
using HotelPms.VerUp.Util;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Text.Json;
 
namespace HotelPms.VerUp.DataBase
{
    public class DBCore : IDisposable
    {
        /// <summary>
        /// 通知イベント
        /// </summary>
        public event MessageEventHandler Output;
 
        /// <summary>
        /// 設定ファイル
        /// </summary>
        public string ConfigFile { get; set; } = AppDomain.CurrentDomain.BaseDirectory + @"Sql\Config.json";
 
        /// <summary>
        /// DB接続情報
        /// </summary>
        public DBConnectItem? DBConnectItem { get; set; }
 
        /// <summary>
        /// エラー発生時続くかどうか
        /// </summary>
        public bool IgnoreError { get; set; } = false;  
 
        /// <summary>
        /// 設定
        /// </summary>
        private Config? m_Config;
        private string? rootPath = string.Empty;
 
        public DBCore(DBConnectItem connectItem) :this(connectItem, string.Empty)
        {            
        }
 
        public DBCore(DBConnectItem connectItem, string config)
        {
            DBConnectItem = connectItem;
            if(config.Length > 0) { ConfigFile = config; }
        }
 
        public void Dispose()
        {
         
        }
 
        /// <summary>
        /// 実行
        /// </summary>
        /// <returns></returns>
        public bool Execute()
        {
            try
            {
                if(!ReadConfig()) { return false; }
 
                int appVer = GetAppVersion();
                rootPath = Path.GetDirectoryName(ConfigFile);
 
                using (MsSqlNet access = new MsSqlNet(DBConnectItem))
                {
                    foreach (ConfigData item in m_Config.Data)
                    {
                        RaiseEvent($"Name:{item.Name},Type:{item.Type}");
 
                        // Sql特定
                        string sql = GetSql(item);
                        if (sql.Length == 0) { if (IgnoreError) { continue; } else { return false; } }
                        if (item.Type == (int)ConfigData.DataType.ExeReaderResult)
                        {
                            string ret = CConvert.ToString(access.ExecuteScalar(sql));
                            if(ret.Length > 0) { sql = ret; } else { continue; }
                        }
 
                        // 実行
                        if (access.ExecuteNonQuery(sql) == -1)
                        {
                            RaiseEvent($"{access.ErrNo}.{access.ErrInfo}");
                            if (!IgnoreError) { return false; }
                        }
                    }
                }
                return true;
            }
            catch(Exception ex)
            {
                RaiseEvent(ex.Message);
                return false;
            }
        }
 
        private string GetSql(ConfigData item)
        {
            try
            {
                if (item.Type == (int)ConfigData.DataType.ExeCommand || item.Type == (int)ConfigData.DataType.ExeReaderResult)
                {
                    return item.Name;
                }
                else
                {
                    string file = Path.Combine(rootPath, item.Name);
                    if (!File.Exists(file)) { return string.Empty; }
 
                    StringBuilder sql = new StringBuilder();
                    string name = Path.GetFileNameWithoutExtension(item.Name);
                    if (item.Type == (int)ConfigData.DataType.View)
                    {
                        sql.AppendLine($"IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[{name}]')) DROP View [dbo].[{name}];");
                    }
                    else if (item.Type == (int)ConfigData.DataType.Fuction)
                    {
                        sql.AppendLine($"IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{name}]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION [dbo].[{name}];");
                    }
                    else if (item.Type == (int)ConfigData.DataType.Procedure)
                    {
                        sql.AppendLine($"IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{name}]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[{name}];");
                    }
                    sql.Append(File.ReadAllText(file, Encoding.UTF8));
                    return sql.ToString();
                }
            }
            catch(Exception ex)
            {
                RaiseEvent(ex.Message);
                return string.Empty;
            }
        }
 
        /// <summary>
        /// イベント通知
        /// </summary>
        /// <param name="msg"></param>
        private void RaiseEvent(string msg)
        {
            if(Output != null) { Output(this, new MessageEventArgs(msg)); }
            OperationLog.Instance.WriteLog(msg);
        }
 
        /// <summary>
        /// ConfigFileを読込
        /// </summary>
        /// <returns></returns>
        private bool ReadConfig()
        {
            try
            {
                if (!File.Exists(ConfigFile)) 
                {
                    RaiseEvent($"設定ファイルが存在しません。{Environment.NewLine}{ConfigFile}");
                    return false; 
                }
                string json = File.ReadAllText(ConfigFile, Encoding.UTF8);
                m_Config = JsonSerializer.Deserialize<Config>(json);                
                return true;
            }
            catch (Exception ex)
            {
                RaiseEvent(ex.Message);
                return false;
            }
        }
 
        private int GetDbVersion(MsSqlNet access)
        {
            try
            {
                return CConvert.ToInt(access.ExecuteScalar("SELECT TOP 1 Version FROM S_Config"));
            }
            catch
            {
                return 0;
            }
        }
 
        /// <summary>
        /// Exeのバージョン番号を取得する
        /// </summary>
        /// <returns></returns>
        private int GetAppVersion()
        {
            string verInfo = string.Empty;
            string[] ver = Assembly.GetEntryAssembly().GetName().Version.ToString().Split(new char[] { '.' });
            verInfo = ver[0] + ver[1] + ver[2].PadLeft(3, '0') + ver[3].PadLeft(3, '0');
            RaiseEvent(verInfo);
            return CConvert.ToInt(verInfo);
        }
    }
}