From 8de75728e0fb5ea11d72b9089a480f9882ff2da5 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 1 Apr 2019 18:33:53 +0200 Subject: [PATCH] Fixed ClassAction not being public Moved SqlSerialise to Function --- Database-Attribute_System/ClassAction.cs | 2 +- Database-Attribute_System/QueryBuilder.cs | 30 ++---------------- .../internal/Function.cs | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index 0a9c6d4..f439e42 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -5,7 +5,7 @@ using System.Text; namespace eu.railduction.netcore.dll.Database_Attribute_System { - class ClassAction + public class ClassAction { /// /// Fills an given dbObject with given data diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs index 4fb3270..35c6dad 100644 --- a/Database-Attribute_System/QueryBuilder.cs +++ b/Database-Attribute_System/QueryBuilder.cs @@ -131,33 +131,9 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System if (i % 2 == 0) query += paramz[i]; // Every 'even' count will just add else // Every 'uneven' count will handle param as passed variable { - string paramString = ""; - if (paramz[i] == null) // Handle null - { - paramString = "null"; - } - else if (paramz[i].GetType() == typeof(string)) // Handle strings - { - paramString = "'" + Function.SqlEscape((string)paramz[i]) + "'"; // wrap in sql-brackets and escape sql, if any - } - else if (paramz[i].GetType() == typeof(byte) || paramz[i].GetType() == typeof(int) || paramz[i].GetType() == typeof(float) || paramz[i].GetType() == typeof(double)) // Handle int, float & double - { - paramString = paramz[i].ToString().Replace(",", "."); // just format to string and form comma to sql-comma - } - else if (paramz[i].GetType() == typeof(DateTime)) // Handle DateTime - { - DateTime dateTime = (DateTime)paramz[i]; - paramString = "'" + Function.SqlSerialise(dateTime) + "'"; // wrap in sql-brackets and convert to sql-datetime - } - else if (paramz[i].GetType() == typeof(Guid)) // Handle DateTime - { - string guid = ((Guid)paramz[i]).ToString(); // Get Guid as string - paramString = "'" + guid + "'"; // wrap in sql-brackets - } - else // Unknown type in params - { - throw new Exception($"Error in query-builder: Type '{paramz[i].GetType().ToString()}' cannot be used!"); - } + string paramString = Function.SqlSerialise(paramz[i]); + if(paramString == null) // Unknown type in params + throw new Exception($"Error in query-builder. Type '{paramz[i].GetType().ToString()}' cannot be serialised!"); // Add formed param to query query += paramString; diff --git a/Database-Attribute_System/internal/Function.cs b/Database-Attribute_System/internal/Function.cs index dde96ab..4342ad7 100644 --- a/Database-Attribute_System/internal/Function.cs +++ b/Database-Attribute_System/internal/Function.cs @@ -103,5 +103,36 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System return tableName; } + + + public static string SqlSerialise(object obj) + { + if (obj == null) // Handle null + { + return "null"; + } + else if (obj.GetType() == typeof(string)) // Handle strings + { + return "'" + SqlEscape((string)obj) + "'"; // wrap in sql-brackets and escape sql, if any + } + else if (obj.GetType() == typeof(byte) || obj.GetType() == typeof(int) || obj.GetType() == typeof(float) || obj.GetType() == typeof(double)) // Handle int, float & double + { + return obj.ToString().Replace(",", "."); // just format to string and form comma to sql-comma + } + else if (obj.GetType() == typeof(DateTime)) // Handle DateTime + { + DateTime dateTime = (DateTime)obj; + return "'" + SqlSerialise(dateTime) + "'"; // wrap in sql-brackets and convert to sql-datetime + } + else if (obj.GetType() == typeof(Guid)) // Handle DateTime + { + string guid = ((Guid)obj).ToString(); // Get Guid as string + return "'" + guid + "'"; // wrap in sql-brackets + } + else + { + return null; + } + } } }