适用于:ThinkPHP3.2.3+
<?php <br />
// +----------------------------------------------------------------------<br>
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]<br>
// +----------------------------------------------------------------------<br>
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.<br>
// +----------------------------------------------------------------------<br>
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )<br>
// +----------------------------------------------------------------------<br>
// | Author: liu21st <liu21st><br>
// +----------------------------------------------------------------------<br>
<br>
namespace Think\Db\Driver;<br>
use Think\Db\Driver;<br>
use PDO;<br>
<br>
/**<br>
* Firebird数据库驱动 <br>
*/<br>
class Firebird extends Driver{<br>
protected $selectSql = 'SELECT %LIMIT% %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%';<br>
/**<br>
* 解析pdo连接的dsn信息<br>
* @access public<br>
* @param array $config 连接信息<br>
* @return string<br>
*/<br>
protected function parseDsn($config){<br>
$dsn = 'firebird:dbname='.$config['hostname'].'/'.($config['hostport']?:3050).':'.$config['database'];<br>
return $dsn;<br>
}<br>
<br>
/**<br>
* 连接数据库方法<br>
* @access public<br>
*/<br>
public function connect($config='',$linkNum=0) {<br>
if ( !isset($this->linkID[$linkNum]) ) {<br>
if(empty($config)) $config = $this->config;<br>
try{<br>
if(empty($config['dsn'])) {<br>
$config['dsn'] = $this->parseDsn($config);<br>
}<br>
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password']);<br>
}catch (\PDOException $e) {<br>
E($e->getMessage());<br>
}<br>
}<br>
return $this->linkID[$linkNum];<br>
}<br>
<br>
/**<br>
* 执行语句<br>
* @access public<br>
* @param string $str sql指令<br>
* @param boolean $fetchSql 不执行只是获取SQL<br>
* @return mixed<br>
*/<br>
public function execute($str,$fetchSql=false) {<br>
$this->initConnect(true);<br>
if ( !$this->_linkID ) return false;<br>
$this->queryStr = $str;<br>
if(!empty($this->bind)){<br>
$that = $this;<br>
$this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind));<br>
}<br>
if($fetchSql){<br>
return $this->queryStr;<br>
}<br>
//释放前次的查询结果<br>
if ( !empty($this->PDOStatement) ) $this->free();<br>
$this->executeTimes++;<br>
N('db_write',1); // 兼容代码<br>
// 记录开始执行时间<br>
$this->debug(true);<br>
$this->PDOStatement = $this->_linkID->prepare($str);<br>
if(false === $this->PDOStatement) {<br>
E($this->error());<br>
}<br>
foreach ($this->bind as $key => $val) {<br>
if(is_array($val)){<br>
$this->PDOStatement->bindValue($key, $val[0], $val[1]);<br>
}else{<br>
$this->PDOStatement->bindValue($key, $val);<br>
}<br>
}<br>
$this->bind = array();<br>
$result = $this->PDOStatement->execute();<br>
$this->debug(false);<br>
if ( false === $result) {<br>
$this->error();<br>
return false;<br>
} else {<br>
$this->numRows = $this->PDOStatement->rowCount();<br>
return $this->numRows;<br>
}<br>
}<br>
<br>
/**<br>
* 取得数据表的字段信息<br>
* @access public<br>
*/<br>
public function getFields($tableName) {<br>
$this->initConnect(true);<br>
list($tableName) = explode(' ', $tableName);<br>
$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';<br>
$result = $this->query($sql);<br>
$info = array();<br>
if($result){<br>
foreach($result as $key => $val){<br>
$info[trim($val['FIELD'])] = array(<br>
'name' => trim($val['FIELD']),<br>
'type' => $val['TYPE'],<br>
'notnull' => (bool) ($val['NULL1'] ==1), // 1表示不为Null<br>
'default' => $val['DEFAULT1'],<br>
'primary' => false,<br>
'autoinc' => false,<br>
);<br>
}<br>
}<br>
<br>
//获取主键<br>
$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.'\')';<br>
$rs_temp = $this->query($sql);<br>
foreach($rs_temp as $row) {<br>
$info[trim($row['FIELD_NAME'])]['primary']=True;<br>
}<br>
<br>
return $info;<br>
}<br>
<br>
/**<br>
* 取得数据库的表信息<br>
* @access public<br>
*/<br>
public function getTables($dbName='') {<br>
$sql='SELECT DISTINCT RDB$RELATION_NAME FROM RDB$RELATION_FIELDS WHERE RDB$SYSTEM_FLAG=0';<br>
$result = $this->query($sql);<br>
$info = array();<br>
foreach ($result as $key => $val) {<br>
$info[$key] = trim(current($val));<br>
}<br>
return $info;<br>
}<br>
<br>
/**<br>
* SQL指令安全过滤<br>
* @access public<br>
* @param string $str SQL指令<br>
* @return string<br>
*/<br>
public function escapeString($str) {<br>
return str_replace("'", "''", $str);<br>
}<br>
<br>
/**<br>
* limit<br>
* @access public<br>
* @param $limit limit表达式<br>
* @return string<br>
*/<br>
public function parseLimit($limit) {<br>
$limitStr = '';<br>
if(!empty($limit)) {<br>
$limit = explode(',',$limit);<br>
if(count($limit)>1) {<br>
$limitStr = ' FIRST '.$limit[1].' SKIP '.$limit[0].' ';<br>
}else{<br>
$limitStr = ' FIRST '.$limit[0].' ';<br>
}<br>
}<br>
return $limitStr;<br>
}<br>
}</liu21st>
AD:真正免费,域名+虚机+企业邮箱=0元