<?php

class MySql
{
	public $sql;
	public $eks = true;
	
	public function getConnection($config)
	{
		$conn = mysql_connect($config['dbConnection.Host'], $config['dbConnection.UserName'], $config['dbConnection.Password']);
		if (!$conn) die ("<h1>Gagal Melakukan Koneksi Ke Server</h1>");
		mysql_select_db($config['dbConnection.DataBase'], $conn) or die ("<h1>Database Tidak Diketemukan di Server<h1>");
	}

	public function getDb()
	{
		return new DB;
	}
	
	public function addSelect($param)
	{
		$ex = explode(' ', $param);
		if(trim(strtoupper($ex[0])) == 'SELECT'){
			$this->sql = $param;
		}else{
			$this->sql = "SELECT $param";
		}
	
		return $this->sql;
	}
	
	public function getFrom($param, $alias)
	{
		$this->sql = $this->sql." FROM ".$this->getDb()->$param.' '.$alias;
		return $this->sql;
	}
	
	public function addLeftJoin($table, $alias, $param)
	{
		$this->sql = $this->sql." LEFT JOIN ".$this->getDb()->$table.' '.$alias.' on ';
		
		foreach($param as $row){
			$this->sql = $this->sql.' ('.$row.')';
		}
		
		return $this->sql;
	}
	
	public function addInnerJoin($table, $alias, $param)
	{
		$this->sql = $this->sql." INNER JOIN ".$this->getDb()->$table.' '.$alias.' on ';
		
		foreach($param as $row){
			$this->sql = $this->sql.' ('.$row.')';
		}
		
		return $this->sql;
		return $this->sql;
	}
	
	public function getWhere($param)
	{
		$this->sql = $this->sql." WHERE $param";
		return $this->sql;
	}
	
	public function andWhere($param)
	{
		$this->sql = $this->sql." AND $param";
		return $this->sql;
	}
	
	public function orWhere($param)
	{
		$this->sql = $this->sql." OR $param";
		return $this->sql;
	}
	
	public function getShortBy($param, $type)
	{
		$this->sql = $this->sql." ORDER BY $param $type";
		return $this->sql;
	}
	
	public function getMultiShort($param)
	{
		$this->sql = $this->sql." ORDER BY $param";
		return $this->sql;
	}
	
	public function getSpecialShort($sql, $param)
	{
		$this->sql = $sql." ORDER BY $param";
		return $this->sql;
	}
	
	public function getLimit($start, $finish)
	{
		$this->sql = $this->sql." LIMIT $start, $finish";
		return $this->sql;
	}
	
	public function getSpecialLimit($sql, $start, $finish)
	{
		return $sql." LIMIT $start, $finish";
	}
	
	public function addInsert($param)
	{
		$ex = explode(' ', $param);
		if(trim(strtoupper($ex[0])) == 'INSERT'){
			$this->sql = $param;
		}else{
			$this->sql = "INSERT INTO ".$this->getDb()->$param;
		}
	
		return $this->sql;
	}
	
	public function getAttrib($param)
	{
		$this->sql = $this->sql." ($param)";
		return $this->sql;
	}
	
	public function addAttrIns($attr)
	{
		$sql_1 = ' (';
		$sql_2 = ' (';
		$no = 1;
		foreach($attr as $key => $n){
			if($no > 1){
				$sql_1.= ', ';
				$sql_2.= ', ';
			}
			$no++;
			$sql_1.= "$key";
			$sql_2.= "'$n'";
		}
		$sql_1.= ')';
		$sql_2.= ')';
		
		$this->sql = $this->sql.$sql_1.' values '.$sql_2;
		return $this->sql;
	}
	
	public function addAttrUp($attr)
	{
		$sql_1 = '';
		$no = 1;
		foreach($attr as $key => $n){
			if($no > 1){
				$sql_1.= ', ';
			}
			$no++;
			$sql_1.= "$key = '$n'";
		}
		
		$this->sql = $this->sql.' set '.$sql_1;
		return $this->sql;
	}
	
	public function getIf($param, $i, $n, $alias)
	{
		$this->sql = $this->sql
					.", if( ".$param
					.", ".$i
					.", ".$n
					.")"
					." as ".$alias
					;
		return $this->sql;
	}
	
	public function getWhereAndIf($param, $i, $n)
	{
		$this->sql = $this->sql
					." AND if (".$param
					.", ".$i
					.", ".$n
					.")"
					;
		return $this->sql;
	}
	
	public function addValues($param)
	{
		$this->sql = $this->sql." VALUES ($param)";
		return $this->sql;
	}
	
	public function addUpdate($param)
	{
		$ex = explode(' ', $param);
		if(trim(strtoupper($ex[0])) == 'UPDATE'){
			$this->sql = $param;
		}else{
			$this->sql = "UPDATE ".$this->getDb()->$param;
		}
	
		return $this->sql;
	}
	
	public function addSet($param)
	{
		$this->sql = $this->sql." SET $param";
		return $this->sql;
	}
	
	public function addDelete($param)
	{
		$ex = explode(' ', $param);
		if(trim(strtoupper($ex[0])) == 'DELETE'){
			$this->sql = $param;
		}else{
			$this->sql = "DELETE FROM ".$this->getDb()->$param;
		}
	
		return $this->sql;
	}
	
	public function queryEks($sql)
	{
		return mysql_query($sql);
	}
	
	public function getNumRows($data)
	{
		return mysql_num_rows($data);
	}
	
	public function getFetchObject($data)
	{
		$result = array();
		while($row = mysql_fetch_object($data)){
			$result[] = $row;
		}
		
		return $result;
	}
	
	public function getFetchArray($data)
	{
		$result = array();
		while($row = mysql_fetch_array($data)){
			$result[] = $row;
		}
		
		return $result;
	}
	
	public function getFlush($sql)
	{
		mysql_query("BEGIN");

		foreach($sql as $row){
			if(!mysql_query($row)){
				mysql_query("ROLLBACK");
				return;
			}
		}
		
		return mysql_query("COMMIT");
		
	}
	
	public function getGroupBy($param)
	{
		$this->sql = $this->sql." GROUP BY $param";
		return $this->sql;
	}
	
	public function getFind($table, $select, $param)
	{
		$sql = $this->addSelect($select);
		$sql = $this->getFrom($table, 'a');
		$sql = $this->getWhere('1=1');
		
		foreach($param as $row){
			$sql = $this->andWhere($row);
		}
		
		$eks = $this->queryEks($sql);
		$res = $this->getFetchObject($eks);

		return $res[0];
	}
	
	public function getFindBy($table, $select, $param, $sort)
	{
		$sql = $this->addSelect($select);
		$sql = $this->getFrom($this->getDb()->$table, 'a');
		$sql = $this->getWhere('1=1');
		
		foreach($param as $row){
			$sql = $this->andWhere($row);
		}
		
		if($sort != null){
			$sql = $this->getMultiShort($sort);
		}

		$eks = $this->queryEks($sql);
		return $this->getFetchObject($eks);
	}
	
	public function getRoolbackFlush($sql)
	{
		mysql_query("BEGIN");

		foreach($sql as $row){
			if(!mysql_query($row)){
				mysql_query("ROLLBACK");
				return;
			}
		}
		
		return mysql_query("COMMIT");
	}
	
	public function getBeginTransaction()
	{
		// return mysql_query("BEGIN"); 
	}
	
	public function getRollBack()
	{
		// return mysql_query("ROLLBACK");
	}
	
	public function getCommit()
	{
		// return mysql_query("COMMIT");
	}
	
	public function getRollCek($res)
	{
		// return mysql_query($res); 
	}
}

/*
//----------------------- Expresi Query ----------------------//
$sql = $this->em->getSelect('nik, username, password');
$sql = $this->em->getSchema($sql, 'user');
$sql = $this->em->getFrom($sql, 'karyawan');
$sql = $this->em->getWhere($sql, "username = '".$_POST['username']."'");
$sql = $this->em->getAndWhere($sql, "password = '".md5($_POST['password'])."'");
$sql = $this->em->getLimit($sql, 0, 5);
*/
