<?php
/**
 * MySQLSQLBuilder
 * 条件文の生成。where xxxx の、xxxxの部分を返す。
 * $Id: MySQLSQLBuilder.inc,v 1.1 2015/10/08 11:18:57 wanggb Exp $
 * @author zhouz
 * @access public
 * @package jp.aimslib2.database.mysql
 */

class MySQLSQLBuilder extends AbstractSQLBuilder{
	
	/**
	 * AND,OR等Where文の中身を生成する。
	 */
	public static function createOptionCondition($param){
		
		$option = "";

		while (list($key, $val) = each($param)) {
			
			if ($option != "") {
				$option .= " AND ";
			}
			
			$mmflg = null;
			$modkey = null;
			if(strlen($key) > 4){
				$mmflg = strtoupper(substr($key, strlen($key) - 4));
				$modkey = substr($key, 0, strlen($key) - 4);
			}

			if($mmflg == "_ALT"){
				if (get_magic_quotes_gpc() == 1) {
					$option .= $modkey . " like '" . $val . "'";
				} else {
					$option .= $modkey . " like '" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			} else if($mmflg == "_MAX"){
				if (get_magic_quotes_gpc() == 1) {
					$option .= $modkey . "<='" . $val . "'";
				} else {
					$option .= $modkey . "<='" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			} else if ($mmflg == "_MIN"){
				if (get_magic_quotes_gpc() == 1) {
					$option .= $modkey . ">='" . $val . "'";
				} else {
					$option .= $modkey . ">='" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			} else if ($mmflg == "_LTN") {
				if (get_magic_quotes_gpc() == 1) {
					$option .= $modkey . "<'" . $val . "'";
				} else {
					$option .= $modkey . "<'" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			} else if ($mmflg == "_GTN") {
				if (get_magic_quotes_gpc() == 1) {
					$option .= $modkey . ">'" . $val . "'";
				} else {
					$option .= $modkey . ">'" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			} else if ($mmflg == "_NOT"){
				if(is_array($val)){
					if (count($val) > 0) {
						if (get_magic_quotes_gpc() == 1) {
							$option .= $modkey . " not in ('";
							$option .= implode("','", $val);
							$option .= "')";
						} else {
							$tmp_list = array();
							foreach ($val as $tmp) {
								$tmp = StringUtil::setMySQLMagicQuotes($tmp);
								array_push($tmp_list, $tmp);
							}

							$option .= $modkey . " not in ('";
							$option .= implode("','", $tmp_list);
							$option .= "')";
						}
					} else {
						$option .= $modkey . " is not null";
					}
					
				} else if (is_int($val)) {
					if (get_magic_quotes_gpc() == 1) {
						$option .= $modkey . "!='" . $val . "'";
					} else {
						$option .= $modkey . "!='" . StringUtil::setMySQLMagicQuotes($val) . "'";
					}
				} else if (is_bool($val)) {
					if ($val) {
						$option .= $modkey . "!=1";
					} else {
						$option .= $modkey . "!=0";
					}
				} else if(($val == "null")||($val == "")){
					$option .= $modkey . " is not null";
				} else {
					if (get_magic_quotes_gpc() == 1) {
						$option .= "(" . $modkey . "!='" . $val . "' OR " . $modkey . " is null)";
					} else {
						$option .= "(" . $modkey . "!='" . StringUtil::setMySQLMagicQuotes($val) . "' OR " . $modkey . " is null)";
					}
				}
			}else{
				if (is_array($val)) {
					if (count($val)> 0) {
						if (get_magic_quotes_gpc() == 1) {
							$option .= $key . " in ('";
							$option .= implode("','", $val);
							$option .= "')";
						} else {	
							$tmp_list = array();
							foreach ($val as $tmp) {
								$tmp = StringUtil::setMySQLMagicQuotes($tmp);
								array_push($tmp_list, $tmp);
							}
							$option .= $key . " in ('";
							$option .= implode("','", $tmp_list);
							$option .= "')";
						}
					} else {
						$option .= $key . " is null";
					}
				} else if (is_int($val)) {
					$option .= $key . "='" . $val . "'";
				} else if (is_bool($val)) {
					if ($val) {
						$option .= $key . "=1";
					} else {
						$option .= $key . "=0";
					}
				} else if ((is_null($val))||($val == "")) {
					$option .= $key . " is null";
				} else {
					if (get_magic_quotes_gpc() == 1) {
						$option .= $key . "='" . $val . "'";
					} else {
						$option .= $key . "='" . StringUtil::setMySQLMagicQuotes($val) . "'";
					}
				}
			}
			
		}
		
		return $option;
	}


	/**
	 * 条件文の生成。set xxxx の、xxxxの部分を返す。
	 */
	public static function createSetValueOption($param){
		
		$option = "";
		while(list($key, $val) = each($param)){
			
			if($option != ""){
				$option .= ", ";
			}
			
			if (is_bool($val)) {
				if ($val) {
					$option .= $key . " = '1'";
				} else {
					$option .= $key . " = '0'";
				}
			} else if (is_null($val)) {
				$option .= $key . " = null";
			} else {
				if (get_magic_quotes_gpc() == 1) {
					$option .= $key . " = '" . $val . "'";
				} else {
					$option .= $key . " = '" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			}
			
		}
		
		return $option;
	}


	/**
	 * 条件文の生成。(xxx) values (xxx)の部分を返す。
	 */
	public static function createInsertValueOption($param){
		
		$str = null;
		$cols = null;
		$vals = null;
		
		while(list($key, $val) = each($param)){
			
			if($cols != ""){
				$cols .= ", ";
			}
			if($vals != ""){
				$vals .= ", ";
			}
			
			$cols .= $key;
			if (is_bool($val)) {
				if ($val) {
					$vals .= "1";
				} else {
					$vals .= "0";
				}
			} else if (is_null($val)) {
				$vals .= "null";
			} else {
				if (get_magic_quotes_gpc() == 1) {
					$vals .= "'" . $val . "'";
				} else {
					$vals .= "'" . StringUtil::setMySQLMagicQuotes($val) . "'";
				}
			}
			
			
		}
		
		$str = "(" . $cols . ") VALUES (" . $vals . ")";
		
		return $str;
	}
	
	/**
	 * 対象となるレコードの先頭とレコード数を指定する文章を生成します。
	 * 不要なパラメータはnullを指定します。
	 */
	public static function createSubQuery($offset, $limit) {
		$subquery = "";
		if (($limit != null)&&(is_numeric($limit)) ){
			$subquery .= " LIMIT " . $limit;
		}
		if (($offset != null)&&(is_numeric($offset)) ){
			$subquery .= " OFFSET " . $offset;
		}
		
		return $subquery;
	}
	
}

?>