Schema.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
  23. use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
  24. use Doctrine\DBAL\Schema\Visitor\Visitor;
  25. /**
  26. * Object representation of a database schema
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.org
  30. * @since 2.0
  31. * @version $Revision$
  32. * @author Benjamin Eberlei <kontakt@beberlei.de>
  33. */
  34. class Schema extends AbstractAsset
  35. {
  36. /**
  37. * @var array
  38. */
  39. protected $_tables = array();
  40. /**
  41. * @var array
  42. */
  43. protected $_sequences = array();
  44. /**
  45. * @var SchemaConfig
  46. */
  47. protected $_schemaConfig = false;
  48. /**
  49. * @param array $tables
  50. * @param array $sequences
  51. * @param array $views
  52. * @param array $triggers
  53. * @param SchemaConfig $schemaConfig
  54. */
  55. public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
  56. {
  57. if ($schemaConfig == null) {
  58. $schemaConfig = new SchemaConfig();
  59. }
  60. $this->_schemaConfig = $schemaConfig;
  61. foreach ($tables AS $table) {
  62. $this->_addTable($table);
  63. }
  64. foreach ($sequences AS $sequence) {
  65. $this->_addSequence($sequence);
  66. }
  67. }
  68. /**
  69. * @return bool
  70. */
  71. public function hasExplicitForeignKeyIndexes()
  72. {
  73. return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
  74. }
  75. /**
  76. * @param Table $table
  77. */
  78. protected function _addTable(Table $table)
  79. {
  80. $tableName = strtolower($table->getName());
  81. if(isset($this->_tables[$tableName])) {
  82. throw SchemaException::tableAlreadyExists($tableName);
  83. }
  84. $this->_tables[$tableName] = $table;
  85. $table->setSchemaConfig($this->_schemaConfig);
  86. }
  87. /**
  88. * @param Sequence $sequence
  89. */
  90. protected function _addSequence(Sequence $sequence)
  91. {
  92. $seqName = strtolower($sequence->getName());
  93. if (isset($this->_sequences[$seqName])) {
  94. throw SchemaException::sequenceAlreadyExists($seqName);
  95. }
  96. $this->_sequences[$seqName] = $sequence;
  97. }
  98. /**
  99. * Get all tables of this schema.
  100. *
  101. * @return array
  102. */
  103. public function getTables()
  104. {
  105. return $this->_tables;
  106. }
  107. /**
  108. * @param string $tableName
  109. * @return Table
  110. */
  111. public function getTable($tableName)
  112. {
  113. $tableName = strtolower($tableName);
  114. if (!isset($this->_tables[$tableName])) {
  115. throw SchemaException::tableDoesNotExist($tableName);
  116. }
  117. return $this->_tables[$tableName];
  118. }
  119. /**
  120. * Does this schema have a table with the given name?
  121. *
  122. * @param string $tableName
  123. * @return Schema
  124. */
  125. public function hasTable($tableName)
  126. {
  127. $tableName = strtolower($tableName);
  128. return isset($this->_tables[$tableName]);
  129. }
  130. /**
  131. * @param string $sequenceName
  132. * @return bool
  133. */
  134. public function hasSequence($sequenceName)
  135. {
  136. $sequenceName = strtolower($sequenceName);
  137. return isset($this->_sequences[$sequenceName]);
  138. }
  139. /**
  140. * @throws SchemaException
  141. * @param string $sequenceName
  142. * @return Doctrine\DBAL\Schema\Sequence
  143. */
  144. public function getSequence($sequenceName)
  145. {
  146. $sequenceName = strtolower($sequenceName);
  147. if(!$this->hasSequence($sequenceName)) {
  148. throw SchemaException::sequenceDoesNotExist($sequenceName);
  149. }
  150. return $this->_sequences[$sequenceName];
  151. }
  152. /**
  153. * @return Doctrine\DBAL\Schema\Sequence[]
  154. */
  155. public function getSequences()
  156. {
  157. return $this->_sequences;
  158. }
  159. /**
  160. * Create a new table
  161. *
  162. * @param string $tableName
  163. * @return Table
  164. */
  165. public function createTable($tableName)
  166. {
  167. $table = new Table($tableName);
  168. $this->_addTable($table);
  169. return $table;
  170. }
  171. /**
  172. * Rename a table
  173. *
  174. * @param string $oldTableName
  175. * @param string $newTableName
  176. * @return Schema
  177. */
  178. public function renameTable($oldTableName, $newTableName)
  179. {
  180. $table = $this->getTable($oldTableName);
  181. $table->_setName($newTableName);
  182. $this->dropTable($oldTableName);
  183. $this->_addTable($table);
  184. return $this;
  185. }
  186. /**
  187. * Drop a table from the schema.
  188. *
  189. * @param string $tableName
  190. * @return Schema
  191. */
  192. public function dropTable($tableName)
  193. {
  194. $tableName = strtolower($tableName);
  195. $table = $this->getTable($tableName);
  196. unset($this->_tables[$tableName]);
  197. return $this;
  198. }
  199. /**
  200. * Create a new sequence
  201. *
  202. * @param string $sequenceName
  203. * @param int $allocationSize
  204. * @param int $initialValue
  205. * @return Sequence
  206. */
  207. public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
  208. {
  209. $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
  210. $this->_addSequence($seq);
  211. return $seq;
  212. }
  213. /**
  214. * @param string $sequenceName
  215. * @return Schema
  216. */
  217. public function dropSequence($sequenceName)
  218. {
  219. $sequenceName = strtolower($sequenceName);
  220. unset($this->_sequences[$sequenceName]);
  221. return $this;
  222. }
  223. /**
  224. * Return an array of necessary sql queries to create the schema on the given platform.
  225. *
  226. * @param AbstractPlatform $platform
  227. * @return array
  228. */
  229. public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  230. {
  231. $sqlCollector = new CreateSchemaSqlCollector($platform);
  232. $this->visit($sqlCollector);
  233. return $sqlCollector->getQueries();
  234. }
  235. /**
  236. * Return an array of necessary sql queries to drop the schema on the given platform.
  237. *
  238. * @param AbstractPlatform $platform
  239. * @return array
  240. */
  241. public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  242. {
  243. $dropSqlCollector = new DropSchemaSqlCollector($platform);
  244. $this->visit($dropSqlCollector);
  245. return $dropSqlCollector->getQueries();
  246. }
  247. /**
  248. * @param Schema $toSchema
  249. * @param AbstractPlatform $platform
  250. */
  251. public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  252. {
  253. $comparator = new Comparator();
  254. $schemaDiff = $comparator->compare($this, $toSchema);
  255. return $schemaDiff->toSql($platform);
  256. }
  257. /**
  258. * @param Schema $fromSchema
  259. * @param AbstractPlatform $platform
  260. */
  261. public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  262. {
  263. $comparator = new Comparator();
  264. $schemaDiff = $comparator->compare($fromSchema, $this);
  265. return $schemaDiff->toSql($platform);
  266. }
  267. /**
  268. * @param Visitor $visitor
  269. */
  270. public function visit(Visitor $visitor)
  271. {
  272. $visitor->acceptSchema($this);
  273. foreach ($this->_tables AS $table) {
  274. $table->visit($visitor);
  275. }
  276. foreach ($this->_sequences AS $sequence) {
  277. $sequence->visit($visitor);
  278. }
  279. }
  280. /**
  281. * Cloning a Schema triggers a deep clone of all related assets.
  282. *
  283. * @return void
  284. */
  285. public function __clone()
  286. {
  287. foreach ($this->_tables AS $k => $table) {
  288. $this->_tables[$k] = clone $table;
  289. }
  290. foreach ($this->_sequences AS $k => $sequence) {
  291. $this->_sequences[$k] = clone $sequence;
  292. }
  293. }
  294. }