<?php
class SQL {

	/**
	 * Funkcja tworzy string gotowy do wstawienia w kwerendzie SELECT
	 * ma on posta� <TABELA>.<POLE> AS <KLASA>_<POLE>, ... <TABELA>.<POLE> AS <KLASA>_<POLE>
	 *
	 * Je�eli klasa o podanej nazwie nie istnieje zwraca '*'
	 * 
	 * @param string $name Nazwa klasy
	 * @return string
	 */
    public static function ToSelect($name,$queryFields = null, $alias = null)
	{
        $return = '*';
    	if(class_exists($name))
		{
	    	$className = $name;
	    	$stmt = '';
	        $list = '';
            $tableName = '';
            $prfx = '';
            if(!empty($alias) && is_string($alias)) {
                $tableName = $alias;
                $prfx = $alias . '_';
            } else {
                $stmt = '$tableName = '. $className .'::$tableName;';
            }
            $stmt .= '$fields = '. $className .'::$fields;';
            eval($stmt);
            
			if(count($queryFields) > 0)
			{
				foreach ($queryFields as $key => $var)
				{
					if(isset($fields[$var]) && $fields[$var]!="")
						$list .= $tableName.'.'.$var.' AS '. $prfx.$className.'_'.$var.', ';
				}
			}
			else
			{
				foreach ($fields as $key => $var)
				{
					if($key[0] == strtolower($key[0]))
						$list .= $tableName.'.'.$key.' AS '. $prfx.$className.'_'.$key.', ';
				}
			}
			
			$list = substr($list, 0, strlen($list) - 2);
			$return = $list;
    	}
    	return $return;
    }

	/**
	 * Funkcja tworzy string gotowy do wstawienia w kwerendzie SELECT
	 * ma on posta� $prefix.<POLE> AS $prefix_<KLASA>_<POLE>, ... <TABELA>.<POLE> AS $prefix_<KLASA>_<POLE>
	 *
	 * Je�eli klasa o podanej nazwie nie istnieje zwraca '*'
	 *
	 * @param string $name Nazwa klasy
	 * @return string
	 */
    public static function ToSelectPrefix($name, $prefix)
	{
    	if(class_exists($name))
		{
	    	$className = $name;

	    	$stmt = '$tableName = '. $className .'::$tableName;';
	    	$stmt .= '$fields = '. $className .'::$fields;';
			eval($stmt);

	        $list = "";
	        foreach ($fields as $key => $var) {
	            if($key[0] == strtolower($key[0]))
	            $list .= $prefix.'.'.$key.' AS '. $prefix . '_' . $className.'_'.$key.', ';
	        }
	        $list = substr($list, 0, strlen($list) - 2);
        	return $list;
    	}
    	else return '*';
    }
    
    public static function ToInsertFields($obj){
	$array = $obj->GetFields();
        $keys = array_keys($array);
        $list = implode(', ', $keys);

        return $list;
    }
    
    
    public static function ToInsertValues($obj){
	$array = $obj->GetFields();
	$db = Registry::Get('db');
	$alist = array();

        foreach ($array as $dbfield => $field) {
            $func = 'Get'. $field;

    		 if($obj->$func() !== "NULL") {
                $alist[] .= '"' .  $db->Escape( $obj->$func() ) . '" ';
            } else {
		$alist[] .= 'NULL ';
	    }
        }
	$list = implode(', ', $alist);
        return $list;
    }

    
    public static function ToUpdateSet($obj){
        $array = $obj->GetFields();
        $alist = array();

		$db = Registry::Get('db');
		
        foreach ($array as $dbfield => $field) {
            $func = 'Get'. $field;
			
    		if (strtolower($obj->$func()) == 'now()')
				$alist[] = "`$dbfield` = NOW() " ;
			else if ($obj->$func() === false)
            {
				$alist[] = '`' . $dbfield . '` = NULL';
			} else if($obj->$func() !== null) {
//                 $alist[] = "`$dbfield` = '" . mysql_real_escape_string($obj->$func()). "'";
                $alist[] = '`' . $dbfield . '` = \'' .  $db->Escape( $obj->$func() ) . '\'';
            }
        }

		$list = implode(', ', $alist);

        return $list;
    }

    public static function ToDeleteSet($obj){
        $array = $obj->GetFields();
        $alist = array();

		$db = Registry::Get('db');

        foreach ($array as $dbfield => $field) {
            $func = 'Get'. $field;
            if ($obj->$func() !== null)
            {
//                 $alist[] = "`$dbfield` = '" . mysql_real_escape_string($obj->$func()). "'";
                $alist[] = '`' . $dbfield . '` = \'' .  $db->Escape($obj->$func()) . '\'';
            }
        }

		$list = implode('AND ', $alist);

        return $list;
    }
	
}
?>