[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.db_api.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.db_api.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  $TOTAL_QUERIES = 0;
  32  
  33  require_once("DB.php");
  34  require_once (APP_INC_PATH . "class.error_handler.php");
  35  
  36  /**
  37   * Class to manage all tasks related to the DB abstraction module. This is only
  38   * useful to mantain a data dictionary of the current database schema tables.
  39   *
  40   * @version 1.0
  41   * @author João Prado Maia <jpm@mysql.com>
  42   */
  43  
  44  class DB_API
  45  {
  46      var $dbh;
  47  
  48      /**
  49       * Connects to the database and creates a data dictionary array to be used
  50       * on database related schema dynamic lookups.
  51       *
  52       * @access public
  53       */
  54      function DB_API()
  55      {
  56          $dsn = array(
  57              'phptype'  => APP_SQL_DBTYPE,
  58              'hostspec' => APP_SQL_DBHOST,
  59              'database' => APP_SQL_DBNAME,
  60              'username' => APP_SQL_DBUSER,
  61              'password' => APP_SQL_DBPASS
  62          );
  63          // if we are using some non-standard mysql port, pass that value in the dsn
  64          if ((defined('APP_SQL_DBPORT')) && (APP_SQL_DBPORT != 3306)) {
  65              $dsn['port'] = APP_SQL_DBPORT;
  66          }
  67          $this->dbh = DB::connect($dsn);
  68          if (PEAR::isError($this->dbh)) {
  69              Error_Handler::logError(array($this->dbh->getMessage(), $this->dbh->getDebugInfo()), __FILE__, __LINE__);
  70              $error_type = "db";
  71              require_once (APP_PATH . "offline.php");
  72              exit;
  73          }
  74      }
  75  
  76  
  77      /**
  78       * Method used to get the last inserted ID. This is a simple
  79       * wrapper to the mysql_insert_id function, as a work around to
  80       * the somewhat annoying implementation of PEAR::DB to create
  81       * separate tables to host the ID sequences.
  82       *
  83       * @access  public
  84       * @return  integer The last inserted ID
  85       */
  86      function get_last_insert_id()
  87      {
  88          return @mysql_insert_id($this->dbh->connection);
  89      }
  90  
  91  
  92      /**
  93       * Returns the escaped version of the given string.
  94       *
  95       * @access  public
  96       * @param   string $str The string that needs to be escaped
  97       * @return  string The escaped string
  98       */
  99      function escapeString($str)
 100      {
 101          static $real_escape_string;
 102  
 103          if (!isset($real_escape_string)) {
 104              $real_escape_string = function_exists('mysql_real_escape_string');
 105          }
 106  
 107          if ($real_escape_string) {
 108              return mysql_real_escape_string($str, $this->dbh->connection);
 109          } else {
 110              return mysql_escape_string($str);
 111          }
 112      }
 113  
 114  
 115      /**
 116       * Returns the SQL used to calculate the difference of 2 dates, not counting weekends.
 117       * This thing is truly a work of art, the type of art that throws lemon juice in your eye and then laughs.
 118       * If $end_date_field is null, the current date is used instead.
 119       *
 120       * @access  public
 121       * @param   string $start_date_field The name of the field the first date is.
 122       * @param   string $end_date_field The name of the field where the second date is.
 123       * @return  string The SQL used to compare the 2 dates.
 124       */
 125      function getNoWeekendDateDiffSQL($start_date_field, $end_date_field = false)
 126      {
 127          if ($end_date_field == false) {
 128              $end_date_field = "'" . Date_API::getCurrentDateGMT() . "'";
 129          }
 130  
 131          // this is crazy, but it does work. Anyone with a better solution email bryan@mysql.com
 132          $sql = "((UNIX_TIMESTAMP($end_date_field) - UNIX_TIMESTAMP($start_date_field)) - (CASE
 133              WHEN DAYOFWEEK($start_date_field) = 1 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field))-1)/7) * 86400 * 2)
 134              WHEN DAYOFWEEK($start_date_field) = 2 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field)))/7) * 86400 *2)
 135              WHEN DAYOFWEEK($start_date_field) = 3 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field))+1)/7) * 86400 *2)
 136              WHEN DAYOFWEEK($start_date_field) = 4 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field))+2)/7) * 86400 *2)
 137              WHEN DAYOFWEEK($start_date_field) = 5 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field))+3)/7) * 86400 *2)
 138              WHEN DAYOFWEEK($start_date_field) = 6 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field))+4)/7) * 86400 *2)
 139              WHEN DAYOFWEEK($start_date_field) = 7 THEN (floor(((TO_DAYS($end_date_field) - TO_DAYS($start_date_field))-2)/7) * 86400 *2)
 140          END) - (CASE
 141              WHEN DAYOFWEEK($start_date_field) = 7 THEN (86400 + (86400 - time_to_sec($start_date_field)))
 142              WHEN DAYOFWEEK($start_date_field) = 1 THEN (86400 - time_to_sec($start_date_field))
 143              ELSE 0
 144          END) - CASE
 145              WHEN DAYOFWEEK($end_date_field) = 7 THEN time_to_sec($end_date_field)
 146              WHEN DAYOFWEEK($end_date_field) = 1 THEN (86400 + time_to_sec($end_date_field))
 147              ELSE 0
 148          END)";
 149          return str_replace("\n", " ", $sql);
 150      }
 151  }
 152  
 153  // benchmarking the included file (aka setup time)
 154  if (APP_BENCHMARK) {
 155      $GLOBALS['bench']->setMarker('Included DB_API Class');
 156  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7