Connection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <liu21st@gmail.com>
  8. // +----------------------------------------------------------------------
  9. namespace think\oracle;
  10. use PDO;
  11. use think\db\Connection as BaseConnection;
  12. /**
  13. * Oracle数据库驱动
  14. */
  15. class Connection extends BaseConnection
  16. {
  17. protected $builder = '\\think\\oracle\\Builder';
  18. /**
  19. * 解析pdo连接的dsn信息
  20. * @access protected
  21. * @param array $config 连接信息
  22. * @return string
  23. */
  24. protected function parseDsn($config)
  25. {
  26. $dsn = 'oci:dbname=';
  27. if (!empty($config['hostname'])) {
  28. // Oracle Instant Client
  29. $dsn .= '//' . $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/';
  30. }
  31. $dsn .= $config['database'];
  32. if (!empty($config['charset'])) {
  33. $dsn .= ';charset=' . $config['charset'];
  34. }
  35. return $dsn;
  36. }
  37. /**
  38. * 取得数据表的字段信息
  39. * @access public
  40. * @param string $tableName
  41. * @return array
  42. */
  43. public function getFields($tableName)
  44. {
  45. $this->initConnect(true);
  46. list($tableName) = explode(' ', $tableName);
  47. $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 (+)";
  48. $pdo = $this->linkID->query($sql);
  49. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  50. $info = [];
  51. if ($result) {
  52. foreach ($result as $key => $val) {
  53. $val = array_change_key_case($val);
  54. $info[$val['column_name']] = [
  55. 'name' => $val['column_name'],
  56. 'type' => $val['data_type'],
  57. 'notnull' => $val['notnull'],
  58. 'default' => $val['data_default'],
  59. 'primary' => $val['pk'],
  60. 'autoinc' => $val['pk'],
  61. ];
  62. }
  63. }
  64. return $this->fieldCase($info);
  65. }
  66. /**
  67. * 取得数据库的表信息(暂时实现取得用户表信息)
  68. * @access public
  69. * @param string $dbName
  70. * @return array
  71. */
  72. public function getTables($dbName = '')
  73. {
  74. $pdo = $this->linkID->query("select table_name from all_tables");
  75. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  76. $info = [];
  77. foreach ($result as $key => $val) {
  78. $info[$key] = current($val);
  79. }
  80. return $info;
  81. }
  82. /**
  83. * 获取最近插入的ID
  84. * @access public
  85. * @param string $sequence 自增序列名
  86. * @return string
  87. */
  88. public function getLastInsID($sequence = null)
  89. {
  90. $pdo = $this->linkID->query("select {$sequence}.currval as id from dual");
  91. $result = $pdo->fetchColumn();
  92. return $result;
  93. }
  94. /**
  95. * SQL性能分析
  96. * @access protected
  97. * @param string $sql
  98. * @return array
  99. */
  100. protected function getExplain($sql)
  101. {
  102. return [];
  103. }
  104. protected function supportSavepoint()
  105. {
  106. return true;
  107. }
  108. }