You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.7 KiB

3 years ago
  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. * Firebird数据库驱动
  15. */
  16. class Firebird extends Driver
  17. {
  18. protected $selectSql = 'SELECT %LIMIT% %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%';
  19. /**
  20. * 解析pdo连接的dsn信息
  21. * @access public
  22. * @param array $config 连接信息
  23. * @return string
  24. */
  25. protected function parseDsn($config)
  26. {
  27. $dsn = 'firebird:dbname=' . $config['hostname'] . '/' . ($config['hostport'] ?: 3050) . ':' . $config['database'];
  28. return $dsn;
  29. }
  30. /**
  31. * 执行语句
  32. * @access public
  33. * @param string $str sql指令
  34. * @param boolean $fetchSql 不执行只是获取SQL
  35. * @return mixed
  36. */
  37. public function execute($str, $fetchSql = false)
  38. {
  39. $this->initConnect(true);
  40. if (!$this->_linkID) {
  41. return false;
  42. }
  43. $this->queryStr = $str;
  44. if (!empty($this->bind)) {
  45. $that = $this;
  46. $this->queryStr = strtr($this->queryStr, array_map(function ($val) use ($that) {return '\'' . $that->escapeString($val) . '\'';}, $this->bind));
  47. }
  48. if ($fetchSql) {
  49. return $this->queryStr;
  50. }
  51. //释放前次的查询结果
  52. if (!empty($this->PDOStatement)) {
  53. $this->free();
  54. }
  55. $this->executeTimes++;
  56. N('db_write', 1); // 兼容代码
  57. // 记录开始执行时间
  58. $this->debug(true);
  59. $this->PDOStatement = $this->_linkID->prepare($str);
  60. if (false === $this->PDOStatement) {
  61. E($this->error());
  62. }
  63. foreach ($this->bind as $key => $val) {
  64. if (is_array($val)) {
  65. $this->PDOStatement->bindValue($key, $val[0], $val[1]);
  66. } else {
  67. $this->PDOStatement->bindValue($key, $val);
  68. }
  69. }
  70. $this->bind = array();
  71. $result = $this->PDOStatement->execute();
  72. $this->debug(false);
  73. if (false === $result) {
  74. $this->error();
  75. return false;
  76. } else {
  77. $this->numRows = $this->PDOStatement->rowCount();
  78. return $this->numRows;
  79. }
  80. }
  81. /**
  82. * 取得数据表的字段信息
  83. * @access public
  84. */
  85. public function getFields($tableName)
  86. {
  87. $this->initConnect(true);
  88. list($tableName) = explode(' ', $tableName);
  89. $sql = 'SELECT RF.RDB$FIELD_NAME AS FIELD,RF.RDB$DEFAULT_VALUE AS DEFAULT1,RF.RDB$NULL_FLAG AS NULL1,TRIM(T.RDB$TYPE_NAME) || \'(\' || F.RDB$FIELD_LENGTH || \')\' as TYPE FROM RDB$RELATION_FIELDS RF LEFT JOIN RDB$FIELDS F ON (F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE) LEFT JOIN RDB$TYPES T ON (T.RDB$TYPE = F.RDB$FIELD_TYPE) WHERE RDB$RELATION_NAME=UPPER(\'' . $tableName . '\') AND T.RDB$FIELD_NAME = \'RDB$FIELD_TYPE\' ORDER By RDB$FIELD_POSITION';
  90. $result = $this->query($sql);
  91. $info = array();
  92. if ($result) {
  93. foreach ($result as $key => $val) {
  94. $info[trim($val['field'])] = array(
  95. 'name' => trim($val['field']),
  96. 'type' => $val['type'],
  97. 'notnull' => (bool) (1 == $val['null1']), // 1表示不为Null
  98. 'default' => $val['default1'],
  99. 'primary' => false,
  100. 'autoinc' => false,
  101. );
  102. }
  103. }
  104. //获取主键
  105. $sql = 'select b.rdb$field_name as field_name from rdb$relation_constraints a join rdb$index_segments b on a.rdb$index_name=b.rdb$index_name where a.rdb$constraint_type=\'PRIMARY KEY\' and a.rdb$relation_name=UPPER(\'' . $tableName . '\')';
  106. $rs_temp = $this->query($sql);
  107. foreach ($rs_temp as $row) {
  108. $info[trim($row['field_name'])]['primary'] = true;
  109. }
  110. return $info;
  111. }
  112. /**
  113. * 取得数据库的表信息
  114. * @access public
  115. */
  116. public function getTables($dbName = '')
  117. {
  118. $sql = 'SELECT DISTINCT RDB$RELATION_NAME FROM RDB$RELATION_FIELDS WHERE RDB$SYSTEM_FLAG=0';
  119. $result = $this->query($sql);
  120. $info = array();
  121. foreach ($result as $key => $val) {
  122. $info[$key] = trim(current($val));
  123. }
  124. return $info;
  125. }
  126. /**
  127. * SQL指令安全过滤
  128. * @access public
  129. * @param string $str SQL指令
  130. * @return string
  131. */
  132. public function escapeString($str)
  133. {
  134. return str_replace("'", "''", $str);
  135. }
  136. /**
  137. * limit
  138. * @access public
  139. * @param $limit limit表达式
  140. * @return string
  141. */
  142. public function parseLimit($limit)
  143. {
  144. $limitStr = '';
  145. if (!empty($limit)) {
  146. $limit = explode(',', $limit);
  147. if (count($limit) > 1) {
  148. $limitStr = ' FIRST ' . $limit[1] . ' SKIP ' . $limit[0] . ' ';
  149. } else {
  150. $limitStr = ' FIRST ' . $limit[0] . ' ';
  151. }
  152. }
  153. return $limitStr;
  154. }
  155. /**
  156. * 随机排序
  157. * @access protected
  158. * @return string
  159. */
  160. protected function parseRand()
  161. {
  162. return 'rand()';
  163. }
  164. }