209 lines
8.6 KiB
C#
209 lines
8.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Adaptation.Si
|
|
{
|
|
|
|
public class SQLDatabase
|
|
{
|
|
|
|
private static string _ConnectionStringIrmnSpc;
|
|
private static string _ConnectionStringG4Wafers;
|
|
|
|
public static void SetConnectionStringIrmnSpc(string connectionString)
|
|
{
|
|
_ConnectionStringIrmnSpc = connectionString;
|
|
}
|
|
|
|
public static void SetConnectionStringG4Wafers(string connectionString)
|
|
{
|
|
_ConnectionStringG4Wafers = connectionString;
|
|
}
|
|
|
|
public static Dictionary<string, List<object>> ExecuteReader(StringBuilder rawSql, List<Tuple<string, string>> parameters, List<Tuple<string, int>> columns, bool isG4Wafers, bool isIrmnSpc)
|
|
{
|
|
Dictionary<string, List<object>> results = new Dictionary<string, List<object>>();
|
|
foreach (var item in parameters)
|
|
rawSql.Replace(item.Item1, item.Item2);
|
|
string sql = rawSql.ToString();
|
|
if (sql.Contains("@"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
if (sql.Contains("<"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
if (sql.Contains(">"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
if (sql.Contains("!"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
foreach (var item in columns)
|
|
results.Add(item.Item1, new List<object>());
|
|
string connectionString;
|
|
if (isG4Wafers && isIrmnSpc)
|
|
throw new Exception();
|
|
else if (isG4Wafers)
|
|
connectionString = _ConnectionStringG4Wafers;
|
|
else if (isIrmnSpc)
|
|
connectionString = _ConnectionStringIrmnSpc;
|
|
else
|
|
throw new Exception();
|
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
using (SqlCommand sqlComm = new SqlCommand(sql.ToString(), conn))
|
|
{
|
|
using (SqlDataReader sqlReader = sqlComm.ExecuteReader())
|
|
{
|
|
while (sqlReader.Read())
|
|
{
|
|
foreach (var item in columns)
|
|
results[item.Item1].Add(sqlReader.GetSqlValue(item.Item2));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
public static Dictionary<int, List<object>> ExecuteReader(StringBuilder rawSql, List<Tuple<string, string>> parameters, int columns, bool isG4Wafers, bool isIrmnSpc)
|
|
{
|
|
Dictionary<int, List<object>> results = new Dictionary<int, List<object>>();
|
|
foreach (var item in parameters)
|
|
rawSql.Replace(item.Item1, item.Item2);
|
|
string sql = rawSql.ToString();
|
|
if (sql.Contains("@"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
if (sql.Contains("{"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
if (sql.Contains("}"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
if (sql.Contains("!"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
string connectionString;
|
|
if (isG4Wafers && isIrmnSpc)
|
|
throw new Exception();
|
|
else if (isG4Wafers)
|
|
connectionString = _ConnectionStringG4Wafers;
|
|
else if (isIrmnSpc)
|
|
connectionString = _ConnectionStringIrmnSpc;
|
|
else
|
|
throw new Exception();
|
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
using (SqlCommand sqlComm = new SqlCommand(sql.ToString(), conn))
|
|
{
|
|
using (SqlDataReader sqlReader = sqlComm.ExecuteReader())
|
|
{
|
|
while (sqlReader.Read())
|
|
{
|
|
for (int i = 0; i < columns; i++)
|
|
{
|
|
if (!results.ContainsKey(i))
|
|
results.Add(i, new List<object>());
|
|
results[i].Add(sqlReader.GetSqlValue(i));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static int? CheckDisableHistory(string sid)
|
|
{
|
|
int? result = null;
|
|
if (sid != "1")
|
|
{
|
|
object exits;
|
|
StringBuilder sql = new StringBuilder();
|
|
sql.Append("SELECT ");
|
|
sql.Append("hs.SID [SID] ");
|
|
sql.Append("FROM [IRMNSPC].[dbo].[HIST_DISABLE] hs ");
|
|
sql.Append("WHERE hs.SID = '").Append(sid).Append("' ");
|
|
using (SqlConnection conn = new SqlConnection(_ConnectionStringIrmnSpc))
|
|
{
|
|
conn.Open();
|
|
using (SqlCommand sqlComm = new SqlCommand(sql.ToString(), conn))
|
|
exits = sqlComm.ExecuteScalar();
|
|
}
|
|
if (exits is null)
|
|
{
|
|
sql.Clear();
|
|
sql.Append("INSERT INTO HIST_DISABLE VALUES ('").Append(sid).Append("' , 0) ");
|
|
using (SqlConnection conn = new SqlConnection(_ConnectionStringIrmnSpc))
|
|
{
|
|
conn.Open();
|
|
using (SqlCommand sqlComm = new SqlCommand(sql.ToString(), conn))
|
|
result = sqlComm.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static object ExecuteScalar(string sql, bool isMappedPart, double totalSecondsSinceLastWriteTime, Tuple<string, string> replace, bool selectForSID, bool isG4Wafers, bool isIrmnSpc)
|
|
{
|
|
object result;
|
|
if (sql.Contains("@"))
|
|
throw new Exception("Invalid query (a parameter didn't get populated)!");
|
|
List<object> results = new List<object>();
|
|
for (int i = 1; i < 15; i++)
|
|
{
|
|
string connectionString;
|
|
if (isG4Wafers && isIrmnSpc)
|
|
throw new Exception();
|
|
else if (isG4Wafers)
|
|
connectionString = _ConnectionStringG4Wafers;
|
|
else if (isIrmnSpc)
|
|
connectionString = _ConnectionStringIrmnSpc;
|
|
else
|
|
throw new Exception();
|
|
using (SqlConnection conn = new SqlConnection(connectionString))
|
|
{
|
|
conn.Open();
|
|
using (SqlCommand sqlComm = new SqlCommand(sql, conn))
|
|
{
|
|
using (SqlDataReader sqlReader = sqlComm.ExecuteReader())
|
|
{
|
|
while (sqlReader.Read())
|
|
results.Add(sqlReader.GetSqlValue(0));
|
|
}
|
|
}
|
|
}
|
|
if (results.Any())
|
|
break;
|
|
if ((i > 10 && !(replace is null)) || (!isMappedPart && totalSecondsSinceLastWriteTime > 1800) || totalSecondsSinceLastWriteTime > 432000 && sql.Contains(replace.Item1)) //30 minutes and 5 days
|
|
{
|
|
sql = sql.Replace(replace.Item1, replace.Item2);
|
|
continue;
|
|
}
|
|
if (!isMappedPart)
|
|
break;
|
|
for (int j = 1; j < 60; j++)
|
|
Thread.Sleep(500);
|
|
}
|
|
if (!isMappedPart && !results.Any())
|
|
result = "-";
|
|
else if (isMappedPart && !results.Any())
|
|
throw new Exception("Didn't find a matching record!");
|
|
else if (isMappedPart && results.Count > 1)
|
|
throw new Exception("Found more than one matching record!");
|
|
else
|
|
{
|
|
result = results[0];
|
|
if (selectForSID && results.Count() == 1)
|
|
{
|
|
int? exits = CheckDisableHistory(result.ToString());
|
|
{ }
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
}
|