Schema.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 LGPL. 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. /**
  24. * Object representation of a database schema
  25. *
  26. * Different vendors have very inconsistent naming with regard to the concept
  27. * of a "schema". Doctrine understands a schema as the entity that conceptually
  28. * wraps a set of database objects such as tables, sequences, indexes and
  29. * foreign keys that belong to each other into a namespace. A Doctrine Schema
  30. * has nothing to do with the "SCHEMA" defined as in PostgreSQL, it is more
  31. * related to the concept of "DATABASE" that exists in MySQL and PostgreSQL.
  32. *
  33. * Every asset in the doctrine schema has a name. A name consists of either a
  34. * namespace.local name pair or just a local unqualified name.
  35. *
  36. * The abstraction layer that covers a PostgreSQL schema is the namespace of an
  37. * database object (asset). A schema can have a name, which will be used as
  38. * default namespace for the unqualified database objects that are created in
  39. * the schema.
  40. *
  41. * In the case of MySQL where cross-database queries are allowed this leads to
  42. * databases being "misinterpreted" as namespaces. This is intentional, however
  43. * the CREATE/DROP SQL visitors will just filter this queries and do not
  44. * execute them. Only the queries for the currently connected database are
  45. * executed.
  46. *
  47. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  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 array
  56. */
  57. protected $_tables = array();
  58. /**
  59. * @var array
  60. */
  61. protected $_sequences = array();
  62. /**
  63. * @var SchemaConfig
  64. */
  65. protected $_schemaConfig = false;
  66. /**
  67. * @param array $tables
  68. * @param array $sequences
  69. * @param array $views
  70. * @param array $triggers
  71. * @param SchemaConfig $schemaConfig
  72. */
  73. public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
  74. {
  75. if ($schemaConfig == null) {
  76. $schemaConfig = new SchemaConfig();
  77. }
  78. $this->_schemaConfig = $schemaConfig;
  79. $this->_setName($schemaConfig->getName() ?: 'public');
  80. foreach ($tables AS $table) {
  81. $this->_addTable($table);
  82. }
  83. foreach ($sequences AS $sequence) {
  84. $this->_addSequence($sequence);
  85. }
  86. }
  87. /**
  88. * @return bool
  89. */
  90. public function hasExplicitForeignKeyIndexes()
  91. {
  92. return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
  93. }
  94. /**
  95. * @param Table $table
  96. */
  97. protected function _addTable(Table $table)
  98. {
  99. $tableName = $table->getFullQualifiedName($this->getName());
  100. if(isset($this->_tables[$tableName])) {
  101. throw SchemaException::tableAlreadyExists($tableName);
  102. }
  103. $this->_tables[$tableName] = $table;
  104. $table->setSchemaConfig($this->_schemaConfig);
  105. }
  106. /**
  107. * @param Sequence $sequence
  108. */
  109. protected function _addSequence(Sequence $sequence)
  110. {
  111. $seqName = $sequence->getFullQualifiedName($this->getName());
  112. if (isset($this->_sequences[$seqName])) {
  113. throw SchemaException::sequenceAlreadyExists($seqName);
  114. }
  115. $this->_sequences[$seqName] = $sequence;
  116. }
  117. /**
  118. * Get all tables of this schema.
  119. *
  120. * @return array
  121. */
  122. public function getTables()
  123. {
  124. return $this->_tables;
  125. }
  126. /**
  127. * @param string $tableName
  128. * @return Table
  129. */
  130. public function getTable($tableName)
  131. {
  132. $tableName = $this->getFullQualifiedAssetName($tableName);
  133. if (!isset($this->_tables[$tableName])) {
  134. throw SchemaException::tableDoesNotExist($tableName);
  135. }
  136. return $this->_tables[$tableName];
  137. }
  138. /**
  139. * @return string
  140. */
  141. private function getFullQualifiedAssetName($name)
  142. {
  143. if ($this->isQuoted($name)) {
  144. $name = $this->trimQuotes($name);
  145. }
  146. if (strpos($name, ".") === false) {
  147. $name = $this->getName() . "." . $name;
  148. }
  149. return strtolower($name);
  150. }
  151. /**
  152. * Does this schema have a table with the given name?
  153. *
  154. * @param string $tableName
  155. * @return Schema
  156. */
  157. public function hasTable($tableName)
  158. {
  159. $tableName = $this->getFullQualifiedAssetName($tableName);
  160. return isset($this->_tables[$tableName]);
  161. }
  162. /**
  163. * Get all table names, prefixed with a schema name, even the default one
  164. * if present.
  165. *
  166. * @return array
  167. */
  168. public function getTableNames()
  169. {
  170. return array_keys($this->_tables);
  171. }
  172. public function hasSequence($sequenceName)
  173. {
  174. $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
  175. return isset($this->_sequences[$sequenceName]);
  176. }
  177. /**
  178. * @throws SchemaException
  179. * @param string $sequenceName
  180. * @return Doctrine\DBAL\Schema\Sequence
  181. */
  182. public function getSequence($sequenceName)
  183. {
  184. $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
  185. if(!$this->hasSequence($sequenceName)) {
  186. throw SchemaException::sequenceDoesNotExist($sequenceName);
  187. }
  188. return $this->_sequences[$sequenceName];
  189. }
  190. /**
  191. * @return Doctrine\DBAL\Schema\Sequence[]
  192. */
  193. public function getSequences()
  194. {
  195. return $this->_sequences;
  196. }
  197. /**
  198. * Create a new table
  199. *
  200. * @param string $tableName
  201. * @return Table
  202. */
  203. public function createTable($tableName)
  204. {
  205. $table = new Table($tableName);
  206. $this->_addTable($table);
  207. return $table;
  208. }
  209. /**
  210. * Rename a table
  211. *
  212. * @param string $oldTableName
  213. * @param string $newTableName
  214. * @return Schema
  215. */
  216. public function renameTable($oldTableName, $newTableName)
  217. {
  218. $table = $this->getTable($oldTableName);
  219. $table->_setName($newTableName);
  220. $this->dropTable($oldTableName);
  221. $this->_addTable($table);
  222. return $this;
  223. }
  224. /**
  225. * Drop a table from the schema.
  226. *
  227. * @param string $tableName
  228. * @return Schema
  229. */
  230. public function dropTable($tableName)
  231. {
  232. $tableName = $this->getFullQualifiedAssetName($tableName);
  233. $table = $this->getTable($tableName);
  234. unset($this->_tables[$tableName]);
  235. return $this;
  236. }
  237. /**
  238. * Create a new sequence
  239. *
  240. * @param string $sequenceName
  241. * @param int $allocationSize
  242. * @param int $initialValue
  243. * @return Sequence
  244. */
  245. public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
  246. {
  247. $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
  248. $this->_addSequence($seq);
  249. return $seq;
  250. }
  251. /**
  252. * @param string $sequenceName
  253. * @return Schema
  254. */
  255. public function dropSequence($sequenceName)
  256. {
  257. $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
  258. unset($this->_sequences[$sequenceName]);
  259. return $this;
  260. }
  261. /**
  262. * Return an array of necessary sql queries to create the schema on the given platform.
  263. *
  264. * @param AbstractPlatform $platform
  265. * @return array
  266. */
  267. public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  268. {
  269. $sqlCollector = new CreateSchemaSqlCollector($platform);
  270. $this->visit($sqlCollector);
  271. return $sqlCollector->getQueries();
  272. }
  273. /**
  274. * Return an array of necessary sql queries to drop the schema on the given platform.
  275. *
  276. * @param AbstractPlatform $platform
  277. * @return array
  278. */
  279. public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  280. {
  281. $dropSqlCollector = new DropSchemaSqlCollector($platform);
  282. $this->visit($dropSqlCollector);
  283. return $dropSqlCollector->getQueries();
  284. }
  285. /**
  286. * @param Schema $toSchema
  287. * @param AbstractPlatform $platform
  288. */
  289. public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  290. {
  291. $comparator = new Comparator();
  292. $schemaDiff = $comparator->compare($this, $toSchema);
  293. return $schemaDiff->toSql($platform);
  294. }
  295. /**
  296. * @param Schema $fromSchema
  297. * @param AbstractPlatform $platform
  298. */
  299. public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
  300. {
  301. $comparator = new Comparator();
  302. $schemaDiff = $comparator->compare($fromSchema, $this);
  303. return $schemaDiff->toSql($platform);
  304. }
  305. /**
  306. * @param Visitor $visitor
  307. */
  308. public function visit(Visitor $visitor)
  309. {
  310. $visitor->acceptSchema($this);
  311. foreach ($this->_tables AS $table) {
  312. $table->visit($visitor);
  313. }
  314. foreach ($this->_sequences AS $sequence) {
  315. $sequence->visit($visitor);
  316. }
  317. }
  318. /**
  319. * Cloning a Schema triggers a deep clone of all related assets.
  320. *
  321. * @return void
  322. */
  323. public function __clone()
  324. {
  325. foreach ($this->_tables AS $k => $table) {
  326. $this->_tables[$k] = clone $table;
  327. }
  328. foreach ($this->_sequences AS $k => $sequence) {
  329. $this->_sequences[$k] = clone $sequence;
  330. }
  331. }
  332. }