123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace core\database;
- use core\basic\Config;
- class Pdo implements Builder
- {
- protected static $pdo;
- protected $master;
- protected $slave;
- protected $begin = false;
- private function __construct()
- {}
- public function __destruct()
- {
- if ($this->begin) {
- $this->commit();
- }
- }
-
- public static function getInstance()
- {
- if (! self::$pdo) {
- self::$pdo = new self();
- }
- return self::$pdo;
- }
-
- public function conn($cfg)
- {
- if (get_db_type() == 'sqlite' && ! extension_loaded('pdo_sqlite')) {
- if (extension_loaded('SQLite3')) {
- error('未检测到您服务器环境的pdo_sqlite数据库扩展,请检查php.ini中是否已经开启该扩展!<br>另外,检测到您服务器支持sqlite3扩展,您也可以修改数据库配置连接驱动为sqlite试试!');
- } else {
- error('未检测到您服务器环境的pdo_sqlite数据库扩展,请检查php.ini中是否已经开启对应的数据库扩展!');
- }
- } elseif (get_db_type() == 'mysql' && ! extension_loaded('pdo_mysql')) {
- if (extension_loaded('mysqli')) {
- error('未检测到您服务器环境的pdo_mysqli数据库扩展,请检查php.ini中是否已经开启该扩展!<br>另外,检测到您服务器支持mysqli扩展,您也可以修改数据库配置连接驱动为mysqli试试!');
- } else {
- error('未检测到您服务器环境的pdo_mysqli数据库扩展,请检查php.ini中是否已经开启对应的数据库扩展!');
- }
- }
-
- $charset = Config::get('database.charset') ?: 'utf8';
- switch (Config::get('database.type')) {
- case 'pdo_mysql':
- $dsn = 'mysql:host=' . $cfg['host'] . ';port=' . $cfg['port'] . ';dbname=' . $cfg['dbname'] . ';charset=' . $charset;
- try {
- $conn = new \PDO($dsn, $cfg['user'], $cfg['passwd']);
- } catch (\PDOException $e) {
- error('PDO方式连接MySQL数据库错误:' . iconv('gbk', 'utf-8', $e->getMessage()));
- }
- break;
- case 'pdo_sqlite':
- $dsn = 'sqlite:' . ROOT_PATH . $cfg['dbname'];
- try {
- $conn = new \PDO($dsn);
- } catch (\PDOException $e) {
- error('PDO方式连接Sqlite数据库错误:' . iconv('gbk', 'utf-8', $e->getMessage()));
- }
- break;
- case 'pdo_pgsql':
- $dsn = 'pgsql:host=' . $cfg['host'] . ';port=' . $cfg['port'] . ';dbname=' . $cfg['dbname'];
- try {
- $conn = new \PDO($dsn, $cfg['user'], $cfg['passwd']);
- } catch (\PDOException $e) {
- error('PDO方式连接Pgsql数据库错误:' . iconv('gbk', 'utf-8', $e->getMessage()));
- }
- break;
- default:
- $dsn = Config::get('database.dsn');
- try {
- $conn = new \PDO($dsn, $cfg['user'], $cfg['passwd']);
- } catch (\PDOException $e) {
- error('PDO方式连接数据库错误:' . iconv('gbk', 'utf-8', $e->getMessage()));
- }
- break;
- }
- return $conn;
- }
-
- public function begin()
- {
- $this->master->beginTransaction();
- $this->begin = true;
- }
-
- public function commit()
- {
- $this->master->commit();
- $this->begin = false;
- }
-
- public function query($sql, $type = 'master')
- {
- $time_s = microtime(true);
- switch ($type) {
- case 'master':
- if (! $this->master) {
- $cfg = Config::get('database');
- $this->master = $this->conn($cfg);
- if ($cfg['type'] == 'pdo_mysql') {
- $this->master->exec("SET sql_mode='NO_ENGINE_SUBSTITUTION'");
- }
- }
-
-
- if ($cfg['type'] == 'pdo_sqlite' && ! $this->begin) {
- $this->begin();
- } elseif ($cfg['type'] == 'pdo_mysql' && Config::get('database.transaction') && ! $this->begin) {
- $this->begin();
- }
-
- $result = $this->master->query($sql);
- if ($result === false) {
- $this->error($sql, 'master');
- }
- break;
- case 'slave':
- if (! $this->slave) {
-
- if (! $cfg = Config::get('database.slave')) {
- $cfg = Config::get('database');
- } else {
-
- if (is_multi_array($cfg)) {
- $count = count($cfg);
- $cfg = $cfg['slave' . mt_rand(1, $count)];
- }
- }
- $this->slave = $this->conn($cfg);
- }
- $result = $this->slave->query($sql) or $this->error($sql, 'slave');
- break;
- }
- return $result;
- }
-
- public function isExist($sql)
- {
- $result = $this->query($sql, 'slave');
- if ($result->fetch()) {
- return true;
- } else {
- return false;
- }
- }
-
- public function rows($table)
- {
- $sql = "SELECT count(*) FROM $table";
- $result = $this->query($sql, 'slave');
- if (! ! $row = $result->fetch(\PDO::FETCH_NUM)) {
- return $row[0];
- } else {
- return 0;
- }
- }
-
- public function fields($table)
- {
- $sql = "SELECT * FROM $table LIMIT 1";
- $result = $this->query($sql, 'slave');
- if ($result) {
- return $result->columnCount();
- } else {
- return false;
- }
- }
-
- public function tableFields($table)
- {
- $rows = array();
- switch (Config::get('database.type')) {
- case 'pdo_mysql':
- $sql = "describe $table";
- $result = $this->query($sql, 'slave');
- while (! ! $row = $result->fetchObject()) {
- $rows[] = $row->Field;
- }
- break;
- case 'pdo_sqlite':
- $sql = "pragma table_info($table)";
- $result = $this->query($sql, 'slave');
- while (! ! $row = $result->fetchObject()) {
- $rows[] = $row->name;
- }
- break;
- case 'pdo_pgsql':
- $sql = "SELECT column_name FROM information_schema.columns WHERE table_name ='$table'";
- $result = $this->query($sql, 'slave');
- while (! ! $row = $result->fetchObject()) {
- $rows[] = $row->column_name;
- }
- break;
- default:
- return array();
- }
- return $rows;
- }
-
- public function one($sql, $type = null)
- {
- $result = $this->query($sql, 'slave');
- $row = array();
- if ($type) {
- $type ++;
- $row = $result->fetch($type);
- } else {
- $row = $result->fetchObject();
- }
- return $row;
- }
-
- public function all($sql, $type = null)
- {
- $result = $this->query($sql, 'slave');
- $rows = array();
- if ($type) {
- $type ++;
- $rows = $result->fetchAll($type);
- } else {
- while (! ! $row = $result->fetchObject()) {
- $rows[] = $row;
- }
- }
- return $rows;
- }
-
- public function amd($sql)
- {
- $result = $this->query($sql, 'master');
- if ($result > 0) {
- return $result;
- } else {
- return 0;
- }
- }
-
- public function insertId()
- {
- return $this->master->lastInsertId();
- }
-
- public function multi($sql)
- {
- $sqls = explode(';', $sql);
- foreach ($sqls as $key => $value) {
- $result = $this->query($value, 'master');
- }
- if ($result) {
- return true;
- } else {
- return false;
- }
- }
-
- protected function error($sql, $conn)
- {
- $errs = $this->$conn->errorInfo();
- $err = '错误:' . $errs[2];
- if (preg_match('/XPATH/i', $err)) {
- $err = '';
- }
- if ($this->begin) {
- $this->$conn->rollBack();
- $this->begin = false;
- }
-
- error('执行SQL发生错误!' . $err);
- }
-
- public function fetchQuery($obj){
- return $obj->fetchAll();
- }
- }
|