No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

274 líneas
10KB

  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. /**
  9. * SchemaBuilderTrait contains shortcut methods to create instances of [[ColumnSchemaBuilder]].
  10. *
  11. * These can be used in database migrations to define database schema types using a PHP interface.
  12. * This is useful to define a schema in a DBMS independent way so that the application may run on
  13. * different DBMS the same way.
  14. *
  15. * For example you may use the following code inside your migration files:
  16. *
  17. * ```php
  18. * $this->createTable('example_table', [
  19. * 'id' => $this->primaryKey(),
  20. * 'name' => $this->string(64)->notNull(),
  21. * 'type' => $this->integer()->notNull()->defaultValue(10),
  22. * 'description' => $this->text(),
  23. * 'rule_name' => $this->string(64),
  24. * 'data' => $this->text(),
  25. * 'created_at' => $this->datetime()->notNull(),
  26. * 'updated_at' => $this->datetime(),
  27. * ]);
  28. * ```
  29. *
  30. * @author Vasenin Matvey <vaseninm@gmail.com>
  31. * @since 2.0.6
  32. */
  33. trait SchemaBuilderTrait
  34. {
  35. /**
  36. * @return Connection the database connection to be used for schema building.
  37. */
  38. protected abstract function getDb();
  39. /**
  40. * Creates a primary key column.
  41. * @param integer $length column size or precision definition.
  42. * This parameter will be ignored if not supported by the DBMS.
  43. * @return ColumnSchemaBuilder the column instance which can be further customized.
  44. * @since 2.0.6
  45. */
  46. public function primaryKey($length = null)
  47. {
  48. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_PK, $length);
  49. }
  50. /**
  51. * Creates a big primary key column.
  52. * @param integer $length column size or precision definition.
  53. * This parameter will be ignored if not supported by the DBMS.
  54. * @return ColumnSchemaBuilder the column instance which can be further customized.
  55. * @since 2.0.6
  56. */
  57. public function bigPrimaryKey($length = null)
  58. {
  59. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGPK, $length);
  60. }
  61. /**
  62. * Creates a char column.
  63. * @param integer $length column size definition i.e. the maximum string length.
  64. * This parameter will be ignored if not supported by the DBMS.
  65. * @return ColumnSchemaBuilder the column instance which can be further customized.
  66. * @since 2.0.8
  67. */
  68. public function char($length = null)
  69. {
  70. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_CHAR, $length);
  71. }
  72. /**
  73. * Creates a string column.
  74. * @param integer $length column size definition i.e. the maximum string length.
  75. * This parameter will be ignored if not supported by the DBMS.
  76. * @return ColumnSchemaBuilder the column instance which can be further customized.
  77. * @since 2.0.6
  78. */
  79. public function string($length = null)
  80. {
  81. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, $length);
  82. }
  83. /**
  84. * Creates a text column.
  85. * @return ColumnSchemaBuilder the column instance which can be further customized.
  86. * @since 2.0.6
  87. */
  88. public function text()
  89. {
  90. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TEXT);
  91. }
  92. /**
  93. * Creates a smallint column.
  94. * @param integer $length column size or precision definition.
  95. * This parameter will be ignored if not supported by the DBMS.
  96. * @return ColumnSchemaBuilder the column instance which can be further customized.
  97. * @since 2.0.6
  98. */
  99. public function smallInteger($length = null)
  100. {
  101. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_SMALLINT, $length);
  102. }
  103. /**
  104. * Creates an integer column.
  105. * @param integer $length column size or precision definition.
  106. * This parameter will be ignored if not supported by the DBMS.
  107. * @return ColumnSchemaBuilder the column instance which can be further customized.
  108. * @since 2.0.6
  109. */
  110. public function integer($length = null)
  111. {
  112. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_INTEGER, $length);
  113. }
  114. /**
  115. * Creates a bigint column.
  116. * @param integer $length column size or precision definition.
  117. * This parameter will be ignored if not supported by the DBMS.
  118. * @return ColumnSchemaBuilder the column instance which can be further customized.
  119. * @since 2.0.6
  120. */
  121. public function bigInteger($length = null)
  122. {
  123. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGINT, $length);
  124. }
  125. /**
  126. * Creates a float column.
  127. * @param integer $precision column value precision. First parameter passed to the column type, e.g. FLOAT(precision).
  128. * This parameter will be ignored if not supported by the DBMS.
  129. * @return ColumnSchemaBuilder the column instance which can be further customized.
  130. * @since 2.0.6
  131. */
  132. public function float($precision = null)
  133. {
  134. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_FLOAT, $precision);
  135. }
  136. /**
  137. * Creates a double column.
  138. * @param integer $precision column value precision. First parameter passed to the column type, e.g. DOUBLE(precision).
  139. * This parameter will be ignored if not supported by the DBMS.
  140. * @return ColumnSchemaBuilder the column instance which can be further customized.
  141. * @since 2.0.6
  142. */
  143. public function double($precision = null)
  144. {
  145. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DOUBLE, $precision);
  146. }
  147. /**
  148. * Creates a decimal column.
  149. * @param integer $precision column value precision, which is usually the total number of digits.
  150. * First parameter passed to the column type, e.g. DECIMAL(precision, scale).
  151. * This parameter will be ignored if not supported by the DBMS.
  152. * @param integer $scale column value scale, which is usually the number of digits after the decimal point.
  153. * Second parameter passed to the column type, e.g. DECIMAL(precision, scale).
  154. * This parameter will be ignored if not supported by the DBMS.
  155. * @return ColumnSchemaBuilder the column instance which can be further customized.
  156. * @since 2.0.6
  157. */
  158. public function decimal($precision = null, $scale = null)
  159. {
  160. $length = [];
  161. if ($precision !== null) {
  162. $length[] = $precision;
  163. }
  164. if ($scale !== null) {
  165. $length[] = $scale;
  166. }
  167. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DECIMAL, $length);
  168. }
  169. /**
  170. * Creates a datetime column.
  171. * @param integer $precision column value precision. First parameter passed to the column type, e.g. DATETIME(precision).
  172. * This parameter will be ignored if not supported by the DBMS.
  173. * @return ColumnSchemaBuilder the column instance which can be further customized.
  174. * @since 2.0.6
  175. */
  176. public function dateTime($precision = null)
  177. {
  178. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATETIME, $precision);
  179. }
  180. /**
  181. * Creates a timestamp column.
  182. * @param integer $precision column value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision).
  183. * This parameter will be ignored if not supported by the DBMS.
  184. * @return ColumnSchemaBuilder the column instance which can be further customized.
  185. * @since 2.0.6
  186. */
  187. public function timestamp($precision = null)
  188. {
  189. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIMESTAMP, $precision);
  190. }
  191. /**
  192. * Creates a time column.
  193. * @param integer $precision column value precision. First parameter passed to the column type, e.g. TIME(precision).
  194. * This parameter will be ignored if not supported by the DBMS.
  195. * @return ColumnSchemaBuilder the column instance which can be further customized.
  196. * @since 2.0.6
  197. */
  198. public function time($precision = null)
  199. {
  200. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIME, $precision);
  201. }
  202. /**
  203. * Creates a date column.
  204. * @return ColumnSchemaBuilder the column instance which can be further customized.
  205. * @since 2.0.6
  206. */
  207. public function date()
  208. {
  209. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATE);
  210. }
  211. /**
  212. * Creates a binary column.
  213. * @param integer $length column size or precision definition.
  214. * This parameter will be ignored if not supported by the DBMS.
  215. * @return ColumnSchemaBuilder the column instance which can be further customized.
  216. * @since 2.0.6
  217. */
  218. public function binary($length = null)
  219. {
  220. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BINARY, $length);
  221. }
  222. /**
  223. * Creates a boolean column.
  224. * @return ColumnSchemaBuilder the column instance which can be further customized.
  225. * @since 2.0.6
  226. */
  227. public function boolean()
  228. {
  229. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN);
  230. }
  231. /**
  232. * Creates a money column.
  233. * @param integer $precision column value precision, which is usually the total number of digits.
  234. * First parameter passed to the column type, e.g. DECIMAL(precision, scale).
  235. * This parameter will be ignored if not supported by the DBMS.
  236. * @param integer $scale column value scale, which is usually the number of digits after the decimal point.
  237. * Second parameter passed to the column type, e.g. DECIMAL(precision, scale).
  238. * This parameter will be ignored if not supported by the DBMS.
  239. * @return ColumnSchemaBuilder the column instance which can be further customized.
  240. * @since 2.0.6
  241. */
  242. public function money($precision = null, $scale = null)
  243. {
  244. $length = [];
  245. if ($precision !== null) {
  246. $length[] = $precision;
  247. }
  248. if ($scale !== null) {
  249. $length[] = $scale;
  250. }
  251. return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_MONEY, $length);
  252. }
  253. }