<commit_before>package DatabaseModel.SQLExpressions;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;

import DatabaseModel.DatabaseObject;

public class SQLExpression<T extends DatabaseObject> {
	
	public boolean hasWhereCondition = false;
	
	public String fromExpression = "FROM ";
	public String selectExpression = "SELECT *";
	public String whereExpression = "WHERE ";
	public Class<T> databaseObject;
	public ArrayList<Field> selectFields = new ArrayList<Field>(); 
	
	/**
	 * Create an SQLExpression that targets a DatabaseObject class.
	 * @param databaseObject - The 
	 */
	public SQLExpression(Class<T> databaseObject) {
		this.databaseObject = databaseObject;
		fromExpression += wrapClass(databaseObject);
	}

	/**
	 * Create a custom select expression by providing fields.
	 * @param fields - The fields to select.
	 * @return the same SQLExpression used for chaining. 
	 */
	public SQLExpression<T> select(Field[] fields) {
		selectExpression = "SELECT ";
		int iterations = fields.length;
		for (Field field : fields) {
			selectExpression += wrapField(field);
			selectFields.add(field);
			
			if (--iterations != 0) {
				selectExpression += ", ";
			}
		}
		
		return this;
	}
	
	/**
	 * Create or append a custom where expression by providing fields and an instance of a type containing the field values.
	 * @param T - Instance of a type that contains the field values.
	 * @param fields - The target fields that will be appended in the where expression, values are contained in the type instance.
	 * @return the same SQLExpression used for chaining.
	 */
	public SQLExpression<T> where (T T, Field[] fields) {
		for (Field field : fields) { 
			whereExpression +=  (hasWhereCondition == true ? " AND " : "") + wrapField(field) + " = " + wrapValue(T, field);
			hasWhereCondition = true;
		}
		
		return this;
	}
	
	/**
	 * Append to a custom where expression with an OR statement.
	 * @param T - Instance of a type that contains the field values.
	 * @param fields - The target fields that will be appended in the where expression, values are contained in the type instance.
	 * @return the same SQLExpression used for chaining.
	 */
	public SQLExpression<T> or (T T, Field[] fields) {
		for (Field field : fields) { 
			whereExpression += (hasWhereCondition == true ? " OR " : "") + wrapField(field) + " = " + wrapValue(T, field);
			hasWhereCondition = true;
		}
		
		return this;
	}
	
	/**
	 * Append to a custom where expression with an AND statement.
	 * @param T - Instance of a type that contains the field values.
	 * @param fields - The target fields that will be appended in the where expression, values are contained in the type instance.
	 * @return the same SQLExpression used for chaining.
	 */
	public SQLExpression<T> and (T T, Field[] fields) {
		for (Field field : fields) { 
			whereExpression += (hasWhereCondition == true ? " AND " : "") + wrapField(field) + " = " + wrapValue(T, field);
			hasWhereCondition = true;
		}
		
		return this;
	}
	
	/**
	 * Used for SQL-field parameterization.
	 * @param field
	 * @return Parameterized SQL-column name.
	 */
	public static String wrapField(Field field) {
		return "`" + field.getName() + "`";
	}
	
	/**
	 * Used for SQL-DatabaseObject parameterization.
	 * @param definition
	 * @return Parameterized SQL-DatabaseObject name.
	 */
	private static String wrapClass(Class<?> definition) {
		return "`" + definition.getSimpleName() + "`";
	}
	
	/**
	 * Used for SQL-DatabaseObject parameterization.
	 * @return Parameterized SQL-DatabaseObject name.
	 */
	public String wrapClass() {
		return "`" + databaseObject.getSimpleName() + "`";
	}
	
	/**
	 * Parameterize a field value before using it in a query.
	 * @param T - The instance that contains the field with the value to parameterize.
	 * @param field - The field that contains the value to parameterize.
	 * @return Parameterized SQL-value.
	 */
	public static <T extends DatabaseObject> String wrapValue(T T, Field field) {
		Class<?> type = field.getType();
		String value = "";
		
		try {
			value = field.get(T).toString();
		}
		catch (Exception e) {
			System.out.println(e);
			return value;
		}
		
		if (type == String.class) {
			return "\"" + value + "\"";
		}
		else if (type == Date.class) {
			return "\"" + value + "\"";
		}
		else if (type == java.util.Date.class) {
		    try {
				return "\"" + new java.sql.Date(((java.util.Date)field.get(T)).getTime()).toString() + "\"";
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		else if (type == Timestamp.class) {
			return "\"" + value + "\"";
		}
		
		return value;
	}

	/**
	 * This may be used to execute functional SQL statements. Ideal for select statements or used in sub-queries or custom inner joins or UNIONS.
	 * @return An SQL statement.
	 */
	public String toString() {
		//System.out.println(selectExpression + " " + fromExpression + " " + (hasWhereCondition ? whereExpression : ""));
		return selectExpression + " " + fromExpression + " " + (hasWhereCondition ? whereExpression : "");
	}
	
}
<commit_msg>NF: Added SQLExpression.in method, EH: Made wrapValue for Objects<commit_after>package DatabaseModel.SQLExpressions;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;

import DatabaseModel.DatabaseObject;
import DatabaseModel.Table;

public class SQLExpression<T extends DatabaseObject> {
	
	public boolean hasWhereCondition = false;
	
	public String fromExpression = "FROM ";
	public String selectExpression = "SELECT *";
	public String whereExpression = "WHERE ";
	public Class<T> databaseObject;
	public ArrayList<Field> selectFields = new ArrayList<Field>(); 
	
	/**
	 * Create an SQLExpression that targets a DatabaseObject class.
	 * @param databaseObject - The 
	 */
	public SQLExpression(Class<T> databaseObject) {
		this.databaseObject = databaseObject;
		fromExpression += wrapClass(databaseObject);
	}

	/**
	 * Create a custom select expression by providing fields.
	 * @param fields - The fields to select.
	 * @return the same SQLExpression used for chaining. 
	 */
	public SQLExpression<T> select(Field[] fields) {
		selectExpression = "SELECT ";
		int iterations = fields.length;
		for (Field field : fields) {
			selectExpression += wrapField(field);
			selectFields.add(field);
			
			if (--iterations != 0) {
				selectExpression += ", ";
			}
		}
		
		return this;
	}
	
	/**
	 * Create or append a custom where expression by providing fields and an instance of a type containing the field values.
	 * @param T - Instance of a type that contains the field values.
	 * @param fields - The target fields that will be appended in the where expression, values are contained in the type instance.
	 * @return the same SQLExpression used for chaining.
	 */
	public SQLExpression<T> where (T T, Field[] fields) {
		for (Field field : fields) { 
			whereExpression +=  (hasWhereCondition == true ? " AND " : "") + wrapField(field) + " = " + wrapValue(T, field);
			hasWhereCondition = true;
		}
		
		return this;
	}
	
	/**
	 * Append to a custom where expression with an OR statement.
	 * @param T - Instance of a type that contains the field values.
	 * @param fields - The target fields that will be appended in the where expression, values are contained in the type instance.
	 * @return the same SQLExpression used for chaining.
	 */
	public SQLExpression<T> or (T T, Field[] fields) {
		for (Field field : fields) { 
			whereExpression += (hasWhereCondition == true ? " OR " : "") + wrapField(field) + " = " + wrapValue(T, field);
			hasWhereCondition = true;
		}
		
		return this;
	}
	
	/**
	 * Append to a custom where expression with an AND statement.
	 * @param T - Instance of a type that contains the field values.
	 * @param fields - The target fields that will be appended in the where expression, values are contained in the type instance.
	 * @return the same SQLExpression used for chaining.
	 */
	public SQLExpression<T> and (T T, Field[] fields) {
		for (Field field : fields) { 
			whereExpression += (hasWhereCondition == true ? " AND " : "") + wrapField(field) + " = " + wrapValue(T, field);
			hasWhereCondition = true;
		}
		
		return this;
	}
	
	public <V extends Table> SQLExpression<T> in (V[] values, Field[] fields)
	{
		HashMap<String, ArrayList<Object>> fieldValues = new HashMap<String, ArrayList<Object>>();
		
		for (V value : values) {
			for (Field field : fields) {
				try {
					if (fieldValues.containsKey(field.getName()) == false) {
						ArrayList<Object> objValues = new ArrayList<Object>();
						Object instanceValue = value.getClass().getField(field.getName()).get(value);
						if (instanceValue != null) {
							objValues.add(instanceValue);
							fieldValues.put(field.getName(), objValues);
						}
					}
					else {
						Object instanceValue = value.getClass().getField(field.getName()).get(value);
						if (instanceValue != null)
							fieldValues.get(field.getName()).add(instanceValue);
					}
				} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
						| SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		

		for (String field : fieldValues.keySet()) {
			this.whereExpression += (this.hasWhereCondition ? " AND " : "") + "`" + field + "` IN ( ";
			ArrayList<Object> valueObjects = fieldValues.get(field);
			int iterations = valueObjects.size(); 
			for (Object object : valueObjects) {
				this.whereExpression += wrapValue(object);

				if (--iterations != 0)
					this.whereExpression += ",";
				else
					this.whereExpression += ")";
			}
			this.hasWhereCondition = true;
		}

		return this; // Chaining.
	}
	/**
	 * Used for SQL-field parameterization.
	 * @param field
	 * @return Parameterized SQL-column name.
	 */
	public static String wrapField(Field field) {
		return "`" + field.getName() + "`";
	}
	
	/**
	 * Used for SQL-DatabaseObject parameterization.
	 * @param definition
	 * @return Parameterized SQL-DatabaseObject name.
	 */
	private static String wrapClass(Class<?> definition) {
		return "`" + definition.getSimpleName() + "`";
	}
	
	/**
	 * Used for SQL-DatabaseObject parameterization.
	 * @return Parameterized SQL-DatabaseObject name.
	 */
	public String wrapClass() {
		return "`" + databaseObject.getSimpleName() + "`";
	}
	
	/**
	 * Parameterize a field value before using it in a query.
	 * @param T - The instance that contains the field with the value to parameterize.
	 * @param field - The field that contains the value to parameterize.
	 * @return Parameterized SQL-value.
	 */
	public static <T extends DatabaseObject> String wrapValue(T T, Field field) {
		Class<?> type = field.getType();
		String value = "";
		
		try {
			value = field.get(T).toString();
		}
		catch (Exception e) {
			System.out.println(e);
			return value;
		}
		
		if (type == String.class) {
			return "\"" + value + "\"";
		}
		else if (type == Date.class) {
			return "\"" + value + "\"";
		}
		else if (type == java.util.Date.class) {
		    try {
				return "\"" + new java.sql.Date(((java.util.Date)field.get(T)).getTime()).toString() + "\"";
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		else if (type == Timestamp.class) {
			return "\"" + value + "\"";
		}
		
		return value;
	}

	/**
	 * Parameterize a field value before using it in a query.
	 * @param Object object - The value to parameterize.
	 * @return Parameterized SQL-value.
	 */
	public static <T extends DatabaseObject> String wrapValue(Object object) {
		Class<?> type = object.getClass();
		String value = "";
		
		try {
			value = object.toString();
		}
		catch (Exception e) {
			System.out.println(e);
			return value;
		}
		
		if (type == String.class) {
			return "\"" + value + "\"";
		}
		else if (type == Date.class) {
			return "\"" + value + "\"";
		}
		else if (type == java.util.Date.class) {
		    try {
				return "\"" + new java.sql.Date(((java.util.Date)object).getTime()).toString() + "\"";
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
		else if (type == Timestamp.class) {
			return "\"" + value + "\"";
		}
		
		return value;
	}

	
	/**
	 * This may be used to execute functional SQL statements. Ideal for select statements or used in sub-queries or custom inner joins or UNIONS.
	 * @return An SQL statement.
	 */
	public String toString() {
		//System.out.println(selectExpression + " " + fromExpression + " " + (hasWhereCondition ? whereExpression : ""));
		return selectExpression + " " + fromExpression + " " + (hasWhereCondition ? whereExpression : "");
	}
	
}


{'fraction_non_alphanumeric': 0.12675397567820393, 'fraction_numerical': 0.00023386342376052386, 'mean_word_length': 3.2949447606293942, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3107856', 'n_tokens_mistral': 4185, 'n_tokens_neox': 3627, 'n_words': 1183}