Kaynağa Gözat

Added abstract MicrosoftSQLClient and AWSClient.

Kenric Nugteren 1 yıl önce
ebeveyn
işleme
7fb929ef79

+ 14 - 0
prs.desktop/Utils/MicrosoftSQL/AWSClient.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PRSDesktop;
+
+public class AWSClient(string connectionString) : MicrosoftSQLClient
+{
+    public string ConnectionString { get; set; } = connectionString;
+
+    protected override string GetConnectionString() => ConnectionString;
+}

+ 111 - 0
prs.desktop/Utils/MicrosoftSQL/MicrosoftSQLClient.cs

@@ -0,0 +1,111 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PRSDesktop;
+
+public abstract class MicrosoftSQLClient : IDisposable
+{
+    private SqlConnection? connection;
+
+    protected abstract string GetConnectionString();
+
+    public bool Connect()
+    {
+        connection = new SqlConnection(GetConnectionString());
+        try
+        {
+            connection.Open();
+        }
+        catch
+        {
+            connection = null;
+        }
+        return IsConnected;
+    }
+
+    public bool IsConnected => connection != null;
+
+    public void Disconnect()
+    {
+        if (connection != null)
+        {
+            connection.Dispose();
+            connection = null;
+        }
+    }
+
+    public DataTable Query(string sql, string tablename, SqlParameter[]? parameters = null)
+    {
+        var result = new DataTable(tablename);
+        if (connection != null)
+        {
+            using (var _command = new SqlCommand(sql.Replace("\r\n"," ").Replace("\n"," ").Replace("\r"," "), connection))
+            {
+                if (parameters is not null)
+                {
+                    foreach (var _parameter in parameters)
+                        _command.Parameters.Add(_parameter);
+                }
+
+                using var _adapter = new SqlDataAdapter(_command);
+                _adapter.Fill(result);
+            }
+            connection.Close();
+        }
+
+        return result;
+    }
+
+    public int ExecuteSQL(string sql, SqlParameter[]? parameters = null)
+    {
+        int result = 0;
+        if(connection != null)
+        {
+            using(var command = new SqlCommand(sql.Replace("\r\n"," ").Replace("\n", " ").Replace("\r"," "), connection))
+            {
+                if (parameters is not null)
+                {
+                    foreach (var _parameter in parameters)
+                        command.Parameters.Add(_parameter);
+                }
+
+                result = command.ExecuteNonQuery();
+            }
+
+            connection.Close();
+        }
+        return result;
+    }
+
+    public static int GetInteger(DataRow row, string field, int defaultvalue = 0)
+    {
+        return row[field] == DBNull.Value ? 0 : Convert.ToInt32(row[field]);
+    }
+    
+    public static double GetDouble(DataRow row, string field, double defaultvalue = 0.0)
+    {
+        return row[field] == DBNull.Value
+            ? 0.0
+            : Convert.ToDouble(row[field]);
+    }
+
+    public static string GetString(DataRow row, string field, string defaultvalue = "")
+    {
+        return row[field] == DBNull.Value ? "" : row[field] as string ?? defaultvalue;
+    }
+    
+    public static byte[] GetBinary(DataRow row, string field)
+    {
+        return row[field] == DBNull.Value ? [] : (byte[])row[field];
+    }
+
+    public void Dispose()
+    {
+        Disconnect();
+    }
+}

+ 12 - 87
prs.desktop/Utils/V6Utils/V6Client.cs → prs.desktop/Utils/MicrosoftSQL/V6Utils/V6Client.cs

@@ -14,11 +14,8 @@ using Syncfusion.Windows.Tools.Controls;
 
 namespace PRSDesktop;
 
-public class V6Client : IDisposable
+public class V6Client : MicrosoftSQLClient
 {
-
-    private SqlConnection? connection;
-
     public V6Settings Settings { get; private set; }
 
     public V6Client()
@@ -34,85 +31,18 @@ public class V6Client : IDisposable
         ?? new V6Settings();
         Settings.CheckSQL();
     }
-    
-    private DataTable QueryV6(string sql, string tablename, SqlParameter[]? parameters = null)
-    {
-        DataTable _result = new DataTable(tablename);
-        if (connection != null)
-        {
-            using (var _command = new SqlCommand(sql.Replace("\r\n"," ").Replace("\n"," ").Replace("\r"," "), connection))
-            {
-                if (parameters?.Any() == true)
-                {
-                    foreach (var _parameter in parameters)
-                        _command.Parameters.Add(_parameter);
-                }
-
-                using (var _adapter = new SqlDataAdapter(_command))
-                    _adapter.Fill(_result);
-            }
-            connection.Close();
-        }
-
-        return _result;
-    }
-
-    public int GetInteger(DataRow row, string field, int defaultvalue = 0)
-    {
-        return row[field] == DBNull.Value ? 0 : Convert.ToInt32(row[field]);
-    }
-    
-    public double GetDouble(DataRow row, string field, double defaultvalue = 0.0)
-    {
-        return row[field] == DBNull.Value
-            ? 0.0
-            : Convert.ToDouble(row[field]);
-    }
-
-    public string GetString(DataRow row, string field, string defaultvalue = "")
-    {
-        return row[field] == DBNull.Value ? "" : row[field] as string ?? defaultvalue;
-    }
-    
-    public byte[] GetBinary(DataRow row, string field)
-    {
-        return row[field] == DBNull.Value ? [] : (byte[])row[field];
-    }
 
-    public bool Connect()
-    {
-        connection = new SqlConnection(Settings.AsConnectionString());
-        try
-        {
-            connection.Open();
-        }
-        catch
-        {
-            connection = null;
-        }
-        return IsConnected;
-    }
-
-    public bool IsConnected => connection != null;
+    protected override string GetConnectionString() => Settings.AsConnectionString();
 
-    public void Disconnect()
-    {
-        if (connection != null)
-        {
-            connection.Dispose();
-            connection = null;
-        }
-    }
-    
     public IEnumerable<V6Quote> GetQuotes()
     {
         try
         {
             List<V6Quote> _projects = new();
-            if (connection == null)
+            if (!IsConnected)
                 return _projects;
         
-            var _quotes = QueryV6(Settings.QuoteSQL,"quotes");
+            var _quotes = Query(Settings.QuoteSQL,"quotes");
         
             foreach (DataRow _row in _quotes.Rows)
                 _projects.Add(DataRowToQuote(_row));
@@ -144,7 +74,7 @@ public class V6Client : IDisposable
         return _quote;
     }
     
-    private string CheckQuery(string query, int number, string variation, int? quoteitem = null)
+    private static string CheckQuery(string query, int number, string variation, int? quoteitem = null)
     {
         string _basefilter = quoteitem == null
             ? $"q.quote_num = '{number}' and q.quote_num_suff = '{variation}' and q.quote_vers = (select max(quote_vers) from quote where quote_id = q.quote_id) "
@@ -157,7 +87,7 @@ public class V6Client : IDisposable
         try
         {
             var _query = CheckQuery(Settings.QuoteSQL, number, variation);
-            var _table = QueryV6(_query,"quote");
+            var _table = Query(_query,"quote");
             return _table.Rows.Count > 0
                 ? DataRowToQuote(_table.Rows[0])
                 : null;
@@ -179,7 +109,7 @@ public class V6Client : IDisposable
             if (_quote == null)
                 return _result;
             var _query = CheckQuery(Settings.ElevationSQL, number, variation);
-            var _table = QueryV6(_query, "items");
+            var _table = Query(_query, "items");
             _result.AddRange(_table.Rows.OfType<DataRow>().Select(DataRowToItem));
         
             return _result;
@@ -208,7 +138,7 @@ public class V6Client : IDisposable
         try
         {
             var _query = CheckQuery(Settings.DrawingsSQL, 0, "", itemnumber);
-            var _table = QueryV6(_query,"drawings");
+            var _table = Query(_query,"drawings");
             return _table.Rows.Count > 0
                 ? DataRowToDrawings(_table.Rows[0])
                 : null;
@@ -302,7 +232,7 @@ public class V6Client : IDisposable
             if (_quote == null)
                 return _result;
             string _query = CheckQuery(Settings.LabourSQL, number, variation, quoteitem);
-            var _table = QueryV6(_query,"labour");
+            var _table = Query(_query,"labour");
             foreach (DataRow _row in _table.Rows)
             {
                 var _labour = DataRowToLabour(_row);
@@ -337,7 +267,7 @@ public class V6Client : IDisposable
             if (_quote == null)
                 return _result;
             string _query = CheckQuery(Settings.ProfileSQL, number, variation, quoteitem);
-            var _table = QueryV6(_query,"profile");
+            var _table = Query(_query,"profile");
         
             foreach (DataRow _row in _table.Rows)
             {
@@ -378,7 +308,7 @@ public class V6Client : IDisposable
             if (_quote == null)
                 return _result;
             string _query = CheckQuery(Settings.ComponentSQL, number, variation, quoteitem);
-            var _table = QueryV6(_query,"sundries");
+            var _table = Query(_query,"sundries");
             foreach (DataRow _row in _table.Rows)
             {
                 var _sundry = DataRowToComponent(_row);
@@ -416,7 +346,7 @@ public class V6Client : IDisposable
             if (_quote == null)
                 return _result;
             string _query = CheckQuery(Settings.GlassSQL, number, variation, quoteitem);
-            var _table = QueryV6(_query,"glass");
+            var _table = Query(_query,"glass");
         
             foreach (DataRow _row in _table.Rows)
             {
@@ -446,9 +376,4 @@ public class V6Client : IDisposable
             Cost = GetDouble(row, nameof(V6Glass.Cost)),
         };
     }
-
-    public void Dispose()
-    {
-        Disconnect();
-    }
 }

+ 0 - 0
prs.desktop/Utils/V6Utils/V6ProjectImport.xaml → prs.desktop/Utils/MicrosoftSQL/V6Utils/V6ProjectImport.xaml


+ 0 - 0
prs.desktop/Utils/V6Utils/V6ProjectImport.xaml.cs → prs.desktop/Utils/MicrosoftSQL/V6Utils/V6ProjectImport.xaml.cs