<?php
/**
 * Class that operate on table 'tbl_db_version'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2017-03-21 01:40
 */
class TblDbVersionMySqlExtDAO extends TblDbVersionMySqlDAO{


	function getDbVersion()
	{
		$page = isset($_POST['page']) ? $_POST['page'] : 1;
		$count = isset($_POST['counts']) ? $_POST['counts'] : 20;
		$from = $count * ($page - 1);

		$where = isset($_POST['product']) || isset($_POST['dbType'])
					? "WHERE tdv.db_type LIKE '%".$_POST['dbType']."%' 
								AND tdv.product LIKE '%".$_POST['product']."%' 
								AND db_status LIKE '%{$_POST['status']}%'"
					: '';
		
		
	 	$sql = 'SELECT tdv.*,(SELECT username FROM tbl_admin WHERE id = tdv.user_id) admin_name,  tuv.description
					FROM tbl_db_version tdv
					
					LEFT JOIN tbl_update_description tuv
					ON tuv.tbl_version_id = tdv.id
					
					'.$where.'
					ORDER BY tdv.updated_at DESC
					LIMIT '.$from.','.$count.'
				';
		$sqlQuery = new SqlQuery($sql);
		return QueryExecutor::execute($sqlQuery);
	}
	
	function getLatestApproved($product,$type)
	{
		$sql = "SELECT * FROM `tbl_db_version` 
					WHERE
							db_status = 'approved'
						AND
							product = '{$product}'
						AND
							db_type = '{$type}'
							
					ORDER BY `updated_at` DESC
				LIMIT 1	
				";
					
		$sqlQuery = new SqlQuery($sql);
		return QueryExecutor::execute($sqlQuery);	
	}
	function getPendingProduct($product,$type)
	{
		$sql = "SELECT id 
					FROM `tbl_db_version` 
						WHERE
								product = '{$product}'
							AND
								db_status = 'pending'
							AND
								db_type = '{$type}'
				LIMIT 1
				";
		$sqlQuery = new SqlQuery($sql);
		return QueryExecutor::execute($sqlQuery);	
	}

	private static function getTblDbVersion($sqlQuery){
		$queue = QueryExecutor::execute($sqlQuery);
		
		if(empty($queue)) return false;
		
		$return = array();
		
		foreach($queue as $each){
			$data = new TblDbVersion();
			
			$data->id = $each['id'];
			$data->userId = $each['user_id'];
			$data->fileName = $each['file_name'];
			$data->product = $each['product'];
			$data->dbVersion = $each['db_version'];
			$data->dbStatus = $each['db_status'];
			$data->dbType = $each['db_type'];
			$data->approvedId = $each['approved_id'];
			$data->createdAt = $each['created_at'];
			$data->updatedAt = $each['updated_at'];
			
			$return[] = $data;
		}
		
		return $return;
	}

	function getDbVersionList()
	{
		$sql = 'SELECT tdv.*
					FROM tbl_db_version tdv
					WHERE 
					tdv.db_type LIKE "%'.$_POST['dbType'].'%" AND
					tdv.product LIKE "%'.$_POST['product'].'%" AND
					tdv.db_status LIKE "%'.$_POST['status'].'%"
				';

		$sqlQuery = new SqlQuery($sql);
		return QueryExecutor::execute($sqlQuery);
	}
}
?>