[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> database_api.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Database API
  19   *
  20   * @package CoreAPI
  21   * @subpackage DatabaseAPI
  22   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  23   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  24   * @link http://www.mantisbt.org
  25   *
  26   * @uses config_api.php
  27   * @uses constant_inc.php
  28   * @uses error_api.php
  29   * @uses logging_api.php
  30   * @uses utility_api.php
  31   * @uses adodb/adodb.inc.php
  32   */
  33  
  34  require_api( 'config_api.php' );
  35  require_api( 'constant_inc.php' );
  36  require_api( 'error_api.php' );
  37  require_api( 'logging_api.php' );
  38  require_api( 'utility_api.php' );
  39  
  40  define( 'ADODB_DIR', config_get( 'library_path' ) . 'adodb' );
  41  require_lib( 'adodb' . DIRECTORY_SEPARATOR . 'adodb.inc.php' );
  42  
  43  /**
  44   * An array in which all executed queries are stored.  This is used for profiling
  45   * @global array $g_queries_array
  46   */
  47  $g_queries_array = array();
  48  
  49  /**
  50   * Stores whether a database connection was succesfully opened.
  51   * @global bool $g_db_connected
  52   */
  53  $g_db_connected = false;
  54  
  55  /**
  56   * Store whether to log queries ( used for show_queries_count/query list)
  57   * @global bool $g_db_log_queries
  58   */
  59  $g_db_log_queries = ( 0 != ( config_get_global( 'log_level' ) & LOG_DATABASE ) );
  60  
  61  /**
  62   * set adodb fetch mode
  63   * @global bool $ADODB_FETCH_MODE
  64   */
  65  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  66  
  67  /**
  68   * Tracks the query parameter count for use with db_aparam().
  69   * @global int $g_db_param_count
  70   */
  71  $g_db_param_count = 0;
  72  
  73  /**
  74   * Open a connection to the database.
  75   * @param string $p_dsn Database connection string ( specified instead of other params)
  76   * @param string $p_hostname Database server hostname
  77   * @param string $p_username database server username
  78   * @param string $p_password database server password
  79   * @param string $p_database_name database name
  80   * @param string $p_db_schema Schema name (only used if database type is DB2)
  81   * @param bool $p_pconnect Use a Persistent connection to database
  82   * @return bool indicating if the connection was successful
  83   */
  84  function db_connect( $p_dsn, $p_hostname = null, $p_username = null, $p_password = null, $p_database_name = null, $p_db_schema = null, $p_pconnect = false ) {
  85      global $g_db_connected, $g_db;
  86      $t_db_type = config_get_global( 'db_type' );
  87  
  88      if( !db_check_database_support( $t_db_type ) ) {
  89          error_parameters( 0, 'PHP Support for database is not enabled' );
  90          trigger_error( ERROR_DB_CONNECT_FAILED, ERROR );
  91      }
  92  
  93      if( empty( $p_dsn ) ) {
  94          $g_db = ADONewConnection( $t_db_type );
  95  
  96          if( $p_pconnect ) {
  97              $t_result = $g_db->PConnect( $p_hostname, $p_username, $p_password, $p_database_name );
  98          } else {
  99              $t_result = $g_db->Connect( $p_hostname, $p_username, $p_password, $p_database_name );
 100          }
 101      } else {
 102          $g_db = ADONewConnection( $p_dsn );
 103          $t_result = $g_db->IsConnected();
 104      }
 105  
 106      if( $t_result ) {
 107          // For MySQL, the charset for the connection needs to be specified.
 108          if( db_is_mysql() ) {
 109              /** @todo Is there a way to translate any charset name to MySQL format? e.g. remote the dashes? */
 110              /** @todo Is this needed for other databases? */
 111              db_query_bound( 'SET NAMES UTF8' );
 112          } else if( db_is_db2() && $p_db_schema !== null && !is_blank( $p_db_schema ) ) {
 113              $t_result2 = db_query_bound( 'set schema ' . $p_db_schema );
 114              if( $t_result2 === false ) {
 115                  db_error();
 116                  trigger_error( ERROR_DB_CONNECT_FAILED, ERROR );
 117                  return false;
 118              }
 119          }
 120      } else {
 121          db_error();
 122          trigger_error( ERROR_DB_CONNECT_FAILED, ERROR );
 123          return false;
 124      }
 125  
 126      $g_db_connected = true;
 127  
 128      return true;
 129  }
 130  
 131  /**
 132   * Returns whether a connection to the database exists
 133   * @global stores database connection state
 134   * @return bool indicating if the a database connection has been made
 135   */
 136  function db_is_connected() {
 137      global $g_db_connected;
 138  
 139      return $g_db_connected;
 140  }
 141  
 142  /**
 143   * Returns whether php supprot for a database is enabled
 144   * @return bool indicating if php current supports the given database type
 145   */
 146  function db_check_database_support( $p_db_type ) {
 147      $t_support = false;
 148      switch( $p_db_type ) {
 149          case 'mysql':
 150              $t_support = function_exists( 'mysql_connect' );
 151              break;
 152          case 'mysqli':
 153              $t_support = function_exists( 'mysqli_connect' );
 154              break;
 155          case 'pgsql':
 156              $t_support = function_exists( 'pg_connect' );
 157              break;
 158          case 'mssql':
 159              $t_support = function_exists( 'mssql_connect' );
 160              break;
 161          case 'oci8':
 162              $t_support = function_exists( 'OCILogon' );
 163              break;
 164          case 'db2':
 165              $t_support = function_exists( 'db2_connect' );
 166              break;
 167          case 'odbc_mssql':
 168              $t_support = function_exists( 'odbc_connect' );
 169              break;
 170          default:
 171              $t_support = false;
 172      }
 173      return $t_support;
 174  }
 175  
 176  /**
 177   * Checks if the database driver is MySQL
 178   * @return bool true if mysql
 179   */
 180  function db_is_mysql() {
 181      $t_db_type = config_get_global( 'db_type' );
 182  
 183      switch( $t_db_type ) {
 184          case 'mysql':
 185          case 'mysqli':
 186              return true;
 187      }
 188  
 189      return false;
 190  }
 191  
 192  /**
 193   * Checks if the database driver is PostgreSQL
 194   * @return bool true if postgres
 195   */
 196  function db_is_pgsql() {
 197      $t_db_type = config_get_global( 'db_type' );
 198  
 199      switch( $t_db_type ) {
 200          case 'postgres':
 201          case 'postgres7':
 202          case 'pgsql':
 203              return true;
 204      }
 205  
 206      return false;
 207  }
 208  
 209  /**
 210   * Checks if the database driver is MS SQL
 211   * @return bool true if postgres
 212   */
 213  function db_is_mssql() {
 214      $t_db_type = config_get_global( 'db_type' );
 215  
 216      switch( $t_db_type ) {
 217          case 'mssql':
 218          case 'odbc_mssql':
 219              return true;
 220      }
 221  
 222      return false;
 223  }
 224  
 225  /**
 226   * Checks if the database driver is DB2
 227   * @return bool true if db2
 228   */
 229  function db_is_db2() {
 230      $t_db_type = config_get_global( 'db_type' );
 231  
 232      switch( $t_db_type ) {
 233          case 'db2':
 234              return true;
 235      }
 236  
 237      return false;
 238  }
 239  
 240  /**
 241   * execute query, requires connection to be opened
 242   * An error will be triggered if there is a problem executing the query.
 243   * @global array of previous executed queries for profiling
 244   * @global adodb database connection object
 245   * @global boolean indicating whether queries array is populated
 246   * @param string $p_query Query string to execute
 247   * @param int $p_limit Number of results to return
 248   * @param int $p_offset offset query results for paging
 249   * @return ADORecordSet|bool adodb result set or false if the query failed.
 250   * @deprecated db_query_bound should be used in preference to this function. This function will likely be removed in 1.2.0 final
 251   */
 252  function db_query( $p_query, $p_limit = -1, $p_offset = -1 ) {
 253      global $g_queries_array, $g_db, $g_db_log_queries;
 254  
 255      $t_start = microtime(true);
 256  
 257      if(( $p_limit != -1 ) || ( $p_offset != -1 ) ) {
 258          $t_result = $g_db->SelectLimit( $p_query, $p_limit, $p_offset );
 259      } else {
 260          $t_result = $g_db->Execute( $p_query );
 261      }
 262  
 263      $t_elapsed = number_format( microtime(true) - $t_start, 4 );
 264  
 265      if( ON == $g_db_log_queries ) {
 266          log_event( LOG_DATABASE, array( $p_query, $t_elapsed), debug_backtrace() );
 267          array_push( $g_queries_array, array( $p_query, $t_elapsed ) );
 268      } else {
 269          array_push( $g_queries_array, array( '', $t_elapsed ) );
 270      }
 271  
 272      if( !$t_result ) {
 273          db_error( $p_query );
 274          trigger_error( ERROR_DB_QUERY_FAILED, ERROR );
 275          return false;
 276      } else {
 277          return $t_result;
 278      }
 279  }
 280  
 281  /**
 282   * execute query, requires connection to be opened
 283   * An error will be triggered if there is a problem executing the query.
 284   * @global array of previous executed queries for profiling
 285   * @global adodb database connection object
 286   * @global boolean indicating whether queries array is populated
 287   * @param string $p_query Parameterlised Query string to execute
 288   * @param array $arr_parms Array of parameters matching $p_query
 289   * @param int $p_limit Number of results to return
 290   * @param int $p_offset offset query results for paging
 291   * @return ADORecordSet|bool adodb result set or false if the query failed.
 292   */
 293  function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset = -1 ) {
 294      global $g_queries_array, $g_db, $g_db_log_queries, $g_db_param_count;
 295  
 296      static $s_check_params;
 297      if( $s_check_params === null ) {
 298          $s_check_params = ( db_is_pgsql() || config_get_global( 'db_type' ) == 'odbc_mssql' );
 299      }
 300  
 301      $t_start = microtime(true);
 302  
 303      if( $arr_parms != null && $s_check_params ) {
 304          $params = count( $arr_parms );
 305          for( $i = 0;$i < $params;$i++ ) {
 306              if( $arr_parms[$i] === false ) {
 307                  $arr_parms[$i] = 0;
 308              }
 309          }
 310      }
 311  
 312      if(( $p_limit != -1 ) || ( $p_offset != -1 ) ) {
 313          $t_result = $g_db->SelectLimit( $p_query, $p_limit, $p_offset, $arr_parms );
 314      } else {
 315          $t_result = $g_db->Execute( $p_query, $arr_parms );
 316      }
 317  
 318      $t_elapsed = number_format( microtime(true) - $t_start, 4 );
 319  
 320      if( ON == $g_db_log_queries ) {
 321          $t_db_type = config_get_global( 'db_type' );
 322          $lastoffset = 0;
 323          $i = 1;
 324          if( !( is_null( $arr_parms ) || empty( $arr_parms ) ) ) {
 325              while( preg_match( '/(\?)/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) {
 326                  if( $i <= count( $arr_parms ) ) {
 327                      if( is_null( $arr_parms[$i - 1] ) ) {
 328                          $replace = 'NULL';
 329                      }
 330                      else if( is_string( $arr_parms[$i - 1] ) ) {
 331                          $replace = "'" . $arr_parms[$i - 1] . "'";
 332                      }
 333                      else if( is_integer( $arr_parms[$i - 1] ) || is_float( $arr_parms[$i - 1] ) ) {
 334                          $replace = (float) $arr_parms[$i - 1];
 335                      }
 336                      else if( is_bool( $arr_parms[$i - 1] ) ) {
 337                          switch( $t_db_type ) {
 338                              case 'pgsql':
 339                                  $replace = "'" . $arr_parms[$i - 1] . "'";
 340                              break;
 341                          default:
 342                              $replace = $arr_parms[$i - 1];
 343                              break;
 344                          }
 345                      } else {
 346                          echo( "Invalid argument type passed to query_bound(): $i" );
 347                          exit( 1 );
 348                      }
 349                      $p_query = utf8_substr( $p_query, 0, $matches[1][1] ) . $replace . utf8_substr( $p_query, $matches[1][1] + utf8_strlen( $matches[1][0] ) );
 350                      $lastoffset = $matches[1][1] + utf8_strlen( $replace );
 351                  } else {
 352                      $lastoffset = $matches[1][1] + 1;
 353                  }
 354                  $i++;
 355              }
 356          }
 357          log_event( LOG_DATABASE, array( $p_query, $t_elapsed), debug_backtrace() );
 358          array_push( $g_queries_array, array( $p_query, $t_elapsed ) );
 359      } else {
 360          array_push( $g_queries_array, array( '', $t_elapsed ) );
 361      }
 362  
 363      # We can't reset the counter because we have queries being built
 364      # and executed while building bigger queries in filter_api. -jreese
 365      # $g_db_param_count = 0;
 366  
 367      if( !$t_result ) {
 368          db_error( $p_query );
 369          trigger_error( ERROR_DB_QUERY_FAILED, ERROR );
 370          return false;
 371      } else {
 372          return $t_result;
 373      }
 374  }
 375  
 376  /**
 377   * Generate a string to insert a parameter into a database query string
 378   * @return string 'wildcard' matching a paramater in correct ordered format for the current database.
 379   */
 380  function db_param() {
 381      global $g_db;
 382      global $g_db_param_count;
 383  
 384      return $g_db->Param( $g_db_param_count++ );
 385  }
 386  
 387  /**
 388   * Retrieve number of rows returned for a specific database query
 389   * @param ADORecordSet $p_result Database Query Record Set to retrieve record count for.
 390   * @return int Record Count
 391   */
 392  function db_num_rows( $p_result ) {
 393      global $g_db;
 394  
 395      return $p_result->RecordCount();
 396  }
 397  
 398  /**
 399   * Retrieve number of rows affected by a specific database query
 400   * @param ADORecordSet $p_result Database Query Record Set to retrieve affected rows for.
 401   * @return int Affected Rows
 402   */
 403  function db_affected_rows() {
 404      global $g_db;
 405  
 406      return $g_db->Affected_Rows();
 407  }
 408  
 409  /**
 410   * Retrieve the next row returned from a specific database query
 411   * @param bool|ADORecordSet $p_result Database Query Record Set to retrieve next result for.
 412   * @return array Database result
 413   */
 414  function db_fetch_array( &$p_result ) {
 415      global $g_db, $g_db_type;
 416  
 417      if( $p_result->EOF ) {
 418          return false;
 419      }
 420  
 421      # mysql obeys FETCH_MODE_BOTH, hence ->fields works, other drivers do not support this
 422      if( $g_db_type == 'mysql' || $g_db_type == 'odbc_mssql' ) {
 423          $t_array = $p_result->fields;
 424          $p_result->MoveNext();
 425          return $t_array;
 426      } else {
 427          $t_row = $p_result->GetRowAssoc( false );
 428          static $t_array_result;
 429          static $t_array_fields;
 430  
 431          if ($t_array_result != $p_result) {
 432              // new query
 433              $t_array_result = $p_result;
 434              $t_array_fields = null;
 435          } else {
 436              if ( $t_array_fields === null ) {
 437                  $p_result->MoveNext();
 438                  return $t_row;
 439              }
 440          }
 441  
 442          $t_convert = false;
 443          $t_fieldcount = $p_result->FieldCount();
 444          for( $i = 0; $i < $t_fieldcount; $i++ ) {
 445              if (isset( $t_array_fields[$i] ) ) {
 446                  $t_field = $t_array_fields[$i];
 447              } else {
 448                  $t_field = $p_result->FetchField( $i );
 449                  $t_array_fields[$i] = $t_field;
 450              }
 451              switch( $t_field->type ) {
 452                  case 'bool':
 453                      switch( $t_row[$t_field->name] ) {
 454                          case 'f':
 455                              $t_row[$t_field->name] = false;
 456                              break;
 457                          case 't':
 458                              $t_row[$t_field->name] = true;
 459                              break;
 460                      }
 461                      $t_convert= true;
 462                      break;
 463                  default :
 464                      break;
 465              }
 466          }
 467  
 468          if ( $t_convert == false ) {
 469              $t_array_fields = null;
 470          }
 471          $p_result->MoveNext();
 472          return $t_row;
 473      }
 474  }
 475  
 476  /**
 477   * Retrieve a result returned from a specific database query
 478   * @param bool|ADORecordSet $p_result Database Query Record Set to retrieve next result for.
 479   * @param int $p_index1 Row to retrieve (optional)
 480   * @param int $p_index2 Column to retrieve (optional)
 481   * @return mixed Database result
 482   */
 483  function db_result( $p_result, $p_index1 = 0, $p_index2 = 0 ) {
 484      global $g_db;
 485  
 486      if( $p_result && ( db_num_rows( $p_result ) > 0 ) ) {
 487          $p_result->Move( $p_index1 );
 488          $t_result = $p_result->GetArray();
 489  
 490          if( isset( $t_result[0][$p_index2] ) ) {
 491              return $t_result[0][$p_index2];
 492          }
 493  
 494          // The numeric index doesn't exist. FETCH_MODE_ASSOC may have been used.
 495          // Get 2nd dimension and make it numerically indexed
 496          $t_result = array_values( $t_result[0] );
 497          return $t_result[$p_index2];
 498      }
 499  
 500      return false;
 501  }
 502  
 503  /**
 504   * return the last inserted id for a specific database table
 505   * @param string $p_table a valid database table name
 506   * @return int last successful insert id
 507   */
 508  function db_insert_id( $p_table = null, $p_field = "id" ) {
 509      global $g_db;
 510  
 511      if( isset( $p_table ) && db_is_pgsql() ) {
 512          $query = "SELECT currval('" . $p_table . "_" . $p_field . "_seq')";
 513          $result = db_query_bound( $query );
 514          return db_result( $result );
 515      }
 516      return $g_db->Insert_ID();
 517  }
 518  
 519  /**
 520   * Check if the specified table exists.
 521   * @param string $p_table_name a valid database table name
 522   * @return bool indicating whether the table exists
 523   */
 524  function db_table_exists( $p_table_name ) {
 525      if( is_blank( $p_table_name ) ) {
 526          return false;
 527      }
 528  
 529      $t_tables = db_get_table_list();
 530  
 531      # Can't use in_array() since it is case sensitive
 532      $t_table_name = utf8_strtolower( $p_table_name );
 533      foreach( $t_tables as $t_current_table ) {
 534          if( utf8_strtolower( $t_current_table ) == $t_table_name ) {
 535              return true;
 536          }
 537      }
 538  
 539      return false;
 540  }
 541  
 542  /**
 543   * Check if the specified table index exists.
 544   * @param string $p_table_name a valid database table name
 545   * @param string $p_index_name a valid database index name
 546   * @return bool indicating whether the index exists
 547   */
 548  function db_index_exists( $p_table_name, $p_index_name ) {
 549      global $g_db, $g_db_schema;
 550  
 551      if( is_blank( $p_index_name ) || is_blank( $p_table_name ) ) {
 552          return false;
 553  
 554          // no index found
 555      }
 556  
 557      $t_indexes = $g_db->MetaIndexes( $p_table_name );
 558  
 559      # Can't use in_array() since it is case sensitive
 560      $t_index_name = utf8_strtolower( $p_index_name );
 561      foreach( $t_indexes as $t_current_index_name => $t_current_index_obj ) {
 562          if( utf8_strtolower( $t_current_index_name ) == $t_index_name ) {
 563              return true;
 564          }
 565      }
 566      return false;
 567  }
 568  
 569  /**
 570   * Check if the specified field exists in a given table
 571   * @param string $p_field_name a database field name
 572   * @param string $p_table_name a valid database table name
 573   * @return bool indicating whether the field exists
 574   */
 575  function db_field_exists( $p_field_name, $p_table_name ) {
 576      global $g_db;
 577      $columns = db_field_names( $p_table_name );
 578      return in_array( $p_field_name, $columns );
 579  }
 580  
 581  /**
 582   * Retrieve list of fields for a given table
 583   * @param string $p_table_name a valid database table name
 584   * @return array array of fields on table
 585   */
 586  function db_field_names( $p_table_name ) {
 587      global $g_db;
 588      $columns = $g_db->MetaColumnNames( $p_table_name );
 589      return is_array( $columns ) ? $columns : array();
 590  }
 591  
 592  /**
 593   * Returns the last error number. The error number is reset after every call to Execute(). If 0 is returned, no error occurred.
 594   * @return int last error number
 595   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 596   */
 597  function db_error_num() {
 598      global $g_db;
 599  
 600      return $g_db->ErrorNo();
 601  }
 602  
 603  /**
 604   * Returns the last status or error message. Returns the last status or error message. The error message is reset when Execute() is called.
 605   * This can return a string even if no error occurs. In general you do not need to call this function unless an ADOdb function returns false on an error.
 606   * @return string last error string
 607   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 608   */
 609  function db_error_msg() {
 610      global $g_db;
 611  
 612      return $g_db->ErrorMsg();
 613  }
 614  
 615  /**
 616   * send both the error number and error message and query (optional) as paramaters for a triggered error
 617   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 618   */
 619  function db_error( $p_query = null ) {
 620      if( null !== $p_query ) {
 621          error_parameters( db_error_num(), db_error_msg(), $p_query );
 622      } else {
 623          error_parameters( db_error_num(), db_error_msg() );
 624      }
 625  }
 626  
 627  /**
 628   * close the connection.
 629   * Not really necessary most of the time since a connection is automatically closed when a page finishes loading.
 630   */
 631  function db_close() {
 632      global $g_db;
 633  
 634      $t_result = $g_db->Close();
 635  }
 636  
 637  /**
 638   * prepare a string before DB insertion
 639   * @param string $p_string unprepared string
 640   * @return string prepared database query string
 641   * @deprecated db_query_bound should be used in preference to this function. This function may be removed in 1.2.0 final
 642   */
 643  function db_prepare_string( $p_string ) {
 644      global $g_db;
 645      $t_db_type = config_get_global( 'db_type' );
 646  
 647      switch( $t_db_type ) {
 648          case 'mssql':
 649          case 'odbc_mssql':
 650          case 'ado_mssql':
 651              if( ini_get( 'magic_quotes_sybase' ) ) {
 652                  return addslashes( $p_string );
 653              } else {
 654                  ini_set( 'magic_quotes_sybase', true );
 655                  $t_string = addslashes( $p_string );
 656                  ini_set( 'magic_quotes_sybase', false );
 657                  return $t_string;
 658              }
 659  
 660              # just making a point with the superfluous break;s  I know it does not execute after a return  ;-)
 661              break;
 662          case 'db2':
 663              $t_escaped = $g_db->qstr( $p_string, false );
 664              return utf8_substr( $t_escaped, 1, utf8_strlen( $t_escaped ) - 2 );
 665              break;
 666          case 'mssql':
 667              break;
 668          case 'odbc_mssql':
 669              break;
 670          case 'mysql':
 671              return mysql_real_escape_string( $p_string );
 672          case 'mysqli':
 673              # For some reason mysqli_escape_string( $p_string ) always returns an empty
 674              # string.  This is happening with PHP v5.0.2.
 675              $t_escaped = $g_db->qstr( $p_string, false );
 676              return utf8_substr( $t_escaped, 1, utf8_strlen( $t_escaped ) - 2 );
 677          case 'postgres':
 678          case 'postgres64':
 679          case 'postgres7':
 680          case 'pgsql':
 681              return pg_escape_string( $p_string );
 682          default:
 683              error_parameters( 'db_type', $t_db_type );
 684              trigger_error( ERROR_CONFIG_OPT_INVALID, ERROR );
 685      }
 686  }
 687  
 688  /**
 689   * prepare a binary string before DB insertion
 690   * @param string $p_string unprepared binary data
 691   * @return string prepared database query string
 692   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 693   */
 694  function db_prepare_binary_string( $p_string ) {
 695      global $g_db;
 696      $t_db_type = config_get_global( 'db_type' );
 697  
 698      switch( $t_db_type ) {
 699          case 'mssql':
 700          case 'odbc_mssql':
 701          case 'ado_mssql':
 702              $content = unpack( "H*hex", $p_string );
 703              return '0x' . $content['hex'];
 704              break;
 705          case 'postgres':
 706          case 'postgres64':
 707          case 'postgres7':
 708          case 'pgsql':
 709              return '\'' . pg_escape_bytea( $p_string ) . '\'';
 710              break;
 711          default:
 712              return '\'' . db_prepare_string( $p_string ) . '\'';
 713              break;
 714      }
 715  }
 716  
 717  /**
 718   * prepare a int for database insertion.
 719   * @param int $p_int integer
 720   * @return int integer
 721   * @deprecated db_query_bound should be used in preference to this function. This function may be removed in 1.2.0 final
 722   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 723   */
 724  function db_prepare_int( $p_int ) {
 725      return (int) $p_int;
 726  }
 727  
 728  /**
 729   * prepare a double for database insertion.
 730   * @param double $p_double double
 731   * @return double double
 732   * @deprecated db_query_bound should be used in preference to this function. This function may be removed in 1.2.0 final
 733   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 734   */
 735  function db_prepare_double( $p_double ) {
 736      return (double) $p_double;
 737  }
 738  
 739  /**
 740   * prepare a boolean for database insertion.
 741   * @param boolean $p_boolean boolean
 742   * @return int integer representing boolean
 743   * @deprecated db_query_bound should be used in preference to this function. This function may be removed in 1.2.0 final
 744   * @todo Use/Behaviour of this function should be reviewed before 1.2.0 final
 745   */
 746  function db_prepare_bool( $p_bool ) {
 747      return (int) (bool) $p_bool;
 748  }
 749  
 750  /**
 751   * return current timestamp for DB
 752   * @todo add param bool $p_gmt whether to use GMT or current timezone (default false)
 753   * @return string Formatted Date for DB insertion e.g. 1970-01-01 00:00:00 ready for database insertion
 754   */
 755  function db_now() {
 756      global $g_db;
 757  
 758      return time();
 759  }
 760  
 761  /**
 762   * convert minutes to a time format [h]h:mm
 763   * @param int $p_min integer representing number of minutes
 764   * @return string representing formatted duration string in hh:mm format.
 765   */
 766  function db_minutes_to_hhmm( $p_min = 0 ) {
 767      return sprintf( '%02d:%02d', $p_min / 60, $p_min % 60 );
 768  }
 769  
 770  /**
 771   * A helper function that generates a case-sensitive or case-insensitive like phrase based on the current db type.
 772   * The field name and value are assumed to be safe to insert in a query (i.e. already cleaned).
 773   * @param string $p_field_name The name of the field to filter on.
 774   * @param bool $p_case_sensitive true: case sensitive, false: case insensitive
 775   * @return string returns (field LIKE 'value') OR (field ILIKE 'value')
 776   */
 777  function db_helper_like( $p_field_name, $p_case_sensitive = false ) {
 778      $t_like_keyword = 'LIKE';
 779  
 780      if( $p_case_sensitive === false ) {
 781          if( db_is_pgsql() ) {
 782              $t_like_keyword = 'ILIKE';
 783          }
 784      }
 785  
 786      return "($p_field_name $t_like_keyword " . db_param() . ')';
 787  }
 788  
 789  /**
 790   * A helper function to compare two dates against a certain number of days
 791   * @param $p_date1_id_or_column
 792   * @param $p_date2_id_or_column
 793   * @param $p_limitstring
 794   * @return string returns database query component to compare dates
 795   * @todo Check if there is a way to do that using ADODB rather than implementing it here.
 796   */
 797  function db_helper_compare_days( $p_date1_id_or_column, $p_date2_id_or_column, $p_limitstring ) {
 798      $t_db_type = config_get_global( 'db_type' );
 799  
 800      $p_date1 = $p_date1_id_or_column;
 801      $p_date2 = $p_date2_id_or_column;
 802      if( is_int( $p_date1_id_or_column ) ) {
 803          $p_date1 = db_param();
 804      }
 805      if( is_int( $p_date2_id_or_column ) ) {
 806          $p_date2 = db_param();
 807      }
 808  
 809      return '((' . $p_date1 . ' - ' . $p_date2 .')' . $p_limitstring . ')';
 810  }
 811  
 812  /**
 813   * count queries
 814   * @return int
 815   */
 816  function db_count_queries() {
 817      global $g_queries_array;
 818  
 819      return count( $g_queries_array );
 820  }
 821  
 822  /**
 823   * count unique queries
 824   * @return int
 825   */
 826  function db_count_unique_queries() {
 827      global $g_queries_array;
 828  
 829      $t_unique_queries = 0;
 830      $t_shown_queries = array();
 831      foreach( $g_queries_array as $t_val_array ) {
 832          if( !in_array( $t_val_array[0], $t_shown_queries ) ) {
 833              $t_unique_queries++;
 834              array_push( $t_shown_queries, $t_val_array[0] );
 835          }
 836      }
 837      return $t_unique_queries;
 838  }
 839  
 840  /**
 841   * get total time for queries
 842   * @return int
 843   */
 844  function db_time_queries() {
 845      global $g_queries_array;
 846      $t_count = count( $g_queries_array );
 847      $t_total = 0;
 848      for( $i = 0;$i < $t_count;$i++ ) {
 849          $t_total += $g_queries_array[$i][1];
 850      }
 851      return $t_total;
 852  }
 853  
 854  /**
 855   * get database table name
 856   * @return string containing full database table name
 857   */
 858  function db_get_table( $p_option ) {
 859      $t_table = $p_option;
 860      $t_prefix = config_get_global( 'db_table_prefix' );
 861      $t_suffix = config_get_global( 'db_table_suffix' );
 862      if ( $t_prefix ) {
 863          $t_table = $t_prefix . '_' . $t_table;
 864      }
 865      if ( $t_suffix ) {
 866          $t_table .= $t_suffix;
 867      }
 868      return $t_table;
 869  }
 870  
 871  /**
 872   * get list database tables
 873   * @return array containing table names
 874   */
 875  function db_get_table_list() {
 876      global $g_db, $g_db_schema;
 877  
 878      if( db_is_db2() ) {
 879          // must pass schema
 880          $t_tables = $g_db->MetaTables( 'TABLE', false, '', $g_db_schema );
 881      } else {
 882          $t_tables = $g_db->MetaTables( 'TABLE' );
 883      }
 884      return $t_tables;
 885  }
 886  


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7