## Apache Zeta Components Manual :: File Source for schema_diff.php

## Source for file schema_diff.php

Documentation is available at schema_diff.php

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 ;
42. /**
43. * All changed tables
44. *
45. * @var array(string=>ezcDbSchemaTableDiff)
46. */
47. public $changedTables ;
49. /**
50. * All removed tables
51. *
52. * @var array(string=>bool)
53. */
54. public $removedTables ;
56. /**
57. * Constructs an ezcDbSchemaDiff object.
58. *
59. * @param array(string=>ezcDbSchemaTable) $newTables
60. * @param array(string=>ezcDbSchemaTableDiff) $changedTables
61. * @param array(string=>bool) $removedTables
62. */
63. public function __construct ( $newTables = array(), $changedTables = array(), $removedTables = array() )
64. {
65. $this-> newTables = $newTables;
66. $this-> changedTables = $changedTables;
67. $this-> removedTables = $removedTables;
68. }
70. static public function __set_state ( array $array )
71. {
72. return new ezcDbSchemaDiff(
73. $array['newTables'], $array['changedTables'], $array['removedTables']
74. );
75. }
77. /**
78. * Checks whether the object in $obj implements the correct $type of reader handler.
79. *
80. * @throws ezcDbSchemaInvalidReaderClassException if the object in $obj is
81. * not a schema reader of the correct type.
82. *
83. * @param ezcDbSchemaReader $obj
84. * @param int $type
85. */
86. static private function checkSchemaDiffReader( $obj, $type )
87. {
88. if ( !( ( $obj->getDiffReaderType() & $type ) == $type ) )
89. {
90. throw new ezcDbSchemaInvalidReaderClassException ( get_class ( $obj ), $type );
91. }
92. }
94. /**
95. * Factory method to create a ezcDbSchemaDiff object from the file $file with the format $format.
96. *
97. * @throws ezcDbSchemaInvalidReaderClassException if the handler associated
98. * with the $format is not a file schema reader.
99. *
100. * @param string $format
101. * @param string $file
102. * @return ezcDbSchemaDiff
103. */
104. static public function createFromFile ( $format, $file )
105. {
106. $className = ezcDbSchemaHandlerManager :: getDiffReaderByFormat ( $format );
107. $reader = new $className();
108. self::checkSchemaDiffReader( $reader, ezcDbSchema ::FILE );
109. return $reader->loadDiffFromFile( $file );
110. }
112. /**
113. * Checks whether the object in $obj implements the correct $type of writer handler.
114. *
115. * @throws ezcDbSchemaInvalidWriterClassException if the object in $obj is
116. * not a schema writer of the correct type.
117. *
118. * @param ezcDbSchemaWriter $obj
119. * @param int $type
120. */
121. static private function checkSchemaDiffWriter( $obj, $type )
122. {
123. if ( !( ( $obj->getDiffWriterType() & $type ) == $type ) )
124. {
125. throw new ezcDbSchemaInvalidWriterClassException ( get_class ( $obj ), $type );
126. }
127. }
129. /**
130. * Writes the schema differences to the file $file in format $format.
131. *
132. * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
133. * with the $format is not a file schema writer.
134. *
135. * @param string $format
136. * @param string $file
137. */
138. public function writeToFile ( $format, $file )
139. {
140. $className = ezcDbSchemaHandlerManager :: getDiffWriterByFormat ( $format );
141. $reader = new $className();
142. self::checkSchemaDiffWriter( $reader, ezcDbSchema ::FILE );
143. $reader->saveDiffToFile( $file, $this );
144. }
146. /**
147. * Upgrades the database $db with the differences.
148. *
149. * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
150. * with the $format is not a database schema writer.
151. *
152. * @param ezcDbHandler $db
153. */
154. public function applyToDb ( ezcDbHandler $db )
155. {
156. $className = ezcDbSchemaHandlerManager :: getDiffWriterByFormat ( $db->getName() );
157. $writer = new $className();
158. self::checkSchemaDiffWriter( $writer, ezcDbSchema ::DATABASE );
159. $writer->applyDiffToDb( $db, $this );
160. }
162. /**
163. * Returns the $db specific SQL queries that would update the database $db
164. *
165. * The database type can be given as both a database handler (instanceof
166. * ezcDbHandler) or the name of the database as string as retrieved through
167. * calling getName() on the database handler object.
168. *
169. * @see ezcDbHandler::getName()
170. *
171. * @throws ezcDbSchemaInvalidWriterClassException if the handler associated
172. * with the $format is not a database schema writer.
173. *
174. * @param string|ezcDbHandler$db
175. * @return array(string)
176. */
177. public function convertToDDL ( $db )
178. {
179. if ( $db instanceof ezcDbHandler )
180. {
181. $db = $db->getName();
182. }
183. $className = ezcDbSchemaHandlerManager :: getDiffWriterByFormat ( $db );
184. $writer = new $className();
185. self::checkSchemaDiffWriter( $writer, ezcDbSchema ::DATABASE );
186. return $writer->convertDiffToDDL( $this );
187. }
188. }
189. ?>

Documentation generated by phpDocumentor 1.4.3