|
@@ -1,81 +1,91 @@
|
|
<?php
|
|
<?php
|
|
-
|
|
|
|
// +----------------------------------------------------------------------
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// +----------------------------------------------------------------------
|
|
-// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
|
|
|
|
|
+// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// +----------------------------------------------------------------------
|
|
// +----------------------------------------------------------------------
|
|
// | Author: liu21st <liu21st@gmail.com>
|
|
// | Author: liu21st <liu21st@gmail.com>
|
|
// +----------------------------------------------------------------------
|
|
// +----------------------------------------------------------------------
|
|
-declare(strict_types=1);
|
|
|
|
|
|
+declare (strict_types = 1);
|
|
|
|
|
|
namespace think\db\connector;
|
|
namespace think\db\connector;
|
|
|
|
|
|
use PDO;
|
|
use PDO;
|
|
|
|
+use think\db\exception\PDOException;
|
|
use think\db\PDOConnection;
|
|
use think\db\PDOConnection;
|
|
-use think\db\BaseQuery;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
- * mysql数据库驱动.
|
|
|
|
|
|
+ * mysql数据库驱动
|
|
*/
|
|
*/
|
|
class Dm extends PDOConnection
|
|
class Dm extends PDOConnection
|
|
{
|
|
{
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * 解析pdo连接的dsn信息.
|
|
|
|
- *
|
|
|
|
- * @param array $config 连接信息
|
|
|
|
- *
|
|
|
|
|
|
+ * 解析pdo连接的dsn信息
|
|
|
|
+ * @access protected
|
|
|
|
+ * @param array $config 连接信息
|
|
* @return string
|
|
* @return string
|
|
*/
|
|
*/
|
|
protected function parseDsn(array $config): string
|
|
protected function parseDsn(array $config): string
|
|
{
|
|
{
|
|
- $dsn = 'dm:dbname=';
|
|
|
|
- if (!empty($config['hostname'])) {
|
|
|
|
- // Oracle Instant Client
|
|
|
|
- $dsn .= '//' . $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/';
|
|
|
|
- }
|
|
|
|
- $dsn .= $config['database'];
|
|
|
|
- if (!empty($config['charset'])) {
|
|
|
|
- $dsn .= ';charset=' . $config['charset'];
|
|
|
|
|
|
+ $dsn = sprintf("dm:host=%s;port=%s;dbname=%s", $config['hostname'], $config['hostport'], $config['database']);
|
|
|
|
+ return $dsn;
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 连接数据库方法
|
|
|
|
+ * @access public
|
|
|
|
+ * @param array $config 连接参数
|
|
|
|
+ * @param integer $linkNum 连接序号
|
|
|
|
+ * @param array|bool $autoConnection 是否自动连接主数据库(用于分布式)
|
|
|
|
+ * @return PDO
|
|
|
|
+ * @throws PDOException
|
|
|
|
+ */
|
|
|
|
+ public function connect(array $config = [], $linkNum = 0, $autoConnection = false): PDO {
|
|
|
|
+ if (empty($config)) {
|
|
|
|
+ $config = $this->config;
|
|
|
|
+ } else {
|
|
|
|
+ $config = array_merge($this->config, $config);
|
|
}
|
|
}
|
|
- return $dsn;
|
|
|
|
|
|
+
|
|
|
|
+ $PDO = parent::connect($config, $linkNum, $autoConnection);
|
|
|
|
+
|
|
|
|
+ $PDO->query(sprintf("SET SCHEMA %s", $config['database']));
|
|
|
|
+ return $PDO;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 取得数据表的字段信息.
|
|
|
|
- *
|
|
|
|
- * @param string $tableName
|
|
|
|
- *
|
|
|
|
|
|
+ * 取得数据表的字段信息
|
|
|
|
+ * @access public
|
|
|
|
+ * @param string $tableName
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
public function getFields(string $tableName): array
|
|
public function getFields(string $tableName): array
|
|
{
|
|
{
|
|
- [$tableName] = explode(' ', $tableName);
|
|
|
|
|
|
+ $tableName = str_replace("`", "", $tableName);
|
|
|
|
|
|
- if (!str_contains($tableName, '`')) {
|
|
|
|
- if (str_contains($tableName, '.')) {
|
|
|
|
- $tableName = str_replace('.', '`.`', $tableName);
|
|
|
|
- }
|
|
|
|
- $tableName = '`' . $tableName . '`';
|
|
|
|
- }
|
|
|
|
|
|
+ $sql = $sql = sprintf("
|
|
|
|
+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(+)
|
|
|
|
+", $tableName, $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->getPDOStatement($sql);
|
|
|
|
|
|
+ $pdo = $this->getPDOStatement($sql);
|
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
|
- $info = [];
|
|
|
|
|
|
+ $info = [];
|
|
|
|
|
|
if (!empty($result)) {
|
|
if (!empty($result)) {
|
|
foreach ($result as $key => $val) {
|
|
foreach ($result as $key => $val) {
|
|
- $val = array_change_key_case($val);
|
|
|
|
|
|
+ $val = array_change_key_case($val);
|
|
|
|
+
|
|
$info[$val['column_name']] = [
|
|
$info[$val['column_name']] = [
|
|
- 'name' => $val['column_name'],
|
|
|
|
- 'type' => $val['data_type'],
|
|
|
|
- 'notnull' => $val['notnull'],
|
|
|
|
|
|
+ 'name' => $val['column_name'],
|
|
|
|
+ 'type' => $val['data_type'],
|
|
|
|
+ 'notnull' => 1 == $val['notnull'],
|
|
'default' => $val['data_default'],
|
|
'default' => $val['data_default'],
|
|
- 'primary' => $val['pk'],
|
|
|
|
- 'autoinc' => $val['pk'],
|
|
|
|
|
|
+ 'primary' => $val['pk'] == 1,
|
|
|
|
+ 'autoinc' => $val['pk'] == 1,
|
|
|
|
+ 'comment' => '',
|
|
];
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -84,20 +94,22 @@ class Dm extends PDOConnection
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 取得数据库的表信息.
|
|
|
|
- *
|
|
|
|
- * @param string $dbName
|
|
|
|
- *
|
|
|
|
|
|
+ * 取得数据库的表信息
|
|
|
|
+ * @access public
|
|
|
|
+ * @param string $dbName
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
public function getTables(string $dbName = ''): array
|
|
public function getTables(string $dbName = ''): array
|
|
{
|
|
{
|
|
- $pdo = $this->linkID->query("select table_name from all_tables");
|
|
|
|
|
|
+ $sql = " SELECT table_name FROM USER_TABLES where TABLESPACE_NAME='MAIN'";
|
|
|
|
+ $pdo = $this->getPDOStatement($sql);
|
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
|
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
|
|
- $info = [];
|
|
|
|
|
|
+ $info = [];
|
|
|
|
+
|
|
foreach ($result as $key => $val) {
|
|
foreach ($result as $key => $val) {
|
|
$info[$key] = current($val);
|
|
$info[$key] = current($val);
|
|
}
|
|
}
|
|
|
|
+
|
|
return $info;
|
|
return $info;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -108,9 +120,8 @@ class Dm extends PDOConnection
|
|
|
|
|
|
/**
|
|
/**
|
|
* 启动XA事务
|
|
* 启动XA事务
|
|
- *
|
|
|
|
- * @param string $xid XA事务id
|
|
|
|
- *
|
|
|
|
|
|
+ * @access public
|
|
|
|
+ * @param string $xid XA事务id
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function startTransXa(string $xid): void
|
|
public function startTransXa(string $xid): void
|
|
@@ -119,43 +130,4 @@ class Dm extends PDOConnection
|
|
$this->linkID->exec("XA START '$xid'");
|
|
$this->linkID->exec("XA START '$xid'");
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 预编译XA事务
|
|
|
|
- *
|
|
|
|
- * @param string $xid XA事务id
|
|
|
|
- *
|
|
|
|
- * @return void
|
|
|
|
- */
|
|
|
|
- public function prepareXa(string $xid): void
|
|
|
|
- {
|
|
|
|
- $this->initConnect(true);
|
|
|
|
- $this->linkID->exec("XA END '$xid'");
|
|
|
|
- $this->linkID->exec("XA PREPARE '$xid'");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 提交XA事务
|
|
|
|
- *
|
|
|
|
- * @param string $xid XA事务id
|
|
|
|
- *
|
|
|
|
- * @return void
|
|
|
|
- */
|
|
|
|
- public function commitXa(string $xid): void
|
|
|
|
- {
|
|
|
|
- $this->initConnect(true);
|
|
|
|
- $this->linkID->exec("XA COMMIT '$xid'");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 回滚XA事务
|
|
|
|
- *
|
|
|
|
- * @param string $xid XA事务id
|
|
|
|
- *
|
|
|
|
- * @return void
|
|
|
|
- */
|
|
|
|
- public function rollbackXa(string $xid): void
|
|
|
|
- {
|
|
|
|
- $this->initConnect(true);
|
|
|
|
- $this->linkID->exec("XA ROLLBACK '$xid'");
|
|
|
|
- }
|
|
|
|
}
|
|
}
|