123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- /**
- * @author: 郭永磊
- * @description: 数据库接口基类
- * @create date: 2015-8-13
- */
- class DbBase{
- public $server;
- public $db;
-
- public $usedPrefix = false;
-
- private $_defaultServer;
- private $_defaultDb;
-
- private $_conn;
- private $_useDefaultServer = false;
-
- private $_queryString;
-
- public $dbTable = array();
- public $mappingName = "default"; //默认映射
- function __construct(){
- $this->_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;
- }
- }
|