You've already forked qlg.tsgz.moe
							
							Init Repo
This commit is contained in:
		
							
								
								
									
										126
									
								
								thinkphp/library/think/db/connector/Mysql.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										126
									
								
								thinkphp/library/think/db/connector/Mysql.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,126 @@
 | 
			
		||||
<?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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										103
									
								
								thinkphp/library/think/db/connector/Pgsql.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										103
									
								
								thinkphp/library/think/db/connector/Pgsql.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,103 @@
 | 
			
		||||
<?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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Pgsql数据库驱动
 | 
			
		||||
 */
 | 
			
		||||
class Pgsql extends Connection
 | 
			
		||||
{
 | 
			
		||||
    protected $builder = '\\think\\db\\builder\\Pgsql';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 解析pdo连接的dsn信息
 | 
			
		||||
     * @access protected
 | 
			
		||||
     * @param array $config 连接信息
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    protected function parseDsn($config)
 | 
			
		||||
    {
 | 
			
		||||
        $dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
 | 
			
		||||
        if (!empty($config['hostport'])) {
 | 
			
		||||
            $dsn .= ';port=' . $config['hostport'];
 | 
			
		||||
        }
 | 
			
		||||
        return $dsn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 取得数据表的字段信息
 | 
			
		||||
     * @access public
 | 
			
		||||
     * @param string $tableName
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function getFields($tableName)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        list($tableName) = explode(' ', $tableName);
 | 
			
		||||
        $sql             = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $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']),
 | 
			
		||||
                    'default' => $val['default'],
 | 
			
		||||
                    'primary' => !empty($val['key']),
 | 
			
		||||
                    'autoinc' => (0 === strpos($val['extra'], 'nextval(')),
 | 
			
		||||
                ];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return $this->fieldCase($info);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 取得数据库的表信息
 | 
			
		||||
     * @access public
 | 
			
		||||
     * @param string $dbName
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function getTables($dbName = '')
 | 
			
		||||
    {
 | 
			
		||||
        $sql    = "select tablename as Tables_in_test from pg_tables where  schemaname ='public'";
 | 
			
		||||
        $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)
 | 
			
		||||
    {
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function supportSavepoint()
 | 
			
		||||
    {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										104
									
								
								thinkphp/library/think/db/connector/Sqlite.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										104
									
								
								thinkphp/library/think/db/connector/Sqlite.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,104 @@
 | 
			
		||||
<?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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Sqlite数据库驱动
 | 
			
		||||
 */
 | 
			
		||||
class Sqlite extends Connection
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    protected $builder = '\\think\\db\\builder\\Sqlite';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 解析pdo连接的dsn信息
 | 
			
		||||
     * @access protected
 | 
			
		||||
     * @param array $config 连接信息
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    protected function parseDsn($config)
 | 
			
		||||
    {
 | 
			
		||||
        $dsn = 'sqlite:' . $config['database'];
 | 
			
		||||
        return $dsn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 取得数据表的字段信息
 | 
			
		||||
     * @access public
 | 
			
		||||
     * @param string $tableName
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function getFields($tableName)
 | 
			
		||||
    {
 | 
			
		||||
        list($tableName) = explode(' ', $tableName);
 | 
			
		||||
        $sql             = 'PRAGMA table_info( ' . $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['name']] = [
 | 
			
		||||
                    'name'    => $val['name'],
 | 
			
		||||
                    'type'    => $val['type'],
 | 
			
		||||
                    'notnull' => 1 === $val['notnull'],
 | 
			
		||||
                    'default' => $val['dflt_value'],
 | 
			
		||||
                    'primary' => '1' == $val['pk'],
 | 
			
		||||
                    'autoinc' => '1' == $val['pk'],
 | 
			
		||||
                ];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return $this->fieldCase($info);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 取得数据库的表信息
 | 
			
		||||
     * @access public
 | 
			
		||||
     * @param string $dbName
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function getTables($dbName = '')
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $sql = "SELECT name FROM sqlite_master WHERE type='table' "
 | 
			
		||||
            . "UNION ALL SELECT name FROM sqlite_temp_master "
 | 
			
		||||
            . "WHERE type='table' ORDER BY name";
 | 
			
		||||
 | 
			
		||||
        $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)
 | 
			
		||||
    {
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function supportSavepoint()
 | 
			
		||||
    {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										122
									
								
								thinkphp/library/think/db/connector/Sqlsrv.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										122
									
								
								thinkphp/library/think/db/connector/Sqlsrv.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,122 @@
 | 
			
		||||
<?php
 | 
			
		||||
// +----------------------------------------------------------------------
 | 
			
		||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
 | 
			
		||||
// +----------------------------------------------------------------------
 | 
			
		||||
// | Copyright (c) 2006-2012 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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Sqlsrv数据库驱动
 | 
			
		||||
 */
 | 
			
		||||
class Sqlsrv extends Connection
 | 
			
		||||
{
 | 
			
		||||
    // PDO连接参数
 | 
			
		||||
    protected $params = [
 | 
			
		||||
        PDO::ATTR_CASE              => PDO::CASE_NATURAL,
 | 
			
		||||
        PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION,
 | 
			
		||||
        PDO::ATTR_STRINGIFY_FETCHES => false,
 | 
			
		||||
    ];
 | 
			
		||||
    protected $builder = '\\think\\db\\builder\\Sqlsrv';
 | 
			
		||||
    /**
 | 
			
		||||
     * 解析pdo连接的dsn信息
 | 
			
		||||
     * @access protected
 | 
			
		||||
     * @param array $config 连接信息
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    protected function parseDsn($config)
 | 
			
		||||
    {
 | 
			
		||||
        $dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
 | 
			
		||||
        if (!empty($config['hostport'])) {
 | 
			
		||||
            $dsn .= ',' . $config['hostport'];
 | 
			
		||||
        }
 | 
			
		||||
        return $dsn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 取得数据表的字段信息
 | 
			
		||||
     * @access public
 | 
			
		||||
     * @param string $tableName
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function getFields($tableName)
 | 
			
		||||
    {
 | 
			
		||||
        list($tableName) = explode(' ', $tableName);
 | 
			
		||||
        $sql             = "SELECT   column_name,   data_type,   column_default,   is_nullable
 | 
			
		||||
        FROM    information_schema.tables AS t
 | 
			
		||||
        JOIN    information_schema.columns AS c
 | 
			
		||||
        ON  t.table_catalog = c.table_catalog
 | 
			
		||||
        AND t.table_schema  = c.table_schema
 | 
			
		||||
        AND t.table_name    = c.table_name
 | 
			
		||||
        WHERE   t.table_name = '$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['column_name']] = [
 | 
			
		||||
                    'name'    => $val['column_name'],
 | 
			
		||||
                    'type'    => $val['data_type'],
 | 
			
		||||
                    'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
 | 
			
		||||
                    'default' => $val['column_default'],
 | 
			
		||||
                    'primary' => false,
 | 
			
		||||
                    'autoinc' => false,
 | 
			
		||||
                ];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
 | 
			
		||||
        // 调试开始
 | 
			
		||||
        $this->debug(true);
 | 
			
		||||
        $pdo = $this->linkID->query($sql);
 | 
			
		||||
        // 调试结束
 | 
			
		||||
        $this->debug(false, $sql);
 | 
			
		||||
        $result = $pdo->fetch(PDO::FETCH_ASSOC);
 | 
			
		||||
        if ($result) {
 | 
			
		||||
            $info[$result['column_name']]['primary'] = true;
 | 
			
		||||
        }
 | 
			
		||||
        return $this->fieldCase($info);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 取得数据表的字段信息
 | 
			
		||||
     * @access public
 | 
			
		||||
     * @param string $dbName
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function getTables($dbName = '')
 | 
			
		||||
    {
 | 
			
		||||
        $sql = "SELECT TABLE_NAME
 | 
			
		||||
            FROM INFORMATION_SCHEMA.TABLES
 | 
			
		||||
            WHERE TABLE_TYPE = 'BASE TABLE'
 | 
			
		||||
            ";
 | 
			
		||||
 | 
			
		||||
        $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)
 | 
			
		||||
    {
 | 
			
		||||
        return [];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								thinkphp/library/think/db/connector/code.jpg
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								thinkphp/library/think/db/connector/code.jpg
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 17 KiB  | 
							
								
								
									
										117
									
								
								thinkphp/library/think/db/connector/pgsql.sql
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										117
									
								
								thinkphp/library/think/db/connector/pgsql.sql
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,117 @@
 | 
			
		||||
CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varchar AS
 | 
			
		||||
$BODY$
 | 
			
		||||
DECLARE
 | 
			
		||||
     v_type varchar;
 | 
			
		||||
BEGIN
 | 
			
		||||
     IF a_type='int8' THEN
 | 
			
		||||
          v_type:='bigint';
 | 
			
		||||
     ELSIF a_type='int4' THEN
 | 
			
		||||
          v_type:='integer';
 | 
			
		||||
     ELSIF a_type='int2' THEN
 | 
			
		||||
          v_type:='smallint';
 | 
			
		||||
     ELSIF a_type='bpchar' THEN
 | 
			
		||||
          v_type:='char';
 | 
			
		||||
     ELSE
 | 
			
		||||
          v_type:=a_type;
 | 
			
		||||
     END IF;
 | 
			
		||||
     RETURN v_type;
 | 
			
		||||
END;
 | 
			
		||||
$BODY$
 | 
			
		||||
LANGUAGE PLPGSQL;
 | 
			
		||||
 | 
			
		||||
CREATE TYPE "public"."tablestruct" AS (
 | 
			
		||||
  "fields_key_name" varchar(100),
 | 
			
		||||
  "fields_name" VARCHAR(200),
 | 
			
		||||
  "fields_type" VARCHAR(20),
 | 
			
		||||
  "fields_length" BIGINT,
 | 
			
		||||
  "fields_not_null" VARCHAR(10),
 | 
			
		||||
  "fields_default" VARCHAR(500),
 | 
			
		||||
  "fields_comment" VARCHAR(1000)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE OR REPLACE FUNCTION "public"."table_msg" (a_schema_name varchar, a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
 | 
			
		||||
$body$
 | 
			
		||||
DECLARE
 | 
			
		||||
     v_ret tablestruct;
 | 
			
		||||
     v_oid oid;
 | 
			
		||||
     v_sql varchar;
 | 
			
		||||
     v_rec RECORD;
 | 
			
		||||
     v_key varchar;
 | 
			
		||||
BEGIN
 | 
			
		||||
     SELECT
 | 
			
		||||
           pg_class.oid  INTO v_oid
 | 
			
		||||
     FROM
 | 
			
		||||
           pg_class
 | 
			
		||||
           INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid AND lower(pg_namespace.nspname) = a_schema_name)
 | 
			
		||||
     WHERE
 | 
			
		||||
           pg_class.relname=a_table_name;
 | 
			
		||||
     IF NOT FOUND THEN
 | 
			
		||||
         RETURN;
 | 
			
		||||
     END IF;
 | 
			
		||||
 | 
			
		||||
     v_sql='
 | 
			
		||||
     SELECT
 | 
			
		||||
           pg_attribute.attname AS fields_name,
 | 
			
		||||
           pg_attribute.attnum AS fields_index,
 | 
			
		||||
           pgsql_type(pg_type.typname::varchar) AS fields_type,
 | 
			
		||||
           pg_attribute.atttypmod-4 as fields_length,
 | 
			
		||||
           CASE WHEN pg_attribute.attnotnull  THEN ''not null''
 | 
			
		||||
           ELSE ''''
 | 
			
		||||
           END AS fields_not_null,
 | 
			
		||||
           pg_attrdef.adsrc AS fields_default,
 | 
			
		||||
           pg_description.description AS fields_comment
 | 
			
		||||
     FROM
 | 
			
		||||
           pg_attribute
 | 
			
		||||
           INNER JOIN pg_class  ON pg_attribute.attrelid = pg_class.oid
 | 
			
		||||
           INNER JOIN pg_type   ON pg_attribute.atttypid = pg_type.oid
 | 
			
		||||
           LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum
 | 
			
		||||
           LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum
 | 
			
		||||
     WHERE
 | 
			
		||||
           pg_attribute.attnum > 0
 | 
			
		||||
           AND attisdropped <> ''t''
 | 
			
		||||
           AND pg_class.oid = ' || v_oid || '
 | 
			
		||||
     ORDER BY pg_attribute.attnum' ;
 | 
			
		||||
 | 
			
		||||
     FOR v_rec IN EXECUTE v_sql LOOP
 | 
			
		||||
         v_ret.fields_name=v_rec.fields_name;
 | 
			
		||||
         v_ret.fields_type=v_rec.fields_type;
 | 
			
		||||
         IF v_rec.fields_length > 0 THEN
 | 
			
		||||
            v_ret.fields_length:=v_rec.fields_length;
 | 
			
		||||
         ELSE
 | 
			
		||||
            v_ret.fields_length:=NULL;
 | 
			
		||||
         END IF;
 | 
			
		||||
         v_ret.fields_not_null=v_rec.fields_not_null;
 | 
			
		||||
         v_ret.fields_default=v_rec.fields_default;
 | 
			
		||||
         v_ret.fields_comment=v_rec.fields_comment;
 | 
			
		||||
         SELECT constraint_name INTO v_key FROM information_schema.key_column_usage WHERE table_schema=a_schema_name AND table_name=a_table_name AND column_name=v_rec.fields_name;
 | 
			
		||||
         IF FOUND THEN
 | 
			
		||||
            v_ret.fields_key_name=v_key;
 | 
			
		||||
         ELSE
 | 
			
		||||
            v_ret.fields_key_name='';
 | 
			
		||||
         END IF;
 | 
			
		||||
         RETURN NEXT v_ret;
 | 
			
		||||
     END LOOP;
 | 
			
		||||
     RETURN ;
 | 
			
		||||
END;
 | 
			
		||||
$body$
 | 
			
		||||
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
 | 
			
		||||
 | 
			
		||||
COMMENT ON FUNCTION "public"."table_msg"(a_schema_name varchar, a_table_name varchar)
 | 
			
		||||
IS '获得表信息';
 | 
			
		||||
 | 
			
		||||
---重载一个函数
 | 
			
		||||
CREATE OR REPLACE FUNCTION "public"."table_msg" (a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
 | 
			
		||||
$body$
 | 
			
		||||
DECLARE
 | 
			
		||||
    v_ret tablestruct;
 | 
			
		||||
BEGIN
 | 
			
		||||
    FOR v_ret IN SELECT * FROM table_msg('public',a_table_name) LOOP
 | 
			
		||||
        RETURN NEXT v_ret;
 | 
			
		||||
    END LOOP;
 | 
			
		||||
    RETURN;
 | 
			
		||||
END;
 | 
			
		||||
$body$
 | 
			
		||||
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
 | 
			
		||||
 | 
			
		||||
COMMENT ON FUNCTION "public"."table_msg"(a_table_name varchar)
 | 
			
		||||
IS '获得表信息';
 | 
			
		||||
		Reference in New Issue
	
	Block a user