Skip to content

Commit 756a032

Browse files
author
idcpj
committed
支持达梦
1 parent cd418d0 commit 756a032

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed

src/db/builder/Dm.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
6+
// +----------------------------------------------------------------------
7+
// | Author: liu21st <[email protected]>
8+
// +----------------------------------------------------------------------
9+
declare (strict_types = 1);
10+
11+
namespace think\db\builder;
12+
13+
use think\db\Builder;
14+
use think\db\Query;
15+
use think\db\exception\DbException as Exception;
16+
use think\db\Raw;
17+
18+
/**
19+
* Oracle数据库驱动
20+
*/
21+
class Dm extends Builder
22+
{
23+
// protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
24+
25+
/**
26+
* limit分析
27+
* @access protected
28+
* @param Query $query 查询对象
29+
* @param mixed $limit
30+
* @return string
31+
*/
32+
protected function parseLimit(Query $query, string $limit): string
33+
{
34+
return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';
35+
36+
}
37+
38+
/**
39+
* 设置锁机制
40+
* @access protected
41+
* @param Query $query 查询对象
42+
* @param bool|false $lock
43+
* @return string
44+
*/
45+
protected function parseLock(Query $query, $lock = false): string
46+
{
47+
if (!$lock) {
48+
return '';
49+
}
50+
51+
return ' FOR UPDATE NOWAIT ';
52+
}
53+
54+
/**
55+
* 字段和表名处理
56+
* @access public
57+
* @param Query $query 查询对象
58+
* @param string $key
59+
* @param bool $strict
60+
* @return string
61+
* @throws Exception
62+
*/
63+
public function parseKey(Query $query, $key, bool $strict = false): string
64+
{
65+
if (is_int($key)) {
66+
return (string) $key;
67+
} elseif ($key instanceof Raw) {
68+
return $this->parseRaw($query, $key);
69+
}
70+
71+
$key = trim($key);
72+
73+
if (strpos($key, '->') && false === strpos($key, '(')) {
74+
// JSON字段支持
75+
[$field, $name] = explode($key, '->');
76+
$key = $field . '."' . $name . '"';
77+
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
78+
[$table, $key] = explode('.', $key, 2);
79+
80+
$alias = $query->getOptions('alias');
81+
82+
if ('__TABLE__' == $table) {
83+
$table = $query->getOptions('table');
84+
$table = is_array($table) ? array_shift($table) : $table;
85+
}
86+
87+
if (isset($alias[$table])) {
88+
$table = $alias[$table];
89+
}
90+
}
91+
92+
if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
93+
throw new Exception('not support data:' . $key);
94+
}
95+
96+
if ('*' != $key && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
97+
$key = '"' . $key . '"';
98+
}
99+
100+
if (isset($table)) {
101+
$key = '"' . $table . '".' . $key;
102+
}
103+
104+
return $key;
105+
}
106+
107+
/**
108+
* 随机排序
109+
* @access protected
110+
* @param Query $query 查询对象
111+
* @return string
112+
*/
113+
protected function parseRand(Query $query): string
114+
{
115+
return 'DBMS_RANDOM.value';
116+
}
117+
}

src/db/connector/Dm.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: liu21st <[email protected]>
10+
// +----------------------------------------------------------------------
11+
declare (strict_types = 1);
12+
13+
namespace think\db\connector;
14+
15+
use PDO;
16+
use think\db\exception\PDOException;
17+
use think\db\PDOConnection;
18+
19+
/**
20+
* mysql数据库驱动
21+
*/
22+
class Dm extends PDOConnection
23+
{
24+
25+
/**
26+
* 解析pdo连接的dsn信息
27+
* @access protected
28+
* @param array $config 连接信息
29+
* @return string
30+
*/
31+
protected function parseDsn(array $config): string
32+
{
33+
$dsn = sprintf("dm:host=%s;port=%s;dbname=%s", $config['hostname'], $config['hostport'], $config['database']);
34+
return $dsn;
35+
}
36+
/**
37+
* 连接数据库方法
38+
* @access public
39+
* @param array $config 连接参数
40+
* @param integer $linkNum 连接序号
41+
* @param array|bool $autoConnection 是否自动连接主数据库(用于分布式)
42+
* @return PDO
43+
* @throws PDOException
44+
*/
45+
public function connect(array $config = [], $linkNum = 0, $autoConnection = false): PDO {
46+
if (empty($config)) {
47+
$config = $this->config;
48+
} else {
49+
$config = array_merge($this->config, $config);
50+
}
51+
52+
$PDO = parent::connect($config, $linkNum, $autoConnection);
53+
54+
$PDO->query(sprintf("SET SCHEMA %s", $config['database']));
55+
return $PDO;
56+
57+
}
58+
59+
/**
60+
* 取得数据表的字段信息
61+
* @access public
62+
* @param string $tableName
63+
* @return array
64+
*/
65+
public function getFields(string $tableName): array
66+
{
67+
$tableName = str_replace("`", "", $tableName);
68+
69+
$sql = $sql = sprintf("
70+
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 user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='%s') b where table_name='%s' and a.column_name=b.column_name(+)
71+
", $tableName, $tableName);
72+
73+
$pdo = $this->getPDOStatement($sql);
74+
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
75+
$info = [];
76+
77+
if (!empty($result)) {
78+
foreach ($result as $key => $val) {
79+
$val = array_change_key_case($val);
80+
81+
$info[$val['column_name']] = [
82+
'name' => $val['column_name'],
83+
'type' => $val['data_type'],
84+
'notnull' => 1 == $val['notnull'],
85+
'default' => $val['data_default'],
86+
'primary' => $val['pk'] == 1,
87+
'autoinc' => $val['pk'] == 1,
88+
'comment' => '',
89+
];
90+
}
91+
}
92+
93+
return $this->fieldCase($info);
94+
}
95+
96+
/**
97+
* 取得数据库的表信息
98+
* @access public
99+
* @param string $dbName
100+
* @return array
101+
*/
102+
public function getTables(string $dbName = ''): array
103+
{
104+
$sql = " SELECT table_name FROM USER_TABLES where TABLESPACE_NAME='MAIN'";
105+
$pdo = $this->getPDOStatement($sql);
106+
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
107+
$info = [];
108+
109+
foreach ($result as $key => $val) {
110+
$info[$key] = current($val);
111+
}
112+
113+
return $info;
114+
}
115+
116+
protected function supportSavepoint(): bool
117+
{
118+
return true;
119+
}
120+
121+
/**
122+
* 启动XA事务
123+
* @access public
124+
* @param string $xid XA事务id
125+
* @return void
126+
*/
127+
public function startTransXa(string $xid): void
128+
{
129+
$this->initConnect(true);
130+
$this->linkID->exec("XA START '$xid'");
131+
}
132+
133+
}

0 commit comments

Comments
 (0)