PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/varios/symfony/vendor/creole/drivers/pgsql/PgSQLConnection.php

https://github.com/cidesa/siga-universitario
PHP | 277 lines | 144 code | 34 blank | 99 comment | 28 complexity | dc314f21cae1295d5e86d20f9171053f MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: PgSQLConnection.php,v 1.21 2005/08/03 17:56:22 hlellelid Exp $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://creole.phpdb.org>.
  20. */
  21. require_once 'creole/Connection.php';
  22. require_once 'creole/common/ConnectionCommon.php';
  23. include_once 'creole/drivers/pgsql/PgSQLResultSet.php';
  24. /**
  25. * PgSQL implementation of Connection.
  26. *
  27. * @author Hans Lellelid <hans@xmpl.org> (Creole)
  28. * @author Stig Bakken <ssb@fast.no> (PEAR::DB)
  29. * @author Lukas Smith (PEAR::MDB)
  30. * @version $Revision: 1.21 $
  31. * @package creole.drivers.pgsql
  32. */
  33. class PgSQLConnection extends ConnectionCommon implements Connection {
  34. /**
  35. * Affected Rows of last executed query.
  36. * Postgres needs this for getUpdateCount()
  37. * We used to store the entire result set
  38. * instead but that can be a large dataset.
  39. * @var int
  40. */
  41. private $result_affected_rows;
  42. /**
  43. * Connect to a database and log in as the specified user.
  44. *
  45. * @param array $dsn The datasource hash.
  46. * @param $flags Any connection flags.
  47. * @access public
  48. * @throws SQLException
  49. * @return void
  50. */
  51. function connect($dsninfo, $flags = 0)
  52. {
  53. global $php_errormsg;
  54. if (!extension_loaded('pgsql')) {
  55. throw new SQLException('pgsql extension not loaded');
  56. }
  57. $this->dsn = $dsninfo;
  58. $this->flags = $flags;
  59. $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT);
  60. $protocol = (isset($dsninfo['protocol'])) ? $dsninfo['protocol'] : 'tcp';
  61. $connstr = '';
  62. if ($protocol == 'tcp') {
  63. if (!empty($dsninfo['hostspec'])) {
  64. $connstr = 'host=' . $dsninfo['hostspec'];
  65. }
  66. if (!empty($dsninfo['port'])) {
  67. $connstr .= ' port=' . $dsninfo['port'];
  68. }
  69. }
  70. if (isset($dsninfo['database'])) {
  71. $connstr .= ' dbname=\'' . addslashes($dsninfo['database']) . '\'';
  72. }
  73. if (!empty($dsninfo['username'])) {
  74. $connstr .= ' user=\'' . addslashes($dsninfo['username']) . '\'';
  75. }
  76. if (!empty($dsninfo['password'])) {
  77. $connstr .= ' password=\'' . addslashes($dsninfo['password']) . '\'';
  78. }
  79. if (!empty($dsninfo['options'])) {
  80. $connstr .= ' options=' . $dsninfo['options'];
  81. }
  82. if (!empty($dsninfo['tty'])) {
  83. $connstr .= ' tty=' . $dsninfo['tty'];
  84. }
  85. if ($persistent) {
  86. $conn = @pg_pconnect($connstr);
  87. } else {
  88. $conn = @pg_connect($connstr);
  89. }
  90. if (!$conn) {
  91. // hide the password from connstr
  92. $cleanconnstr = preg_replace('/password=\'.*?\'($|\s)/', 'password=\'*********\'', $connstr);
  93. throw new SQLException('Could not connect', $php_errormsg, $cleanconnstr);
  94. }
  95. $this->dblink = $conn;
  96. // Incluido por lhernandez 05/03/07-17:55:00
  97. // Para solventar el problema de los schemas en pgsql. Actualizado 05/02/09
  98. //-------------------------------------------------------
  99. if(!empty($_SESSION['schema']) && !empty($dsninfo['schema']) && strstr($dsninfo['schema'],'SIMA')!=''){
  100. if($dsninfo['schema']!=$_SESSION['schema'] && $dsninfo['schema']!='SIMA_USER' ) $dsninfo['schema']=$_SESSION['schema'];
  101. }
  102. if(!empty($dsninfo['schema']))
  103. $result = @pg_query($this->dblink,'SET search_path TO '.chr(34).$dsninfo['schema'].chr(34).';');
  104. //-------------------------------------------------------
  105. if(!empty($dsninfo['encoding']))
  106. $result = @pg_query($this->dblink,'SET CLIENT_ENCODING TO '.chr(34).$dsninfo['encoding'].chr(34).';');
  107. }
  108. /**
  109. * @see Connection::applyLimit()
  110. */
  111. public function applyLimit(&$sql, $offset, $limit)
  112. {
  113. if ( $limit > 0 ) {
  114. $sql .= " LIMIT ".$limit;
  115. }
  116. if ( $offset > 0 ) {
  117. $sql .= " OFFSET ".$offset;
  118. }
  119. }
  120. /**
  121. * @see Connection::disconnect()
  122. */
  123. function close()
  124. {
  125. $ret = @pg_close($this->dblink);
  126. $this->result_affected_rows = null;
  127. $this->dblink = null;
  128. return $ret;
  129. }
  130. /**
  131. * @see Connection::simpleQuery()
  132. */
  133. function executeQuery($sql, $fetchmode = null)
  134. {
  135. //$result = @pg_query($this->dblink,'SET search_path TO SIMA_USER;');
  136. $result = @pg_query($this->dblink, $sql);
  137. //print("Resultado Query =".$result);
  138. if (!$result) {
  139. throw new SQLException('Could not execute query', pg_last_error($this->dblink), $sql);
  140. }
  141. $this->result_affected_rows = (int) @pg_affected_rows($result);
  142. return new PgSQLResultSet($this, $result, $fetchmode);
  143. }
  144. /**
  145. * @see Connection::simpleUpdate()
  146. */
  147. function executeUpdate($sql)
  148. {
  149. $result = @pg_query($this->dblink, $sql);
  150. if (!$result) {
  151. throw new SQLException('Could not execute update', pg_last_error($this->dblink), $sql);
  152. }
  153. $this->result_affected_rows = (int) @pg_affected_rows($result);
  154. return $this->result_affected_rows;
  155. }
  156. /**
  157. * Start a database transaction.
  158. * @throws SQLException
  159. * @return void
  160. */
  161. protected function beginTrans()
  162. {
  163. $result = @pg_query($this->dblink, "BEGIN");
  164. if (!$result) {
  165. throw new SQLException('Could not begin transaction', pg_last_error($this->dblink));
  166. }
  167. }
  168. /**
  169. * Commit the current transaction.
  170. * @throws SQLException
  171. * @return void
  172. */
  173. protected function commitTrans()
  174. {
  175. $result = @pg_query($this->dblink, "COMMIT");
  176. if (!$result) {
  177. throw new SQLException('Could not commit transaction', pg_last_error($this->dblink));
  178. }
  179. }
  180. /**
  181. * Roll back (undo) the current transaction.
  182. * @throws SQLException
  183. * @return void
  184. */
  185. protected function rollbackTrans()
  186. {
  187. $result = @pg_query($this->dblink, "ROLLBACK");
  188. if (!$result) {
  189. throw new SQLException('Could not rollback transaction', pg_last_error($this->dblink));
  190. }
  191. }
  192. /**
  193. * Gets the number of rows affected by the data manipulation
  194. * query.
  195. * @see Statement::getUpdateCount()
  196. * @return int Number of rows affected by the last query.
  197. */
  198. function getUpdateCount()
  199. {
  200. if ( $this->result_affected_rows === null ) {
  201. throw new SQLException('getUpdateCount called before any sql queries were executed');
  202. }
  203. return $this->result_affected_rows;
  204. }
  205. /**
  206. * @see Connection::getDatabaseInfo()
  207. */
  208. public function getDatabaseInfo()
  209. {
  210. require_once 'creole/drivers/pgsql/metadata/PgSQLDatabaseInfo.php';
  211. return new PgSQLDatabaseInfo($this);
  212. }
  213. /**
  214. * @see Connection::getIdGenerator()
  215. */
  216. public function getIdGenerator()
  217. {
  218. require_once 'creole/drivers/pgsql/PgSQLIdGenerator.php';
  219. return new PgSQLIdGenerator($this);
  220. }
  221. /**
  222. * @see Connection::prepareStatement()
  223. */
  224. public function prepareStatement($sql)
  225. {
  226. require_once 'creole/drivers/pgsql/PgSQLPreparedStatement.php';
  227. return new PgSQLPreparedStatement($this, $sql);
  228. }
  229. /**
  230. * @see Connection::prepareCall()
  231. */
  232. public function prepareCall($sql) {
  233. throw new SQLException('PostgreSQL does not support stored procedures.');
  234. }
  235. /**
  236. * @see Connection::createStatement()
  237. */
  238. public function createStatement()
  239. {
  240. require_once 'creole/drivers/pgsql/PgSQLStatement.php';
  241. return new PgSQLStatement($this);
  242. }
  243. }