Oracle.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. {
  18. private $table = '';
  19. protected $selectSql = 'SELECT * FROM (SELECT thinkphp.*, rownum AS numrow FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%) thinkphp ) %LIMIT%%COMMENT%';
  20. /**
  21. * 解析pdo连接的dsn信息
  22. * @access public
  23. * @param array $config 连接信息
  24. * @return string
  25. */
  26. protected function parseDsn($config)
  27. {
  28. $dsn = 'oci:dbname=//' . $config['hostname'] . ($config['hostport'] ? ':' . $config['hostport'] : '') . '/' . $config['database'];
  29. if (!empty($config['charset'])) {
  30. $dsn .= ';charset=' . $config['charset'];
  31. }
  32. return $dsn;
  33. }
  34. /**
  35. * 执行语句
  36. * @access public
  37. * @param string $str sql指令
  38. * @param boolean $fetchSql 不执行只是获取SQL
  39. * @return integer
  40. */
  41. public function execute($str, $fetchSql = false)
  42. {
  43. $this->initConnect(true);
  44. if (!$this->_linkID) {
  45. return false;
  46. }
  47. $this->queryStr = $str;
  48. if (!empty($this->bind)) {
  49. $that = $this;
  50. $this->queryStr = strtr($this->queryStr, array_map(function ($val) use ($that) {return '\'' . $that->escapeString($val) . '\'';}, $this->bind));
  51. }
  52. if ($fetchSql) {
  53. return $this->queryStr;
  54. }
  55. $flag = false;
  56. if (preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $str, $match)) {
  57. $this->table = C("DB_SEQUENCE_PREFIX") . str_ireplace(C("DB_PREFIX"), "", $match[2]);
  58. $flag = (boolean) $this->query("SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper($this->table) . "'");
  59. }
  60. //释放前次的查询结果
  61. if (!empty($this->PDOStatement)) {
  62. $this->free();
  63. }
  64. $this->executeTimes++;
  65. N('db_write', 1); // 兼容代码
  66. // 记录开始执行时间
  67. $this->debug(true);
  68. $this->PDOStatement = $this->_linkID->prepare($str);
  69. if (false === $this->PDOStatement) {
  70. $this->error();
  71. return false;
  72. }
  73. foreach ($this->bind as $key => $val) {
  74. if (is_array($val)) {
  75. $this->PDOStatement->bindValue($key, $val[0], $val[1]);
  76. } else {
  77. $this->PDOStatement->bindValue($key, $val);
  78. }
  79. }
  80. $this->bind = array();
  81. $result = $this->PDOStatement->execute();
  82. $this->debug(false);
  83. if (false === $result) {
  84. $this->error();
  85. return false;
  86. } else {
  87. $this->numRows = $this->PDOStatement->rowCount();
  88. if ($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
  89. $this->lastInsID = $this->_linkID->lastInsertId();
  90. }
  91. return $this->numRows;
  92. }
  93. }
  94. /**
  95. * 取得数据表的字段信息
  96. * @access public
  97. */
  98. public function getFields($tableName)
  99. {
  100. list($tableName) = explode(' ', $tableName);
  101. $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 "
  102. . "from user_tab_columns a,(select column_name from user_constraints c,user_cons_columns col "
  103. . "where c.constraint_name=col.constraint_name and c.constraint_type='P'and c.table_name='" . strtoupper($tableName)
  104. . "') b where table_name='" . strtoupper($tableName) . "' and a.column_name=b.column_name(+)");
  105. $info = array();
  106. if ($result) {
  107. foreach ($result as $key => $val) {
  108. $info[strtolower($val['column_name'])] = array(
  109. 'name' => strtolower($val['column_name']),
  110. 'type' => strtolower($val['data_type']),
  111. 'notnull' => $val['notnull'],
  112. 'default' => $val['data_default'],
  113. 'primary' => $val['pk'],
  114. 'autoinc' => $val['pk'],
  115. );
  116. }
  117. }
  118. return $info;
  119. }
  120. /**
  121. * 取得数据库的表信息(暂时实现取得用户表信息)
  122. * @access public
  123. */
  124. public function getTables($dbName = '')
  125. {
  126. $result = $this->query("select table_name from user_tables");
  127. $info = array();
  128. foreach ($result as $key => $val) {
  129. $info[$key] = current($val);
  130. }
  131. return $info;
  132. }
  133. /**
  134. * SQL指令安全过滤
  135. * @access public
  136. * @param string $str SQL指令
  137. * @return string
  138. */
  139. public function escapeString($str)
  140. {
  141. return str_ireplace("'", "''", $str);
  142. }
  143. /**
  144. * limit
  145. * @access public
  146. * @return string
  147. */
  148. public function parseLimit($limit)
  149. {
  150. $limitStr = '';
  151. if (!empty($limit)) {
  152. $limit = explode(',', $limit);
  153. if (count($limit) > 1) {
  154. $limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
  155. } else {
  156. $limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
  157. }
  158. }
  159. return $limitStr ? ' WHERE ' . $limitStr : '';
  160. }
  161. /**
  162. * 设置锁机制
  163. * @access protected
  164. * @return string
  165. */
  166. protected function parseLock($lock = false)
  167. {
  168. if (!$lock) {
  169. return '';
  170. }
  171. return ' FOR UPDATE NOWAIT ';
  172. }
  173. /**
  174. * 随机排序
  175. * @access protected
  176. * @return string
  177. */
  178. protected function parseRand()
  179. {
  180. return 'DBMS_RANDOM.value';
  181. }
  182. }