Source for file SQLUtils.static.php

Documentation is available at SQLUtils.static.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: SQLUtils.static.php,v 1.11 2007/09/17 13:54:31 adamfranco Exp $
  9. */
  10.  
  11. /**
  12. * This is a static class that provides functions for the running of arbitrary
  13. * SQL strings and files.
  14. *
  15. *
  16. * @package harmoni.dbc
  17. *
  18. * @copyright Copyright &copy; 2005, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: SQLUtils.static.php,v 1.11 2007/09/17 13:54:31 adamfranco Exp $
  22. * @static
  23. */
  24.  
  25. class SQLUtils {
  26.  
  27. /**
  28. * Parse SQL textfile to remove comments and line returns.
  29. *
  30. * @param string $file The file to be parsed
  31. * @return string The parsed SQL string.
  32. * @access public
  33. * @since 7/2/04
  34. * @static
  35. */
  36. public static function parseSQLFile ( $file ) {
  37. $queryString = file_get_contents($file);
  38. if ($queryString)
  39. return self::parseSQLString($queryString);
  40. else
  41. throw new DatabaseException("The file, '".$file."' was empty or doesn't exist.");
  42. }
  43. /**
  44. * Parse SQL string to remove comments and line returns.
  45. *
  46. * @param string $queryString The string to be parsed
  47. * @return string The parsed SQL string.
  48. * @access public
  49. * @since 7/2/04
  50. * @static
  51. */
  52. public static function parseSQLString ( $queryString ) {
  53. // Remove the comments
  54. $queryString = ereg_replace("(#|--)[^\n\r]*(\n|\r|\n\r)", "", $queryString);
  55. // Remove the line returns
  56. $queryString = ereg_replace("\n|\r", " ", $queryString);
  57. // Remove multiple spaces
  58. $queryString = ereg_replace("\ +", " ", $queryString);
  59. // Remove backticks included by MySQL since they aren't needed anyway.
  60. $queryString = ereg_replace("`", "", $queryString);
  61. return $queryString;
  62. }
  63. /**
  64. * Break up a SQL string with multiple queries (separated by ';') and run each
  65. * query
  66. *
  67. * @param string $queryString The string of queries.
  68. * @param integer $dbIndex The database index to run the queries on.
  69. * @return void
  70. * @access public
  71. * @since 7/2/04
  72. * @static
  73. */
  74. public static function multiQuery ( $queryString, $dbIndex ) {
  75. // break up the query string.
  76. $queryStrings = explode(";", $queryString);
  77. $dbHandler = Services::getService("DatabaseManager");
  78. // Run each query
  79. foreach ($queryStrings as $string) {
  80. $string = trim($string);
  81. if ($string) {
  82. $query = new GenericSQLQuery();
  83. $query->addSQLQuery($string);
  84. $genericResult =$dbHandler->query($query, $dbIndex);
  85. }
  86. }
  87. }
  88. /**
  89. * Run all of the queries in a text file. Comments must start with '#' and
  90. * queries must be separated by ';'.
  91. *
  92. * @param string $file The input file containing the queries.
  93. * @param integer $dbIndex The index of the database to run the queries on.
  94. * @return void
  95. * @access public
  96. * @since 7/2/04
  97. * @static
  98. */
  99. public static function runSQLfile ($file, $dbIndex) {
  100. $string = self::parseSQLFile($file);
  101. self::multiQuery($string, $dbIndex);
  102. }
  103. /**
  104. * Run all of the files with a given extention in a directory as SQL files.
  105. *
  106. * @param string $dir
  107. * @param integer $dbIndex The index of the database to run the queries on.
  108. * @param optional string $extn The file extention to execute, default: 'sql'.
  109. * @return void
  110. * @access public
  111. * @since 9/11/07
  112. */
  113. public static function runSQLdir ($dir, $dbIndex, $extn = 'sql') {
  114. $sqlFiles = array();
  115. if ($handle = opendir($dir)) {
  116. while (false !== ($file = readdir($handle))) {
  117. if ($file != "." && $file != "..") {
  118. $path = $dir."/".$file;
  119. // Recurse into sub directories
  120. if (is_dir($path))
  121. self::runSQLdir($path, $dbIndex, $extn);
  122. // Run any SQL files
  123. else if (preg_match('/.+\.'.$extn.'$/i', $file))
  124. $sqlFiles[] = $path;
  125. // Ignore any other files.
  126. }
  127. }
  128. closedir($handle);
  129. } else {
  130. throw new Exception ("Could not open SQL directory, '$dir', for reading.");
  131. }
  132. sort ($sqlFiles);
  133. foreach ($sqlFiles as $path)
  134. self::runSQLfile($path, $dbIndex);
  135. }
  136. }
  137. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:40 -0400 by phpDocumentor 1.3.0RC3