@ -7,59 +7,86 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
{
{
public class QueryBuilder
public class QueryBuilder
{
{
/// <summary>
public static string SelectByPrimaryKeys < T > ( T classObject )
/// 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/>
Type classType = classObject . GetType ( ) ;
/// </summary>
/// <param name="handler">Method to send querys to the database</param>
// Get db-table-name from class
/// <param name="primaryKey">Database comparison value</param>
string tableName = Function . GetDbTableName ( classType ) ;
/// <returns>Built SQL-Query</returns>
public static string SelectByPrimaryKey ( Type classType , object comparisonValue ) = > SelectByPrimaryKeys ( classType , comparisonValue ) ;
// Get class db-fields
/// <summary>
Dictionary < string , object > dbPrimaryKeys = new Dictionary < string , object > ( ) { } ;
/// Build an SELECT-SQL-query by database-primary-keys<pragma/>
Dictionary < string , object > dbAttributes = new Dictionary < string , object > ( ) { } ;
/// Will build the query-string based on the <paramref name="comparisonValues"/> including serialising and escaping for SQL.<para/>
Dictionary < string , object > dbForeignKeys = new Dictionary < string , object > ( ) { } ;
/// </summary>
Function . ReadDbClassFields ( classObject , ref dbPrimaryKeys , ref dbAttributes , ref dbForeignKeys ) ;
/// <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>
// Build where statements with primaryKey/s
/// <returns>Built SQL-Query</returns>
object [ ] param = Function . BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
public static string SelectByPrimaryKeys ( Type classType , params object [ ] comparisonValues )
// Add SQL-command part
param [ 0 ] + = $"SELECT * FROM {tableName} WHERE " ;
// Build and return the query
return BuildQuery ( param ) ;
}
public static string UpdateByPrimaryKeys < T > ( T classObject )
{
{
// Check if class has attribute 'DbObject' and get the database table-name
Type classType = classObject . GetType ( ) ;
if ( ! ( classType . GetCustomAttribute ( typeof ( DbObject ) , true ) is DbObject dbObjectAttribute ) ) throw new InvalidOperationException ( "Cannot generate SQL-Query of class. Missing Attribute 'DbObject'" ) ;
string tableName = dbObjectAttribute . _tableName ? ? classType . Name ; // If no alternative table-name is specified, use the class-name
// Iterate thru all properties
// Get db-table-name from class
List < string > dbPrimaryKeys = new List < string > ( ) { } ;
string tableName = Function . GetDbTableName ( classType ) ;
foreach ( System . Reflection . FieldInfo fi in classType . GetRuntimeFields ( ) )
// 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 ) ;
// Add foreign-keys to attributes
foreach ( KeyValuePair < string , object > dbForeignKey in dbForeignKeys )
{
{
// Get primaryKey attribute from current property
dbAttributes . Add ( dbForeignKey . Key , dbForeignKey . Value ) ;
if ( fi . GetCustomAttribute ( typeof ( DbPrimaryKey ) , true ) is DbPrimaryKey pkey )
{
string dbAttributeName = pkey . _attributeName ? ? fi . 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!" ) ;
// Build set-parameters
object [ ] paramSet = Function . BuildKeyEqualQuery ( dbAttributes , ", " ) ;
// Add SQL-command part
paramSet [ 0 ] + = $"UPDATE {tableName} SET " ;
// Build where-parameters
object [ ] paramWhere = Function . BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
// Add SQL-command part
paramWhere [ 0 ] + = " WHERE " ;
object [ ] param = new object [ comparisonValues . Length * 2 ] ;
// Build and return the query
for ( int i = 0 , c = 0 ; c < param . Length ; i + + , c + = 2 )
return BuildQuery ( paramSet , paramWhere ) ;
{
}
string sql_string = "" ;
if ( i = = 0 ) sql_string + = $"SELECT * FROM {tableName} WHERE " ;
public static string DeleteByPrimaryKeys < T > ( T classObject )
else sql_string + = " AND " ;
{
sql_string + = $"{dbPrimaryKeys[i]}=" ;
Type classType = classObject . GetType ( ) ;
param [ c ] = sql_string ;
// Get db-table-name from class
param [ c + 1 ] = comparisonValues [ i ] ;
string tableName = Function . GetDbTableName ( classType ) ;
}
return BuildQuery ( param ) ;
// 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 ) ;
// Build where-parameters
object [ ] paramWhere = Function . BuildKeyEqualQuery ( dbPrimaryKeys , " AND " ) ;
// Add SQL-command part
paramWhere [ 0 ] + = $"DELETE FROM {tableName} WHERE " ;
// Build and return the query
return BuildQuery ( paramWhere ) ;
}
}
/// <summary>
/// <summary>
/// Build an SQL-query<para/>
/// Build an SQL-query<para/>
/// Will build the query-string based on the <paramref name="param"/>s including serialising and escaping for SQL.<para/>
/// Will build the query-string based on the <paramref name="param"/>s including serialising and escaping for SQL.<para/>