using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Data.OleDb;
using System.Data;
using System.Data.SQLite;

namespace WhiteMacro.Common
{
    public class Sql
    {
        // ─────────────────────────────────────────────────
        // ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        // ■■■ メンバー定数 ■■■
        // ─────────────────────────────────────────────────
        public const string Any = "%";
        public const string WhereMnKey = "{0}='{1}'";
        // ─────────────────────────────────────────────────
        // ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
        // ■■■ PUBLIC ■■■
        // ─────────────────────────────────────────────────
        // ■ トランザクション実行
        // ─────────────────────────────────────────────────
        public static bool TransactionRun(Queue<string> pSql)
        {
            bool ret = true;

            using (SQLiteConnection conn = new SQLiteConnection(ComCst.DataBase.conStr))
            {
                SQLiteCommand command = conn.CreateCommand();

                conn.Open();

                SQLiteTransaction transaction = conn.BeginTransaction();

                command.Transaction = transaction;

                try
                {
                    while (pSql.Count > 0)
                    {
                        command.CommandText = pSql.Dequeue();
                        command.ExecuteNonQuery();
                    }

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    ret = false;
#if DEBUG
                    ComFnc.ExcepMsg(ex);
#endif
                }
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ 文字列変換(文字列→文字列)
        // ─────────────────────────────────────────────────
        public static string ValToStr(string pVal)
        {
            string ret = pVal;

            if (ret == null)
            {
                ret = "Null";
            }
            else
            {
                if (ret.Contains("'") == true)
                {
                    ret = ret.Replace("'", "''");
                }

                if (ret.Contains("\"") == true)
                {
                    ret = ret.Replace("\"", "\"\"");
                }

                ret = "'" + ret + "'";
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ 数値変換(数値:整数→文字列型の数値)
        // ─────────────────────────────────────────────────
        public static string ValToNum(int pVal)
        {
            string ret = null;

            ret = pVal.ToString().Trim();

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ 数値変換(数値:倍精度整数→文字列型の数値)
        // ─────────────────────────────────────────────────
        public static string ValToNum(long pVal)
        {
            string ret = null;

            ret = pVal.ToString().Trim();

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ Yes・No変換(bool型→Yes・No)
        // ─────────────────────────────────────────────────
        public static string ValToBol(bool pVal)
        {
            string ret = null;

            if (pVal == true)
            {
                ret = "True";
            }
            else
            {
                ret = "False";
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ 日付/時刻 変換(日付/時刻→日付/時刻)
        // ─────────────────────────────────────────────────
        public static string ValToDTm(DateTime? pVal, MakeSqlFormat.DTmKind pKind)
        {
            string ret = null;
            string format = null;

            if (pVal == null)
            {
                ret = "Null";
            }
            else
            {
                switch (pKind)
                {
                    case MakeSqlFormat.DTmKind.Date:
                        format = "#{0:yyyy/MM/dd}#";
                        break;
                    case MakeSqlFormat.DTmKind.Time:
                        format = "#{0:hh:mm:ss}#";
                        break;
                    case MakeSqlFormat.DTmKind.DateTime:
                        format = "#{0:yyyy/MM/dd hh:mm:ss}#";
                        break;
                    default:
                        break;
                }

                ret = string.Format(format, pVal);
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ BOOL → Yes/No型 変換
        // ─────────────────────────────────────────────────
        public static int? SetSqlYesNo(bool pVal)
        {
            int? ret = null;

            ret = ((pVal == true) ? (-1) : (0));

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ SQL句追加
        // ─────────────────────────────────────────────────
        public static string SetSqlCmd(string pWhere, string pGroupBy, string pOrderBy)
        {
            string ret = null;

            string where = ((pWhere != "" ? " WHERE " + pWhere : ""));
            string groupBy = ((pGroupBy != "" ? " GROUP BY " + pGroupBy : ""));
            string orderBy = ((pOrderBy != "" ? " ORDER BY " + pOrderBy : ""));

            ret = where + groupBy + orderBy;

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ 括り除去
        // ─────────────────────────────────────────────────
        public static string ExceptKnot(string pItemName)
        {
            string itemName = pItemName.Trim();

            itemName = itemName.Replace("[", "").Trim();
            itemName = itemName.Replace("]", "").Trim();
            itemName = itemName.Trim();

            return itemName;
        }
        // ─────────────────────────────────────────────────
        // ◆ NULL?
        // ─────────────────────────────────────────────────
        public static bool IsNull(string pVal)
        {
            bool ret = false;

            string strValTrim = pVal.Trim();

            if (strValTrim.CompareTo("Null") == 0 || 
                strValTrim.CompareTo("NULL") == 0)
            {
                ret = true;
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ NULL? or ""
        // ─────────────────────────────────────────────────
        public static bool IsNullOrEmpty(string pVal)
        {
            bool ret = false;

            string strValTrim = pVal.Trim();

            if (strValTrim.CompareTo("Null") == 0 ||
                strValTrim.CompareTo("NULL") == 0 ||
                strValTrim.Length == 0)
            {
                ret = true;
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ◆ Not NULL? or ""
        // ─────────────────────────────────────────────────
        public static bool NotIsNullOrEmpty(string pVal)
        {
            return !Sql.IsNullOrEmpty(pVal);
        }
        // ─────────────────────────────────────────────────
        // ◆ RetValToStr
        // ─────────────────────────────────────────────────
        public static string RetValToStr(string pVal)
        {
            string ret = null;

            if (pVal == null)
            {
                ret = "";
            }
            if (Sql.IsNullOrEmpty(pVal))
            {
                ret = "";
            }
            else
            {
                ret = pVal.Trim();
            }

            return ret;
        }
        // ─────────────────────────────────────────────────
        // ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    }
}