Login   Register  
PHP Classes
elePHPant
Icontem

File: PgSQL_Driver.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of John Luxford  >  Database.php  >  PgSQL_Driver.php  >  Download  
File: PgSQL_Driver.php
Role: ???
Content type: text/plain
Description: The PostgreSQL driver classes.
Class: Database.php
Author: By
Last change:
Date: 2001-08-31 20:14
Size: 9,187 bytes
 

Contents

Class file image Download
<?php
//
// +----------------------------------------------------------------------+
// | Sitellite - Content Management System                                |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001 Simian Systems                                    |
// +----------------------------------------------------------------------+
// | This software is released under the Simian Open Software License.    |
// | Please see the accompanying file OPENLICENSE for licensing details!  |
// |                                                                      |
// | You should have received a copy of the Simian Open Software License  |
// | along with this program; if not, write to Simian Systems,            |
// | 101-314 Broadway, Winnipeg, MB, R3C 0S7, CANADA.  The Simian         |
// | Public License is also available at the following web site           |
// | address: <http://www.simian.ca/license.php>                          |
// +----------------------------------------------------------------------+
// | Authors: John Luxford <lux@simian.ca>                                |
// +----------------------------------------------------------------------+
//
// PgSQL_Driver is the Database driver for the PostgreSQL database system.
//

/*!

<package name="PgSQL_Driver">

<class	name="PgSQL_Query"
			parent="Query"
			access="public"
			date="2001-05-29 11:05:37"
			version="1.0">

	<author	name="John Luxford"
				email="lux@simian.ca"
				url="http://www.simian.ca/" />

	<summary>PgSQL_Driver is the Database driver for the PostgreSQL database system.
PgSQL_Query provides all of the PostgreSQL-specific functionality
for Query objects.  It is accessed through the Database class API.</summary>

	<example>$q = new PgSQL_Query ("select * from table");
if ($q->execute ()) {
	while ($row = $q->fetch ()) {
		// do something with $row
	}
	$q->free ();
} else {
	// query failed
}</example> !*/

class PgSQL_Query extends Query {
	/*! <property name="sql" access="public" type="string">
	<summary>Contains the SQL query to be executed.</summary>
	</property> !*/
	var $sql = "";

	/*! <property name="result" access="public" type="resource">
	<summary>Contains the result identifier for the current execution.</summary>
	</property> !*/
	var $result = "";

	/*! <property name="field" access="public" type="string">
	<summary>Currently unused.</summary>
	</property> !*/
	var $field = "";

	/*! <property name="connection" access="public" type="resource">
	<summary>The resource ID for the database connection to be queried.</summary>
	</property> !*/
	var $connection = "";

	/*! <method name="PgSQL_Query" access="public">
	<summary>Constructor method.

	$sql is the SQL query you wish to execute with this object.
	$conn is the database connection resource to be queried.</summary>
	<param name="sql" type="string" />
	<param name="conn" type="resource" />
	</method> !*/
	function PgSQL_Query ($sql = "", $conn = "") {
		$this->Query ($sql, $conn);
	}

	/*! <method name="execute" access="public">
	<summary>Executes the current SQL query.

	$values is an array of values to substitute.  The syntax for denoting
	bind_values in the SQL query is a double-questionmark (??).

	Returns the new SQL query.  Note: does not modify the actual $sql property.</summary>
	<param name="values" type="array" />
	<returns type="resource" />
	</method> !*/
	function execute () {
		// call bind_values
		$this->bind_values (func_get_args ());

		// query the database
		$this->result = pg_exec ($this->connection, $this->tmp_sql);
		$this->counter = 0;
		return $this->result;
	}

	/*! <method name="field" access="public">
	<summary>Returns the name of the specified column in the table currently being queried.

	$num is the column number.  Note: Some database systems begin with
	column 0, while others with 1.</summary>
	<param name="num" type="integer" />
	<returns type="string" />
	</method> !*/
	function field ($num = 0) {
		if ($this->result) {
			return pg_fieldname ($this->result, $num);
		} else {
			return 0;
		}
	}

	/*! <method name="rows" access="public">
	<summary>Returns the number of rows affected or found by the current query.</summary>
	<returns type="integer" />
	</method> !*/
	function rows () {
		if ($this->result) {
			return pg_numrows ($this->result);
		} else {
			return 0;
		}
	}

	/*! <method name="lastid" access="public">
	<summary>Returns the OID generated by the database during the previous
	SQL insert query.</summary>
	<returns type="integer" />
	</method> !*/
	function lastid () {
		if ($this->result) {
			return pg_getlastoid ($this->result);
		} else {
			return 0;
		}
	}

	/*! <method name="fetch" access="public">
	<summary>Returns the next row of data from the current query, always in the
	form of an object, with each column as its properties.</summary>
	<returns type="object" />
	</method> !*/
	function fetch () {
		if ($this->result) {
			$this->counter++;
			return pg_fetch_object ($this->result, $this->counter - 1);
		} else {
			return 0;
		}
	}

	/*! <method name="free" access="public">
	<summary>Tells the database system to let go of its data from the previous
	query.</summary>
	</method> !*/
	function free () {
		if ($this->result) {
			return pg_freeresult ($this->result);
		}
	}

	/*! <method name="begin" access="public">
	<summary>Issues a mysql "begin" statement, beginning a transaction.</summary>
	</method> !*/
	function begin () {
		pg_exec ("begin");
	}

	/*! <method name="commit" access="public">
	<summary>Issues a mysql "commit" statement, committing the current transaction.</summary>
	</method> !*/
	function commit () {
		pg_exec ("commit");
	}

	/*! <method name="rollback" access="public">
	<summary>Issues a mysql "rollback" statement, rolling back the current transaction.</summary>
	</method> !*/
	function rollback () {
		pg_exec ("rollback");
	}
}

/*! </class>

<class	name="PgSQL_Driver"
			access="public"
			date="2001-05-29 11:05:37"
			version="1.0">

	<author	name="John Luxford"
				email="lux@simian.ca"
				url="http://www.simian.ca/" />

	<summary>PgSQL_Driver provides all of the PostgreSQL-specific functionality
for Database objects.  It is accessed through the Database class API.</summary>

	<example>$db = new PgSQL_Driver ("name", "host", "user", "pass", 1);

if ($db->connection) {
	// connection worked
} else {
	// connection failed
}</example> !*/

class PgSQL_Driver {
	/*! <property name="connection" access="public" type="resource">
	<summary>Contains the database connection resource.</summary>
	</property> !*/
	var $connection;

	/*! <property name="name" access="public" type="string">
	<summary>Contains the name of the database being used.</summary>
	</property> !*/
	var $name;

	/*! <property name="host" access="public" type="string">
	<summary>Contains the name of the database host.</summary>
	</property> !*/
	var $host;

	/*! <property name="user" access="public" type="string">
	<summary>Contains the username used to connect to the current database.</summary>
	</property> !*/
	var $user;

	/*! <property name="pass" access="public" type="string">
	<summary>Contains the password used to connect to the current database.</summary>
	</property> !*/
	var $pass;

	/*! <property name="persistent" access="public" type="boolean">
	<summary>A 1 or 0 (true or false, and true by default), specifying whether
	to establish a persistent connection or not.</summary>
	</property> !*/
	var $persistent;

	/*! <method name="PgSQL_Driver" access="public">
	<summary>Constructor method.</summary>
	<param name="name" type="string" />
	<param name="host" type="string" />
	<param name="user" type="string" />
	<param name="pass" type="string" />
	<param name="persistent" type="boolean" default="1" />
	</method> !*/
	function PgSQL_Driver ($name = "", $host = "", $user = "", $pass = "", $persistent = 1) {
		$this->name = $name;
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->persistent = $persistent;
		$this->connect ();
	}

	/*! <method name="connect" access="public">
	<summary>Establishes a connection with the database</summary>
	</method> !*/
	function connect () {
		if ($this->persistent) {
			$this->connection = pg_pconnect ("host=" . $this->host . " port=5432 dbname=" . $this->name . " user=" . $this->user . " password=" . $this->pass);
		} else {
			$this->connection = pg_connect ("host=" . $this->host . " port=5432 dbname=" . $this->name . " user=" . $this->user . " password=" . $this->pass);
		}
	}

	/*! <method name="query" access="public">
	<summary>Creates and returns a Query object.

	$sql is the SQL query you wish to execute with this object.</summary>
	<param name="sql" type="string" />
	<returns type="object" />
	</method> !*/
	function query ($sql = "") {
		// inherits full functionality from Database class
	}
}

/*! </class>

</package> !*/

?>