i_database.class.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Install database. Provides access to the Database class and allows to add
  4. * hooks for logging, testing, etc during installation.
  5. *
  6. * @license see /license.txt
  7. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  8. */
  9. class iDatabase extends Database
  10. {
  11. private static $is_logging = true;
  12. static function is_logging()
  13. {
  14. return self::$is_logging;
  15. }
  16. static function set_is_logging($value)
  17. {
  18. self::$is_logging = $value;
  19. }
  20. static function select_db($database_name, $connection = null)
  21. {
  22. if (self::is_logging()) {
  23. Log::notice(__FUNCTION__ . ' ' . $database_name, Log::frame(1));
  24. }
  25. return parent::select_db($database_name, $connection);
  26. }
  27. static function query($query, $connection = null, $file = null, $line = null)
  28. {
  29. if (self::is_logging()) {
  30. $query = str_replace("\n", '', $query);
  31. Log::notice(__FUNCTION__ . ' ' . $query, Log::frame(1));
  32. }
  33. $result = parent::query($query, $connection, $file, $line);
  34. if (empty($result)) {
  35. $backtrace = debug_backtrace(); // Retrieving information about the caller statement.
  36. $caller = isset($backtrace[0]) ? $backtrace[0] : array();
  37. $file = $caller['file'];
  38. $line = $caller['line'];
  39. $message = " sql: $query \n file: $file \n line:$line";
  40. Log::error($message);
  41. }
  42. return $result;
  43. }
  44. /**
  45. * Returns true if the table exists in the database, false otherwise.
  46. * @param string $database
  47. * @param string table
  48. * @return boolean
  49. */
  50. static
  51. function table_exists($database, $table)
  52. {
  53. $tables = mysql_list_tables($db);
  54. while (list ($temp) = mysql_fetch_array($tables)) {
  55. if (strtolower($temp) == strtolower($table)) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. }