Schema.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
  21. use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
  22. use Doctrine\DBAL\Schema\Visitor\Visitor;
  23. use Doctrine\DBAL\Platforms\AbstractPlatform;
  24. /**
  25. * Object representation of a database schema.
  26. *
  27. * Different vendors have very inconsistent naming with regard to the concept
  28. * of a "schema". Doctrine understands a schema as the entity that conceptually
  29. * wraps a set of database objects such as tables, sequences, indexes and
  30. * foreign keys that belong to each other into a namespace. A Doctrine Schema
  31. * has nothing to do with the "SCHEMA" defined as in PostgreSQL, it is more
  32. * related to the concept of "DATABASE" that exists in MySQL and PostgreSQL.
  33. *
  34. * Every asset in the doctrine schema has a name. A name consists of either a
  35. * namespace.local name pair or just a local unqualified name.
  36. *
  37. * The abstraction layer that covers a PostgreSQL schema is the namespace of an
  38. * database object (asset). A schema can have a name, which will be used as
  39. * default namespace for the unqualified database objects that are created in
  40. * the schema.
  41. *
  42. * In the case of MySQL where cross-database queries are allowed this leads to
  43. * databases being "misinterpreted" as namespaces. This is intentional, however
  44. * the CREATE/DROP SQL visitors will just filter this queries and do not
  45. * execute them. Only the queries for the currently connected database are
  46. * executed.
  47. *
  48. * @link www.doctrine-project.org
  49. * @since 2.0
  50. * @author Benjamin Eberlei <kontakt@beberlei.de>
  51. */
  52. class Schema extends AbstractAsset
  53. {
  54. /**
  55. * @var \Doctrine\DBAL\Schema\Table[]
  56. */
  57. protected $_tables = array();
  58. /**
  59. * @var \Doctrine\DBAL\Schema\Sequence[]
  60. */
  61. protected $_sequences = array();
  62. /**
  63. * @var \Doctrine\DBAL\Schema\SchemaConfig
  64. */
  65. protected $_schemaConfig = false;
  66. /**
  67. * @param \Doctrine\DBAL\Schema\Table[] $tables
  68. * @param \Doctrine\DBAL\Schema\Sequence[] $sequences
  69. * @param \Doctrine\DBAL\Schema\SchemaConfig $schemaConfig
  70. */
  71. public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
  72. {
  73. if ($schemaConfig == null) {
  74. $schemaConfig = new SchemaConfig();
  75. }
  76. $this->_schemaConfig = $schemaConfig;
  77. $this->_setName($schemaConfig->getName() ?: 'public');
  78. foreach ($tables as $table) {
  79. $this->_addTable($table);
  80. }
  81. foreach ($sequences as $sequence) {
  82. $this->_addSequence($sequence);
  83. }
  84. }
  85. /**
  86. * @return boolean
  87. */
  88. public function hasExplicitForeignKeyIndexes()
  89. {
  90. return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
  91. }
  92. /**
  93. * @param \Doctrine\DBAL\Schema\Table $table
  94. *
  95. * @return void
  96. *
  97. * @throws \Doctrine\DBAL\Schema\SchemaException
  98. */
  99. protected function _addTable(Table $table)
  100. {
  101. $tableName = $table->getFullQualifiedName($this->getName());
  102. if(isset($this->_tables[$tableName])) {
  103. throw SchemaException::tableAlreadyExists($tableName);
  104. }
  105. $this->_tables[$tableName] = $table;
  106. $table->setSchemaConfig($this->_schemaConfig);
  107. }
  108. /**
  109. * @param \Doctrine\DBAL\Schema\Sequence $sequence
  110. *
  111. * @return void
  112. *
  113. * @throws \Doctrine\DBAL\Schema\SchemaException
  114. */
  115. protected function _addSequence(Sequence $sequence)
  116. {
  117. $seqName = $sequence->getFullQualifiedName($this->getName());
  118. if (isset($this->_sequences[$seqName])) {
  119. throw SchemaException::sequenceAlreadyExists($seqName);
  120. }
  121. $this->_sequences[$seqName] = $sequence;
  122. }
  123. /**
  124. * Gets all tables of this schema.
  125. *
  126. * @return \Doctrine\DBAL\Schema\Table[]
  127. */
  128. public function getTables()
  129. {
  130. return $this->_tables;
  131. }
  132. /**
  133. * @param string $tableName
  134. *
  135. * @return \Doctrine\DBAL\Schema\Table
  136. *
  137. * @throws \Doctrine\DBAL\Schema\SchemaException
  138. */
  139. public function getTable($tableName)
  140. {
  141. $tableName = $this->getFullQualifiedAssetName($tableName);
  142. if (!isset($this->_tables[$tableName])) {
  143. throw SchemaException::tableDoesNotExist($tableName);
  144. }
  145. return $this->_tables[$tableName];
  146. }
  147. /**
  148. * @param string $name
  149. *
  150. * @return string
  151. */
  152. private function getFullQualifiedAssetName($name)
  153. {
  154. if ($this->isIdentifierQuoted($name)) {
  155. $name = $this->trimQuotes($name);
  156. }
  157. if (strpos($name, ".") === false) {
  158. $name = $this->getName() . "." . $name;
  159. }
  160. return strtolower($name);
  161. }
  162. /**
  163. * Does this schema have a table with the given name?
  164. *
  165. * @param string $tableName
  166. *
  167. * @return boolean
  168. */
  169. public function hasTable($tableName)
  170. {
  171. $tableName = $this->getFullQualifiedAssetName($tableName);
  172. return isset($this->_tables[$tableName]);
  173. }
  174. /**
  175. * Gets all table names, prefixed with a schema name, even the default one if present.
  176. *
  177. * @return array
  178. */
  179. public function getTableNames()
  180. {
  181. return array_keys($this->_tables);
  182. }
  183. /**
  184. * @param string $sequenceName
  185. *
  186. * @return boolean
  187. */
  188. public function hasSequence($sequenceName)
  189. {
  190. $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
  191. return isset($this->_sequences[$sequenceName]);
  192. }
  193. /**
  194. * @param string $sequenceName
  195. *
  196. * @return \Doctrine\DBAL\Schema\Sequence
  197. *
  198. * @throws \Doctrine\DBAL\Schema\SchemaException
  199. */
  200. public function getSequence($sequenceName)
  201. {
  202. $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
  203. if(!$this->hasSequence($sequenceName)) {
  204. throw SchemaException::sequenceDoesNotExist($sequenceName);
  205. }
  206. return $this->_sequences[$sequenceName];
  207. }
  208. /**
  209. * @return \Doctrine\DBAL\Schema\Sequence[]
  210. */
  211. public function getSequences()
  212. {
  213. return $this->_sequences;
  214. }
  215. /**
  216. * Creates a new table.
  217. *
  218. * @param string $tableName
  219. *
  220. * @return \Doctrine\DBAL\Schema\Table
  221. */
  222. public function createTable($tableName)
  223. {
  224. $table = new Table($tableName);
  225. $this->_addTable($table);
  226. foreach ($this->_schemaConfig->getDefaultTableOptions() as $name => $value) {
  227. $table->addOption($name, $value);
  228. }
  229. return $table;
  230. }
  231. /**
  232. * Renames a table.
  233. *
  234. * @param string $oldTableName
  235. * @param string $newTableName
  236. *
  237. * @return \Doctrine\DBAL\Schema\Schema
  238. */
  239. public function renameTable($oldTableName, $newTableName)
  240. {
  241. $table = $this->getTable($oldTableName);
  242. $table->_setName($newTableName);
  243. $this->dropTable($oldTableName);
  244. $this->_addTable($table);
  245. return $this;
  246. }
  247. /**
  248. * Drops a table from the schema.
  249. *
  250. * @param string $tableName
  251. *
  252. * @return \Doctrine\DBAL\Schema\Schema
  253. */
  254. public function dropTable($tableName)
  255. {
  256. $tableName = $this->getFullQualifiedAssetName($tableName);
  257. $this->getTable($tableName);
  258. unset($this->_tables[$tableName]);
  259. return $this;
  260. }
  261. /**
  262. * Creates a new sequence.
  263. *
  264. * @param string $sequenceName
  265. * @param integer $allocationSize
  266. * @param integer $initialValue
  267. *
  268. * @return \Doctrine\DBAL\Schema\Sequence
  269. */
  270. public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
  271. {
  272. $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
  273. $this->_addSequence($seq);
  274. return $seq;
  275. }
  276. /**
  277. * @param string $sequenceName
  278. *
  279. * @return \Doctrine\DBAL\Schema\Schema
  280. */
  281. public function dropSequence($sequenceName)
  282. {
  283. $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
  284. unset($this->_sequences[$sequenceName]);
  285. return $this;
  286. }
  287. /**
  288. * Returns an array of necessary SQL queries to create the schema on the given platform.
  289. *
  290. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  291. *
  292. * @return array
  293. */
  294. public function toSql(AbstractPlatform $platform)
  295. {
  296. $sqlCollector = new CreateSchemaSqlCollector($platform);
  297. $this->visit($sqlCollector);
  298. return $sqlCollector->getQueries();
  299. }
  300. /**
  301. * Return an array of necessary SQL queries to drop the schema on the given platform.
  302. *
  303. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  304. *
  305. * @return array
  306. */
  307. public function toDropSql(AbstractPlatform $platform)
  308. {
  309. $dropSqlCollector = new DropSchemaSqlCollector($platform);
  310. $this->visit($dropSqlCollector);
  311. return $dropSqlCollector->getQueries();
  312. }
  313. /**
  314. * @param \Doctrine\DBAL\Schema\Schema $toSchema
  315. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  316. *
  317. * @return array
  318. */
  319. public function getMigrateToSql(Schema $toSchema, AbstractPlatform $platform)
  320. {
  321. $comparator = new Comparator();
  322. $schemaDiff = $comparator->compare($this, $toSchema);
  323. return $schemaDiff->toSql($platform);
  324. }
  325. /**
  326. * @param \Doctrine\DBAL\Schema\Schema $fromSchema
  327. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  328. *
  329. * @return array
  330. */
  331. public function getMigrateFromSql(Schema $fromSchema, AbstractPlatform $platform)
  332. {
  333. $comparator = new Comparator();
  334. $schemaDiff = $comparator->compare($fromSchema, $this);
  335. return $schemaDiff->toSql($platform);
  336. }
  337. /**
  338. * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
  339. *
  340. * @return void
  341. */
  342. public function visit(Visitor $visitor)
  343. {
  344. $visitor->acceptSchema($this);
  345. foreach ($this->_tables as $table) {
  346. $table->visit($visitor);
  347. }
  348. foreach ($this->_sequences as $sequence) {
  349. $sequence->visit($visitor);
  350. }
  351. }
  352. /**
  353. * Cloning a Schema triggers a deep clone of all related assets.
  354. *
  355. * @return void
  356. */
  357. public function __clone()
  358. {
  359. foreach ($this->_tables as $k => $table) {
  360. $this->_tables[$k] = clone $table;
  361. }
  362. foreach ($this->_sequences as $k => $sequence) {
  363. $this->_sequences[$k] = clone $sequence;
  364. }
  365. }
  366. }