import java.util.*;
import java.sql.*;
import java.math.*;

class  Select
{
	public static boolean execute(Row r, Column c, String att, String op, String val)
	{
		boolean satisfy=false; /* result of row value satisfying the condition: user entered value */
		int res = 99; /* result of comparing a row value against the user entered value */

		if ( att.equals("st") || att.equals("et") ) /* comparing rows based on TIME column values */
		{
			int time = 0;
			if ( att.equals("st") )
				time = r.getRow_st();
			else /* assume wanted end time values */
				time = r.getRow_et();

			/* compare the row time value against the user entered value */
			res = cmpTimeVals(time, val, att);
		}
		else /* comparing rows based on ANY COULMN values EXCEPT TIME column values */
		{

			/* get the column type of the column name: 'att' */
			int colPos = c.getColPos(att);
			int colType = c.getPosType(colPos);

			/* get row value (object) */
			Object rowVal = r.getRowVal(colPos);

			/* compare the row value against the user entered value */
			res = cmpVals(rowVal, val, colType, att);
			//System.out.print("rowVal="+rowVal+" "+op+" userVal= "+val);
			//System.out.println(" and cmpRes="+res);
		}

		//System.out.println("res=" + res);

		/* display any error when comparing values */
		if (res==99)
		{
			System.out.print("Error in Select: comparing row value with user entered value");
			//System.exit(0); /* terminate application */
		}

		/* if row value is null then don't compare and discard the row for comparing */
		if (res!=101)
		{
			if(op.equals("=")) // Equality Constraint
			{
				if (res==0) /* the row value and the user entered value is the same */
				{
					satisfy=true;
				}
			}
			else if(op.equals(">"))	// Greater Than Constraint
			{
				if (res>0) /* the row value is greater than the user entered value */
				{
					satisfy=true;
				}
			}
			else if(op.equals(">=")) // Greater Than or Equal To Constraint
			{
				if (res>=0) /* the row value is greater than or equal to the user entered value */
				{
					satisfy=true;
				}
			}
			else if(op.equals("<"))	// Less Than Constraint
			{
				if (res<0) /* the row value is less than the user entered value */
				{
					satisfy=true;
				}
			}
			else if(op.equals("<=")) // Less Than or Equal To Constraint
			{
				if (res<=0) /* the row value is less than or equal to the user entered value */
				{
					satisfy=true;
				}
			}
			else if(op.equals("<>")) // Not equal
			{
				if (res!=0) /* the row value is not equal to the user entered value */
				{
					satisfy=true;
				}
			}
			else
			{
				System.out.print("Unkown operator '" + op + "' in 'Where' clause");
				System.out.print(" with column attribute '" + att + "'\n");
				System.out.println("Application supports the following comparison operators:");
				System.out.println(">, >=, =, <=, <");
				//System.exit(0); /* terminate application */
			}
		}

		return satisfy;
	}



	private static int cmpVals(Object rowVal, String val, int colType, String att)
	{
		
		int res = 99; /* the result of comparing row values with user entered value */

		switch (colType)
		{
			case 4: // INTEGER type
			case 5: // SMALL INTEGER type
				if (rowVal.equals("null")) /* if row value is null then don't compare aginst user entered value */
					res = 101;
				else
				{
					Integer intRowVal = (Integer) rowVal; /* stores converted row value */
					
					/* check that user has entered a integer value: the correct type */
					int checkInt = 0;
					try
					{
						 checkInt = Integer.parseInt(val);
					}
					catch (NumberFormatException e)
					{
						System.out.print("Error: The data value '" + val + "' entered does not match");
						System.out.println(" the column type 'int/small int' for column title '" + att + "'\n");
						//System.exit(0); /* terminate application */
						//throw new NumberFormatException("caught error: " + e);
					}

					Integer intVal = new Integer(checkInt); /* stores converted user entered value */
					res = intRowVal.compareTo(intVal); /* compare the row value against the user entered value */
				}
				break;

			case 3: // DECIMAL type
			case 6: // FLOAT type
			case 7: // REAL type
				if (rowVal.equals("null")) /* if row value is null then don't compare aginst user entered value */
					res = 101;
				else
				{	
					Float fltRowVal = (Float) (rowVal);

					/* check that user has entered a decimal/float/real value: the correct type */
					float checkFloat = 0;
					try
					{
						 checkFloat = Float.parseFloat(val);
					}
					catch (NumberFormatException e)
					{
						System.out.print("Error: The data value '" + val + "' entered does not match");
						System.out.println(" the column type 'decimal/float/real' for column title '" + att + "'\n");
						//System.exit(0); /* terminate application */
						//throw new NumberFormatException("caught error: " + e);
					}

					Float fltVal = new Float(checkFloat); /* stores converted user entered value */
					res = fltRowVal.compareTo(fltVal); /* compare the row value against the user entered value */
				}
				break;

			case 1: // CHAR type
			case 12: // STRING (VARCHAR) type
				if (rowVal.equals("null")) /* if row value is null then don't compare aginst user entered value */
					res = 101;
				else
				{
					String strRowVal = rowVal.toString(); /* stores converted row value */

					/* get string value that is embedded in single quotes */
					int valLen = val.length();
					String firstchar = val.substring(0, 1);
					String lastchar = val.substring(valLen-1, valLen);
					String substrVal = val;
					
					if (firstchar.equals("'") &&  lastchar.equals("'"))
						substrVal = val.substring(1, valLen-1);
					else
					{
						System.out.println("Forgot single quote(s): ' around the string value = "+val);
						//System.exit(0); /* terminate application */
					}

					res = substrVal.compareTo(strRowVal); /* compare the row value against the user entered value */
				}
				break;

			case 91: // DATE type
				if (rowVal.equals("null")) /* if row value is null then don't compare aginst user entered value */
					res = 101;
				else
				{
					java.sql.Date dateRowVal = (java.sql.Date) (rowVal); /* stores converted row value */

					/* check that user has entered a date value: the correct type */
					java.sql.Date checkDate = new java.sql.Date(0);
					try
					{
						 checkDate = java.sql.Date.valueOf(val);
					}
					catch (Exception e)
					{
						System.out.print("Error: The data value '" + val + "' entered does not match");
						System.out.println(" the column type 'date' for column title '" + att + "'\n");
						//System.exit(0); /* terminate application */
						//throw new NumberFormatException("caught error: " + e);
					}

					java.sql.Date dateVal = checkDate; /* stores converted user entered value */
					res = dateRowVal.compareTo(dateVal); /* compare the row value against the user entered value */
				}
				break;

			case 2: // NUMERIC type
				if (rowVal.equals("null")) /* if row value is null then don't compare aginst user entered value */
					res = 101;
				else
				{	
					BigDecimal decRowVal = (BigDecimal) (rowVal);

					/* check that user has entered a numeric value: the correct type */
					double checkDec = 0;
					try
					{
						 checkDec = Double.parseDouble(val);
					}
					catch (NumberFormatException e)
					{
						System.out.print("Error: The data value '" + val + "' entered does not match");
						System.out.println(" the column type 'numeric' for column title '" + att + "'\n");
						System.exit(0); /* terminate application */
						//throw new NumberFormatException("caught error: " + e);
					}

					BigDecimal decVal = new BigDecimal(checkDec); /* stores converted user entered value */
					res = decRowVal.compareTo(decVal); /* compare the row value against the user entered value */
				}
				break;

			

			default: // UNKNOWN type
				System.out.println("Unkown type '" + colType + "' in Select");
				System.out.println("The application does not handle '" + colType + "'");
				//System.out.println("Application supports the following column types:");
				//System.out.println("int (4), real (7) and string/char/varchar (12)");
				System.out.println("Please reformat the column in the database table");
				//System.exit(0); /* terminate application */
				break;
		}

		return res;
	}

	private static int cmpTimeVals(int time, String val, String att)
	{
		int res=99; /* the result of comparing time values with user entered value */

		Integer timeVal = new Integer(time);
		
		/* check that user has entered a int value: the correct type of time (st or et) */
		int checkInt = 0;
		try
		{
			 checkInt = Integer.parseInt(val);
		}
		catch (NumberFormatException e)
		{
			System.out.print("Error: The data value '" + val + "' entered does not match");
			System.out.println(" the column type 'int' for the time column '" + att + "'\n");
			//System.exit(0); /* terminate application */
			//throw new NumberFormatException("caught error: " + e);
		}

		Integer intVal = new Integer(checkInt); /* stores converted user entered value */
		res = timeVal.compareTo(intVal); /* compare the row value against the user entered value */

		return res;
	}

			
}
