Pelzini

This is the code documentation for the Pelzini project

source of /viewer/database_postgresql.php

The PostgreSQL wrapper functions
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. /**
  22.  * The PostgreSQL wrapper functions
  23.  * @package Viewer
  24.  * @author Josh Heidenreich
  25.  * @since 0.2
  26.  **/
  27.  
  28.  
  29. /**
  30.  * Connects to the PostgreSQL database
  31.  **/
  32. function db_connect($settings)
  33. {
  34. global $db_connection, $settings;
  35.  
  36. $connect = '';
  37. if (isset($settings['server'])) $connect .= "host='{$settings['server']}' ";
  38. if (isset($settings['username'])) $connect .= "user='{$settings['username']}' ";
  39. if (isset($settings['password'])) $connect .= "password='{$settings['password']}' ";
  40. if (isset($settings['name'])) $connect .= "dbname='{$settings['name']}' ";
  41.  
  42. $db_connection = pg_connect($connect);
  43. }
  44.  
  45.  
  46. /**
  47.  * Makes a query to the PostgreSQL database
  48.  **/
  49. function db_query($q)
  50. {
  51. //echo "<pre>{$q}</pre>";
  52.  
  53. return pg_query($q);
  54. }
  55.  
  56.  
  57. /**
  58.  * Escapses a string for use by the PostgreSQL database
  59.  **/
  60. function db_escape($str)
  61. {
  62. return pg_escape_string($str);
  63. }
  64.  
  65.  
  66. /**
  67.  * Quotes a string as nessasary for use by hte PostgreSQL database
  68.  * The result will be different depending on the type of the input.
  69.  * - a number will be left as is
  70.  * - a string will be quoted
  71.  * - a null value will be returned as NULL
  72.  **/
  73. function db_quote($str)
  74. {
  75. if ($str === null) {
  76. return 'NULL';
  77.  
  78. } else if (is_int($str)) {
  79. return $str;
  80.  
  81. } else {
  82. return "'" . pg_escape_string($str) . "'";
  83. }
  84. }
  85.  
  86.  
  87. /**
  88.  * Fetches a PostgreSQL result set as an associative array
  89.  **/
  90. function db_fetch_assoc($res)
  91. {
  92. return pg_fetch_assoc($res);
  93. }
  94.  
  95.  
  96. /**
  97.  * Returns the number of rows in a PostgreSQL result set
  98.  **/
  99. function db_num_rows($res)
  100. {
  101. return pg_num_rows($res);
  102. }
  103.  
  104.  
  105. /**
  106.  * Returns the number of rows affected by the last PostgreSQL query that was executed
  107.  **/
  108. function db_affected_rows($res)
  109. {
  110. return pg_affected_rows($res);
  111. }
  112.  
  113.  
  114. /**
  115.  * Returns the last unique ID generated by a query
  116.  **/
  117. function db_insert_id()
  118. {
  119. $res = $this->query ('SELECT LASTVAL()');
  120. $row = $this->fetch_row ($res);
  121. return $row[0];
  122. }
  123.  
  124.  
  125. ?>
  126.