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