thinkphp6 phpstudy php8 达梦数据库

thinkphp6 phpstudy php8 达梦数据库

php8 pdo方式连接达梦数据库

1、下载php8 TS版本

图片

下载地址:https://windows.php.net/download/

2、解压至:phpstudy目录下:phpstudy_pro\Extensions\php

3、将达梦安装目录下(D:\dmdbms\drivers\php_pdo)的 php80ts_dm.dll pdots_dm.dll dmdpi.dll 这三个文件复制到php的ext目录下(D:\BC\phpstudy_pro\Extensions\php\php8.0.25ts\ext

4、php.ini中 增加

图片

5、到这里。dm 以及 pdo配置完成

6、tp6配置
6.1 config/database文件中

在这里插入图片描述

6.2 vendor/topthink/think-orm/src/db/builder下创建Dm.php文件(根据网上的方法已修改为适用于tp6

<?php
declare (strict_types = 1);
// +----------------------------------------------------------------------
// | 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 $query, string $limit): string
    {
        $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 $query, $lock = false): string
    {
        if (!$lock) {
            return '';
        }
        return ' FOR UPDATE NOWAIT ';
    }
    /**
     * 字段和表名处理
     * @access public
     * @param  Query  $query  查询对象
     * @param  string $key
     * @param  string $strict
     * @return string
     */
    public function parseKey(Query $query, $key, bool $strict = false): string
    {
        $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 $query): string
    {
        return 'DBMS_RANDOM.value';
    }
}

6.3 vendor/topthink/think-orm/src/db/connector下创建Dm.php(适用于tp6

<?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;
use think\db\PDOConnection;
use think\db\ConnectionInterface;
/**
 * Dm数据库驱动
 */
// class Dm extends Connection
class Dm extends PDOConnection
{
    /**
     * 解析pdo连接的dsn信息
     * @access protected
     * @param array $config 连接信息
     * @return string
     */
    protected function parseDsn(array $config): string
    {
        // $dsn = 'dm:host=';
        // if (!empty($config['hostname'])) {
        //     //  dm Instant Client
        //     $dsn .=  $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/';
        //     // $dsn .=  $config['hostname'];
        // }
        // if (!empty($config['charset'])) {
        //     $dsn .= ';charset=' . $config['charset'];
        // }
        // return $dsn;
        $dsn = "dm:host=".$config['hostname'];
        return $dsn;
    }
    /**
     * 取得数据表的字段信息
     * @access public
     * @param string $tableName
     * @return array
     */
    public function getFields(string $tableName): array
    {
        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);
        $pdo    = $this->getPDOStatement($sql);
        $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(string $dbName = ''): array
    {
        $sql    = 'select table_name from all_tables';
        // $pdo    = $this->query($sql, [], false, true);
        // $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
        $pdo    = $this->getPDOStatement($sql);
        $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)
    public function getLastInsID(BaseQuery $query, string $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(string $sql)
    {
        return [];
    }
    protected function supportSavepoint(): bool
    {
        return true;
    }
}

7、至此,配置完毕

8、增删改查一如既往,只是关于数据库中的字段都要大写

                       

点击阅读全文

上一篇 2023年 6月 10日 am10:23
下一篇 2023年 6月 10日 am10:26