_defaultServer = Yii::app()->params["default_server"]; $this->_defaultDb = Yii::app()->params["default_db"]; $this->_resetServer(); $this->dbTable["default"] = " school_id = ".$this->_getSchoolId(); $params = func_get_args(); if($params){ if(!empty($params[0]) && isset($params[1])){ $this->db["name"] = $params[0]; $this->_useDefaultServer = true; if(!empty($params[1])) $this->db["t_pre"] = $params[1]; } else if(!empty($params[0])) $this->mappingName = $params[0]; } else if($this->_getSchoolId() === false){ $this->_useDefaultServer = true; } $this->_condition = $this->dbTable[$this->mappingName]; } private function _getSchoolId(){ return !empty(Yii::app()->session["school_id"]) ? Yii::app()->session["school_id"] : false; } private function _resetServer(){ $this->server = $this->_defaultServer; $this->db = $this->_defaultDb; } private function _connect(){ $dsn = "mysql:host={$this->server["addr"]};dbname={$this->db["name"]}"; $this->_conn = new PDO($dsn, $this->server["username"], $this->server["password"]); } private function _connTrigger(){ if(!is_object($this->_conn)) $this->connect(); } private function _connectTarget(){ $this->_resetServer(); $this->_connect(); $sql = "select * from `{$this->_defaultDb["t_pre"]}{$this->_defaultDb["t_servers"]}` where{$this->_condition}"; $rs = $this->getRow($sql); if($rs){ $this->server = array( "addr" => $rs["database_host"], "username" => $rs["database_user"], "password" => $rs["database_password"], ); $this->db = array( "name" => $rs["database_name"], "t_pre" => isset($rs["t_pre"]) ? $rs["t_pre"] : "", ); $this->_connect(); } } public function connect(){ if($this->_useDefaultServer) $this->_connect(); else $this->_connectTarget(); } private function _getSqlType($sql){ $arr = explode(" ", trim(str_replace(" ", " ", $sql)), 2); return strtolower($arr[0]); } private function _limit($offset, $limit){ return $limit > 0 ? " limit {$offset}, {$limit}" : ""; } private function _query($sql, $fetch = null, $offset = 0, $limit = -1){ $this->_connTrigger(); if(is_object($this->_conn) && $fetch){ $sqlType = $this->_getSqlType($sql); if($this->usedPrefix) $sql = preg_replace("/{{(.*?)}}/", "{$this->db["t_pre"]}$1", $sql); if($sqlType === "select" || $sqlType === "show"){ if($fetch === "ALL" && $limit > 0){ $sql = preg_replace("/\s*limit[^\)]*$/i", "", $sql); $sql .= $this->_limit($offset, $limit); } $handle = $this->_conn->prepare($sql); $handle->execute(); switch($fetch){ case "ALL": $rs = $handle->fetchAll(PDO::FETCH_ASSOC); break; case "ROW": $rs = $handle->fetch(PDO::FETCH_ASSOC); break; case "COUNT": $rs = $handle->rowCount(); break; case "HANDLE": $rs = $handle; break; default: $rs = false; break; } } else $rs = $this->_conn->exec($sql); return $rs; } return false; } public function getConn(){ $this->_connTrigger(); return $this->_conn; } public function exec($sql){ $this->_connTrigger(); return $this->_conn->exec($sql); } public function getHandle($sql){ return $this->_query($sql, "HANDLE"); } public function getRs($sql, $offset = 0, $limit = -1){ return $this->_query($sql, "ALL", $offset, $limit); } public function getRow($sql){ return $this->_query($sql, "ROW"); } public function countRs($sql){ return $this->_query($sql, "COUNT"); } public function insert($tableName, $data){ if($tableName && $data){ $dimension = Arr::countDimension($data); if($dimension === 1){ $fields = ""; $values = ""; foreach($data as $field => $val){ $fields .= ",`{$field}`"; $values .= ",'{$val}'"; } $fields = substr($fields, 1); $values = substr($values, 1); $sql = "insert into `{$tableName}`({$fields}) values({$values})"; } else if($dimension === 2){ } return $this->exec($sql); } return false; } }