PageRenderTime 81ms CodeModel.GetById 72ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/class/class.postgresql.php

http://freshdns.googlecode.com/
PHP | 186 lines | 140 code | 34 blank | 12 comment | 9 complexity | 17efbf0b383195e830d4e50c48025f56 MD5 | raw file
  1<?
  2// THEORATICALLY THIS SHOULD WORK?!
  3
  4class postgresql extends database {
  5	private $master;
  6	private $slave;
  7	private $NRslaveQ;
  8	private $NRmasterQ;
  9	private $PGserver;
 10	private $masterPGserver;
 11	private $PGservers;
 12	private $replication;
 13	private $username;
 14	private $password;
 15	private $database;
 16	
 17	/*****************************************************/
 18	
 19	function __construct () {
 20		if (session_id() == "") session_start();
 21		
 22		// REPLICATION STANDARD OFF
 23		$this->replication = 0;
 24	}
 25	
 26	function __destruct () {
 27		$this->disconnect_pg();
 28		unset($this->PGserver, $this->masterPGserver, $this->NRmasterQ, $this->NRslaveQ);
 29		unset($this->username, $this->password, $this->database, $this->replication);
 30	}
 31	
 32	/*****************************************************/
 33	
 34	function initiate ()
 35	{
 36		// PICK RANDOM READER AND SAVE IT (PERSISTENT)
 37		if(!isset($_SESSION['PGserver'])){
 38			$aantal = count($this->PGservers)-1;
 39			$_SESSION['PGserver'] = rand(0,$aantal);
 40		}
 41		
 42		// SET MYSQL SERVER (SLAVE)
 43		$this->PGserver = $this->PGservers[$_SESSION['PGserver']];
 44		
 45		// CONNECT TO MYSQL
 46		$this->setstats();
 47		if(!$this->connect_to_pg()){
 48			throw new Exception("No mysql connection was made");
 49		}
 50	}
 51	
 52	function connect_to_pg (){
 53		// CONNECT TO A SLAVE
 54		$this->slave = @pg_connect("host=".$this->masterPGserver." dbname=".$this->database." user=".$this->username." password=".$this->password) or die (pg_last_error());
 55		
 56		if(!$this->slave){
 57			throw new Exception("No slave connection");
 58			return FALSE;
 59		}else{
 60			return @pg_options($this->master);
 61		}
 62	}
 63	
 64	function connect_to_pg_master(){
 65		if($this->replication==1)
 66		{
 67			// CONNECT TO THE MASTER
 68			$this->master = @pg_connect("host=".$this->masterPGserver." dbname=".$this->database." user=".$this->username." password=".$this->password) or die (pg_last_error());
 69		
 70			if(!$this->master){
 71				throw new Exception("No master connection");
 72				return FALSE;
 73			}else{
 74				return @pg_options($this->master);
 75			}
 76		}else
 77		{
 78			// ONLY CONNECT ONCE!
 79			$this->master = $this->slave;
 80		}
 81	}
 82	
 83	function disconnect_pg (){
 84		@pg_close($this->master);
 85		@pg_close($this->slave);
 86	}
 87	
 88	/*****************************************************/
 89	
 90	function query_slave($query){
 91		if(!$this->slave){
 92			$this->connect_to_pg();
 93		}
 94		
 95		$this->NRslaveQ++;
 96		$query = pg_query($query, $this->slave);
 97		return $query;
 98	}
 99	
100	function query_master($query){
101		if(!$this->master){
102			$this->connect_to_pg_master();
103		}
104		
105		$this->NRmasterQ++;
106		$query = pg_query($query, $this->master);
107		return $query;
108	}
109	
110	function error ()
111	{
112		return pg_last_error();
113	}
114	
115	function fetch_array($query)
116	{
117		return pg_fetch_array($query);
118	}
119	
120	function num_rows ($query)
121	{
122		return pg_num_rows($query);
123	}
124	
125	function escape_string ($string)
126	{
127		return pg_escape_string($string);
128	}
129	
130	/*****************************************************/
131	
132	function showstats (){
133		$return = array(
134		'masterQ' => $this->NRmasterQ,
135		'slaveQ' => $this->NRslaveQ
136		);
137		
138		return $return;
139	}
140	
141	function setstats(){
142		$this->NRmasterQ 	= '0';
143		$this->NRslaveQ 	= '0';
144	}
145	
146	function setUsername ($username)
147	{
148		$this->username = $username;
149	}
150	
151	function setPassword ($password)
152	{
153		$this->password = $password;
154	}
155	
156	function setDatabase ($database)
157	{
158		$this->database = $database;
159	}
160	
161	function setMasterHost ($master)
162	{
163		$this->masterPGserver = $master;
164	}
165	
166	function setSlaveHosts ($slaves = array())
167	{
168		$this->PGservers = $slaves;
169	}
170	
171	function setReplication ($replication)
172	{
173		$this->replication = $replication;
174	}
175	
176	function setVars ($username, $password, $database, $master, $slave = array(), $replication='')
177	{
178		$this->setUsername($username);
179		$this->setPassword($password);
180		$this->setDatabase($database);
181		$this->setMasterHost($master);
182		$this->setSlaveHosts($slave);
183		$this->setReplication($replication);
184	}
185}
186?>