Added QueryBuilder
> Added build for SelectByPrimaryKey/s Added Attributes Added Function > SqlSerialise and escape
This commit is contained in:
		
							parent
							
								
									5e763263ac
								
							
						
					
					
						commit
						47e396b802
					
				
							
								
								
									
										22
									
								
								Database-Attribute_System/Attributes/DbAttribute.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								Database-Attribute_System/Attributes/DbAttribute.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
 | 
				
			||||||
 | 
					    public class DbAttribute : Attribute
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string _attributeName;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Marks variable as database-attribute
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
 | 
				
			||||||
 | 
					        public DbAttribute(string attributeName =  null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            this._attributeName = attributeName;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										25
									
								
								Database-Attribute_System/Attributes/DbForeignKey.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Database-Attribute_System/Attributes/DbForeignKey.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
 | 
				
			||||||
 | 
					    public class DbForeignKey : Attribute
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public Type _classType;
 | 
				
			||||||
 | 
					        public string _attributeName;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Marks variable as foreignKey of given class
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="classType">Type of class to which this is the ForeignKey</param>
 | 
				
			||||||
 | 
					        /// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
 | 
				
			||||||
 | 
					        public DbForeignKey(Type classType, string attributeName = null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            this._classType = classType;
 | 
				
			||||||
 | 
					            this._attributeName = attributeName;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										24
									
								
								Database-Attribute_System/Attributes/DbObject.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Database-Attribute_System/Attributes/DbObject.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Diagnostics;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Reflection;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
 | 
				
			||||||
 | 
					    public class DbObject : Attribute
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string _tableName;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Marks variable as database-table
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="tableName">Name of database-table (case-sensitivity is determined from database-table-settings) ['null' if the same as class-name]</param>
 | 
				
			||||||
 | 
					        public DbObject(string tableName = null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            this._tableName = tableName;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										23
									
								
								Database-Attribute_System/Attributes/DbPrimaryKey.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Database-Attribute_System/Attributes/DbPrimaryKey.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
 | 
				
			||||||
 | 
					    public class DbPrimaryKey : Attribute
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public Type _classType;
 | 
				
			||||||
 | 
					        public string _attributeName;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Marks variable as primaryKey fo current class
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="dbAttributeName">Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]</param>
 | 
				
			||||||
 | 
					        public DbPrimaryKey(string attributeName = null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            this._attributeName = attributeName;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,8 +0,0 @@
 | 
				
			|||||||
using System;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
namespace eu.railduction.netcore.dll.Database_Attribute_System
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    public class Class1
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										20
									
								
								Database-Attribute_System/Function.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Database-Attribute_System/Function.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Text;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace eu.railduction.netcore.dll.Database_Attribute_System
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    class Function
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public static string SqlEscape(string str)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            string str_cpy = str.Replace(@"'", @"\'");
 | 
				
			||||||
 | 
					            return str_cpy;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public static string SqlSerialise(DateTime dt)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return dt.ToString("yyyy-MM-dd HH:mm:ss");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										115
									
								
								Database-Attribute_System/QueryBuilder.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								Database-Attribute_System/QueryBuilder.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,115 @@
 | 
				
			|||||||
 | 
					using eu.railduction.netcore.dll.Database_Attribute_System.Attributes;
 | 
				
			||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Reflection;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace eu.railduction.netcore.dll.Database_Attribute_System
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class QueryBuilder
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Build an SELECT-SQL-query by a database-primary-key<pragma/>
 | 
				
			||||||
 | 
					        /// Will build the query-string based on the <paramref name="comparisonValue"/> including serialising and escaping for SQL.<para/>
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="handler">Method to send querys to the database</param>
 | 
				
			||||||
 | 
					        /// <param name="primaryKey">Database comparison value</param>
 | 
				
			||||||
 | 
					        /// <returns>Built SQL-Query</returns>
 | 
				
			||||||
 | 
					        public static string SelectByPrimaryKey(Type classType, object comparisonValue) => SelectByPrimaryKeys(classType, comparisonValue);
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Build an SELECT-SQL-query by database-primary-keys<pragma/>
 | 
				
			||||||
 | 
					        /// Will build the query-string based on the <paramref name="comparisonValues"/> including serialising and escaping for SQL.<para/>
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="handler">Method to send querys to the database</param>
 | 
				
			||||||
 | 
					        /// <param name="primaryKeys">Database comparison values (Must be the exact same amount as primary keys in class/table)</param>
 | 
				
			||||||
 | 
					        /// <returns>Built SQL-Query</returns>
 | 
				
			||||||
 | 
					        public static string SelectByPrimaryKeys(Type classType, params object[] comparisonValues)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            // Check if class has attribute 'DbObject' and get the database table-name
 | 
				
			||||||
 | 
					            DbObject dbObjectAttribute = classType.GetCustomAttribute(typeof(DbObject), true) as DbObject;
 | 
				
			||||||
 | 
					            if (dbObjectAttribute == null) throw new InvalidOperationException("Cannot generate SQL-Query of class. Missing Attribute 'DbObject'");
 | 
				
			||||||
 | 
					            string tableName = dbObjectAttribute._tableName ?? dbObjectAttribute._tableName;    // If no alternative table-name is specified, use the class-name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Iterate thru all properties
 | 
				
			||||||
 | 
					            List<string> dbPrimaryKeys = new List<string>() { };
 | 
				
			||||||
 | 
					            foreach (System.Reflection.PropertyInfo pi in classType.GetProperties())
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                // Get primaryKey attribute from current property
 | 
				
			||||||
 | 
					                DbPrimaryKey pkey = pi.GetCustomAttribute(typeof(DbPrimaryKey) ,true) as DbPrimaryKey;
 | 
				
			||||||
 | 
					                if(pkey != null)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    string dbAttributeName = pkey._attributeName ?? pi.Name;     // If no alternative attribute-name is specified, use the property-name
 | 
				
			||||||
 | 
					                    dbPrimaryKeys.Add(dbAttributeName);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (comparisonValues.Length != dbPrimaryKeys.Count)
 | 
				
			||||||
 | 
					                throw new InvalidOperationException("Primary-key number of class/table and number of comparison values is not equal!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            object[] whereParams = new object[comparisonValues.Length*2];
 | 
				
			||||||
 | 
					            for (int i=0,c=0; c<whereParams.Length; i++,c+=2)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                string sql_string = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (i > 0) sql_string += " AND ";
 | 
				
			||||||
 | 
					                sql_string += $"{dbPrimaryKeys[i]}=";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                whereParams[c] = sql_string;
 | 
				
			||||||
 | 
					                whereParams[c+1] = comparisonValues[i];
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return BuildQuery($"SELECT * FROM {tableName} WHERE ", whereParams);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// Build an SQL-query<para/>
 | 
				
			||||||
 | 
					        /// Will build the query-string based on the <paramref name="param"/>s including serialising and escaping for SQL.<para/>
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="param">Params with alternating query-parts and objects beginning with a query-part
 | 
				
			||||||
 | 
					        /// <para/>Example: "SELECT * FROM table WHERE id=", id, " AND name=", name</param>
 | 
				
			||||||
 | 
					        public static string BuildQuery(params object[] param)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            string query = "";
 | 
				
			||||||
 | 
					            for (int i = 0; i < param.Length; i++)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if (i % 2 == 0) query += param[i];  // Every 'even' count will just add
 | 
				
			||||||
 | 
					                else       // Every 'uneven' count will handle param as passed variable    
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    string paramString = "";
 | 
				
			||||||
 | 
					                    if (param[i] == null)            // Handle null
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        paramString = "null";
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else if (param[i].GetType() == typeof(string))       // Handle strings
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        paramString = "'" + Function.SqlEscape((string)param[i]) + "'";  // wrap in sql-brackets and escape sql, if any
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else if (param[i].GetType() == typeof(int) || param[i].GetType() == typeof(float) || param[i].GetType() == typeof(double))  // Handle int, float & double
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        paramString = param[i].ToString().Replace(",", ".");    // just format to string and form comma to sql-comma
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else if (param[i].GetType() == typeof(DateTime))        // Handle DateTime
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        DateTime dateTime = (DateTime)param[i];
 | 
				
			||||||
 | 
					                        paramString = "'" + Function.SqlSerialise(dateTime) + "'";     // wrap in sql-brackets and convert to sql-datetime
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else if (param[i].GetType() == typeof(Guid))        // Handle DateTime
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        string guid = ((Guid)param[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 '{param.GetType().ToString()}' cannot be used!");
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    // Add formed param to query
 | 
				
			||||||
 | 
					                    query += paramString;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // return built query
 | 
				
			||||||
 | 
					            return query;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user