/DatabaseSchema/src/schema_diff.php

https://github.com/F5/zetacomponents · PHP · 189 lines · 66 code · 10 blank · 113 comment · 5 complexity · 04284d21f2ccb365d2e2e86bfbb13cb7 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package DatabaseSchema
  25. */
  26. /**
  27. * ezcDbSchemaDiff is the main class for schema differences operations.
  28. *
  29. * @package DatabaseSchema
  30. * @version //autogentag//
  31. * @mainclass
  32. */
  33. class ezcDbSchemaDiff
  34. {
  35. /**
  36. * All added tables
  37. *
  38. * @var array(string=>ezcDbSchemaTable)
  39. */
  40. public $newTables;
  41. /**
  42. * All changed tables
  43. *
  44. * @var array(string=>ezcDbSchemaTableDiff)
  45. */
  46. public $changedTables;
  47. /**
  48. * All removed tables
  49. *
  50. * @var array(string=>bool)
  51. */
  52. public $removedTables;
  53. /**
  54. * Constructs an ezcDbSchemaDiff object.
  55. *
  56. * @param array(string=>ezcDbSchemaTable) $newTables
  57. * @param array(string=>ezcDbSchemaTableDiff) $changedTables
  58. * @param array(string=>bool) $removedTables
  59. */
  60. public function __construct( $newTables = array(), $changedTables = array(), $removedTables = array() )
  61. {
  62. $this->newTables = $newTables;
  63. $this->changedTables = $changedTables;
  64. $this->removedTables = $removedTables;
  65. }
  66. static public function __set_state( array $array )
  67. {
  68. return new ezcDbSchemaDiff(
  69. $array['newTables'], $array['changedTables'], $array['removedTables']
  70. );
  71. }
  72. /**
  73. * Checks whether the object in $obj implements the correct $type of reader handler.
  74. *
  75. * @throws ezcDbSchemaInvalidReaderClassException if the object in $obj is
  76. * not a schema reader of the correct type.
  77. *
  78. * @param ezcDbSchemaReader $obj
  79. * @param int $type
  80. */
  81. static private function checkSchemaDiffReader( $obj, $type )
  82. {
  83. if ( !( ( $obj->getDiffReaderType() & $type ) == $type ) )
  84. {
  85. throw new ezcDbSchemaInvalidReaderClassException( get_class( $obj ), $type );
  86. }
  87. }
  88. /**
  89. * Factory method to create a ezcDbSchemaDiff object from the file $file with the format $format.
  90. *
  91. * @throws ezcDbSchemaInvalidReaderClassException if the handler associated
  92. * with the $format is not a file schema reader.
  93. *
  94. * @param string $format
  95. * @param string $file
  96. * @return ezcDbSchemaDiff
  97. */
  98. static public function createFromFile( $format, $file )
  99. {
  100. $className = ezcDbSchemaHandlerManager::getDiffReaderByFormat( $format );
  101. $reader = new $className();
  102. self::checkSchemaDiffReader( $reader, ezcDbSchema::FILE );
  103. return $reader->loadDiffFromFile( $file );
  104. }
  105. /**
  106. * Checks whether the object in $obj implements the correct $type of writer handler.
  107. *
  108. * @throws ezcDbSchemaInvalidWriterClassException if the object in $obj is
  109. * not a schema writer of the correct type.
  110. *
  111. * @param ezcDbSchemaWriter $obj
  112. * @param int $type
  113. */
  114. static private function checkSchemaDiffWriter( $obj, $type )
  115. {
  116. if ( !( ( $obj->getDiffWriterType() & $type ) == $type ) )
  117. {
  118. throw new ezcDbSchemaInvalidWriterClassException( get_class( $obj ), $type );
  119. }
  120. }
  121. /**
  122. * Writes the schema differences to the file $file in format $format.
  123. *
  124. * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
  125. * with the $format is not a file schema writer.
  126. *
  127. * @param string $format
  128. * @param string $file
  129. */
  130. public function writeToFile( $format, $file )
  131. {
  132. $className = ezcDbSchemaHandlerManager::getDiffWriterByFormat( $format );
  133. $reader = new $className();
  134. self::checkSchemaDiffWriter( $reader, ezcDbSchema::FILE );
  135. $reader->saveDiffToFile( $file, $this );
  136. }
  137. /**
  138. * Upgrades the database $db with the differences.
  139. *
  140. * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
  141. * with the $format is not a database schema writer.
  142. *
  143. * @param ezcDbHandler $db
  144. */
  145. public function applyToDb( ezcDbHandler $db )
  146. {
  147. $className = ezcDbSchemaHandlerManager::getDiffWriterByFormat( $db->getName() );
  148. $writer = new $className();
  149. self::checkSchemaDiffWriter( $writer, ezcDbSchema::DATABASE );
  150. $writer->applyDiffToDb( $db, $this );
  151. }
  152. /**
  153. * Returns the $db specific SQL queries that would update the database $db
  154. *
  155. * The database type can be given as both a database handler (instanceof
  156. * ezcDbHandler) or the name of the database as string as retrieved through
  157. * calling getName() on the database handler object.
  158. *
  159. * @see ezcDbHandler::getName()
  160. *
  161. * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
  162. * with the $format is not a database schema writer.
  163. *
  164. * @param string|ezcDbHandler $db
  165. * @return array(string)
  166. */
  167. public function convertToDDL( $db )
  168. {
  169. if ( $db instanceof ezcDbHandler )
  170. {
  171. $db = $db->getName();
  172. }
  173. $className = ezcDbSchemaHandlerManager::getDiffWriterByFormat( $db );
  174. $writer = new $className();
  175. self::checkSchemaDiffWriter( $writer, ezcDbSchema::DATABASE );
  176. return $writer->convertDiffToDDL( $this );
  177. }
  178. }
  179. ?>