【php开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了php开源代码-dbo_postgres.php的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
<?php
/* SVN FILE: $Id: dbo_postgres.php 4409 2007-02-02 13:20:59Z phpnut $ */
/**
* PostgreSQL layer for DBO.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp Cake
PHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.model.dbo
* @since CakePHP(tm) v 0.9.1.114
* @version $Revision: 4409 $
* @modifiedby $LastChangedBy: phpnut $
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Include DBO.
*/
uses ('model' . DS . 'datasources' . DS . 'dbo_source');
/**
* PostgreSQL layer for DBO.
*
* Long description for class
*
* @package cake
* @subpackage cake.cake.libs.model.dbo
*/
class DboPostgres extends DboSource{
var $description = "PostgreSQL DBO Driver";
var $_baseConfig = array(
'connect' => 'pg_pconnect',
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cake',
'port' => 5432
);
var $columns = array('primary_key' => array('name' => 'serial NOT NULL'),
'string' => array('name' => 'varchar', 'limit' => '255'),
'text' => array('name' => 'text'),
'integer' => array('name' => 'integer'),
'float' => array('name' => 'float'),
'datetime' => array('name' => 'timestamp'),
'timestamp' => array('name' => 'timestamp'),
'time' => array('name' => 'time'),
'date' => array('name' => 'date'),
'binary' => array('name' => 'bytea'),
'boolean' => array('name' => 'boolean'),
'number' => array('name' => 'numeric'),
'inet' => array('name' => 'inet'));
var $startQuote = '"';
var $endQuote = '"';
/**
* Connects to the database using options in the given configuration array.
*
* @return True if successfully connected.
*/
function connect() {
$config = $this->config;
$connect = $config['connect'];
$this->connection = $connect("host='{$config['host']}' port='{$config['port']}' dbname='{$config['database']}' user='{$config['login']}' pass
word='{$config['password']}'");
if ($this->connection) {
$this->connected = true;
} else {
$this->connected = false;
}
return $this->connected;
}
/**
* Disconnects from database.
*
* @return boolean True if the database could be disconnected, else false
*/
function disconnect() {
$this->connected=!@pg_close($this->connection);
return !$this->connected;
}
/**
* Executes given SQL statement.
*
* @param string $sql SQL statement
* @return resource Result resource identifier
*/
function _execute($sql) {
return pg_query($this->connection, $
sql);
}
/**
* Returns a row from given resultset as an array .
*
* @return array The fetched row as an array
*/
function fetchRow($assoc = false) {
if (is_resource($this->_result)) {
$this->resultSet($this->_result);
$resultRow=$this->fetchResult();
return $resultRow;
} else {
return null;
}
}
/**
* Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
*
* @return array Array of tablenames in the database
*/
function listSources() {
$cache = parent::listSources();
if ($cache != null) {
return $cache;
}
$sql = "SELECT table_name as name FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'public';";
$result = $this->fetchAll($sql);
if (!$result) {
return array();
} else {
$tables = array();
foreach($result as $item) {
$tables[] = $item[0]['name'];
}
parent::listSources($tables);
return $tables;
}
}
/**
* Returns an array of the fields in given table name.
*
* @param string $tableName Name of database table to inspect
* @return array Fields in table. Keys are name and type
*/
function &describe(&$model) {
$cache = parent::describe($model);
if ($cache != null) {
return $cache;
}
$fields = false;
$cols = $this->fetchAll("SELECT DISTINCT column_name AS name, data_type AS type, is_nullable AS null, column_default AS default, ordinal_position FROM information_schema.columns WHERE table_name =" . $this