PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Doctrine/dbal/lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureFederationsSynchronizer.php

https://gitlab.com/adrianjose605/SaintW
PHP | 297 lines | 164 code | 42 blank | 91 comment | 11 complexity | d50ea6bcf1d4aa2e8e4569edcc2ab401 MD5 | raw file
  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\Sharding\SQLAzure;
  20. use Doctrine\DBAL\Schema\Schema;
  21. use Doctrine\DBAL\Connection;
  22. use Doctrine\DBAL\Types\Type;
  23. use Doctrine\DBAL\Schema\Synchronizer\AbstractSchemaSynchronizer;
  24. use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
  25. use Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer;
  26. /**
  27. * SQL Azure Schema Synchronizer.
  28. *
  29. * Will iterate over all shards when performing schema operations. This is done
  30. * by partitioning the passed schema into subschemas for the federation and the
  31. * global database and then applying the operations step by step using the
  32. * {@see \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer}.
  33. *
  34. * @author Benjamin Eberlei <kontakt@beberlei.de>
  35. */
  36. class SQLAzureFederationsSynchronizer extends AbstractSchemaSynchronizer
  37. {
  38. const FEDERATION_TABLE_FEDERATED = 'azure.federated';
  39. const FEDERATION_DISTRIBUTION_NAME = 'azure.federatedOnDistributionName';
  40. /**
  41. * @var \Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager
  42. */
  43. private $shardManager;
  44. /**
  45. * @var \Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer
  46. */
  47. private $synchronizer;
  48. /**
  49. * @param \Doctrine\DBAL\Connection $conn
  50. * @param \Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager $shardManager
  51. * @param \Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer|null $sync
  52. */
  53. public function __construct(Connection $conn, SQLAzureShardManager $shardManager, SchemaSynchronizer $sync = null)
  54. {
  55. parent::__construct($conn);
  56. $this->shardManager = $shardManager;
  57. $this->synchronizer = $sync ?: new SingleDatabaseSynchronizer($conn);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getCreateSchema(Schema $createSchema)
  63. {
  64. $sql = array();
  65. list($global, $federation) = $this->partitionSchema($createSchema);
  66. $globalSql = $this->synchronizer->getCreateSchema($global);
  67. if ($globalSql) {
  68. $sql[] = "-- Create Root Federation\n" .
  69. "USE FEDERATION ROOT WITH RESET;";
  70. $sql = array_merge($sql, $globalSql);
  71. }
  72. $federationSql = $this->synchronizer->getCreateSchema($federation);
  73. if ($federationSql) {
  74. $defaultValue = $this->getFederationTypeDefaultValue();
  75. $sql[] = $this->getCreateFederationStatement();
  76. $sql[] = "USE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " = " . $defaultValue . ") WITH RESET, FILTERING = OFF;";
  77. $sql = array_merge($sql, $federationSql);
  78. }
  79. return $sql;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getUpdateSchema(Schema $toSchema, $noDrops = false)
  85. {
  86. return $this->work($toSchema, function($synchronizer, $schema) use ($noDrops) {
  87. return $synchronizer->getUpdateSchema($schema, $noDrops);
  88. });
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getDropSchema(Schema $dropSchema)
  94. {
  95. return $this->work($dropSchema, function($synchronizer, $schema) {
  96. return $synchronizer->getDropSchema($schema);
  97. });
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function createSchema(Schema $createSchema)
  103. {
  104. $this->processSql($this->getCreateSchema($createSchema));
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function updateSchema(Schema $toSchema, $noDrops = false)
  110. {
  111. $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function dropSchema(Schema $dropSchema)
  117. {
  118. $this->processSqlSafely($this->getDropSchema($dropSchema));
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getDropAllSchema()
  124. {
  125. $this->shardManager->selectGlobal();
  126. $globalSql = $this->synchronizer->getDropAllSchema();
  127. if ($globalSql) {
  128. $sql[] = "-- Work on Root Federation\nUSE FEDERATION ROOT WITH RESET;";
  129. $sql = array_merge($sql, $globalSql);
  130. }
  131. $shards = $this->shardManager->getShards();
  132. foreach ($shards as $shard) {
  133. $this->shardManager->selectShard($shard['rangeLow']);
  134. $federationSql = $this->synchronizer->getDropAllSchema();
  135. if ($federationSql) {
  136. $sql[] = "-- Work on Federation ID " . $shard['id'] . "\n" .
  137. "USE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " = " . $shard['rangeLow'].") WITH RESET, FILTERING = OFF;";
  138. $sql = array_merge($sql, $federationSql);
  139. }
  140. }
  141. $sql[] = "USE FEDERATION ROOT WITH RESET;";
  142. $sql[] = "DROP FEDERATION " . $this->shardManager->getFederationName();
  143. return $sql;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function dropAllSchema()
  149. {
  150. $this->processSqlSafely($this->getDropAllSchema());
  151. }
  152. /**
  153. * @param \Doctrine\DBAL\Schema\Schema $schema
  154. *
  155. * @return array
  156. */
  157. private function partitionSchema(Schema $schema)
  158. {
  159. return array(
  160. $this->extractSchemaFederation($schema, false),
  161. $this->extractSchemaFederation($schema, true),
  162. );
  163. }
  164. /**
  165. * @param \Doctrine\DBAL\Schema\Schema $schema
  166. * @param boolean $isFederation
  167. *
  168. * @return \Doctrine\DBAL\Schema\Schema
  169. *
  170. * @throws \RuntimeException
  171. */
  172. private function extractSchemaFederation(Schema $schema, $isFederation)
  173. {
  174. $partitionedSchema = clone $schema;
  175. foreach ($partitionedSchema->getTables() as $table) {
  176. if ($isFederation) {
  177. $table->addOption(self::FEDERATION_DISTRIBUTION_NAME, $this->shardManager->getDistributionKey());
  178. }
  179. if ( $table->hasOption(self::FEDERATION_TABLE_FEDERATED) !== $isFederation) {
  180. $partitionedSchema->dropTable($table->getName());
  181. } else {
  182. foreach ($table->getForeignKeys() as $fk) {
  183. $foreignTable = $schema->getTable($fk->getForeignTableName());
  184. if ($foreignTable->hasOption(self::FEDERATION_TABLE_FEDERATED) !== $isFederation) {
  185. throw new \RuntimeException("Cannot have foreign key between global/federation.");
  186. }
  187. }
  188. }
  189. }
  190. return $partitionedSchema;
  191. }
  192. /**
  193. * Work on the Global/Federation based on currently existing shards and
  194. * perform the given operation on the underlying schema synchronizer given
  195. * the different partitioned schema instances.
  196. *
  197. * @param \Doctrine\DBAL\Schema\Schema $schema
  198. * @param \Closure $operation
  199. *
  200. * @return array
  201. */
  202. private function work(Schema $schema, \Closure $operation)
  203. {
  204. list($global, $federation) = $this->partitionSchema($schema);
  205. $sql = array();
  206. $this->shardManager->selectGlobal();
  207. $globalSql = $operation($this->synchronizer, $global);
  208. if ($globalSql) {
  209. $sql[] = "-- Work on Root Federation\nUSE FEDERATION ROOT WITH RESET;";
  210. $sql = array_merge($sql, $globalSql);
  211. }
  212. $shards = $this->shardManager->getShards();
  213. foreach ($shards as $shard) {
  214. $this->shardManager->selectShard($shard['rangeLow']);
  215. $federationSql = $operation($this->synchronizer, $federation);
  216. if ($federationSql) {
  217. $sql[] = "-- Work on Federation ID " . $shard['id'] . "\n" .
  218. "USE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " = " . $shard['rangeLow'].") WITH RESET, FILTERING = OFF;";
  219. $sql = array_merge($sql, $federationSql);
  220. }
  221. }
  222. return $sql;
  223. }
  224. /**
  225. * @return string
  226. */
  227. private function getFederationTypeDefaultValue()
  228. {
  229. $federationType = Type::getType($this->shardManager->getDistributionType());
  230. switch ($federationType->getName()) {
  231. case Type::GUID:
  232. $defaultValue = '00000000-0000-0000-0000-000000000000';
  233. break;
  234. case Type::INTEGER:
  235. case Type::SMALLINT:
  236. case Type::BIGINT:
  237. $defaultValue = '0';
  238. break;
  239. default:
  240. $defaultValue = '';
  241. break;
  242. }
  243. return $defaultValue;
  244. }
  245. /**
  246. * @return string
  247. */
  248. private function getCreateFederationStatement()
  249. {
  250. $federationType = Type::getType($this->shardManager->getDistributionType());
  251. $federationTypeSql = $federationType->getSqlDeclaration(array(), $this->conn->getDatabasePlatform());
  252. return "--Create Federation\n" .
  253. "CREATE FEDERATION " . $this->shardManager->getFederationName() . " (" . $this->shardManager->getDistributionKey() . " " . $federationTypeSql ." RANGE)";
  254. }
  255. }