Alter.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace addons\cms\library;
  3. class Alter
  4. {
  5. protected static $instance = null;
  6. protected $config = [];
  7. protected $data = [
  8. 'table' => '',
  9. 'oldname' => '',
  10. 'name' => '',
  11. 'type' => 'VARCHAR',
  12. 'length' => '255',
  13. 'content' => '',
  14. 'comment' => '',
  15. 'after' => '',
  16. ];
  17. public function __construct($options = [])
  18. {
  19. $this->options = array_merge($this->config, $options);
  20. }
  21. public static function instance($options = [])
  22. {
  23. if (is_null(self::$instance)) {
  24. self::$instance = new static($options);
  25. }
  26. return self::$instance;
  27. }
  28. public function setTable($table)
  29. {
  30. $this->data['table'] = db()->name($table)->getTable();
  31. return $this;
  32. }
  33. public function setType($type)
  34. {
  35. switch ($type) {
  36. case 'checkbox':
  37. case 'selects':
  38. $this->data['type'] = 'SET';
  39. break;
  40. case 'radio':
  41. case 'select':
  42. $this->data['type'] = 'ENUM';
  43. break;
  44. case 'number':
  45. $this->data['type'] = 'INT';
  46. break;
  47. case 'date':
  48. case 'datetime':
  49. case 'time':
  50. $this->data['type'] = strtoupper($type);
  51. break;
  52. case 'editor':
  53. $this->data['type'] = 'TEXT';
  54. break;
  55. default:
  56. $this->data['type'] = 'VARCHAR';
  57. break;
  58. }
  59. return $this;
  60. }
  61. public function setOldname($oldname)
  62. {
  63. $this->data['oldname'] = $oldname;
  64. return $this;
  65. }
  66. public function setName($name)
  67. {
  68. $this->data['name'] = $name;
  69. return $this;
  70. }
  71. public function setLength($length)
  72. {
  73. $this->data['length'] = $length;
  74. return $this;
  75. }
  76. public function setContent($content)
  77. {
  78. $this->data['content'] = $content;
  79. return $this;
  80. }
  81. public function setComment($comment)
  82. {
  83. $this->data['comment'] = $comment;
  84. return $this;
  85. }
  86. public function setDefaultvalue($defaultvalue)
  87. {
  88. $this->data['defaultvalue'] = $defaultvalue;
  89. return $this;
  90. }
  91. public function setDecimals($decimals)
  92. {
  93. $this->data['decimals'] = $decimals;
  94. return $this;
  95. }
  96. protected function process()
  97. {
  98. if ($this->data['type'] == 'INT') {
  99. if ($this->data['decimals'] > 0) {
  100. $this->data['type'] = 'DECIMAL';
  101. $this->data['length'] = "({$this->data['length']},{$this->data['decimals']})";
  102. } else {
  103. $this->data['length'] = "({$this->data['length']})";
  104. }
  105. $this->data['defaultvalue'] = $this->data['defaultvalue'] == '' ? 'NULL' : $this->data['defaultvalue'];
  106. } elseif (in_array($this->data['type'], ['SET', 'ENUM'])) {
  107. $content = \app\common\model\Config::decode($this->data['content']);
  108. $this->data['length'] = "('" . implode("','", array_keys($content)) . "')";
  109. $this->data['defaultvalue'] = in_array($this->data['defaultvalue'], array_keys($content)) ? $this->data['defaultvalue'] : ($this->data['type'] == 'ENUM' ? key($content) : '');
  110. } elseif (in_array($this->data['type'], ['DATE', 'TIME', 'DATETIME'])) {
  111. $this->data['length'] = '';
  112. $this->data['defaultvalue'] = "NULL";
  113. } elseif (in_array($this->data['type'], ['TEXT'])) {
  114. $this->data['length'] = "(0)";
  115. $this->data['defaultvalue'] = 'NULL';
  116. } else {
  117. $this->data['length'] = "({$this->data['length']})";
  118. }
  119. $this->data['defaultvalue'] = strtoupper($this->data['defaultvalue']) === 'NULL' ? "NULL" : "'{$this->data['defaultvalue']}'";
  120. }
  121. /**
  122. * 获取添加字段的SQL
  123. * @return string
  124. */
  125. public function getAddSql()
  126. {
  127. $this->process();
  128. $sql = "ALTER TABLE `{$this->data['table']}` "
  129. . "ADD `{$this->data['name']}` {$this->data['type']} {$this->data['length']} "
  130. . "DEFAULT {$this->data['defaultvalue']} "
  131. . "COMMENT '{$this->data['comment']}' "
  132. . ($this->data['after'] ? "AFTER `{$this->data['after']}`" : '');
  133. return $sql;
  134. }
  135. public function getModifySql()
  136. {
  137. $this->process();
  138. $sql = "ALTER TABLE `{$this->data['table']}` "
  139. . ($this->data['oldname'] ? 'CHANGE' : 'MODIFY') . " COLUMN " . ($this->data['oldname'] ? "`{$this->data['oldname']}`" : '') . " `{$this->data['name']}` {$this->data['type']} {$this->data['length']} "
  140. . "DEFAULT {$this->data['defaultvalue']} "
  141. . "COMMENT '{$this->data['comment']}' "
  142. . ($this->data['after'] ? "AFTER `{$this->data['after']}`" : '');
  143. return $sql;
  144. }
  145. /**
  146. * 获取删除字段的SQL
  147. * @return string
  148. */
  149. public function getDropSql()
  150. {
  151. $sql = "ALTER TABLE `{$this->data['table']}` "
  152. . "DROP `{$this->data['name']}`";
  153. return $sql;
  154. }
  155. }