Oracle.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Db\Driver;
  12. use Think\Db\Driver;
  13. /**
  14. * Oracle数据库驱动
  15. */
  16. class Oracle extends Driver{
  17. private $table = '';
  18. protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
  19. /**
  20. * 解析pdo连接的dsn信息
  21. * @access public
  22. * @param array $config 连接信息
  23. * @return string
  24. */
  25. protected function parseDsn($config){
  26. $dsn = 'oci:dbname=//'.$config['hostname'].($config['hostport']?':'.$config['hostport']:'').'/'.$config['database'];
  27. if(!empty($config['charset'])) {
  28. $dsn .= ';charset='.$config['charset'];
  29. }
  30. return $dsn;
  31. }
  32. /**
  33. * 执行语句
  34. * @access public
  35. * @param string $str sql指令
  36. * @return integer
  37. */
  38. public function execute($str,$bind=[]) {
  39. $this->initConnect(true);
  40. if ( !$this->_linkID ) return false;
  41. $this->queryStr = $str;
  42. if(!empty($bind)){
  43. $this->queryStr .= '[ '.print_r($bind,true).' ]';
  44. }
  45. $flag = false;
  46. if(preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $str, $match)) {
  47. $this->table = C("DB_SEQUENCE_PREFIX").str_ireplace(C("DB_PREFIX"), "", $match[2]);
  48. $flag = (boolean)$this->query("SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper($this->table) . "'");
  49. }
  50. //释放前次的查询结果
  51. if ( !empty($this->PDOStatement) ) $this->free();
  52. $this->executeTimes++;
  53. // 记录开始执行时间
  54. $this->debug(true);
  55. $this->PDOStatement = $this->_linkID->prepare($str);
  56. if(false === $this->PDOStatement) {
  57. E($this->error());
  58. }
  59. $result = $this->PDOStatement->execute($bind);
  60. $this->debug(false);
  61. if ( false === $result) {
  62. $this->error();
  63. return false;
  64. } else {
  65. $this->numRows = $this->PDOStatement->rowCount();
  66. if($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
  67. $this->lastInsID = $this->_linkID->lastInsertId();
  68. }
  69. return $this->numRows;
  70. }
  71. }
  72. /**
  73. * 取得数据表的字段信息
  74. * @access public
  75. */
  76. public function getFields($tableName) {
  77. list($tableName) = explode(' ', $tableName);
  78. $result = $this->query("select a.column_name,data_type,decode(nullable,'Y',0,1) notnull,data_default,decode(a.column_name,b.column_name,1,0) pk "
  79. ."from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col "
  80. ."where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='".strtoupper($tableName)
  81. ."') b where table_name='".strtoupper($tableName)."' and a.column_name=b.column_name(+)");
  82. $info = array();
  83. if($result) {
  84. foreach ($result as $key => $val) {
  85. $info[strtolower($val['column_name'])] = array(
  86. 'name' => strtolower($val['column_name']),
  87. 'type' => strtolower($val['data_type']),
  88. 'notnull' => $val['notnull'],
  89. 'default' => $val['data_default'],
  90. 'primary' => $val['pk'],
  91. 'autoinc' => $val['pk'],
  92. );
  93. }
  94. }
  95. return $info;
  96. }
  97. /**
  98. * 取得数据库的表信息(暂时实现取得用户表信息)
  99. * @access public
  100. */
  101. public function getTables($dbName='') {
  102. $result = $this->query("select table_name from user_tables");
  103. $info = array();
  104. foreach ($result as $key => $val) {
  105. $info[$key] = current($val);
  106. }
  107. return $info;
  108. }
  109. /**
  110. * SQL指令安全过滤
  111. * @access public
  112. * @param string $str SQL指令
  113. * @return string
  114. */
  115. public function escapeString($str) {
  116. return str_ireplace("'", "''", $str);
  117. }
  118. /**
  119. * limit
  120. * @access public
  121. * @return string
  122. */
  123. public function parseLimit($limit) {
  124. $limitStr = '';
  125. if(!empty($limit)) {
  126. $limit = explode(',',$limit);
  127. if(count($limit)>1)
  128. $limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0]+$limit[1]) . ")";
  129. else
  130. $limitStr = "(numrow>0 AND numrow<=".$limit[0].")";
  131. }
  132. return $limitStr?' WHERE '.$limitStr:'';
  133. }
  134. /**
  135. * 设置锁机制
  136. * @access protected
  137. * @return string
  138. */
  139. protected function parseLock($lock=false) {
  140. if(!$lock) return '';
  141. return ' FOR UPDATE NOWAIT ';
  142. }
  143. }