<?php
 
	class classQueryGlobal
	{
		//---------------single ton---------------------
		private static $instance;
		
		private $rawSelect = NULL;
		private $rawInsert = NULL;
		private $rawUpdate = NULL;
		private $rawDelete = NULL;
		
		private $genericSelect = NULL;
		private $genericInsert = NULL;
		private $genericUpdate = NULL;
		private $genericDelete = NULL;
		private $server = NULL;
	
		public static function createObject($db)
		{
			/* if (!isset(self::$instance)) {
				$className = __CLASS__;
				self::$instance = new $className($db);
			}
			return self::$instance;
			*/
			$className = __CLASS__;
			return  new $className($db);
		}
		
		////////////////////single ton end////////////////
		
		function setRawSelect($query = NULL) {
			if(is_null($query) OR empty($query))
			{
				throw new MyException(109100);
			}
			
			$this->rawSelect = $query;
		}
		
		function setRawInsert($query = NULL) {
			if(is_null($query) OR empty($query))
			{
				throw new MyException(109101);
			}
			
			$this->rawInsert = $query;
		}
		
		function setRawUpdate($query = NULL) {
			if(is_null($query) OR empty($query))
			{
				throw new MyException(109102);
			}
			
			$this->rawUpdate = $query;
		}
		
		function setRawDelete($query = NULL) {
			if(is_null($query) OR empty($query))
			{
				throw new MyException(109103);
			}
			
			$this->rawDelete = $query;
		}
		
		function executeSelect($throwException = true) {
			if(!is_null($this->rawSelect))
			{
				return mysql_query($this->rawSelect);
			}
			elseif(!is_null($this->genericSelect))
			{
				return mysql_query($this->genericSelect);
			}
			elseif($throwException)
			{
				//throw new MyException("There is an error in Select Query. Error: " . mysql_error(),"There is an internal server Error. Please try again in a moment.");
			   $token=array('var1'=> mysql_error());
			   throw new MyException(109104,$token);
			    
			}
			elseif(!$throwException)
			{
				return 0;
			}
		}
		
		function executeInsert($throwException = true) {
			if(!is_null($this->rawInsert))
			{
				return mysql_query($this->rawInsert);
			}
			elseif(!is_null($this->genericInsert))
			{
				return mysql_query($this->genericInsert);
			}
			elseif($throwException)
			{
				//throw new MyException("There is an error in Insert Query. Error: " . mysql_error(),"There is an internal server Error. Please try again in a moment.");
			$token=array('var1'=> mysql_error());
			   throw new MyException(109105,$token);
			}
			elseif(!$throwException)
			{
				return 0;
			}
		}
		
		function executeUpdate($throwException = true) {
			if(!is_null($this->rawUpdate))
			{
				return mysql_query($this->rawUpdate);
			}
			elseif(!is_null($this->genericUpdate))
			{
				return mysql_query($this->genericUpdate);
			}
			elseif($throwException)
			{
				//throw new MyException("There is an error in Update Query. Error: " . mysql_error(),"There is an internal server Error. Please try again in a moment.");
			$token=array('var1'=> mysql_error());
			   throw new MyException(109106,$token);
			}
			elseif(!$throwException)
			{
				return 0;
			}
		}
		
		function executeDelete($throwException = true) {
			if(!is_null($this->rawDelete))
			{
				return mysql_query($this->rawDelete);
			}
			elseif(!is_null($this->genericDelete))
			{
				return mysql_query($this->genericDelete);
			}
			elseif($throwException)
			{
				//throw new MyException("There is an error in Delete Query. Error: " . mysql_error(),"There is an internal server Error. Please try again in a moment.");
			$token=array('var1'=> mysql_error());
			   throw new MyException(109107,$token);
			}
			elseif(!$throwException)
			{
				return 0;
			}
		}
		
		/**
		 * Query Maker '1.5'. It supports 'AND','OR'.
		 * @param array_type $where_condition_on
		 * @param array_type $where_condition_value
		 * @param unknown_type $sort
		 * @param unknown_type $sortby
		 * @param unknown_type $limit
		 * @return string
		 */
		function Select_Query($get_column_values = NULL, $where_condition_on = NULL, $from_tables = NULL, $sort="", $sortby="", $limit="",$groupby="",$server,$debug_mode='off')
		{
	// Leo: Get your desired Table values.
			$table_names = "";
			if(!is_null($from_tables))
			{
				foreach($from_tables as $key => $value)
				{
					$table_names .= $value . ",";
				}
				$table_names = substr($table_names,0,-1);
			}
			else
			{
				die("Please provide table name(s).");
			}	
				
	// Leo: Get your desired values.
			$column_names = "";
			if(!is_null($get_column_values))
			{
				foreach($get_column_values as $key => $value)
				{
					$column_names .= $value . ",";
				}
				$column_names = substr($column_names,0,-1);
			}
			else
			{
				die("Please provide column names.");
			}
	// Leo: check AND~ , OR~ statement.
			
	// Leo: Query_starts_here
			$query = "SELECT ";
			$query .= $column_names;
			$query .= " FROM " . $table_names . " ";
			if(!is_null($where_condition_on))
			{
				$contions = @$where_condition_on;
				foreach($contions as $keys => $values)
				{
					$check = strstr($keys,"~");
					if(empty($check))
					{
						$array[1] = $keys;
						$array[2] = '=';
					}
					else
					{
						$array = explode("~",$keys);
						$count = count($array);
						$logical_operators = array("AND","OR");
						$conditional_operators = array('=','<>','!=','<=','>=','<','>','LIKE','IN','NOT IN','NOT','IS','IS NOT');
						switch($count):
						
							case 2:
								if(in_array($array[0],$logical_operators))
								{
									$array[2] = '=';
								}
								elseif(in_array($array[1],$conditional_operators))
								{
									$array[0] = 'AND';
								}
								else
								{
									if($debug_mode != 'off')
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN|NOT]"');
								}
							break;
							
							case 3:
								if(!in_array($array[0],$logical_operators) OR !in_array($array[2],$conditional_operators))
								{
									if($debug_mode != 'off')
									{
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN|NOT]"' . ": $keys");
									}
								}
							break;
							
							case 4:
								if(!in_array($array[0],$logical_operators) OR !in_array($array[2],$conditional_operators))
								{
									if($debug_mode != 'off')
									{
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN][~][1,2,3,...]"' . ": $keys");
									}
								}
							break;
							
						endswitch;
					}
					
					if(!isset($first_condition_ok))
					{
						/*if(!in_array($array[1],$conditional_operators) && count($array) > 1)
						{
							$array[2] = $array[1];
							$array[1] = $array[0];
							unset($array[0]);
						}*/
						$query .= "WHERE " . $array[1] . " " . $array[2] . " " . $values . " ";
						$first_condition_ok = "ok";
					}
					elseif(isset($first_condition_ok))
					{
						$query .= $array[0] . " " . $array[1] . " " . $array[2] . " " . $values . " ";
					}
					else
					{
						die("Please provide where_condition_on in 'AND~column_name' or 'OR~column_name' format.");
					}
				}
			}
			if(!empty($groupby))
			{
				$query .= "GROUP BY $groupby";
			}	
			if(!empty($sortby) AND !empty($sort))
			{
				$query .= "ORDER BY $sortby $sort ";
			}
			if(!empty($limit))
			{
				$query .= "LIMIT $limit";
			}
	// Leo: End of Query
			if($debug_mode == 'query')
			{
				return $query;
			}
			$this->rawSelect = $query;
			$data = $this->check_fetch_query($query,'select',$server,$debug_mode);		
			return $data;
		}
		//---------------INSERT Query---------------------
		//------------------------------------------------
		
		/**
		 * Enter description here ...
		 * @param array_type $where_condition_on
		 * @param array_type $where_condition_value
		 * @param unknown_type $sort
		 * @param unknown_type $sortby
		 * @param unknown_type $limit
		 * @return string
		 */
		function Insert_Query($get_column_names = "", $get_column_values = "", $into_table = "", $server = "",$debug_mode='off')
		{
	// Leo: Get your desired values.
			$column_names = "";
			if(!empty($get_column_names))
			{
				foreach($get_column_names as $key => $value)
				{
					$column_names .= $value . ",";
				}
				$column_names = substr($column_names,0,-1);
			}
			else
			{
				die("Please provide column names.");
			}
	
	//Leo: Get your desired values.
			$column_values = "";
			if(!empty($get_column_values))
			{
				foreach($get_column_values as $key => $value)
				{
					$column_values .= "'" . addslashes($value) . "',";
				}
				$column_values = substr($column_values,0,-1);
			}
			else
			{
				die("Please provide column names.");
			}
	
	//Leo: Check if table name is proper.
			if(empty($into_table))
			{
				die("Please provide table name.");
			}
			
			$query = "";
			$query .= "INSERT INTO $into_table (";
			$query .= $column_names;
			$query .= ") VALUES (";
			$query .= $column_values;
			$query .= ")";
			
			$this->rawInsert = $query;
			return $this->check_fetch_query($query,'insert',$server,$debug_mode);
		}
		
		/*
		 * @param array_type $where_condition_on
		 * @param array_type $where_condition_value
		 * @param unknown_type $sort
		 * @param unknown_type $sortby
		 * @param unknown_type $limit
		 * @return string
		 */
		function Update_Query($get_column_names = "", $get_column_values = "", $table = "", $where_condition_on = "", $server = "", $debug_mode = 'off')
		{
			
			$updation_statements = "";
			$count_column_names = count($get_column_names);
			$count_column_values = count($get_column_values);
			
			if($count_column_names == $count_column_values)
			{
				for($i = 0; $i < $count_column_names; $i++)
				{
					$updation_statements .= $get_column_names[$i] . " = '" . $get_column_values[$i] . "',";
				}
			}
			else
			{
				if($debug_mode == 'on')
				{
					die("Updates of Column names and values are not equal.");
				}
			}
			
			$query = "";
			$query .= "UPDATE $table SET ";
			$query .= substr($updation_statements,0,-1) . ' ';
			if(!is_null($where_condition_on))
			{
				$contions = @$where_condition_on;
				foreach($contions as $keys => $values)
				{
					$check = strstr($keys,"~");
					if(empty($check))
					{
						$array[1] = $keys;
						$array[2] = '=';
					}
					else
					{
						$array = explode("~",$keys);
						$count = count($array);
						$logical_operators = array("AND","OR");
						$conditional_operators = array('=','<>','!=','<=','>=','<','>','LIKE','IN','NOT IN','NOT','IS','IS NOT');
						switch($count):
						
							case 2:
								if(in_array($array[0],$logical_operators))
								{
									$array[2] = '=';
								}
								elseif(in_array($array[1],$conditional_operators))
								{
									$array[0] = 'AND';
								}
								else
								{
									if($debug_mode != 'off')
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN|NOT]"');
								}
							break;
							
							case 3:
								if(!in_array($array[0],$logical_operators) OR !in_array($array[2],$conditional_operators))
								{
									if($debug_mode != 'off')
									{
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN|NOT]"' . ": $keys");
									}
								}
							break;
							
							case 4:
								if(!in_array($array[0],$logical_operators) OR !in_array($array[2],$conditional_operators))
								{
									if($debug_mode != 'off')
									{
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN][~][1,2,3,...]"' . ": $keys");
									}
								}
							break;
							
						endswitch;
					}
					
					if(!isset($first_condition_ok))
					{
						/*if(!in_array($array[1],$conditional_operators) && count($array) > 1)
						{
							$array[2] = $array[1];
							$array[1] = $array[0];
							unset($array[0]);
						}*/
						$query .= "WHERE " . $array[1] . " " . $array[2] . " " . $values . " ";
						$first_condition_ok = "ok";
					}
					elseif(isset($first_condition_ok))
					{
						$query .= $array[0] . " " . $array[1] . " " . $array[2] . " " . $values . " ";
					}
					else
					{
						die("Please provide where_condition_on in 'AND~column_name' or 'OR~column_name' format.");
					}
				}
			}
			$this->rawUpdate = $query;
			$this->check_fetch_query($query,'update',$server,$debug_mode);
		}

		/**
		 * Deletion Query Maker '1.0'. It supports 'AND','OR'.
		 * @param array_type $where_condition_on
		 * @param array_type $where_condition_value
		 * @param unknown_type $sort
		 * @param unknown_type $sortby
		 * @param unknown_type $limit
		 * @return string
		 */
		function Delete_Query($where_condition_on = NULL, $from_tables = NULL,$server,$debug_mode='off')
		{
	// Leo: Get your desired Table values.
			$table_names = "";
			if(!is_null($from_tables))
			{
				foreach($from_tables as $key => $value)
				{
					$table_names .= $value . ",";
				}
				$table_names = substr($table_names,0,-1);
			}
			else
			{
				die("Please provide table name(s).");
			}
	// Leo: check AND~ , OR~ statement.
			
	// Leo: Query_starts_here
			$query = "DELETE ";
			$query .= "FROM " . $table_names . " ";
			if(!is_null($where_condition_on))
			{
				$contions = @$where_condition_on;
				foreach($contions as $keys => $values)
				{
					$check = strstr($keys,"~");
					if(empty($check))
					{
						$array[1] = $keys;
						$array[2] = '=';
					}
					else
					{
						$array = explode("~",$keys);
						$count = count($array);
						$logical_operators = array("AND","OR");
						$conditional_operators = array('=','<>','!=','<=','>=','<','>','LIKE','IN','NOT IN','NOT');
						switch($count):
						
							case 2:
								if(in_array($array[0],$logical_operators))
								{
									$array[2] = '=';
								}
								elseif(in_array($array[1],$conditional_operators))
								{
									$array[0] = 'AND';
								}
								else
								{
									if($debug_mode != 'off')
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN|NOT]"');
								}
							break;
							
							case 3:
								if(!in_array($array[0],$logical_operators) OR !in_array($array[2],$conditional_operators))
								{
									if($debug_mode != 'off')
									{
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN|NOT]"' . ": $keys");
									}
								}
							break;
							
							case 4:
								if(!in_array($array[0],$logical_operators) OR !in_array($array[2],$conditional_operators))
								{
									if($debug_mode != 'off')
									{
										die('Please provide a proper format like "[AND|OR][~]COLUMN_NAME[~][=|<>|!=|<|>|<=|>=|LIKE|IN|NOT IN][~][1,2,3,...]"' . ": $keys");
									}
								}
							break;
							
						endswitch;
					}
					
					if(!isset($first_condition_ok))
					{
						/*if(!in_array($array[1],$conditional_operators) && count($array) > 1)
						{
							$array[2] = $array[1];
							$array[1] = $array[0];
							unset($array[0]);
						}*/
						$query .= "WHERE " . $array[1] . " " . $array[2] . " " . $values . " ";
						$first_condition_ok = "ok";
					}
					elseif(isset($first_condition_ok))
					{
						$query .= $array[0] . " " . $array[1] . " " . $array[2] . " " . $values . " ";
					}
					else
					{
						die("Please provide where_condition_on in 'AND~column_name' or 'OR~column_name' format.");
					}
				}
			}
			if(!empty($groupby))
			{
				$query .= "GROUP BY $groupby";
			}	
			if(!empty($sortby) AND !empty($sort))
			{
				$query .= "ORDER BY $sortby $sort ";
			}
			if(!empty($limit))
			{
				$query .= "LIMIT $limit";
			}
	// Leo: End of Query
			if($debug_mode == 'query')
			{
				return $query;
			}
			$this->rawDelete = $query;
			$data = $this->check_fetch_query($query,'delete',$server,$debug_mode);		
			return $data;
		}
		
		/**
		 * Enter 1.2 here ...
		 * @param unknown_type $query
		 * @param unknown_type $which_query
		 * @return unknown
		 */
		function check_fetch_query($query,$type,$server,$debug_mode='on')
		{
			$server = $this->server;
			
			if($debug_mode == 'on')
			{
				echo $query . "<br /><br />";
			}
			if(empty($server))
			{
				$server = med2tec;
			}
			
			$result = mysql_query($query,$server);
			if(!$result)
			{
				if($type == 'update')
				{
					if($debug_mode == 'on')
					{
						die('Update failed ' . mysql_error($server));
					}
					else
					{
						return 0;
					}
				}
				elseif($type == 'insert')
				{
					if($debug_mode == 'on')
					{
						die('Insert Failed ' . mysql_error($server));
					}
					else
					{
						return 0;
					}
				}
				elseif($type == 'select')
				{
					if($debug_mode == 'on')
					{
						die('Select Failed ' . mysql_error($server));
					}
					else
					{
						return 0;
					}
				}
				elseif($type == 'delete')
				{
					if($debug_mode == 'on')
					{
						die('Select Failed ' . mysql_error($server));
					}
					else
					{
						return 0;
					}
				}
			}
			
			if($type == 'delete')
			{
				return 1;
			}
			if($type == 'insert')
			{
				return mysql_insert_id($server);
			}
			if($type == 'select')
			{
				$data = $this->fetch_query($result);
				//$data['result'] = $result;
				return $data;
			}
		}
		
		/**
		 * Enter description here ...
		 * @param unknown_type $result
		 * @param unknown_type $which_query
		 * @return multitype:
		 */
		function fetch_query($result,$insert_id="")
		{
			$data = array();
			while($row = mysql_fetch_assoc(@$result))
			{
				$data[]=$row;
			}
			return $data;
		}
	}
?>