From 1a1c8e71fcd14858f595029f089b2d4a00202b32 Mon Sep 17 00:00:00 2001
From: ogi <Administrator@S-OGI-PC>
Date: Fri, 05 Dec 2025 09:24:16 +0900
Subject: [PATCH] プロジェクトファイルを追加。

---
 HotelPms.VerUp/DataBase/DBCore.cs |  197 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 197 insertions(+), 0 deletions(-)

diff --git a/HotelPms.VerUp/DataBase/DBCore.cs b/HotelPms.VerUp/DataBase/DBCore.cs
new file mode 100644
index 0000000..d85049a
--- /dev/null
+++ b/HotelPms.VerUp/DataBase/DBCore.cs
@@ -0,0 +1,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);
+        }
+    }
+}

--
Gitblit v1.10.0