Source for file sql.php

Documentation is available at sql.php

  1. <?php
  2. /**
  3. * SQL interface functions, return statuscodes as appropriate
  4. *
  5. * The SQL functions are encapsulated here to make it easier
  6. * to keep track of them, particularly for porting to other databases. Encapsulation is
  7. * done here with a functional interface not an object interface.
  8. *
  9. @todo //TODO: work out why we didn't just use PEAR::DB and be done with it right from the beginning
  10. *
  11. @author     Stuart Prescott
  12. @copyright  Copyright Stuart Prescott
  13. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  14. @version    $Id$
  15. @package    Bumblebee
  16. @subpackage FormsLibrary
  17. */
  18.  
  19. /** Load ancillary functions */
  20. require_once 'inc/typeinfo.php';
  21.  
  22. /** status codes for success/failure of database actions */
  23. require_once('inc/statuscodes.php');
  24.  
  25. /**
  26. * run an sql query without returning data
  27. *
  28. @param string $q       the sql query, properly constructed and quoted
  29. @param boolean $fatal_sql   db errors are fatal
  30. @return integer status from statuscodes
  31. */
  32. function db_quiet($q$fatal_sql=0{
  33.   //preDump(debug_backtrace());
  34.   // returns from statuscodes
  35.   $sql mysql_query($q);
  36.   echoSQL($q);
  37.   if ($sql{
  38.     return echoSQLerror(mysql_error()$fatal_sql);
  39.   else {
  40.     return STATUS_OK// should this return the $sql handle?
  41.   }
  42. }
  43.  
  44. /**
  45. * run an sql query and return the sql handle for further requests
  46. *
  47. @param string $q       the sql query, properly constructed and quoted
  48. @param boolean $fatal_sql   db errors are fatal
  49. @return resource mysql query handle
  50. */
  51. function db_get($q$fatal_sql=0{
  52.   //preDump(debug_backtrace());
  53.   // returns from statuscodes or a db handle
  54.   $sql mysql_query($q);
  55.   echoSQL($q);
  56.   if ($sql{
  57.     return echoSQLerror(mysql_error()$fatal_sql);
  58.   else {
  59.     return $sql;
  60.   }
  61. }
  62.  
  63. /**
  64. * run an sql query and return the single (or first) row returned
  65. *
  66. @param string $q       the sql query, properly constructed and quoted
  67. @param boolean $fatal_sql   db errors are fatal
  68. @return mixed   array if successful, false if error
  69. */
  70. function db_get_single($q$fatal_sql=0{
  71.   $sql db_get($q$fatal_sql);
  72.   //preDump($sql);
  73.   return ($sql != STATUS_ERR mysql_fetch_array($sqlfalse);
  74. }
  75.  
  76. /**
  77. * return the last insert ID from the database
  78. @return integer id (row number) of previous insert operation
  79. */
  80. function db_new_id({
  81.   return mysql_insert_id();
  82. }
  83.  
  84. /**
  85. * return the next row from a query
  86. *
  87. @param resource db query handle
  88. @return array next row from query
  89. */
  90. function db_fetch_array($sql{
  91.   if (is_resource($sql)) return null;
  92.   return mysql_fetch_array($sql);
  93. }
  94.  
  95.  
  96. /**
  97. * get the number of rows returned by a query
  98. *
  99. @param resource db query handle
  100. @return integer number of rows
  101. */
  102. function db_num_rows($sql{
  103.   return mysql_num_rows($sql);
  104. }
  105.  
  106. /**
  107. * echo the SQL query to the browser
  108. *
  109. @param string $echo       the sql query
  110. @param boolean $success   query was successful
  111. */
  112. function echoSQL($echo$success=0{
  113.   $conf ConfigReader::getInstance();
  114.   if ($conf->VerboseSQL{
  115.     echo "<div class='sql'>".xssqw($echofalse)
  116.         .($success '<div>'.T_('(successful)').'</div>' '')
  117.         ."</div>";
  118.   }
  119. }
  120.  
  121.  
  122. /**
  123. * echo the SQL query to the browser
  124. *
  125. @param string $echo       the sql query
  126. @param boolean $fatal     die on error
  127. */
  128. function echoSQLerror($echo$fatal=0{
  129.   $conf ConfigReader::getInstance();
  130.   if ($echo != '' && $echo{
  131.     if ($conf->VerboseSQL{
  132.       echo "<div class='sql error'>"xssqw($echo."</div>";
  133.     }
  134.    if ($fatal{
  135.       echo "<div class='sql error'>"
  136.         .sprintf(T_("Ooops. Something went very wrong. Please send the following log information to <a href='mailto:%s'>your Bumblebee Administrator</a> along with a description of what you were doing and ask them to pass it on to the Bumblebee developers. Thanks!")$conf->AdminEmail)
  137.         .'</div>';
  138.       if ($conf->VerboseSQL{
  139.         preDump(debug_backtrace());
  140.       else {
  141.         logmsg(1"SQL ERROR=[$echo]");
  142.         logmsg(1serialize(debug_backtrace()));
  143.       }
  144.       die('<b>'.T_('Fatal SQL error. Aborting.').'</b>');
  145.     }
  146.   }
  147.   return STATUS_ERR;
  148. }
  149.  
  150. /**
  151. * construct and perform a simple SQL select
  152. *
  153. @param string  $table  name of the table (will have TABLEPREFIX added to it
  154. @param mixed   $key    single column name or list of columns for the WHERE clause
  155. @param mixed   $value  single value or list of values for WHERE $key=$value
  156. @param boolean $fatal     die on error
  157. @param boolean $countonly   run a COUNT(*) query not a SELECT query
  158. @return mixed   array if successful, false if error
  159. @global string prefix for tabl nname
  160. */
  161. function quickSQLSelect($table$key$value$fatal=1$countonly=0{
  162.   global $TABLEPREFIX;
  163.   if (is_array($key)) {
  164.     if ($key != ''{
  165.       $key array($key);
  166.     else {
  167.       $key array();
  168.     }
  169.   }
  170.   if (is_array($value)) {
  171.     if ($value != ''{
  172.       $value array($value);
  173.     else {
  174.       $value array();
  175.     }
  176.   }
  177.   $where array();
  178.   foreach ($key as $k => $col{
  179.     $where[$col.'='.qw($value[$k]);
  180.   }
  181.   $q 'SELECT '.($countonly 'count(*)' '*')
  182.       .' FROM '.$TABLEPREFIX.$table
  183.       .(count($where' WHERE '.join($where,' AND ''')
  184.       .' LIMIT 1';         // we only ever return one row from this func, so LIMIT the query.
  185.   return db_get_single($q$fatal);
  186. }
  187.  
  188. /**
  189. * returns the current version of the database that is being talked to
  190. @return string database version
  191. */
  192. function db_get_version({
  193.   $conf ConfigReader::getInstance();
  194.   if ($conf->status->databasereturn "unavailable";
  195.   return mysql_get_server_info();
  196. }
  197.  
  198. /**
  199. * returns the name of the database software that is being talked to
  200. @return string database server software name
  201. */
  202. function db_get_name({
  203.   return 'MySQL';
  204. }
  205.  
  206. ?>

Documentation generated on Tue, 06 Mar 2007 10:02:00 +0000 by phpDocumentor 1.3.0