<?php
/**
 * PostgresSQLBuilderJP
 * 条件文の生成。where xxxx の、xxxxの部分を返す。
 * 日本語テーブル、フィールドを利用しているデータベース用
 * $Id: PostgresSQLBuilderJP.inc,v 1.1 2015/10/08 11:18:57 wanggb Exp $
 * @author iimuro
 * @access public
 * @package jp.aimslib2.database.postgres
 */

class PostgresSQLBuilderJP extends PostgresSQLBuilder{
	/**
	 * AND,OR等Where文の中身を生成する。
	 */
	public function createOptionCondition($param){
		
		$option = "";
		while (list($key, $val) = each($param)) {
			
			if ($option != "") {
				$option .= " AND ";
			}
			
			$mmflg = "";
			$modkey = "";
			if(strlen($key) > 4){
				$mmflg = strtoupper(substr($key, strlen($key) - 4));
				$modkey = substr($key, 0, strlen($key) - 4);
			}
			if($mmflg == "_ALT"){
				$option .= "\"" . $modkey . "\"::text like '" . $val . "'";
			} else if($mmflg == "_MAX"){
				$option .= "\"" . $modkey . "\"<='" . $val . "'";
			} else if ($mmflg == "_MIN"){
				$option .= "\"" . $modkey . "\">='" . $val . "'";
			} else if ($mmflg == "_NOT"){
				if(is_array($val)){
					if (count($val) > 0) {
						$option .= "\"" . $key . "\" not in ('";
						$option .= implode("','", $val);
						$option .= "')";
					} else {
						$option .= "\"" . $key . "\" is not null";
					}
					
				} else if (is_int($val)) {
					$option .= "\"" . $modkey . "\"!='" . $val . "'";
				}else if(($val == "null")||($val == "")){
					$option .= "\"" . $modkey . "\" is not null";
				}else{
					//$option .= $modkey . "!='" . $val . "'";
					$option .= "(\"" . $modkey . "\"!='" . $val . "' OR \"" . $modkey . "\" is null)";
				}
			}else{
				if (is_array($val)) {
					if (count($val) > 0) {
						$option .= "\"" . $key . "\" in ('";
						$option .= implode("','", $val);
						$option .= "')";
					} else {
						$option .= "\"" . $key . "\" is null";
					}
				} else if (is_int($val)) {
					$option .= "\"" . $key . "\"='" . $val . "'";
				} else if (($val == "null")||($val == "")) {
					$option .= "\"" . $key . "\" is null";
				} else {
					$option .= "\"" . $key . "\"='" . $val . "'";
				}
			}
			
		}
		
		return $option;
	}


	/**
	 * created 10/27/2002 by Yu.
	 * 条件文の生成。set xxxx の、xxxxの部分を返す。
	 */
	// copyed 10/25/2002 by NAGASAWA Teruo
	public static function createSetValueOption($param){
		
		$option = "";
		while(list($key, $val) = each($param)){
			
			if($option != ""){
				$option .= ",";
			}
			
			$option .= "\"" . $key . "\"='" . $val . "'";
			
			
		}
		
		return $option;
	}


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

?>