diff --git a/var/Typecho/Db.php b/var/Typecho/Db.php index 9e05a096..a4367d41 100644 --- a/var/Typecho/Db.php +++ b/var/Typecho/Db.php @@ -1,11 +1,4 @@ _adapterName = $adapterName; + $this->adapterName = $adapterName; /** 数据库适配器 */ $adapterName = 'Typecho_Db_Adapter_' . $adapterName; @@ -115,18 +108,18 @@ class Typecho_Db throw new Typecho_Db_Exception("Adapter {$adapterName} is not available"); } - $this->_prefix = $prefix; + $this->prefix = $prefix; /** 初始化内部变量 */ - $this->_connectedPool = []; + $this->connectedPool = []; - $this->_config = [ + $this->config = [ self::READ => [], self::WRITE => [] ]; //实例化适配器对象 - $this->_adapter = new $adapterName(); + $this->adapter = new $adapterName(); } /** @@ -137,7 +130,7 @@ class Typecho_Db */ public function getAdapterName(): string { - return $this->_adapterName; + return $this->adapterName; } /** @@ -148,7 +141,7 @@ class Typecho_Db */ public function getPrefix(): string { - return $this->_prefix; + return $this->prefix; } /** @@ -158,11 +151,11 @@ class Typecho_Db public function addConfig(Typecho_Config $config, int $op) { if ($op & self::READ) { - $this->_config[self::READ][] = $config; + $this->config[self::READ][] = $config; } if ($op & self::WRITE) { - $this->_config[self::WRITE][] = $config; + $this->config[self::WRITE][] = $config; } } @@ -176,13 +169,13 @@ class Typecho_Db */ public function getConfig(int $op): Typecho_Config { - if (empty($this->_config[$op])) { + if (empty($this->config[$op])) { /** Typecho_Db_Exception */ throw new Typecho_Db_Exception('Missing Database Connection'); } - $key = array_rand($this->_config[$op]); - return $this->_config[$op][$key]; + $key = array_rand($this->config[$op]); + return $this->config[$op][$key]; } /** @@ -192,7 +185,7 @@ class Typecho_Db */ public function flushPool() { - $this->_connectedPool = []; + $this->connectedPool = []; } /** @@ -205,13 +198,13 @@ class Typecho_Db */ public function selectDb(int $op) { - if (!isset($this->_connectedPool[$op])) { + if (!isset($this->connectedPool[$op])) { $selectConnectionConfig = $this->getConfig($op); - $selectConnectionHandle = $this->_adapter->connect($selectConnectionConfig); - $this->_connectedPool[$op] = $selectConnectionHandle; + $selectConnectionHandle = $this->adapter->connect($selectConnectionConfig); + $this->connectedPool[$op] = $selectConnectionHandle; } - return $this->_connectedPool[$op]; + return $this->connectedPool[$op]; } /** @@ -221,7 +214,7 @@ class Typecho_Db */ public function sql(): Typecho_Db_Query { - return new Typecho_Db_Query($this->_adapter, $this->_prefix); + return new Typecho_Db_Query($this->adapter, $this->prefix); } /** @@ -248,7 +241,7 @@ class Typecho_Db */ public function getVersion(int $op = self::READ): string { - return $this->_adapter->getVersion($this->selectDb($op)); + return $this->adapter->getVersion($this->selectDb($op)); } /** @@ -260,7 +253,7 @@ class Typecho_Db */ public static function set(Typecho_Db $db) { - self::$_instance = $db; + self::$instance = $db; } /** @@ -272,12 +265,12 @@ class Typecho_Db */ public static function get(): Typecho_Db { - if (empty(self::$_instance)) { + if (empty(self::$instance)) { /** Typecho_Db_Exception */ throw new Typecho_Db_Exception('Missing Database Object'); } - return self::$_instance; + return self::$instance; } /** @@ -347,8 +340,8 @@ class Typecho_Db */ public function truncate($table) { - $table = preg_replace("/^table\./", $this->_prefix, $table); - $this->_adapter->truncate($table, $this->selectDb(self::WRITE)); + $table = preg_replace("/^table\./", $this->prefix, $table); + $this->adapter->truncate($table, $this->selectDb(self::WRITE)); } /** @@ -371,7 +364,7 @@ class Typecho_Db $table = $query->getAttribute('table'); $op = (self::UPDATE == $action || self::DELETE == $action || self::INSERT == $action) ? self::WRITE : self::READ; - } else if (!is_string($query)) { + } elseif (!is_string($query)) { /** 如果query不是对象也不是字符串,那么将其判断为查询资源句柄,直接返回 */ return $query; } @@ -380,7 +373,7 @@ class Typecho_Db $handle = $this->selectDb($op); /** 提交查询 */ - $resource = $this->_adapter->query($query instanceof Typecho_Db_Query ? + $resource = $this->adapter->query($query instanceof Typecho_Db_Query ? $query->prepare($query) : $query, $handle, $op, $action, $table); if ($action) { @@ -388,9 +381,9 @@ class Typecho_Db switch ($action) { case self::UPDATE: case self::DELETE: - return $this->_adapter->affectedRows($resource, $handle); + return $this->adapter->affectedRows($resource, $handle); case self::INSERT: - return $this->_adapter->lastInsertId($resource, $handle); + return $this->adapter->lastInsertId($resource, $handle); case self::SELECT: default: return $resource; @@ -405,26 +398,21 @@ class Typecho_Db * 一次取出所有行 * * @param mixed $query 查询对象 - * @param array|null $filter 行过滤器函数,将查询的每一行作为第一个参数传入指定的过滤器中 + * @param callable|null $filter 行过滤器函数,将查询的每一行作为第一个参数传入指定的过滤器中 * * @return array * @throws Typecho_Db_Exception */ - public function fetchAll($query, ?array $filter = null): array + public function fetchAll($query, ?callable $filter = null): array { //执行查询 - $resource = $this->query($query, self::READ); + $resource = $this->query($query); $result = []; - /** 取出过滤器 */ - if (!empty($filter)) { - [$object, $method] = $filter; - } - //取出每一行 - while ($rows = $this->_adapter->fetch($resource)) { + while ($rows = $this->adapter->fetch($resource)) { //判断是否有过滤器 - $result[] = $filter ? call_user_func([&$object, $method], $rows) : $rows; + $result[] = $filter ? call_user_func($filter, $rows) : $rows; } return $result; @@ -434,22 +422,17 @@ class Typecho_Db * 一次取出一行 * * @param mixed $query 查询对象 - * @param array|null $filter 行过滤器函数,将查询的每一行作为第一个参数传入指定的过滤器中 + * @param callable|null $filter 行过滤器函数,将查询的每一行作为第一个参数传入指定的过滤器中 * * @return mixed * @throws Typecho_Db_Exception */ - public function fetchRow($query, ?array $filter = null) + public function fetchRow($query, ?callable $filter = null) { - $resource = $this->query($query, self::READ); + $resource = $this->query($query); - /** 取出过滤器 */ - if ($filter) { - [$object, $method] = $filter; - } - - return ($rows = $this->_adapter->fetch($resource)) ? - ($filter ? $object->$method($rows) : $rows) : + return ($rows = $this->adapter->fetch($resource)) ? + ($filter ? call_user_func($filter, $rows) : $rows) : []; } @@ -464,14 +447,14 @@ class Typecho_Db */ public function fetchObject($query, ?array $filter = null) { - $resource = $this->query($query, self::READ); + $resource = $this->query($query); /** 取出过滤器 */ if ($filter) { [$object, $method] = $filter; } - return ($rows = $this->_adapter->fetchObject($resource)) ? + return ($rows = $this->adapter->fetchObject($resource)) ? ($filter ? $object->$method($rows) : $rows) : new stdClass(); }