@ -7,21 +7,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
{
public class QueryBuilder
{
/// <summary>
/// Builds an SELECT-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <returns>SELECT-Sql-query</returns>
public static string SelectByPrimaryKeys < T > ( T classObject )
{
Type classType = classObject . GetType ( ) ;
// Get db-table-name from class
string tableName = Function . GetDbTableName ( classType ) ;
string tableName = Db Function. GetDbTableName ( classType ) ;
// Get class db-fields
Dictionary < string , object > dbPrimaryKeys = new Dictionary < string , object > ( ) { } ;
Dictionary < string , object > dbAttributes = new Dictionary < string , object > ( ) { } ;
Dictionary < string , object > dbForeignKeys = new Dictionary < string , object > ( ) { } ;
Function . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
DbFunction . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
if ( dbPrimaryKeys . Count = = 0 ) throw new InvalidOperationException ( $"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!" ) ;
// Build where statements with primaryKey/s
object [ ] param = Function. BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
object [ ] param = Db Function. BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
// Add SQL-command part
param [ 0 ] = $"SELECT * FROM {tableName} WHERE " + param [ 0 ] ;
@ -29,33 +37,40 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return BuildQuery ( param ) ;
}
/// <summary>
/// Builds an UPDATE-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <returns>UPDATE-Sql-query</returns>
public static string UpdateByPrimaryKeys < T > ( T classObject )
{
Type classType = classObject . GetType ( ) ;
// Get db-table-name from class
string tableName = Function . GetDbTableName ( classType ) ;
string tableName = Db Function. GetDbTableName ( classType ) ;
// Get class db-fields
Dictionary < string , object > dbPrimaryKeys = new Dictionary < string , object > ( ) { } ;
Dictionary < string , object > dbAttributes = new Dictionary < string , object > ( ) { } ;
Dictionary < string , object > dbForeignKeys = new Dictionary < string , object > ( ) { } ;
Function . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
DbFunction . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
if ( dbPrimaryKeys . Count = = 0 ) throw new InvalidOperationException ( $"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!" ) ;
// Add foreign-keys to attributes
foreach ( KeyValuePair < string , object > dbForeignKey in dbForeignKeys )
foreach ( KeyValuePair < string , object > dbForeignKey in dbForeignKeys )
{
dbAttributes . Add ( dbForeignKey . Key , dbForeignKey . Value ) ;
}
// Build set-parameters
object [ ] paramSet = Function. BuildKeyEqualQuery ( dbAttributes , ", " ) ;
object [ ] paramSet = Db Function. BuildKeyEqualQuery ( dbAttributes , ", " ) ;
// Add SQL-command part
paramSet [ 0 ] = $"UPDATE {tableName} SET " + paramSet [ 0 ] ;
// Build where-parameters
object [ ] paramWhere = Function. BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
object [ ] paramWhere = Db Function. BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
// Add SQL-command part
paramWhere [ 0 ] = " WHERE " + paramWhere [ 0 ] ;
@ -63,21 +78,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return BuildQuery ( paramSet , paramWhere ) ;
}
/// <summary>
/// Builds an DELETE-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <returns>DELETE-Sql-query</returns>
public static string DeleteByPrimaryKeys < T > ( T classObject )
{
Type classType = classObject . GetType ( ) ;
// Get db-table-name from class
string tableName = Function . GetDbTableName ( classType ) ;
string tableName = Db Function. GetDbTableName ( classType ) ;
// Get class db-fields
Dictionary < string , object > dbPrimaryKeys = new Dictionary < string , object > ( ) { } ;
Dictionary < string , object > dbAttributes = new Dictionary < string , object > ( ) { } ;
Dictionary < string , object > dbForeignKeys = new Dictionary < string , object > ( ) { } ;
Function . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
DbFunction . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
if ( dbPrimaryKeys . Count = = 0 ) throw new InvalidOperationException ( $"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!" ) ;
// Build where-parameters
object [ ] paramWhere = Function. BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
object [ ] paramWhere = Db Function. BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
// Add SQL-command part
paramWhere [ 0 ] = $"DELETE FROM {tableName} WHERE " + paramWhere [ 0 ] ;
@ -90,25 +113,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <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/>
/// (Supported-Types: null, string, byte, int, float, double, DateTime, Guid)
/// </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>
/// <para/>Example: "SELECT * FROM table WHERE id=", id, " AND name=", name<para/>
/// Info: Any object[] will be opened recursively and added as <paramref name="param"/> accordingly!
/// </param>
public static string BuildQuery ( params object [ ] param )
{
// Convert array to list and add object[] to it according ly
// Convert array to list and add object[] to it recursive ly
List < object > paramz = new List < object > ( ) { } ;
foreach ( object obj in param )
{
if ( ! ( obj is object [ ] ) )
paramz . Add ( obj ) ;
else
{
foreach ( object obj2 in ( object [ ] ) obj )
{
paramz . Add ( obj2 ) ;
}
}
}
Function . RecursiveParameterCopying ( ref paramz , ( object [ ] ) param ) ;
string query = "" ;
for ( int i = 0 ; i < paramz . Count ; i + + )
@ -125,7 +140,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
{
paramString = "'" + Function . SqlEscape ( ( string ) paramz [ i ] ) + "'" ; // wrap in sql-brackets and escape sql, if any
}
else if ( paramz [ i ] . GetType ( ) = = typeof ( int) | | paramz [ i ] . GetType ( ) = = typeof ( float ) | | paramz [ i ] . GetType ( ) = = typeof ( double ) ) // Handle int, float & double
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
}