<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <hide@address.com>
// +----------------------------------------------------------------------
// $Id: DbPgsql.class.php 2741 2012-02-17 09:00:07Z liu21st $
/**
+---------------------------
* Pgsqlæ°æ®åºé©±å¨ç±»
+---------------------------
*/
class DbPgsql extends Db{
/**
+----------------------------------------------------------
* æ¶æå½æ° è¯»åæ°æ®åºé
置信æ¯
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $config æ°æ®åºé
ç½®æ°ç»
+----------------------------------------------------------
*/
public function __construct($config='') {
if ( !extension_loaded('pgsql') ) {
throw_exception(L('_NOT_SUPPERT_').':pgsql');
}
if(!empty($config)) {
$this->config = $config;
if(empty($this->config['params'])) {
$this->config['params'] = array();
}
}
}
/**
+----------------------------------------------------------
* è¿æ¥æ°æ®åºæ¹æ³
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function connect($config='',$linkNum=0) {
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
$pconnect = !empty($config['params']['persist'])? $config['params']['persist']:$this->pconnect;
$conn = $pconnect ? 'pg_pconnect':'pg_connect';
$this->linkID[$linkNum] = $conn('host='.$config['hostname'].' port='.$config['hostport'].' dbname='.$config['database'].' user='.$config['username'].' password='.$config['password']);
if (0 !== pg_connection_status($this->linkID[$linkNum])){
throw_exception($this->error(false));
}
//设置ç¼ç
pg_set_client_encoding($this->linkID[$linkNum], C('DB_CHARSET'));
//$pgInfo = pg_version($this->linkID[$linkNum]);
//$dbVersion = $pgInfo['server'];
// æ è®°è¿æ¥æå
$this->connected = true;
//æ³¨éæ°æ®åºå®å
¨ä¿¡æ¯
if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
}
return $this->linkID[$linkNum];
}
/**
+----------------------------------------------------------
* éæ¾æ¥è¯¢ç»æ
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function free() {
pg_free_result($this->queryID);
$this->queryID = null;
}
/**
+----------------------------------------------------------
* æ§è¡æ¥è¯¢ è¿åæ°æ®é
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $str sqlæä»¤
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function query($str) {
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//鿾忬¡çæ¥è¯¢ç»æ
if ( $this->queryID ) $this->free();
N('db_query',1);
// è®°å½å¼å§æ§è¡æ¶é´
G('queryStartTime');
$this->queryID = pg_query($this->_linkID,$str);
$this->debug();
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = pg_num_rows($this->queryID);
return $this->getAll();
}
}
/**
+----------------------------------------------------------
* æ§è¡è¯å¥
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $str sqlæä»¤
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function execute($str) {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//鿾忬¡çæ¥è¯¢ç»æ
if ( $this->queryID ) $this->free();
N('db_write',1);
// è®°å½å¼å§æ§è¡æ¶é´
G('queryStartTime');
$result = pg_query($this->_linkID,$str);
$this->debug();
if ( false === $result ) {
$this->error();
return false;
} else {
$this->numRows = pg_affected_rows($result);
$this->lastInsID = $this->last_insert_id();
return $this->numRows;
}
}
/**
+----------------------------------------------------------
* ç¨äºè·åæåæå
¥çID
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return integer
+----------------------------------------------------------
*/
public function last_insert_id() {
$query = "SELECT LASTVAL() AS insert_id";
$result = pg_query($this->_linkID,$query);
list($last_insert_id) = pg_fetch_array($result,null,PGSQL_ASSOC);
pg_free_result($result);
return $last_insert_id;
}
/**
+----------------------------------------------------------
* å¯å¨äºå¡
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function startTrans() {
$this->initConnect(true);
if ( !$this->_linkID ) return false;
//æ°æ®rollback æ¯æ
if ($this->transTimes == 0) {
pg_exec($this->_linkID,'begin;');
}
$this->transTimes++;
return ;
}
/**
+----------------------------------------------------------
* ç¨äºéèªå¨æäº¤ç¶æä¸é¢çæ¥è¯¢æäº¤
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function commit() {
if ($this->transTimes > 0) {
$result = pg_exec($this->_linkID,'end;');
if(!$result){
throw_exception($this->error());
}
$this->transTimes = 0;
}
return true;
}
/**
+----------------------------------------------------------
* äºå¡åæ»
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function rollback() {
if ($this->transTimes > 0) {
$result = pg_exec($this->_linkID,'abort;');
if(!$result){
throw_exception($this->error());
}
$this->transTimes = 0;
}
return true;
}
/**
+----------------------------------------------------------
* è·å¾ææçæ¥è¯¢æ°æ®
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @return array
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
private function getAll() {
//è¿åæ°æ®é
$result = pg_fetch_all($this->queryID);
pg_result_seek($this->queryID,0);
return $result;
}
/**
+----------------------------------------------------------
* å徿°æ®è¡¨çåæ®µä¿¡æ¯
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function getFields($tableName) {
$result = $this->query("select a.attname as \"Field\",
t.typname as \"Type\",
a.attnotnull as \"Null\",
i.indisprimary as \"Key\",
d.adsrc as \"Default\"
from pg_class c
inner join pg_attribute a on a.attrelid = c.oid
inner join pg_type t on a.atttypid = t.oid
left join pg_attrdef d on a.attrelid=d.adrelid and d.adnum=a.attnum
left join pg_index i on a.attnum=ANY(i.indkey) and c.oid = i.indrelid
where (c.relname='{$tableName}' or c.relname = lower('{$tableName}')) AND a.attnum > 0
order by a.attnum asc;");
$info = array();
if($result) {
foreach ($result as $key => $val) {
$info[$val['Field']] = array(
'name' => $val['Field'],
'type' => $val['Type'],
'notnull' => (bool) ($val['Null'] == 't'?1:0), // 't' is 'not null'
'default' => $val['Default'],
'primary' => (strtolower($val['Key']) == 't'),
'autoinc' => (strtolower($val['Default']) == "nextval('{$tableName}_id_seq'::regclass)"),
);
}
}
return $info;
}
/**
+----------------------------------------------------------
* å徿°æ®åºç表信æ¯
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function getTables($dbName='') {
$result = $this->query("select tablename as Tables_in_test from pg_tables where schemaname ='public'");
$info = array();
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
return $info;
}
/**
+----------------------------------------------------------
* å
³éæ°æ®åº
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function close() {
if($this->_linkID){
pg_close($this->_linkID);
}
$this->_linkID = null;
}
/**
+----------------------------------------------------------
* æ°æ®åºé误信æ¯
* å¹¶æ¾ç¤ºå½åçSQLè¯å¥
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function error($result = true) {
$this->error = $result?pg_result_error($this->queryID): pg_last_error($this->_linkID);
if($this->debug && '' != $this->queryStr){
$this->error .= "\n [ SQLè¯å¥ ] : ".$this->queryStr;
}
return $this->error;
}
/**
+----------------------------------------------------------
* SQLæä»¤å®å
¨è¿æ»¤
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $str SQLæä»¤
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
public function escapeString($str) {
return pg_escape_string($str);
}
/**
+----------------------------------------------------------
* limit
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
public function parseLimit($limit) {
$limitStr = '';
if(!empty($limit)) {
$limit = explode(',',$limit);
if(count($limit)>1) {
$limitStr .= ' LIMIT '.$limit[1].' OFFSET '.$limit[0].' ';
}else{
$limitStr .= ' LIMIT '.$limit[0].' ';
}
}
return $limitStr;
}
}