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