weixin_43639491 2022-08-09 17:05 采纳率: 100%
浏览 238
已结题

TP5.1连接达梦数据库 有封装好的方法吗

需要使用TP5.1连接达梦数据库,有封装好的方法吗?最好有完整的demo

  • 写回答

1条回答 默认 最新

  • 我啥都会 2022-08-09 21:06
    关注

    1.新建Dm.php。

    
    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: liu21st <liu21st@gmail.com>
    // +----------------------------------------------------------------------
    
    namespace think\db\builder;
    
    use think\db\Builder;
    use think\db\Query;
    
    /**
     * Dm数据库驱动
     */
    class Dm extends Builder
    {
        protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT  %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
    
        /**
         * limit分析
         * @access protected
         * @param  Query $query 查询对象
         * @param  mixed $limit
         * @return string
         */
        protected function parseLimit($query, $limit)
        {
            $limitStr = '';
    
            if (!empty($limit)) {
                $limit = explode(',', $limit);
    
                if (count($limit) > 1) {
                    $limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
                } else {
                    $limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
                }
    
            }
    
            return $limitStr ? ' WHERE ' . $limitStr : '';
        }
    
        /**
         * 设置锁机制
         * @access protected
         * @param  Query      $query 查询对象
         * @param  bool|false $lock
         * @return string
         */
        protected function parseLock($query, $lock = false)
        {
            if (!$lock) {
                return '';
            }
    
            return ' FOR UPDATE NOWAIT ';
        }
    
        /**
         * 字段和表名处理
         * @access public
         * @param  Query  $query  查询对象
         * @param  string $key
         * @param  string $strict
         * @return string
         */
        public function parseKey($query, $key, $strict = false)
        {
            $key = trim($key);
    
            if (strpos($key, '->') && false === strpos($key, '(')) {
                // JSON字段支持
                list($field, $name) = explode($key, '->');
                $key                = $field . '."' . $name . '"';
            }
    
            return $key;
        }
    
        /**
         * 随机排序
         * @access protected
         * @param  Query $query 查询对象
         * @return string
         */
        protected function parseRand($query)
        {
            return 'DBMS_RANDOM.value';
        }
    }
    
    

    2.新建Dm.php

    
    ```php
    
    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: liu21st <liu21st@gmail.com>
    // +----------------------------------------------------------------------
    
    namespace think\db\connector;
    
    use PDO;
    use think\db\BaseQuery;
    use think\db\Connection;
    
    /**
     * Dm数据库驱动
     */
    class Dm extends Connection
    {
        /**
         * 解析pdo连接的dsn信息
         * @access protected
         * @param array $config 连接信息
         * @return string
         */
        protected function parseDsn($config)
        {
            $dsn = 'dm:';
    
            if (!empty($config['hostname'])) {
                //  Oracle Instant Client
                $dsn .=  $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/';
            }
    
    
            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);
            $sql             = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)";
    
            $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' => $val['notnull'],
                        'default' => $val['data_default'],
                        'primary' => $val['pk'],
                        'autoinc' => $val['pk'],
                    ];
                }
            }
    
            return $this->fieldCase($info);
        }
    
        /**
         * 取得数据库的表信息(暂时实现取得用户表信息)
         * @access   public
         * @param string $dbName
         * @return array
         */
        public function getTables($dbName = '')
        {
            $sql    = 'select table_name from all_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;
        }
    
        /**
         * 获取最近插入的ID(如果使用自增列,需去掉此方法)
         * @access public
         * @param BaseQuery $query    查询对象
         * @param string    $sequence 自增序列名
         * @return mixed
         */
        public function getLastInsID($sequence = null)
        {
            $pdo      = $this->linkID->query("select {$sequence}.currval as id from dual");
            $insertId = $pdo->fetchColumn();
    
            return $this->autoInsIDType($query, $insertId);
        }
    
        /**
         * SQL性能分析
         * @access protected
         * @param string $sql
         * @return array
         */
        protected function getExplain($sql)
        {
            return [];
        }
    
        protected function supportSavepoint()
        {
            return true;
        }
    }
    
    
    3.修改 database.php
    
    ```php
    
    <?php
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: liu21st <liu21st@gmail.com>
    // +----------------------------------------------------------------------
    
    return [
        // 数据库类型
        'type'            => 'Dm',
        // 服务器地址
        'hostname'        => '127.0.0.1',
        // 数据库名
        // 'database'        => 'SYSDBA',
        // 用户名
        'username'        => 'MEI',
        // 密码
        'password'        => '888888888',
        // 端口
        'hostport'        => '5237',
        // 连接dsn
        'dsn'             => '',
        // 数据库连接参数
        'params'          => [],
        // 数据库编码默认采用utf8
        'charset'         => 'utf8',
        // 数据库表前缀
        'prefix'          => '',
        // 数据库调试模式
        'debug'           => true,
        // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
        'deploy'          => 0,
        // 数据库读写是否分离 主从式有效
        'rw_separate'     => false,
        // 读写分离后 主服务器数量
        'master_num'      => 1,
        // 指定从服务器序号
        'slave_no'        => '',
        // 自动读取主库数据
        'read_master'     => false,
        // 是否严格检查字段是否存在
        'fields_strict'   => true,
        // 数据集返回类型
        'resultset_type'  => 'array',
        // 自动写入时间戳字段
        'auto_timestamp'  => false,
        // 时间字段取出后的默认时间格式
        'datetime_format' => 'Y-m-d H:i:s',
        // 是否需要进行SQL性能分析
        'sql_explain'     => false,
        // Builder类
        'builder'         => '',
        // Query类
        'query'           => '\\think\\db\\Query',
        // 是否需要断线重连
        'break_reconnect' => false,
        // 断线标识字符串
        'break_match_str' => [],
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月18日
  • 已采纳回答 8月10日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持