Column.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\Types\Type;
  21. use Doctrine\DBAL\Schema\Visitor\Visitor;
  22. /**
  23. * Object representation of a database column
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @version $Revision$
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. */
  31. class Column extends AbstractAsset
  32. {
  33. /**
  34. * @var \Doctrine\DBAL\Types\Type
  35. */
  36. protected $_type;
  37. /**
  38. * @var int
  39. */
  40. protected $_length = null;
  41. /**
  42. * @var int
  43. */
  44. protected $_precision = 10;
  45. /**
  46. * @var int
  47. */
  48. protected $_scale = 0;
  49. /**
  50. * @var bool
  51. */
  52. protected $_unsigned = false;
  53. /**
  54. * @var bool
  55. */
  56. protected $_fixed = false;
  57. /**
  58. * @var bool
  59. */
  60. protected $_notnull = true;
  61. /**
  62. * @var string
  63. */
  64. protected $_default = null;
  65. /**
  66. * @var bool
  67. */
  68. protected $_autoincrement = false;
  69. /**
  70. * @var array
  71. */
  72. protected $_platformOptions = array();
  73. /**
  74. * @var string
  75. */
  76. protected $_columnDefinition = null;
  77. /**
  78. * @var string
  79. */
  80. protected $_comment = null;
  81. /**
  82. * Create a new Column
  83. *
  84. * @param string $columnName
  85. * @param Doctrine\DBAL\Types\Type $type
  86. * @param int $length
  87. * @param bool $notNull
  88. * @param mixed $default
  89. * @param bool $unsigned
  90. * @param bool $fixed
  91. * @param int $precision
  92. * @param int $scale
  93. * @param array $platformOptions
  94. */
  95. public function __construct($columnName, Type $type, array $options=array())
  96. {
  97. $this->_setName($columnName);
  98. $this->setType($type);
  99. $this->setOptions($options);
  100. }
  101. /**
  102. * @param array $options
  103. * @return Column
  104. */
  105. public function setOptions(array $options)
  106. {
  107. foreach ($options AS $name => $value) {
  108. $method = "set".$name;
  109. if (method_exists($this, $method)) {
  110. $this->$method($value);
  111. }
  112. }
  113. return $this;
  114. }
  115. /**
  116. * @param Type $type
  117. * @return Column
  118. */
  119. public function setType(Type $type)
  120. {
  121. $this->_type = $type;
  122. return $this;
  123. }
  124. /**
  125. * @param int $length
  126. * @return Column
  127. */
  128. public function setLength($length)
  129. {
  130. if($length !== null) {
  131. $this->_length = (int)$length;
  132. } else {
  133. $this->_length = null;
  134. }
  135. return $this;
  136. }
  137. /**
  138. * @param int $precision
  139. * @return Column
  140. */
  141. public function setPrecision($precision)
  142. {
  143. if (!is_numeric($precision)) {
  144. $precision = 10; // defaults to 10 when no valid precision is given.
  145. }
  146. $this->_precision = (int)$precision;
  147. return $this;
  148. }
  149. /**
  150. * @param int $scale
  151. * @return Column
  152. */
  153. public function setScale($scale)
  154. {
  155. if (!is_numeric($scale)) {
  156. $scale = 0;
  157. }
  158. $this->_scale = (int)$scale;
  159. return $this;
  160. }
  161. /**
  162. *
  163. * @param bool $unsigned
  164. * @return Column
  165. */
  166. public function setUnsigned($unsigned)
  167. {
  168. $this->_unsigned = (bool)$unsigned;
  169. return $this;
  170. }
  171. /**
  172. *
  173. * @param bool $fixed
  174. * @return Column
  175. */
  176. public function setFixed($fixed)
  177. {
  178. $this->_fixed = (bool)$fixed;
  179. return $this;
  180. }
  181. /**
  182. * @param bool $notnull
  183. * @return Column
  184. */
  185. public function setNotnull($notnull)
  186. {
  187. $this->_notnull = (bool)$notnull;
  188. return $this;
  189. }
  190. /**
  191. *
  192. * @param mixed $default
  193. * @return Column
  194. */
  195. public function setDefault($default)
  196. {
  197. $this->_default = $default;
  198. return $this;
  199. }
  200. /**
  201. *
  202. * @param array $platformOptions
  203. * @return Column
  204. */
  205. public function setPlatformOptions(array $platformOptions)
  206. {
  207. $this->_platformOptions = $platformOptions;
  208. return $this;
  209. }
  210. /**
  211. *
  212. * @param string $name
  213. * @param mixed $value
  214. * @return Column
  215. */
  216. public function setPlatformOption($name, $value)
  217. {
  218. $this->_platformOptions[$name] = $value;
  219. return $this;
  220. }
  221. /**
  222. *
  223. * @param string
  224. * @return Column
  225. */
  226. public function setColumnDefinition($value)
  227. {
  228. $this->_columnDefinition = $value;
  229. return $this;
  230. }
  231. public function getType()
  232. {
  233. return $this->_type;
  234. }
  235. public function getLength()
  236. {
  237. return $this->_length;
  238. }
  239. public function getPrecision()
  240. {
  241. return $this->_precision;
  242. }
  243. public function getScale()
  244. {
  245. return $this->_scale;
  246. }
  247. public function getUnsigned()
  248. {
  249. return $this->_unsigned;
  250. }
  251. public function getFixed()
  252. {
  253. return $this->_fixed;
  254. }
  255. public function getNotnull()
  256. {
  257. return $this->_notnull;
  258. }
  259. public function getDefault()
  260. {
  261. return $this->_default;
  262. }
  263. public function getPlatformOptions()
  264. {
  265. return $this->_platformOptions;
  266. }
  267. public function hasPlatformOption($name)
  268. {
  269. return isset($this->_platformOptions[$name]);
  270. }
  271. public function getPlatformOption($name)
  272. {
  273. return $this->_platformOptions[$name];
  274. }
  275. public function getColumnDefinition()
  276. {
  277. return $this->_columnDefinition;
  278. }
  279. public function getAutoincrement()
  280. {
  281. return $this->_autoincrement;
  282. }
  283. public function setAutoincrement($flag)
  284. {
  285. $this->_autoincrement = $flag;
  286. return $this;
  287. }
  288. public function setComment($comment)
  289. {
  290. $this->_comment = $comment;
  291. return $this;
  292. }
  293. public function getComment()
  294. {
  295. return $this->_comment;
  296. }
  297. /**
  298. * @param Visitor $visitor
  299. */
  300. public function visit(\Doctrine\DBAL\Schema\Visitor $visitor)
  301. {
  302. $visitor->accept($this);
  303. }
  304. /**
  305. * @return array
  306. */
  307. public function toArray()
  308. {
  309. return array_merge(array(
  310. 'name' => $this->_name,
  311. 'type' => $this->_type,
  312. 'default' => $this->_default,
  313. 'notnull' => $this->_notnull,
  314. 'length' => $this->_length,
  315. 'precision' => $this->_precision,
  316. 'scale' => $this->_scale,
  317. 'fixed' => $this->_fixed,
  318. 'unsigned' => $this->_unsigned,
  319. 'autoincrement' => $this->_autoincrement,
  320. 'columnDefinition' => $this->_columnDefinition,
  321. 'comment' => $this->_comment,
  322. ), $this->_platformOptions);
  323. }
  324. }