Column.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. * @var array
  83. */
  84. protected $_customSchemaOptions = array();
  85. /**
  86. * Create a new Column
  87. *
  88. * @param string $columnName
  89. * @param Doctrine\DBAL\Types\Type $type
  90. * @param int $length
  91. * @param bool $notNull
  92. * @param mixed $default
  93. * @param bool $unsigned
  94. * @param bool $fixed
  95. * @param int $precision
  96. * @param int $scale
  97. * @param array $platformOptions
  98. */
  99. public function __construct($columnName, Type $type, array $options=array())
  100. {
  101. $this->_setName($columnName);
  102. $this->setType($type);
  103. $this->setOptions($options);
  104. }
  105. /**
  106. * @param array $options
  107. * @return Column
  108. */
  109. public function setOptions(array $options)
  110. {
  111. foreach ($options AS $name => $value) {
  112. $method = "set".$name;
  113. if (method_exists($this, $method)) {
  114. $this->$method($value);
  115. }
  116. }
  117. return $this;
  118. }
  119. /**
  120. * @param Type $type
  121. * @return Column
  122. */
  123. public function setType(Type $type)
  124. {
  125. $this->_type = $type;
  126. return $this;
  127. }
  128. /**
  129. * @param int $length
  130. * @return Column
  131. */
  132. public function setLength($length)
  133. {
  134. if($length !== null) {
  135. $this->_length = (int)$length;
  136. } else {
  137. $this->_length = null;
  138. }
  139. return $this;
  140. }
  141. /**
  142. * @param int $precision
  143. * @return Column
  144. */
  145. public function setPrecision($precision)
  146. {
  147. if (!is_numeric($precision)) {
  148. $precision = 10; // defaults to 10 when no valid precision is given.
  149. }
  150. $this->_precision = (int)$precision;
  151. return $this;
  152. }
  153. /**
  154. * @param int $scale
  155. * @return Column
  156. */
  157. public function setScale($scale)
  158. {
  159. if (!is_numeric($scale)) {
  160. $scale = 0;
  161. }
  162. $this->_scale = (int)$scale;
  163. return $this;
  164. }
  165. /**
  166. *
  167. * @param bool $unsigned
  168. * @return Column
  169. */
  170. public function setUnsigned($unsigned)
  171. {
  172. $this->_unsigned = (bool)$unsigned;
  173. return $this;
  174. }
  175. /**
  176. *
  177. * @param bool $fixed
  178. * @return Column
  179. */
  180. public function setFixed($fixed)
  181. {
  182. $this->_fixed = (bool)$fixed;
  183. return $this;
  184. }
  185. /**
  186. * @param bool $notnull
  187. * @return Column
  188. */
  189. public function setNotnull($notnull)
  190. {
  191. $this->_notnull = (bool)$notnull;
  192. return $this;
  193. }
  194. /**
  195. *
  196. * @param mixed $default
  197. * @return Column
  198. */
  199. public function setDefault($default)
  200. {
  201. $this->_default = $default;
  202. return $this;
  203. }
  204. /**
  205. *
  206. * @param array $platformOptions
  207. * @return Column
  208. */
  209. public function setPlatformOptions(array $platformOptions)
  210. {
  211. $this->_platformOptions = $platformOptions;
  212. return $this;
  213. }
  214. /**
  215. *
  216. * @param string $name
  217. * @param mixed $value
  218. * @return Column
  219. */
  220. public function setPlatformOption($name, $value)
  221. {
  222. $this->_platformOptions[$name] = $value;
  223. return $this;
  224. }
  225. /**
  226. *
  227. * @param string
  228. * @return Column
  229. */
  230. public function setColumnDefinition($value)
  231. {
  232. $this->_columnDefinition = $value;
  233. return $this;
  234. }
  235. public function getType()
  236. {
  237. return $this->_type;
  238. }
  239. public function getLength()
  240. {
  241. return $this->_length;
  242. }
  243. public function getPrecision()
  244. {
  245. return $this->_precision;
  246. }
  247. public function getScale()
  248. {
  249. return $this->_scale;
  250. }
  251. public function getUnsigned()
  252. {
  253. return $this->_unsigned;
  254. }
  255. public function getFixed()
  256. {
  257. return $this->_fixed;
  258. }
  259. public function getNotnull()
  260. {
  261. return $this->_notnull;
  262. }
  263. public function getDefault()
  264. {
  265. return $this->_default;
  266. }
  267. public function getPlatformOptions()
  268. {
  269. return $this->_platformOptions;
  270. }
  271. public function hasPlatformOption($name)
  272. {
  273. return isset($this->_platformOptions[$name]);
  274. }
  275. public function getPlatformOption($name)
  276. {
  277. return $this->_platformOptions[$name];
  278. }
  279. public function getColumnDefinition()
  280. {
  281. return $this->_columnDefinition;
  282. }
  283. public function getAutoincrement()
  284. {
  285. return $this->_autoincrement;
  286. }
  287. public function setAutoincrement($flag)
  288. {
  289. $this->_autoincrement = $flag;
  290. return $this;
  291. }
  292. public function setComment($comment)
  293. {
  294. $this->_comment = $comment;
  295. return $this;
  296. }
  297. public function getComment()
  298. {
  299. return $this->_comment;
  300. }
  301. /**
  302. * @param string $name
  303. * @param mixed $value
  304. * @return Column
  305. */
  306. public function setCustomSchemaOption($name, $value)
  307. {
  308. $this->_customSchemaOptions[$name] = $value;
  309. return $this;
  310. }
  311. /**
  312. * @param string $name
  313. * @return boolean
  314. */
  315. public function hasCustomSchemaOption($name)
  316. {
  317. return isset($this->_customSchemaOptions[$name]);
  318. }
  319. /**
  320. * @param string $name
  321. * @return mixed
  322. */
  323. public function getCustomSchemaOption($name)
  324. {
  325. return $this->_customSchemaOptions[$name];
  326. }
  327. /**
  328. * @param array $customSchemaOptions
  329. * @return Column
  330. */
  331. public function setCustomSchemaOptions(array $customSchemaOptions)
  332. {
  333. $this->_customSchemaOptions = $customSchemaOptions;
  334. return $this;
  335. }
  336. /**
  337. * @return array
  338. */
  339. public function getCustomSchemaOptions()
  340. {
  341. return $this->_customSchemaOptions;
  342. }
  343. /**
  344. * @param Visitor $visitor
  345. */
  346. public function visit(\Doctrine\DBAL\Schema\Visitor $visitor)
  347. {
  348. $visitor->accept($this);
  349. }
  350. /**
  351. * @return array
  352. */
  353. public function toArray()
  354. {
  355. return array_merge(array(
  356. 'name' => $this->_name,
  357. 'type' => $this->_type,
  358. 'default' => $this->_default,
  359. 'notnull' => $this->_notnull,
  360. 'length' => $this->_length,
  361. 'precision' => $this->_precision,
  362. 'scale' => $this->_scale,
  363. 'fixed' => $this->_fixed,
  364. 'unsigned' => $this->_unsigned,
  365. 'autoincrement' => $this->_autoincrement,
  366. 'columnDefinition' => $this->_columnDefinition,
  367. 'comment' => $this->_comment,
  368. ), $this->_platformOptions, $this->_customSchemaOptions);
  369. }
  370. }