<?php

//Query Class
class MySQLQuery
{
	public function MySQLQuery()
	{
		
	}
	
	public function Insert($pTable, $pFields)
	{
		$counter = count($pFields);
		if($counter > 0)
		{
			$query = '';
		
			$query .= 'INSERT INTO ' . $pTable . ' (';

			$counterVariable = $counter;
			//columns
			foreach($pFields as $field)
			{
				$query .= $field->name;
				
				if($counterVariable != 1)
				{
					$query .= ', ';
				}	
				
				$counterVariable--;
			}
			$counterVariable = $counter;
			$query .= ') VALUES (';
		
			//values
			foreach($pFields as $field)
			{
				$query .= self::AlterValue($field);
				
				if($counterVariable != 1)
				{
					$query .= ', ';
				}	
				
				$counterVariable--;
			}
			
			$query .= ')';
		}
		return $query;
		
	}
	
	public function Update($pTable, $pFields, $pWhere)
	{
		$counter = count($pFields);
		if($counter > 0)
		{
			$query = '';
		
			$query .= 'UPDATE ' . $pTable . ' SET ';

			$counterVariable = $counter;
			//columns
			foreach($pFields as $field)
			{
				$query .= $field->name . ' = ' . self::AlterValue($field);
				
				if($counterVariable != 1)
				{
					$query .= ', ';
				}	
				
				$counterVariable--;
			}
			
			$query .= ' WHERE ' . $pWhere->name . ' = ' . self::AlterValue($pWhere);
		

		}
		return $query;
	}
	
	public function AlterValue($pField)
	{
		switch($pField->type)
		{
			case 'text':
				return ("'" . $pField->value . "'");
				break;
			case 'number':
				return ("'" . $pField->value . "'");
				break;
			case 'bool':
				return ("'" . $pField->value . "'");
				break;
			case 'datetime':
				return ("'" . $pField->value . "'");
				break;
			case 'decimal':
				return ("'" . $pField->value . "'");
				break;
			case 'null':
				return $pField->value;
				break;
		}
		
		return '';
	}
}

//Field Class
class MySQLField
{
	//attributes
	public $name;
	public $value;
	public $type;
	
	//contructor
	public function MySQLField()
	{
		
	}
	
	//methods
	public function Set($pName, $pValue, $pType)
	{
		$this->name = $pName;
		$this->value = $pValue;
		$this->type = $pType;
	}
	
}

?>