PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/Zend/Db/Sql/Ddl/AlterTable.php

https://gitlab.com/jalon/doadoronline
PHP | 268 lines | 159 code | 42 blank | 67 comment | 7 complexity | 51e8d36aeb5a916b7a6ec974637befe7 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Db\Sql\Ddl;
  10. use Zend\Db\Adapter\Platform\PlatformInterface;
  11. use Zend\Db\Adapter\Platform\Sql92 as AdapterSql92Platform;
  12. use Zend\Db\Sql\AbstractSql;
  13. class AlterTable extends AbstractSql implements SqlInterface
  14. {
  15. const ADD_COLUMNS = 'addColumns';
  16. const ADD_CONSTRAINTS = 'addConstraints';
  17. const CHANGE_COLUMNS = 'changeColumns';
  18. const DROP_COLUMNS = 'dropColumns';
  19. const DROP_CONSTRAINTS = 'dropConstraints';
  20. const TABLE = 'table';
  21. /**
  22. * @var array
  23. */
  24. protected $addColumns = array();
  25. /**
  26. * @var array
  27. */
  28. protected $addConstraints = array();
  29. /**
  30. * @var array
  31. */
  32. protected $changeColumns = array();
  33. /**
  34. * @var array
  35. */
  36. protected $dropColumns = array();
  37. /**
  38. * @var array
  39. */
  40. protected $dropConstraints = array();
  41. /**
  42. * Specifications for Sql String generation
  43. * @var array
  44. */
  45. protected $specifications = array(
  46. self::TABLE => "ALTER TABLE %1\$s\n",
  47. self::ADD_COLUMNS => array(
  48. "%1\$s" => array(
  49. array(1 => 'ADD COLUMN %1$s', 'combinedby' => ",\n")
  50. )
  51. ),
  52. self::CHANGE_COLUMNS => array(
  53. "%1\$s" => array(
  54. array(2 => 'CHANGE COLUMN %1$s %2$s', 'combinedby' => ",\n"),
  55. )
  56. ),
  57. self::DROP_COLUMNS => array(
  58. "%1\$s" => array(
  59. array(1 => 'DROP COLUMN %1$s', 'combinedby' => ",\n"),
  60. )
  61. ),
  62. self::ADD_CONSTRAINTS => array(
  63. "%1\$s" => array(
  64. array(1 => 'ADD %1$s', 'combinedby' => ",\n"),
  65. )
  66. ),
  67. self::DROP_CONSTRAINTS => array(
  68. "%1\$s" => array(
  69. array(1 => 'DROP CONSTRAINT %1$s', 'combinedby' => ",\n"),
  70. )
  71. )
  72. );
  73. /**
  74. * @var string
  75. */
  76. protected $table = '';
  77. /**
  78. * @param string $table
  79. */
  80. public function __construct($table = '')
  81. {
  82. ($table) ? $this->setTable($table) : null;
  83. }
  84. /**
  85. * @param string $name
  86. * @return self
  87. */
  88. public function setTable($name)
  89. {
  90. $this->table = $name;
  91. return $this;
  92. }
  93. /**
  94. * @param Column\ColumnInterface $column
  95. * @return self
  96. */
  97. public function addColumn(Column\ColumnInterface $column)
  98. {
  99. $this->addColumns[] = $column;
  100. return $this;
  101. }
  102. /**
  103. * @param string $name
  104. * @param Column\ColumnInterface $column
  105. * @return self
  106. */
  107. public function changeColumn($name, Column\ColumnInterface $column)
  108. {
  109. $this->changeColumns[$name] = $column;
  110. return $this;
  111. }
  112. /**
  113. * @param string $name
  114. * @return self
  115. */
  116. public function dropColumn($name)
  117. {
  118. $this->dropColumns[] = $name;
  119. return $this;
  120. }
  121. /**
  122. * @param string $name
  123. * @return self
  124. */
  125. public function dropConstraint($name)
  126. {
  127. $this->dropConstraints[] = $name;
  128. return $this;
  129. }
  130. /**
  131. * @param Constraint\ConstraintInterface $constraint
  132. * @return self
  133. */
  134. public function addConstraint(Constraint\ConstraintInterface $constraint)
  135. {
  136. $this->addConstraints[] = $constraint;
  137. return $this;
  138. }
  139. /**
  140. * @param string|null $key
  141. * @return array
  142. */
  143. public function getRawState($key = null)
  144. {
  145. $rawState = array(
  146. self::TABLE => $this->table,
  147. self::ADD_COLUMNS => $this->addColumns,
  148. self::DROP_COLUMNS => $this->dropColumns,
  149. self::CHANGE_COLUMNS => $this->changeColumns,
  150. self::ADD_CONSTRAINTS => $this->addConstraints,
  151. self::DROP_CONSTRAINTS => $this->dropConstraints,
  152. );
  153. return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState;
  154. }
  155. /**
  156. * @param PlatformInterface $adapterPlatform
  157. * @return string
  158. */
  159. public function getSqlString(PlatformInterface $adapterPlatform = null)
  160. {
  161. // get platform, or create default
  162. $adapterPlatform = ($adapterPlatform) ?: new AdapterSql92Platform;
  163. $sqls = array();
  164. $parameters = array();
  165. foreach ($this->specifications as $name => $specification) {
  166. $parameters[$name] = $this->{'process' . $name}($adapterPlatform, null, null, $sqls, $parameters);
  167. if ($specification && is_array($parameters[$name]) && ($parameters[$name] != array(array()))) {
  168. $sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]);
  169. }
  170. if (stripos($name, 'table') === false && $parameters[$name] !== array(array())) {
  171. $sqls[] = ",\n";
  172. }
  173. }
  174. // remove last ,\n
  175. array_pop($sqls);
  176. $sql = implode('', $sqls);
  177. return $sql;
  178. }
  179. protected function processTable(PlatformInterface $adapterPlatform = null)
  180. {
  181. return array($adapterPlatform->quoteIdentifier($this->table));
  182. }
  183. protected function processAddColumns(PlatformInterface $adapterPlatform = null)
  184. {
  185. $sqls = array();
  186. foreach ($this->addColumns as $column) {
  187. $sqls[] = $this->processExpression($column, $adapterPlatform)->getSql();
  188. }
  189. return array($sqls);
  190. }
  191. protected function processChangeColumns(PlatformInterface $adapterPlatform = null)
  192. {
  193. $sqls = array();
  194. foreach ($this->changeColumns as $name => $column) {
  195. $sqls[] = array(
  196. $adapterPlatform->quoteIdentifier($name),
  197. $this->processExpression($column, $adapterPlatform)->getSql()
  198. );
  199. }
  200. return array($sqls);
  201. }
  202. protected function processDropColumns(PlatformInterface $adapterPlatform = null)
  203. {
  204. $sqls = array();
  205. foreach ($this->dropColumns as $column) {
  206. $sqls[] = $adapterPlatform->quoteIdentifier($column);
  207. }
  208. return array($sqls);
  209. }
  210. protected function processAddConstraints(PlatformInterface $adapterPlatform = null)
  211. {
  212. $sqls = array();
  213. foreach ($this->addConstraints as $constraint) {
  214. $sqls[] = $this->processExpression($constraint, $adapterPlatform);
  215. }
  216. return array($sqls);
  217. }
  218. protected function processDropConstraints(PlatformInterface $adapterPlatform = null)
  219. {
  220. $sqls = array();
  221. foreach ($this->dropConstraints as $constraint) {
  222. $sqls[] = $adapterPlatform->quoteIdentifier($constraint);
  223. }
  224. return array($sqls);
  225. }
  226. }