PageRenderTime 73ms CodeModel.GetById 22ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/system/engine/engine_library/Copy of dbtools.enginelib.php

https://bitbucket.org/threetopia/thtech
PHP | 1166 lines | 1118 code | 18 blank | 30 comment | 84 complexity | 3013ed3bdda778b1c89b11ad3f8c1df9 MD5 | raw file
   1<?php
   2/*
   3 * File name		: database.class.php
   4 * Author			: Tri Hartanto
   5 * Site				: trihartanto.com
   6 * Framework		: thtech
   7 * Library type		: Engine Database
   8 * Version			: 4
   9 * License			: GPL
  10 * Create Date		: 23 February 2011
  11 * Modified Date	: 29 Jan 2014
  12 * File Description	: This file contains database class to used by the framework and CMS system.
  13 *
  14 * For more license information please kindly open and read LICENSE.txt file
  15 */
  16class DBTools_EngineLibrary extends Systems
  17{		
  18	private $DBToolsLibrary = array();
  19	private function loadDBLibrary($dbtype=NULL)
  20	{
  21		$libraryFile = dirname(__FILE__).'/dbtools_library/'.strtolower($dbtype).'.dbtoolslib.php';
  22		if(!isset($this->DBToolsLibrary[$dbtype]) and file_exists($libraryFile))
  23		{
  24			include_once($libraryFile);
  25			$libraryClass = $dbtype.'_DBToolsLibrary';
  26			$this->DBToolsLibrary[$dbtype] = new $libraryClass();
  27		}
  28		if(isset($this->DBToolsLibrary[$dbtype]))
  29		{
  30			return $this->DBToolsLibrary[$dbtype];
  31		}
  32		$this->_SysEngine->httptools_errorprocess(array('error'=>'500','message'=>'Can\'t open '.$dbtype.' database library.'));
  33		return false;
  34	}
  35	
  36	public $_SysDBData = array();
  37	
  38	private function getDBData($data=NULL)
  39	{
  40		$DBConfig = (isset($data['DBConfig']))?$data['DBConfig']:NULL;
  41		$DBPerRequest = false;
  42		/*
  43		 * Build database source and config
  44		 */
  45		if(!empty($DBConfig))
  46		{
  47			$DBInstance = '_DBPerRequest';
  48			$DBSource = $DBInstance;
  49			$DBConfig = $DBConfig;
  50			$DBPerRequest = true;
  51		}
  52		else if(!empty($this->_CurProgramConfig['DBConfig']))
  53		{
  54			$DBInstance = $this->_CurProgramData['uprgm_name'];
  55			if(isset($this->_SysDBData[$this->_CurProgramData['uprgm_name']]))
  56			{
  57				$DBSource = $this->_SysDBData[$DBInstance]['DBSource'];
  58				$DBConfig = $this->_SysDBData[$DBInstance]['DBConfig'];
  59			}
  60			else
  61			{
  62				$DBSource = $DBInstance;
  63				$DBConfig = $this->_CurProgramConfig['DBConfig'];
  64			}
  65			$DBPerRequest = (!empty($DBConfig['DBPerRequest']))?$DBConfig['DBPerRequest']:false;
  66		}
  67		else
  68		{
  69			$DBInstance = '_DBToolsDefaultConnection';
  70			if(isset($this->_SysDBData[$DBInstance]))
  71			{
  72				$DBSource = $this->_SysDBData[$DBInstance]['DBSource'];
  73				$DBConfig = $this->_SysDBData[$DBInstance]['DBConfig'];
  74			}
  75			else
  76			{
  77				$DBSource = $DBInstance;
  78				$DBConfig = (!empty($this->_LoaderConfig['DBConfig']))?$this->_LoaderConfig['DBConfig']:$this->_SysEngine->configtools_db_config(array('SelectConfig'=>'auto'));
  79			}
  80		}
  81		$DBData = array('DBInstance'=>$DBInstance,'DBSource'=>$DBSource,'DBConfig'=>$DBConfig,'DBPerRequest'=>$DBPerRequest);
  82		/*
  83		 * Initiate connection (This mean the connection never been made and stored (Per Request,First init))
  84		 */
  85		if(!isset($this->_SysDBData[$DBInstance]))
  86		{
  87			$DBConnect = ($DBLibrary = $this->loadDBLibrary($DBConfig['DBType']))?$DBLibrary->connect($DBData):false;
  88			if($DBConnect!=false)
  89			{
  90				$DBData = array('DBInstance'=>$DBInstance,'DBSource'=>$DBConnect,'DBConfig'=>$DBConfig,'DBPerRequest'=>$DBPerRequest);
  91			}
  92			else
  93			{
  94				$this->_SysEngine->httptools_errorprocess(array('error'=>'500','message'=>'Can\'t connect to database.'));
  95			}
  96			/*
  97			 * Then write DBData to GLOBALS variable under their own instance
  98			 */
  99			if($DBPerRequest===false)
 100			{
 101				$this->_SysDBData[$DBInstance] = $DBData;
 102			}
 103		}
 104		return $DBData;
 105	}
 106	
 107	public function Execute($data=NULL)
 108	{
 109		$data['DBData'] = $this->getDBData($data);
 110		if((isset($data['SQL']['SQLSelect']) or isset($data['SQL']['SQLInsert']) or isset($data['SQL']['SQLUpdate']) or isset($data['SQL']['SQLDelete'])) and is_array($data['SQL']))
 111		{
 112		   $BuildSQL = $this->BuildSQL($data);
 113		   $data['SQL'] = $BuildSQL['SQL'];
 114		   $data['SQLAction'] = $BuildSQL['SQLAction'];
 115		}
 116		if(strtoupper($data['SQLAction']) == 'SELECT')
 117		{
 118		   $result = $this->getData($data);
 119		}
 120		else if(in_array(strtoupper($data['SQLAction']),array('INSERT','DELETE','UPDATE')))
 121		{
 122		   $result = $this->executeData($data);
 123		}
 124		if($data['DBData']['DBPerRequest']===true)
 125		{
 126			$CloseDB = $this->Close($data);
 127			unset($data['DBData']['DBSource']);
 128		}
 129		if($result===false)
 130		{
 131			$this->_SysEngine->httptools_errorprocess(array('error'=>'500','message'=>'Could not execute to your database.'));
 132		}
 133		return array('DBData'=>$data['DBData'],"SQL"=>$BuildSQL['SQL'],'result'=>$result);
 134	}
 135	
 136	private function getData($data=NULL)
 137	{
 138		/*
 139		 * Query SQL to Database library
 140		 */
 141	   	if(array_key_exists($data['DBData']['DBConfig']['DBType'],$this->DBToolsLibrary))
 142		{
 143			return $this->DBToolsLibrary[$data['DBData']['DBConfig']['DBType']]->getData($data);
 144		}
 145		return false;
 146	}
 147	
 148	private function executeData($data=NULL)
 149	{
 150	   	/*
 151		 * Execute SQL Query to Database library
 152		 */
 153		if(array_key_exists($data['DBData']['DBConfig']['DBType'],$this->DBToolsLibrary))
 154		{
 155			return $this->DBToolsLibrary[$data['DBData']['DBConfig']['DBType']]->executeData($data);
 156		}
 157		return false;
 158	}
 159	
 160	public function Close($data=NULL)
 161	{
 162		$DBInstance = $data['DBData']['DBInstance'];
 163		$_SysDBData = (array_key_exists($DBInstance,$this->_SysDBData))?$this->_SysDBData[$DBInstance]:$data['DBData'];
 164		$DBType = (!empty($_SysDBData['DBConfig']))?$_SysDBData['DBConfig']['DBType']:NULL;
 165		if(array_key_exists($DBType,$this->DBToolsLibrary))
 166		{
 167			$this->DBToolsLibrary[$DBType]->close(array('DBSource'=>$_SysDBData['DBSource']));
 168			unset($this->_SysDBData[$DBInstance]);
 169		}
 170	}
 171	
 172	public function BuildSQL($data=NULL)
 173	{
 174		$DBData = (!empty($data['DBData']))?$data['DBData']:((!empty($this->_SysDBData['default']))?$this->_SysDBData['default']:NULL);
 175		$DBInstance = (!empty($DBData['DBInstance']))?$DBData['DBInstance']:NULL;
 176		$DBType = (!empty($DBData['DBConfig']['DBType']))?$DBData['DBConfig']['DBType']:NULL;
 177		$SQLAction = strtoupper(str_replace('SQL','',current(array_keys($data['SQL']))));
 178		if(array_key_exists($DBType,$this->DBToolsLibrary) and method_exists($this->DBToolsLibrary[$DBType],'BuildSQL'))
 179		{
 180			$SQL = $this->DBToolsLibrary[$DBType]->BuildSQL($data);
 181		}
 182		else
 183		{
 184			$SQL = $this->defaultBuildSQL($data);
 185		}
 186		return array('SQL'=>$SQL,'SQLAction'=>$SQLAction);
 187	}
 188	
 189	private function defaultBuildSQL($data=NULL,$deep=0,$loop=0)
 190	{
 191		$SQL = "";
 192		if(!empty($data['SQL']))
 193		{
 194			foreach($data['SQL'] as $key=>$val)
 195			{
 196				if(!empty($val) and method_exists($this,$key))
 197				{
 198					$SQL .= $this->$key($val,$deep,$loop);
 199				}
 200			}
 201		}
 202		if($deep>0)
 203		{
 204			$SQL = "(".$SQL.")";
 205		}
 206		return $SQL;
 207	}
 208	
 209	private function SQLSelect($SQL=array(),$deep=0,$loop=0)
 210	{
 211		$SQLSelect = '';
 212		if(!empty($SQL))
 213		{
 214			foreach($SQL as $key=>$val)
 215			{
 216				if(!empty($val))
 217				{
 218					$SQLSelect .= ($loop==0)?"SELECT ":",";
 219					if(!is_numeric($key) and !method_exists($this,$key))
 220					{
 221						$key1 = $key;
 222						$key = "SQLAS";
 223						$val = array($key1=>$val);
 224					}
 225					if(!is_numeric($key) and method_exists($this,$key))
 226					{
 227						$SQLSelect .= $this->$key($val,$deep,0);
 228					}
 229					else if(is_numeric($key) and is_array($val))
 230					{
 231						$deep++;
 232						$SQLSelect .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 233					}
 234					else
 235					{
 236						$ExplodeVal = explode("'",$val);
 237						if(empty($ExplodeVal[0]) and !empty($val))
 238						{
 239							$val = substr($val,1,-1);
 240						}
 241						$JSONVal = json_decode($val);
 242						$val = (!empty($JSONVal))?$val:((!is_numeric($key))?addslashes($val):$val);
 243						$SQLSelect .= $val;
 244					}
 245					$loop++;
 246				}
 247			}
 248		}
 249		return $SQLSelect;
 250	}
 251	
 252	private function SQLAS($SQL=array(),$deep=0,$loop=0)
 253	{
 254		$SQLAS = '';
 255		if(!empty($SQL))
 256		{
 257			foreach($SQL as $key=>$val)
 258			{
 259				if($val!==NULL)
 260				{
 261					$SQLAS .= ($loop==0)?"":",";
 262					if(!is_numeric($key) and method_exists($this,$key))
 263					{
 264						$SQLAS .= $this->$key($val,$deep,$loop);
 265					}
 266					else if(is_array($val))
 267					{
 268						$deep++;
 269						$SQLAS .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 270					}
 271					else
 272					{
 273						$SQLAS .= $val;
 274					}
 275					$SQLAS .= " AS `".$key."`";
 276					$loop++;
 277				}
 278			}
 279		}
 280		return $SQLAS;
 281	}
 282	
 283	private function SQLFrom($SQL=array(),$deep=0,$loop=0)
 284	{
 285		$SQLFrom  = '';
 286		if(!empty($SQL))
 287		{
 288			foreach($SQL as $key=>$val)
 289			{
 290				if(!empty($val))
 291				{
 292					$SQLFrom .= ($loop==0)?" FROM ":",";
 293					if(!is_numeric($key) and method_exists($this,$key))
 294					{
 295						$SQLFrom .= $this->$key($val,$deep,$loop);
 296					}
 297					else if(is_numeric($key) and is_array($val))
 298					{
 299						$deep++;
 300						$SQLFrom .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 301					}
 302					else
 303					{
 304						$SQLFrom .= ((is_string($key))?"`".$key."` ".$val."":"".$val."");
 305					}
 306					$loop++;
 307				}
 308			}
 309		}
 310		return $SQLFrom;
 311	}
 312	
 313	private function SQLJoin($SQL=array(),$deep=0,$loop=0)
 314	{
 315		$SQLJoin  = '';
 316		if(!empty($SQL))
 317		{
 318			foreach($SQL as $key=>$val)
 319			{
 320				if(is_array($val))
 321				{
 322					$loop1=0;
 323					foreach($val as $key1=>$val1)
 324					{
 325						if(!empty($val1))
 326						{
 327							$loop1++;
 328							$SQLJoin .= (!empty($val1))?(" ".$key." JOIN "):"";
 329							if(!is_numeric($key1) and method_exists($this,$key1))
 330							{
 331								$SQLJoin .= $this->$key1($val1);
 332							}
 333							else if(is_numeric($key1) and is_array($val1))
 334							{
 335								$deep++;
 336								$SQLJoin .= $this->defaultBuildSQL(array('SQL'=>$val1),$deep);
 337							}	
 338							else if(is_array($val1))
 339							{
 340								foreach($val1 as $key2=>$val2)
 341								{
 342									if(!empty($val2))
 343									{
 344										if(!is_numeric($key2) and method_exists($this,$key2))
 345										{
 346											$SQLJoin .= $key1." ".$this->$key2($val2);
 347										}
 348										else if(is_numeric($key2) and is_array($val2))
 349										{
 350											$deep++;
 351											$SQLJoin .= $this->defaultBuildSQL(array('SQL'=>$val2),$deep);
 352										}
 353										else
 354										{
 355											$SQLJoin .= ((is_string($key2))?"`".$key2."` ":"").$val2."";
 356										} 
 357									}
 358								}
 359							}				
 360							else
 361							{
 362								$SQLJoin .= ((!is_numeric($key1))?$key1.' ON ':'').$val1;
 363							}
 364						}
 365					}
 366				}
 367				else
 368				{
 369					$ExplodeVal = explode("'",$val);
 370					if(empty($ExplodeVal[0]) and !empty($val))
 371					{
 372						$val = substr($val,1,-1);
 373					}
 374					$JSONVal = json_decode($val,true);
 375					$val = (!empty($JSONVal))?$val:addslashes($val);
 376					$SQLJoin .= $val;
 377				}
 378				$loop++;
 379			}
 380		}
 381		return $SQLJoin;
 382	}
 383	
 384	private function SQLON($SQL=array(),$deep=0,$loop=0)
 385	{
 386		$SQLON = "";
 387		if(!empty($SQL))
 388		{
 389			$inloop=0;
 390			foreach($SQL as $key=>$val)
 391			{
 392				if($val!==NULL)
 393				{
 394					if($inloop>0 and !is_numeric($key) and !method_exists($this,$key))
 395					{
 396						$key1 = $key;
 397						$key = "SQLAND";
 398						$val = array($key1=>$val);
 399					}
 400					$SQLON .= ($loop==0)?" ON ":"";
 401					if(!is_numeric($key) and method_exists($this,$key))
 402					{
 403						$SQLON .= $this->$key($val,$deep,$loop);
 404					}
 405					else if(is_numeric($key) and is_array($val))
 406					{
 407						$deep++;
 408						$SQLON .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 409					}
 410					else
 411					{
 412						$ExplodeVal = explode("'",$val);
 413						if(empty($ExplodeVal[0]) and !empty($val))
 414						{
 415							$val = substr($val,1,-1);
 416						}
 417						$JSONVal = json_decode($val,true);
 418						$val = (!empty($JSONVal))?$val:addslashes($val);
 419						$SQLON .= ((is_string($key))?"".$key."=":"").$val."";
 420					}
 421					$loop++;
 422					$inloop++;
 423				}
 424			}
 425		}
 426		return $SQLON;
 427	}
 428	
 429	private function SQLAND($SQL=array(),$deep=0,$loop=0)
 430	{
 431		$SQLAND = "";
 432		if(!empty($SQL))
 433		{
 434			foreach($SQL as $key=>$val)
 435			{
 436				if($val!==NULL)
 437				{
 438					$SQLAND .= ($loop==0)?"":" AND ";
 439					if(!is_numeric($key) and method_exists($this,$key))
 440					{
 441						$SQLAND .= $this->$key($val,$deep,$loop);
 442					}
 443					else if(is_numeric($key) and is_array($val))
 444					{
 445						$deep++;
 446						$SQLAND .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 447					}
 448					else
 449					{
 450						$ExplodeVal = explode("'",$val);
 451						if(empty($ExplodeVal[0]) and !empty($val))
 452						{
 453							$val = substr($val,1,-1);
 454						}
 455						$JSONVal = json_decode($val,true);
 456						$val = (!empty($JSONVal))?$val:addslashes($val);
 457						$SQLAND .= ((is_string($key))?" ".$key."=":"").$val."";
 458					}
 459					$loop++;
 460				}
 461			}
 462		}
 463		return $SQLAND;
 464	}
 465	
 466	private function SQLOR($SQL=array(),$deep=0,$loop=0)
 467	{
 468		$SQLOR = "";
 469		if(!empty($SQL))
 470		{
 471			foreach($SQL as $key=>$val)
 472			{
 473				if(!empty($val))
 474				{
 475					$SQLOR .= ($loop==0)?"":" OR ";
 476					if(!is_numeric($key) and method_exists($this,$key))
 477					{
 478						$SQLOR .= $this->$key($val,$deep,$loop);
 479					}
 480					else if(is_numeric($key) and is_array($val))
 481					{
 482						$deep++;
 483						$SQLOR .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 484					}
 485					else
 486					{
 487						$ExplodeVal = explode("'",$val);
 488						if(empty($ExplodeVal[0]) and !empty($val))
 489						{
 490							$val = substr($val,1,-1);
 491						}
 492						$JSONVal = json_decode($val,true);
 493						$val = (!empty($JSONVal))?$val:addslashes($val);
 494						$SQLOR .= ((!is_numeric($key))?"".$key."='".$val."'":$val);
 495					}
 496					$loop++;
 497				}
 498			}
 499		}
 500		return $SQLOR;
 501	}
 502	
 503	private function SQLIN($SQL=array(),$deep=0,$loop=0)
 504	{
 505		$SQLIN = "";
 506		if(!empty($SQL))
 507		{
 508			$inloop = 0;
 509			foreach($SQL as $key=>$val)
 510			{
 511				if(!empty($val))
 512				{
 513					if($inloop>0 and !is_numeric($key) and !method_exists($this,$key))
 514					{
 515						$key1 = $key;
 516						$key = "SQLAND";
 517						$val = array("SQLIN"=>array($key1=>$val));
 518					}
 519					if(!is_numeric($key) and method_exists($this,$key))
 520					{
 521						$SQLIN .= $this->$key($val,$deep,$loop);
 522					}
 523					else if(is_numeric($key) and is_array($val))
 524					{
 525						$deep++;
 526						$SQLIN .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 527					}
 528					else
 529					{
 530						$SQLIN .= ((!method_exists($this,$key))?$key:"")." IN ";
 531						$loop1 = 0;
 532						$SQLIN .= "(";
 533						foreach($val as $key1=>$val1)
 534						{
 535							if(!is_numeric($key1) and method_exists($this,$key1))
 536							{
 537								$SQLIN .= $this->$key1($val1,$deep,$loop);
 538							}
 539							else if(is_numeric($key1) and is_array($val1))
 540							{
 541								$SQLIN .= $this->defaultBuildSQL(array('SQL'=>$val1),$deep);
 542							}
 543							else
 544							{
 545								$SQLIN .= (($loop1>0)?",":"");
 546								$ExplodeVal = explode("'",$val1);
 547								if(empty($ExplodeVal[0]) and !empty($val))
 548								{
 549									$val1 = substr($val1,1,-1);
 550								}
 551								$JSONVal = json_decode($val1,true);
 552								$val1 = (!empty($JSONVal))?$val1:addslashes($val1);
 553								$SQLIN .= "'".$val1."'";
 554								$loop1++;
 555							}
 556						}
 557						$SQLIN .= ")";
 558					}
 559					$loop++;
 560					$inloop++;
 561				}
 562			}
 563		}
 564		return $SQLIN;
 565	}
 566	
 567	private function SQLNOTIN($SQL=array(),$deep=0,$loop=0)
 568	{
 569		$SQLNOTIN  = "";
 570		if(!empty($SQL))
 571		{
 572			$inloop = 0;
 573			foreach($SQL as $key=>$val)
 574			{
 575				if(!empty($val))
 576				{
 577					if($inloop>0 and !is_numeric($key) and !method_exists($this,$key))
 578					{
 579						$key1 = $key;
 580						$key = "SQLAND";
 581						$val = array("SQLNOTIN"=>array($key1=>$val));
 582						//echo $key,"|";print_r($val);echo "<br><br>";
 583					}
 584					if(!is_numeric($key) and method_exists($this,$key))
 585					{
 586						$SQLNOTIN .= $this->$key($val,$deep,$loop);
 587					}
 588					else if(is_numeric($key) and is_array($val))
 589					{
 590						$deep++;
 591						$SQLNOTIN .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 592					}
 593					else
 594					{
 595						$SQLNOTIN .= ((!method_exists($this,$key))?$key:"")." NOT IN ";
 596						$loop1 = 0;
 597						$SQLNOTIN .= "(";
 598						foreach($val as $key1=>$val1)
 599						{
 600							$SQLNOTIN .= (($loop1>0)?",":"");
 601							$ExplodeVal = explode("'",$val1);
 602							if(empty($ExplodeVal[0]) and !empty($val))
 603							{
 604								$val1 = substr($val1,1,-1);
 605							}
 606							$JSONVal = json_decode($val1,true);
 607							$val1 = (!empty($JSONVal))?$val1:addslashes($val1);
 608							$SQLNOTIN .= "'".$val1."'";
 609							$loop1++;
 610						}
 611						$SQLNOTIN .= ")";
 612					}
 613					$loop++;
 614					$inloop++;
 615				}
 616			}
 617		}
 618		return $SQLNOTIN;
 619	}
 620
 621	private function SQLISNOT($SQL=array(),$deep=0,$loop=0)
 622	{
 623		$SQLISNOT  = "";
 624		if(!empty($SQL))
 625		{
 626			$inloop = 0;
 627			foreach($SQL as $key=>$val)
 628			{
 629				if($val!='')
 630				{
 631					if($inloop>0 and !is_numeric($key) and !method_exists($this,$key))
 632					{
 633						$key1 = $key;
 634						$key = "SQLAND";
 635						$val = array("SQLISNOT"=>array($key1=>$val));
 636					}
 637					if(!is_numeric($key) and method_exists($this,$key))
 638					{
 639						$SQLISNOT .= $this->$key($val,$deep,$loop);
 640					}
 641					else if(is_numeric($key) and is_array($val))
 642					{
 643						$deep++;
 644						$SQLISNOT .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 645					}
 646					else
 647					{
 648						$SQLISNOT .= ((!empty($key))?"".$key."":"")." IS NOT ".$val;
 649					}
 650					$loop++;
 651					$inloop++;
 652				}
 653			}
 654		}
 655		return $SQLISNOT;
 656	}
 657	
 658	private function SQLIS($SQL=array(),$deep=0,$loop=0)
 659	{
 660		$SQLIS = "";
 661		if(!empty($SQL))
 662		{
 663			$inloop = 0;
 664			foreach($SQL as $key=>$val)
 665			{
 666				if(!empty($val))
 667				{
 668					if($inloop>0 and !is_numeric($key) and !method_exists($this,$key))
 669					{
 670						$key1 = $key;
 671						$key = "SQLAND";
 672						$val = array("SQLIS"=>array($key1=>$val));
 673					}
 674					if(!is_numeric($key) and method_exists($this,$key))
 675					{
 676						$SQLIS .= $this->$key($val,$deep,$loop);
 677					}
 678					else if(is_numeric($key) and is_array($val))
 679					{
 680						$deep++;
 681						$SQLIS .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 682					}
 683					else
 684					{
 685						$SQLIS .= ((!empty($key))?"".$key."":"")." IS ".$val;
 686					}
 687					$loop++;
 688					$inloop++;
 689				}
 690			}
 691		}
 692		return $SQLIS;
 693	}
 694	
 695	private function SQLIF($SQL=array(),$deep=0,$loop=0)
 696	{
 697		$SQLIF = "";
 698		if(!empty($SQL))
 699		{
 700			$outloop = 0;
 701			foreach($SQL as $key0=>$val0)
 702			{
 703				if(!is_numeric($key0) and method_exists($this,$key0))
 704				{
 705					$SQLIF .= $this->$key0($val0,$deep,$loop);
 706				}
 707				else if($outloop>1)
 708				{
 709					$key0 = "SQLAND"; 
 710					$SQLIF .= $this->$key0(array("SQLIF"=>array($val0)),$deep);
 711				}
 712				else if(!empty($val0))
 713				{
 714					$inloop = 0;
 715					$count = count($val0);
 716					foreach($val0 as $key1=>$val1)
 717					{
 718						$SQLIF .= ($inloop==0)?"IF(":",";
 719						if(!is_numeric($key1) and method_exists($this,$key1))
 720						{
 721							$SQLIF .= $this->$key1($val1,$deep,$loop);
 722						}
 723						else if(is_numeric($key1) and is_array($val1))
 724						{
 725							$deep++;
 726							$SQLIF .= $this->defaultBuildSQL(array('SQL'=>$val1),$deep);
 727						}
 728						else
 729						{
 730							$ExplodeVal1 = explode("'",$val1);
 731							if(empty($ExplodeVal1[0]) and !empty($val1))
 732							{
 733								$val1 = substr($val1,1,-1);
 734							}
 735							$JSONVal1 = json_decode($val1,true);
 736							$SQLIF .= ((!is_numeric($key1))?$key1."=".((!empty($JSONVal1))?$val1:"'".addslashes($val1)."'"):"".$val1."");
 737						}
 738						$SQLIF .= ($inloop==$count)?")":"";
 739						$inloop++;
 740					}
 741				}
 742				$loop++;
 743				$outloop++;
 744			}
 745		}
 746		return $SQLIF;
 747	}
 748		
 749	private function SQLLIKE($SQL=array(),$deep=0,$loop=0)
 750	{
 751		$SQLLIKE  = "";
 752		if(!empty($SQL))
 753		{
 754			$inloop = 0;
 755			foreach($SQL as $key=>$val)
 756			{
 757				if(!empty($val))
 758				{
 759					if($inloop>0 and !is_numeric($key) and !method_exists($this,$key))
 760					{
 761						$key1 = $key;
 762						$key = "SQLAND";
 763						$val = array("SQLLIKE"=>array($key1=>$val));
 764					}
 765					$SQLLIKE .= ((!is_numeric($key) and !method_exists($this,$key))?"".$key." LIKE ":"");
 766					if(!is_numeric($key) and method_exists($this,$key))
 767					{
 768						$SQLLIKE .= $this->$key($val,$deep,$loop);
 769					}
 770					else if(is_numeric($key) and is_array($val))
 771					{
 772						$deep++;
 773						$SQLLIKE .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 774					}
 775					else
 776					{
 777						$SQLLIKE .= "'".$val."'";
 778					} 
 779					$loop++;
 780					$inloop++;
 781				}
 782			}
 783		}
 784		return $SQLLIKE;
 785	}
 786	
 787	private function SQLWhere($SQL=NULL,$deep=0,$loop=0)
 788	{
 789		$SQLWhere  = "";
 790		if(!empty($SQL))
 791		{
 792			foreach($SQL as $key=>$val)
 793			{
 794				if($val!==NULL)
 795				{
 796					$SQLWhere .= ($loop==0 and !empty($val))?" WHERE ":(($key==='SQLOR')?"":" AND ");
 797					if(!is_numeric($key) and method_exists($this,$key))
 798					{
 799						$SQLWhere .= $this->$key($val,$deep,$loop);
 800					}
 801					else if(is_numeric($key) and is_array($val))
 802					{
 803						$deep++;
 804						$SQLWhere .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 805					}
 806					else
 807					{
 808						$ExplodeVal = explode("'",$val);
 809						if(empty($ExplodeVal[0]) and !empty($val))
 810						{
 811							$val = substr($val,1,-1);
 812						}
 813						$JSONVal = json_decode($val,true);
 814						$val = (!empty($JSONVal))?$val:addslashes($val);
 815						$SQLWhere .= ((!is_numeric($key))?"".$key."='".$val."'":$val);
 816					}
 817					$loop++;
 818				}
 819			}
 820		}
 821		return $SQLWhere;
 822	}
 823	
 824	private function SQLOPERAND($SQL=NULL,$deep=0,$loop=0)
 825	{
 826		$SQLOPERAND = "";
 827		if(!empty($SQL))
 828		{
 829			$inloop = 0;
 830			foreach($SQL as $key0=>$val0)
 831			{
 832				if(!empty($val0))
 833				{
 834					foreach($val0 as $key1=>$val1)
 835					{
 836						if($inloop>0 and !is_numeric($key1) and !method_exists($this,$key1))
 837						{
 838							$key2 = $key0;
 839							$val1 = array("SQLOPERAND"=>array($key2=>array($key1=>$val1)));
 840							$key1 = "SQLAND";
 841						}
 842						//$SQLOPERAND .= ((!is_numeric($key) and !method_exists($this,$key))?"".$key." LIKE ":"");
 843						if(!is_numeric($key1) and method_exists($this,$key1))
 844						{
 845							$SQLOPERAND .= $this->$key1($val1,$deep,$loop);
 846						}
 847						else if(is_numeric($key1) and is_array($val1))
 848						{
 849							$deep++;
 850							$SQLOPERAND .= $this->defaultBuildSQL(array('SQL'=>$val1),$deep);
 851						}
 852						else
 853						{
 854							$SQLOPERAND .= $key1.' '.$key0.' '.$val1;
 855						} 
 856						$loop++;
 857						$inloop++;
 858					}
 859				}
 860			}
 861		}
 862		return $SQLOPERAND;
 863	}
 864	
 865	private function SQLUpdate($SQL=NULL,$deep=0,$loop=0)
 866	{
 867		$SQLUpdate = "";
 868		if(!empty($SQL))
 869		{
 870			foreach($SQL as $key=>$val)
 871			{
 872				if(!empty($val))
 873				{
 874					$SQLUpdate .= ($loop==0 and !empty($val))?" UPDATE ":",";
 875					if(!is_numeric($key) and method_exists($this,$key))
 876					{
 877						$SQLUpdate .= $this->$key($val,$deep,$loop);
 878					}
 879					else if(is_numeric($key) and is_array($val))
 880					{
 881						$deep++;
 882						$SQLUpdate .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 883					}
 884					else 
 885					{
 886						$ExplodeVal = explode("'",$val);
 887						if(empty($ExplodeVal[0]) and !empty($val))
 888						{
 889							$val = substr($val,1,-1);
 890						}
 891						$JSONVal = json_decode($val,true);
 892						$val = (!empty($JSONVal))?$val:addslashes($val);
 893						$SQLUpdate .= "".$val."";
 894					}
 895					$loop++;
 896				}
 897			}
 898		}
 899		return $SQLUpdate;
 900	}
 901	
 902	private function SQLInsert($SQL=NULL,$deep=0,$loop=0)
 903	{
 904		$SQLInsert  = "";
 905		if(!empty($SQL))
 906		{
 907			foreach($SQL as $key=>$val)
 908			{
 909				if(!empty($val))
 910				{
 911					$SQLInsert .= ($loop==0 and !empty($val))?" INSERT INTO ":"";
 912					if(!is_numeric($key) and method_exists($this,$key))
 913					{
 914						$SQLInsert .= $this->$key($val,$deep,$loop);
 915					}
 916					else if(is_numeric($key) and is_array($val))
 917					{
 918						$deep++;
 919						$SQLInsert .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 920					}
 921					else
 922					{
 923						$ExplodeVal = explode("'",$val);
 924						if(empty($ExplodeVal[0]) and !empty($val))
 925						{
 926							$val = substr($val,1,-1);
 927						}
 928						$JSONVal = json_decode($val,true);
 929						$val = (!empty($JSONVal))?$val:addslashes($val);
 930						$SQLInsert .= "".$val."";
 931					}
 932					$loop++;
 933				}
 934			}
 935		}
 936		return $SQLInsert;
 937	}
 938	
 939	private function SQLSet($SQL=NULL,$deep=0,$loop=0)
 940	{
 941		$SQLSet  = "";
 942		if(!empty($SQL))
 943		{
 944			foreach($SQL as $key=>$val)
 945			{
 946				if($val!==NULL)
 947				{
 948					$SQLSet .= ($loop==0)?" SET ":",";
 949					if(!is_numeric($key) and method_exists($this,$key))
 950					{
 951						$SQLSet .= $this->$key($val,$deep,$loop);
 952					}
 953					else if(is_numeric($key) and is_array($val))
 954					{
 955						$deep++;
 956						$SQLSet .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
 957					}
 958					else
 959					{
 960						$ExplodeVal = explode("'",$val);
 961						if(empty($ExplodeVal[0]) and !empty($val))
 962						{
 963							$val = substr($val,1,-1);
 964						}
 965						$JSONVal = json_decode($val,true);
 966						$val = (!empty($JSONVal))?$val:addslashes($val);
 967						$SQLSet .= ((!is_numeric($key))?"".$key."=":"").(($val!==NULL)?"'".$val."'":'NULL');
 968					}
 969					$loop++;
 970				}
 971			}
 972		}
 973		return $SQLSet;
 974	}
 975	
 976	private function SQLValues($SQL=NULL,$deep=0,$loop=0)
 977	{
 978		$SQLValues  = "";
 979		if(!empty($SQL))
 980		{
 981			$countColumn = count($SQL);
 982			$countValue = ((count($SQL,COUNT_RECURSIVE))-($countColumn))/$countColumn;
 983			$SQLColumn = "";
 984			$SQLValue = "";
 985			for($loop=0;$loop<$countValue;$loop++)
 986			{
 987				$columnloop = 0;
 988				$SQLValue .= ($loop==0)?"":",";
 989				foreach($SQL as $key=>$val)
 990				{
 991					$columnloop++;
 992					if($loop==0)
 993					{
 994						$SQLColumn .= ($columnloop==1)?"":",";
 995						$SQLColumn .= $key;
 996					}
 997					$SQLValue .= ($columnloop==1)?"(":",";
 998					$ExplodeVal = explode("'",$val[$loop]);
 999					if(empty($ExplodeVal[0]) and isset($val[$loop]) and !is_numeric($val[$loop]))
1000					{
1001						$val[$loop] = substr($val[$loop],1,-1);
1002					}
1003					$JSONVal = json_decode($val[$loop],true);
1004					$val[$loop] = (!empty($JSONVal))?$val[$loop]:addslashes($val[$loop]);
1005					$SQLValue .= ($val[$loop]!==NULL)?(($val[$loop]!='' and is_numeric($val[$loop]))?"".$val[$loop]."":"'".$val[$loop]."'"):'NULL';
1006					$SQLValue .= ($columnloop==$countColumn)?")":"";
1007				}
1008			}
1009			$SQLValues .= " (";
1010			$SQLValues .= $SQLColumn;
1011			$SQLValues .= ")";
1012			$SQLValues .= " VALUES ";
1013			$SQLValues .=  $SQLValue;
1014		}
1015		return $SQLValues;
1016	}
1017	
1018	private function SQLGroup($SQL=NULL,$deep=0,$loop=0)
1019	{
1020		$SQLGroup  = "";
1021		if(!empty($SQL))
1022		{
1023			foreach($SQL as $key=>$val)
1024			{
1025				if(!empty($val))
1026				{
1027					$SQLGroup .= ($loop==0 and !empty($val))?" GROUP BY ":",";
1028					if(!is_numeric($key) and method_exists($this,$key))
1029					{
1030						$SQLGroup .= $this->$key($val,$deep,$loop);
1031					}
1032					else if(is_numeric($key) and is_array($val))
1033					{
1034						$deep++;
1035						$SQLGroup .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
1036					}
1037					else
1038					{
1039						$ExplodeVal = explode("'",$val);
1040						if(empty($ExplodeVal[0]) and !empty($val))
1041						{
1042							$val = substr($val,1,-1);
1043						}
1044						$JSONVal = json_decode($val);
1045						$val = (!empty($JSONVal))?$val:addslashes($val);
1046						$SQLGroup .= "".$val."";
1047					}
1048					$loop++;
1049				}
1050			}
1051		}
1052		return $SQLGroup;
1053	}
1054	
1055	private function SQLOrder($SQL=NULL,$deep=0,$loop=0)
1056	{
1057		$SQLOrder  = "";
1058		if(!empty($SQL))
1059		{
1060			foreach($SQL as $key=>$val)
1061			{
1062				if(!empty($val))
1063				{
1064					$SQLOrder .= ($loop==0 and !empty($val))?" ORDER BY ":",";
1065					if(!is_numeric($key) and method_exists($this,$key))
1066					{
1067						$SQLOrder .= $this->$key($val,$deep,$loop);
1068					}
1069					else if(is_numeric($key) and is_array($val))
1070					{
1071						$deep++;
1072						$SQLOrder .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
1073					}
1074					else
1075					{
1076						$ExplodeVal = explode("'",$val);
1077						if(empty($ExplodeVal[0]))
1078						{
1079							$val = substr($val,1,-1);
1080						}
1081						$JSONVal = json_decode($val);
1082						$val = (!empty($JSONVal))?$val:addslashes($val);
1083						$SQLOrder .= ((!is_numeric($key))?"".$key." ":"")."".$val."";
1084					}
1085					$loop++;
1086				}
1087			}
1088		}
1089		return $SQLOrder;
1090	}
1091	
1092	private function SQLHaving($SQL=NULL,$deep=0,$loop=0)
1093	{
1094		$SQLHaving  = "";
1095		if(!empty($SQL))
1096		{
1097			foreach($SQL as $key=>$val)
1098			{
1099				if(!empty($val))
1100				{
1101					$SQLHaving .= ($loop==0 and !empty($val))?" HAVING ":" AND ";
1102					if(!is_numeric($key) and method_exists($this,$key))
1103					{
1104						$SQLHaving .= $this->$key($val,$deep,$loop);
1105					}
1106					else if(is_numeric($key) and is_array($val))
1107					{
1108						$deep++;
1109						$SQLHaving .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
1110					}
1111					else
1112					{
1113						$ExplodeVal = explode("'",$val);
1114						if(empty($ExplodeVal[0]) and !empty($val))
1115						{
1116							$val = substr($val,1,-1);
1117						}
1118						$JSONVal = json_decode($val);
1119						$val = (!empty($JSONVal))?$val:addslashes($val);
1120						$SQLHaving .= ((!is_numeric($key))?"".$key."='".$val."'":"".$val."");
1121					}
1122					$loop++;
1123				}
1124			}
1125		}
1126		return $SQLHaving;
1127	}
1128	
1129	private function SQLLimit($SQL=NULL,$deep=0,$loop=0)
1130	{
1131		$SQLHaving  = "";
1132		if(!empty($SQL))
1133		{
1134			foreach($SQL as $key=>$val)
1135			{
1136				if(!empty($val))
1137				{
1138					$SQLHaving .= ($loop==0 and !empty($val))?" LIMIT ":",";
1139					if(!is_numeric($key) and method_exists($this,$key))
1140					{
1141						$SQLHaving .= $this->$key($val,$deep,$loop);
1142					}
1143					else if(is_numeric($key) and is_array($val))
1144					{
1145						$deep++;
1146						$SQLHaving .= $this->defaultBuildSQL(array('SQL'=>$val),$deep);
1147					}
1148					else
1149					{
1150						$ExplodeVal = explode("'",$val);
1151						if(empty($ExplodeVal[0]) and !empty($val))
1152						{
1153							$val = substr($val,1,-1);
1154						}
1155						$JSONVal = json_decode($val);
1156						$val = (!empty($JSONVal))?$val:addslashes($val);
1157						$SQLHaving .= ((!empty($key))?"".$key.",":"")."".$val."";
1158					}
1159					$loop++;
1160				}
1161			}
1162		}
1163		return $SQLHaving;
1164	}
1165}
1166?>