|
| 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