Server : nginx/1.20.2 System : Linux VM-4-4-centos 3.10.0-1160.66.1.el7.x86_64 #1 SMP Wed May 18 16:02:34 UTC 2022 x86_64 User : www ( 1000) PHP Version : 5.6.40 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv Directory : /www/wwwroot/greatapp.cn/thinkphp/library/think/db/connector/ |
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- namespace think\db\connector; use PDO; use think\db\Connection; use think\Log; /** * mysql数据库驱动 */ class Mysql extends Connection { protected $builder = '\\think\\db\\builder\\Mysql'; /** * 解析pdo连接的dsn信息 * @access protected * @param array $config 连接信息 * @return string */ protected function parseDsn($config) { if (!empty($config['socket'])) { $dsn = 'mysql:unix_socket=' . $config['socket']; } elseif (!empty($config['hostport'])) { $dsn = 'mysql:host=' . $config['hostname'] . ';port=' . $config['hostport']; } else { $dsn = 'mysql:host=' . $config['hostname']; } $dsn .= ';dbname=' . $config['database']; if (!empty($config['charset'])) { $dsn .= ';charset=' . $config['charset']; } return $dsn; } /** * 取得数据表的字段信息 * @access public * @param string $tableName * @return array */ public function getFields($tableName) { list($tableName) = explode(' ', $tableName); if (false === strpos($tableName, '`')) { if (strpos($tableName, '.')) { $tableName = str_replace('.', '`.`', $tableName); } $tableName = '`' . $tableName . '`'; } $sql = 'SHOW COLUMNS FROM ' . $tableName; $pdo = $this->query($sql, [], false, true); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; if ($result) { foreach ($result as $key => $val) { $val = array_change_key_case($val); $info[$val['field']] = [ 'name' => $val['field'], 'type' => $val['type'], 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes 'default' => $val['default'], 'primary' => (strtolower($val['key']) == 'pri'), 'autoinc' => (strtolower($val['extra']) == 'auto_increment'), ]; } } return $this->fieldCase($info); } /** * 取得数据库的表信息 * @access public * @param string $dbName * @return array */ public function getTables($dbName = '') { $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; $pdo = $this->query($sql, [], false, true); $result = $pdo->fetchAll(PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); } return $info; } /** * SQL性能分析 * @access protected * @param string $sql * @return array */ protected function getExplain($sql) { $pdo = $this->linkID->query("EXPLAIN " . $sql); $result = $pdo->fetch(PDO::FETCH_ASSOC); $result = array_change_key_case($result); if (isset($result['extra'])) { if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) { Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn'); } } return $result; } protected function supportSavepoint() { return true; } }