+class SQLHelper{public$s;public$connections;function __construct($a,$b=null,$c=null,$d=null,$e=array()){$this->connections=array();$f=gettype($a);if($f=="array"){if(gettype($a[0])=="array"){foreach($a as$g=>$h){$i=isset($h["host"])?$h["host"]: "";$b=isset($h["db"])?$h["db"]: "";$c=isset($h["user"])?$h["user"]: "";$d=isset($h["password"])?$h["password"]: "";$j=isset($h["options"])?$h["options"]: array();$k=self::connect($i,$b,$c,$d,$j);array_push($this->connections,$k);}}else{foreach($a as$g=>$h){array_push($this->connections,$h);}}$this->s=$this->connections[0];}else if($f=="string"){$this->s=self::connect($a,$b,$c,$d,$e);array_push($this->connections,$this->s);}else{array_push($this->connections,$a);$this->s=$a;}}static function connect($i,$b,$c,$d,$e=array()){$l="mysql";$m=false;if(gettype($e)=="string"){if(strpos($e,":")!==false){$m=$e;}else{$l=strtolower($e);}}else if(isset($e["dbtype"]))$l=strtolower($e["dbtype"]);if(!$m){$n="";switch($l){case "pgsql":$n="pgsql";$o=array("dbname"=>$b,"host"=>$i);if(isset($e["port"]))$o["port"]=$e["port"];break;case "sybase":$n="dblib";$o=array("dbname"=>$b,"host"=>$i);if(isset($e["port"]))$o["port"]=$e["port"];break;case "oracle":$n="oci";$o=array("dbname"=>isset($i)? "//".$i.":".(isset($e["port"])?$e["port"]: "1521")."/".$b :$b);break;default:$n="mysql";$o=array("dbname"=>$b);if(isset($e["socket"]))$o["unix_socket"]=$e["socket"];else{$o["host"]=$i;if(isset($e["port"]))$o["port"]=$e["port"];}break;}$m=$n.":";if(isset($e['charset'])){$o['charset']=$e['charset'];}$m=$n.":";$p=0;foreach($o as$g=>$q){if($p!=0){$m.=";";}$m.=$g."=".$q;$p++;}}return new SuperSQL($m,$c,$d);}private static function rmComments($r){$s=strpos($r,"#");if($s!==false){$r=substr($r,0,$s);}return trim($r);}private static function escape($t){$u=strtolower(gettype($t));if($u=="boolean"){$t=$t ? "1" : "0";}else if($u=="string"){$t="'".$t."'";}else if($u=="double"||$u=="integer"){$t=(int)$t;}else if($u=="null"){$t="0";}return$t;}private static function escape2($t){if(is_numeric($t)){return(int)$t;}else{return "'".$t."'";}}private static function includes($q,$v){foreach($v as$h){if(strpos($q,$h)!==false)return true;}return false;}private static function containsAdv($v){foreach($v as$g=>$q){if(gettype($q)=="array")return true;if(self::includes($g,array("[","#"))||self::includes($q,array("[")))return true;if(self::includes($q,array("DISTINCT","INSERT INTO","INTO")))return true;}return false;}function change($w){$this->s=$this->connections[$w];return$this->s;}function getCon($x=false){if($x){return$this->connections;}else{return$this->s;}}function get($y,$z,$aa,$ba=null){$ca=$this->s->SELECT($y,$z,$aa,$ba,1)->getData();return($ca&&$ca[0])?$ca[0]: false;}function create($y,$o){$da="CREATE TABLE `".$y."` (";$s=0;foreach($o as$g=>$q){if($s!=0){$da.=", ";}$da.="`".$g."` ".$q;$s++;}$da.=")";return$this->s->query($da);}function drop($y){return$this->s->query("DROP TABLE `".$y."`");}function replace($y,$o,$aa=array()){$ea=array();foreach($o as$g=>$q){$r="`".self::rmComments($g)."`";foreach($q as$fa=>$h){$r="REPLACE(".$r.", ".self::escape2($fa).", ".self::escape($h).")";}$ea["#".$g]=$r;}return$this->s->UPDATE($y,$ea,$aa);}function select($y,$z=array(),$aa=array(),$ba=null,$ga=false){if(gettype($y)=="array"||self::containsAdv($z)||self::containsAdv($aa)||$ba){return$this->s->SELECT($y,$z,$aa,$ba,$ga);}else{if(gettype($ga)=="integer")$ga="LIMIT ".(int)$ga;return$this->s->sSELECT($y,$z,$aa,$ga);}}function insert($y,$o){if(gettype($y)=="array"||self::containsAdv($o)){return$this->s->INSERT($y,$o);}else{return$this->s->sINSERT($y,$o);}}function update($y,$o,$aa=array()){if(gettype($y)=="array"||self::containsAdv($o)||self::containsAdv($aa)){return$this->s->UPDATE($y,$o,$aa);}else{return$this->s->sUPDATE($y,$o,$aa);}}function delete($y,$aa=array()){if(gettype($y)=="array"||self::containsAdv($aa)){return$this->s->DELETE($y,$aa);}else{return$this->s->sDELETE($y,$aa);}}function query($ha,$p=null){return$this->s->con->query($ha,$p);}function transact($ia){return$this->s->transact($ia);}}
0 commit comments