Source for file MySqlUtils.class.php

Documentation is available at MySqlUtils.class.php

  1. <?php
  2. /**
  3. * @since 9/11/07
  4. * @package harmoni.dbc.mysql
  5. *
  6. * @copyright Copyright &copy; 2007, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: MySqlUtils.class.php,v 1.2 2007/09/13 18:14:02 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * This is a static class with some utility functions.
  14. *
  15. * @since 9/11/07
  16. * @package harmoni.dbc.mysql
  17. *
  18. * @copyright Copyright &copy; 2007, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: MySqlUtils.class.php,v 1.2 2007/09/13 18:14:02 adamfranco Exp $
  22. */
  23. class MySqlUtils {
  24. /**
  25. * Make all column names lowercase
  26. *
  27. * @param integer $dbIndex
  28. * @return void
  29. * @access public
  30. * @since 9/11/07
  31. */
  32. public function columnNamesToLowercase ($dbIndex) {
  33. $dbc = Services::getService("DBHandler");
  34. $tables = $dbc->getTableList($dbIndex);
  35. $numChanged = 0;
  36. foreach ($tables as $table) {
  37. $result = $dbc->query(new GenericSQLQuery("DESCRIBE ".$table), $dbIndex);
  38. $result = $result->returnAsSelectQueryResult();
  39. while ($result->hasNext()) {
  40. $field = $result->field("Field");
  41. if ($field != strtolower($field)) {
  42. $query = new GenericSQLQuery("ALTER TABLE ".$table." CHANGE `".$field."` `".strtolower($field)."` ".$result->field("Type"));
  43. $dbc->query($query, $dbIndex);
  44. $numChanged++;
  45. printpre($query->asString());
  46. }
  47. $result->advanceRow();
  48. }
  49. }
  50. print "$numChanged columns were renamed.";
  51. }
  52. /**
  53. * Make the changes to the database required to go from harmoni-0.11.0 to harmoni-0.12.0.
  54. *
  55. * @param integer $dbIndes
  56. * @return void
  57. * @access public
  58. * @since 9/13/07
  59. */
  60. public function harmoni_0_12_0_update ($dbIndex) {
  61. $dbc = Services::getService("DBHandler");
  62. $tables = $dbc->getTableList($dbIndex);
  63. $changeTables = array ('j_node_node', 'node_ancestry');
  64. $changeTableStatus = array('j_node_node' => false, 'node_ancestry' => false);
  65. // Check which tables need to be updated and update if needed.
  66. foreach ($changeTables as $table) {
  67. if (in_array($table, $tables)) {
  68. $result = $dbc->query(new GenericSQLQuery("DESCRIBE ".$table), $dbIndex);
  69. $result = $result->returnAsSelectQueryResult();
  70. while ($result->hasNext()) {
  71. if ($result->field("Field") == 'fk_hierarchy') {
  72. $changeTableStatus[$table] = true;
  73. break;
  74. }
  75. $result->advanceRow();
  76. }
  77. if (!$changeTableStatus[$table]) {
  78. // Alter the table
  79. printpre("Adding column fk_hierarchy to $table");
  80. $dbc->query(new GenericSQLQuery("ALTER TABLE `".$table."` ADD `fk_hierarchy` VARCHAR( 170 ) FIRST ;"), $dbIndex);
  81. }
  82. }
  83. }
  84. // Select the hierarchy ids and update the table
  85. if (in_array(false, $changeTableStatus)) {
  86. // Look up which nodes belong to which hierarchy
  87. $query = new SelectQuery;
  88. $query->addTable("node");
  89. $query->addColumn("node_id");
  90. $query->addColumn("fk_hierarchy");
  91. $query->addOrderBy("fk_hierarchy");
  92. $hierarchies = array();
  93. $result = $dbc->query($query, $dbIndex);
  94. while ($result->hasMoreRows()) {
  95. // Create an array to hold the ids of all nodes in the hierarchy
  96. if (!isset($hierarchies[$result->field('fk_hierarchy')]))
  97. $hierarchies[$result->field('fk_hierarchy')] = array();
  98. $hierarchies[$result->field('fk_hierarchy')][] = $result->field('node_id');
  99. $result->advanceRow();
  100. }
  101. $result->free();
  102. // Update each table's fk_hierarchy
  103. foreach ($hierarchies as $hierarchyId => $nodeIds) {
  104. if (!$changeTableStatus['j_node_node']) {
  105. $query = new UpdateQuery;
  106. $query->addValue('fk_hierarchy', $hierarchyId);
  107. $query->setTable('j_node_node');
  108. $query->addWhere("fk_child IN ('".implode("', '", $nodeIds)."')");
  109. $result = $dbc->query($query, $dbIndex);
  110. printpre("Updated fk_hierarchy on ".$result->getNumberOfRows()." rows in j_node_node.");
  111. }
  112. if (!$changeTableStatus['j_node_node']) {
  113. $query = new UpdateQuery;
  114. $query->addValue('fk_hierarchy', $hierarchyId);
  115. $query->setTable('node_ancestry');
  116. $query->addWhere("fk_node IN ('".implode("', '", $nodeIds)."')");
  117. $result = $dbc->query($query, $dbIndex);
  118. printpre("Updated fk_hierarchy on ".$result->getNumberOfRows()." rows in node_ancestry.");
  119. }
  120. }
  121. // Alter the table to be NOT NULL
  122. foreach ($changeTables as $table) {
  123. if (!$changeTableStatus[$table]) {
  124. // Alter the table
  125. $dbc->query(new GenericSQLQuery("ALTER TABLE `".$table."` CHANGE `fk_hierarchy` `fk_hierarchy` VARCHAR( 170 ) NOT NULL"), $dbIndex);
  126. printpre("Altering column fk_hierarchy in $table to be NOT NULL.");
  127. }
  128. }
  129. }
  130. // Alter the names of the Scheduling columns
  131. if (in_array('sc_item', $tables)) {
  132. try {
  133. $dbc->query(new GenericSQLQuery("ALTER TABLE `sc_item` CHANGE `start` `start_date` BIGINT( 20 ) NULL DEFAULT NULL , CHANGE `end` `end_date` BIGINT( 20 ) NULL DEFAULT NULL "), $dbIndex);
  134. printpre("Renaming columns start and end to start_date and end_date in the sc_item table.");
  135. } catch (QueryDatabaseException $e) {}
  136. }
  137. }
  138. }
  139.  
  140. ?>

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