<?php
/**
 * Class queryBuilder
 * @uses db
 *
 */
class queryBuilder {
	
	/**
	 * Builds a simple select query
	 * with a WHERE, IN, NOT IN
	 * ORDER ASC, DESC and LIMIT
	 * 
	 * 
	 * @param $table string       	
	 * @param $filter array       	
	 * @param $filter_way array       	
	 * @param $order_field array       	
	 * @throws Exception
	 * @return $string;
	 * @return &reference $db
	 */
	public static function buildSelect(&$db, $table, $filter = array(), $filter_way = array(), $order_field = array(), $limit = array()) {
		
		$db = db::get ();
		
		// First part of select
		$q = "SELECT * FROM " . $db->real_escape_string ( $table ) . " ";
		
		// Build the where
		
		if(! empty ( $filter )){
			
			$q .= 'WHERE ';
			$c = 1;
			foreach($filter as $field=>$val){
				$q .= $db->real_escape_string ( $field ) . ' ';
				
			
				if(array_key_exists ( $field, $filter_way )){
					
					switch ($filter_way [$field]){
						case dao::WHERE_IN :
							
							if(! is_array ( $filter [$field] ))
								throw new Exception ( 'Filter for' . $field . 'expects an array as parameter' );
							
							$q .= 'IN (';
							
							$i = 1;
							foreach($filter [$field] as $value){
								$q .= "'" . $db->real_escape_string ( $value ) . "'";
								$q .= ($i == sizeof ( $filter [$field] ) ? '' : ',');
								$i ++;
							}
							
							$q .= ') ';
						
						break;
						
						case dao::WHERE_NOT :
							
							if(! is_array ( $filter [$field] ))
								throw new Exception ( 'Filter for' . $field . 'expects an array as parameter' );
							
							$q .= 'NOT IN (';
							
							$i = 1;
							
							foreach($filter [$field] as $value){
								$q .= "'" . $db->real_escape_string ( $value ) . "'";
								$q .= ($i == sizeof ( $filter [$field] ) ? '' : ',');
								$i ++;
							}
							
							$q .= ') ';
						
						break;
					}
				
				}else{
					$q .= "= '" . $db->real_escape_string ( $val ) . "' ";
				
				}
				
				if($c != sizeof ( $filter )){
					$q .= 'AND ';
				}
				
				$c ++;
			
			}
			
		
			
		}
		
		if(!empty($order_field)){
			switch($order_field[0]){
				case dao::ORDER_ASC:
					$q .= 'ORDER BY '.key($order_field[0]) . ' ASC';
					break;
				case dao::ORDER_DESC:
					$q .= 'ORDER BY '.key($order_field[0]) . ' DESC';
					break;
		
			}
		}
		
		return $q;
	
	}
		
		public static function buildInsert(&$db, $oTable ){
			$db = db::get();
			$q =  "INSERT INTO ".$oTable->table." ";
			$class = get_class($oTable);
	 		$q .= '(';		
			$i = 1;
			
			foreach($class::$arTableFields as $key => $value){
				
				if($oTable->primaryKey == $class::$arTableFields[$key])
					continue;
				if(empty($oTable->arAssoc[$key]))
					continue;
				if(in_array($key, db::$reservedMysqlWords))
					$key = "`".$key."`";
					
				$q .= $key;	
				$q .= ($i == sizeof($oTable->arAssoc) ? '' : ',');
				$i++;
			}
			
			$q.= ')'; 
			$q.= ' VALUES (';
			
			
			$i = 1;
			foreach($class::$arTableFields as $key => $value){
		
				if($oTable->primaryKey == $class::$arTableFields[$key])
					continue;
				if(empty($oTable->arAssoc[$key]))
					continue;			
				$q .= (!empty($oTable->arAssoc[$key]) ? (is_numeric($oTable->arAssoc[$key]) ? $oTable->arAssoc[$key] : "'".$db->real_escape_string($oTable->arAssoc[$key])."'" ) : "' '");
					
				$q .= ($i == sizeof($oTable->arAssoc) ? '' : ',');
				$i++;
					
				
			}
			
			$q.= ') ';
			return $q;
		}
		
		public static function buildUpdate(&$db, $oTable ){
			$db = db::get();
			$q =  "UPDATE ".$oTable->table." ";
			$class = get_class($oTable);
			$q .= 'SET ';
			$i = 1;
				
			foreach($class::$arTableFields as $key => $value){
		
				if($oTable->primaryKey == $class::$arTableFields[$key])
					continue;
				if(empty($oTable->arAssoc[$key]))
					continue;
				if(in_array($key, db::$reservedMysqlWords))
					$key = "`".$key."`";
					
				$q .= $key .' = '. (!empty($oTable->arAssoc[$key]) ? (is_numeric($oTable->arAssoc[$key]) ? $oTable->arAssoc[$key] : "'".$db->real_escape_string($oTable->arAssoc[$key])."'" ) : "' '");
					$q .= ($i == sizeof($oTable->arAssoc) ? '' : ',');
				$i++;
			}
				
			$q .= ' WHERE ' . $oTable->primaryKey . ' = ' .$oTable->arAssoc[$oTable->primaryKey];

			return $q;
		}
		public static function buildDelete(&$db, $oTable ){
			$db = db::get();
			$q =  "DELETE FROM ".$oTable->table." ";
			$q .= ' WHERE ' . $oTable->primaryKey . ' = ' .$oTable->arAssoc[$oTable->primaryKey];
		
			return $q;
		}
		

	public static function simpleSelect($table, $primkey, $val){
		return 'SELECT * FROM '.$table.' WHERE '.$primkey . ' = ' . $val ;		
	}

}
