PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/db/SchemaBuilder.php

https://gitlab.com/brucealdridge/yii2
PHP | 465 lines | 168 code | 49 blank | 248 comment | 4 complexity | 0b681dfeecd31ba829ea2ff17e4cf3a0 MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use Yii;
  9. use yii\base\Object;
  10. /**
  11. * SchemaBuilder is the class help to define DB's schema types.
  12. *
  13. * For example you may use the following code inside your migration files:
  14. *
  15. * ```php
  16. * $this->createTable('table', [
  17. * 'name' => Schema::string(64)->notNull(),
  18. * 'type' => Schema::integer()->notNull()->default(10),
  19. * 'description' => Schema::text(),
  20. * 'rule_name' => Schema::string(64),
  21. * 'data' => Schema::text(),
  22. * 'created_at' => Schema::integer(),
  23. * 'updated_at' => Schema::integer(),
  24. *]);
  25. * ```
  26. *
  27. * @method SchemaBuilder default($default = null) see [[SchemaBuilder::_default()]] for more info
  28. *
  29. * @author Vasenin Matvey <vaseninm@gmail.com>
  30. * @since 2.0.5
  31. */
  32. abstract class SchemaBuilder extends Object
  33. {
  34. /**
  35. * @var string column type
  36. */
  37. protected $type;
  38. /**
  39. * @var integer column size
  40. */
  41. protected $length;
  42. /**
  43. * @var boolean whether value is not nullable
  44. */
  45. protected $isNotNull = false;
  46. /**
  47. * @var boolean whether value should be unique
  48. */
  49. protected $isUnique = false;
  50. /**
  51. * @var string check value of column
  52. */
  53. protected $check;
  54. /**
  55. * @var mixed default value of column
  56. */
  57. protected $default;
  58. /**
  59. * Makes column a primary key
  60. *
  61. * @param integer $length
  62. *
  63. * @return self
  64. */
  65. public static function primaryKey($length = null)
  66. {
  67. return static::create(Schema::TYPE_PK, $length);
  68. }
  69. /**
  70. * Makes column a big primary key
  71. *
  72. * @param integer $length
  73. *
  74. * @return self
  75. */
  76. public static function bigPrimaryKey($length = null)
  77. {
  78. return static::create(Schema::TYPE_BIGPK, $length);
  79. }
  80. /**
  81. * Makes column a string
  82. *
  83. * @param integer $length
  84. *
  85. * @return self
  86. */
  87. public static function string($length = null)
  88. {
  89. return static::create(Schema::TYPE_STRING, $length);
  90. }
  91. /**
  92. * Makes column a text
  93. *
  94. * @param integer $length
  95. *
  96. * @return self
  97. */
  98. public static function text($length = null)
  99. {
  100. return static::create(Schema::TYPE_TEXT, $length);
  101. }
  102. /**
  103. * Makes column a smallint
  104. *
  105. * @param integer $length
  106. *
  107. * @return self
  108. */
  109. public static function smallInteger($length = null)
  110. {
  111. return static::create(Schema::TYPE_SMALLINT, $length);
  112. }
  113. /**
  114. * Makes column a integer
  115. *
  116. * @param integer $length
  117. *
  118. * @return self
  119. */
  120. public static function integer($length = null)
  121. {
  122. return static::create(Schema::TYPE_INTEGER, $length);
  123. }
  124. /**
  125. * Makes column a bigint
  126. *
  127. * @param integer $length
  128. *
  129. * @return self
  130. */
  131. public static function bigInteger($length = null)
  132. {
  133. return static::create(Schema::TYPE_BIGINT, $length);
  134. }
  135. /**
  136. * Makes column a float
  137. *
  138. * @param integer $precision
  139. * @param integer $scale
  140. *
  141. * @return self
  142. */
  143. public static function float($precision = null, $scale = null)
  144. {
  145. return static::createNumeric(Schema::TYPE_FLOAT, $precision, $scale);
  146. }
  147. /**
  148. * Makes column a double
  149. *
  150. * @param integer $precision
  151. * @param integer $scale
  152. *
  153. * @return self
  154. */
  155. public static function double($precision = null, $scale = null)
  156. {
  157. return static::createNumeric(Schema::TYPE_DOUBLE, $precision, $scale);
  158. }
  159. /**
  160. * Makes column a decimal
  161. *
  162. * @param integer $precision
  163. * @param integer $scale
  164. *
  165. * @return self
  166. */
  167. public static function decimal($precision = null, $scale = null)
  168. {
  169. return static::createNumeric(Schema::TYPE_DECIMAL, $precision, $scale);
  170. }
  171. /**
  172. * Makes column a datetime
  173. *
  174. * @param integer $length
  175. *
  176. * @return self
  177. */
  178. public static function dateTime($length = null)
  179. {
  180. return static::create(Schema::TYPE_DATETIME, $length);
  181. }
  182. /**
  183. * Makes column a timestamp
  184. *
  185. * @param integer $length
  186. *
  187. * @return self
  188. */
  189. public static function timestamp($length = null)
  190. {
  191. return static::create(Schema::TYPE_TIMESTAMP, $length);
  192. }
  193. /**
  194. * Makes column a time
  195. *
  196. * @param integer $length
  197. *
  198. * @return self
  199. */
  200. public static function time($length = null)
  201. {
  202. return static::create(Schema::TYPE_TIME, $length);
  203. }
  204. /**
  205. * Makes column a date
  206. *
  207. * @return self
  208. */
  209. public static function date()
  210. {
  211. return static::create(Schema::TYPE_DATE);
  212. }
  213. /**
  214. * Makes column a binary
  215. *
  216. * @param integer $length
  217. *
  218. * @return self
  219. */
  220. public static function binary($length = null)
  221. {
  222. return static::create(Schema::TYPE_BINARY, $length);
  223. }
  224. /**
  225. * Makes column a boolean
  226. *
  227. * @param integer $length
  228. *
  229. * @return self
  230. */
  231. public static function boolean($length = null)
  232. {
  233. return static::create(Schema::TYPE_BOOLEAN, $length);
  234. }
  235. /**
  236. * Makes column a money
  237. *
  238. * @param integer $precision
  239. * @param integer $scale
  240. *
  241. * @return self
  242. */
  243. public static function money($precision = null, $scale = null)
  244. {
  245. return static::createNumeric(Schema::TYPE_MONEY, $precision, $scale);
  246. }
  247. /**
  248. * Makes column not nullable
  249. *
  250. * @return $this
  251. */
  252. public function notNull()
  253. {
  254. $this->isNotNull = true;
  255. return $this;
  256. }
  257. /**
  258. * Makes column unique
  259. *
  260. * @return $this
  261. */
  262. public function unique()
  263. {
  264. $this->isUnique = true;
  265. return $this;
  266. }
  267. /**
  268. * Calls the named method which is not a class method.
  269. *
  270. * Do not call this method directly as it is a PHP magic method that
  271. * will be implicitly called when an unknown method is being invoked.
  272. *
  273. * @param string $name the method name
  274. * @param array $arguments method parameters
  275. *
  276. * @return mixed the method return value
  277. */
  278. public function __call($name, $arguments)
  279. {
  280. if ($name === 'default') {
  281. return call_user_func_array([$this, '_default'], $arguments);
  282. }
  283. return parent::__call($name, $arguments);
  284. }
  285. /**
  286. * Specify check value for the column
  287. *
  288. * @param string $check
  289. *
  290. * @return $this
  291. */
  292. public function check($check)
  293. {
  294. $this->check = $check;
  295. return $this;
  296. }
  297. /**
  298. * Build full string for create the column's schema
  299. *
  300. * @return string
  301. */
  302. public function __toString()
  303. {
  304. return
  305. $this->type .
  306. $this->buildLengthString() .
  307. $this->buildNotNullString() .
  308. $this->buildUniqueString() .
  309. $this->buildDefaultString() .
  310. $this->buildCheckString();
  311. }
  312. /**
  313. * Specify default value for the column
  314. *
  315. * @param mixed $default
  316. *
  317. * @return $this
  318. */
  319. protected function _default($default = null)
  320. {
  321. $this->default = $default;
  322. return $this;
  323. }
  324. /**
  325. * Returns string with length of column
  326. *
  327. * @return string
  328. */
  329. protected function buildLengthString()
  330. {
  331. return ($this->length !== null ? "({$this->length})" : '');
  332. }
  333. /**
  334. * Returns string with NOT NULL if isNotNull is true, otherwise returns
  335. * empty string
  336. *
  337. * @return string
  338. */
  339. protected function buildNotNullString()
  340. {
  341. return $this->isNotNull === true ? ' NOT NULL' : '';
  342. }
  343. /**
  344. * Returns string with UNIQUE if isUnique is true, otherwise returns
  345. * empty string
  346. *
  347. * @return string
  348. */
  349. protected function buildUniqueString()
  350. {
  351. return $this->isUnique === true ? ' UNIQUE' : '';
  352. }
  353. /**
  354. * Returns string with default value of column
  355. *
  356. * @return string
  357. */
  358. protected function buildDefaultString()
  359. {
  360. $string = '';
  361. if ($this->default !== null) {
  362. $string .= ' DEFAULT ';
  363. switch (gettype($this->default)) {
  364. case 'integer':
  365. case 'double':
  366. $string .= $this->default;
  367. break;
  368. case 'boolean':
  369. $string .= $this->default ? 'TRUE' : 'FALSE';
  370. break;
  371. default:
  372. $string .= "'{$this->default}'";
  373. }
  374. }
  375. return $string;
  376. }
  377. /**
  378. * Returns check value of column
  379. *
  380. * @return string
  381. */
  382. protected function buildCheckString()
  383. {
  384. return ($this->check !== null ? " CHECK ({$this->check})" : '');
  385. }
  386. /**
  387. * Create schema builder for types with length
  388. *
  389. * @param string $type schema of column
  390. * @param integer $length length of column
  391. * @return self
  392. */
  393. protected static function create($type, $length = null)
  394. {
  395. $object = new static;
  396. $object->type = $type;
  397. $object->length = $length;
  398. return $object;
  399. }
  400. /**
  401. * Create schema builder for numeric types types with precision and scale
  402. *
  403. * @param string $type schema of column
  404. * @param integer $precision precision of column
  405. * @param integer $scale scale of column
  406. * @return self
  407. */
  408. protected static function createNumeric($type, $precision = null, $scale = null)
  409. {
  410. $length = null;
  411. if ($precision !== null) {
  412. $length = $precision . ($scale !== null ? ",$scale" : '');
  413. }
  414. return self::create($type, $length);
  415. }
  416. }