SQLLib.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\workorder\library;
  3. class SQLLib
  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. 'values' => '',
  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. case 'switch':
  56. $this->data['type'] = 'switch';
  57. break;
  58. default:
  59. $this->data['type'] = 'VARCHAR';
  60. break;
  61. }
  62. return $this;
  63. }
  64. public function setOldname($oldname)
  65. {
  66. $this->data['oldname'] = $oldname;
  67. return $this;
  68. }
  69. public function setName($name)
  70. {
  71. $this->data['name'] = $name;
  72. return $this;
  73. }
  74. public function setFieldLength($length)
  75. {
  76. $this->data['length'] = $length;
  77. return $this;
  78. }
  79. public function setValues($values)
  80. {
  81. $this->data['values'] = $values;
  82. return $this;
  83. }
  84. public function setComment($comment)
  85. {
  86. $this->data['comment'] = $comment;
  87. return $this;
  88. }
  89. public function setDefaultvalue($defaultvalue)
  90. {
  91. $this->data['defaultvalue'] = $defaultvalue;
  92. return $this;
  93. }
  94. public function setDecimals($decimals)
  95. {
  96. $this->data['decimals'] = $decimals;
  97. return $this;
  98. }
  99. protected function prebuilt()
  100. {
  101. if ($this->data['type'] == 'INT') {
  102. if ($this->data['decimals'] > 0) {
  103. $this->data['type'] = 'DECIMAL';
  104. $this->data['length'] = "({$this->data['length']},{$this->data['decimals']})";
  105. } else {
  106. $this->data['length'] = "({$this->data['length']})";
  107. }
  108. $this->data['defaultvalue'] = $this->data['defaultvalue'] == '' ? 'NULL' : $this->data['defaultvalue'];
  109. } elseif ($this->data['type'] == 'switch') {
  110. $this->data['type'] = 'TINYINT';
  111. $this->data['length'] = "(1)";
  112. $this->data['comment'] .= $this->data['comment'] . ':0=关,1=开';
  113. } elseif (in_array($this->data['type'], ['SET', 'ENUM'])) {
  114. $fieldValues = \app\common\model\Config::decode($this->data['values']);
  115. $this->data['comment'] .= ':';
  116. foreach ($fieldValues as $key => $value) {
  117. $this->data['comment'] .= $key . '=' . $value . ',';
  118. }
  119. $this->data['comment'] = trim($this->data['comment'], ',');
  120. $this->data['length'] = "('" . implode("','", array_keys($fieldValues)) . "')";
  121. $this->data['defaultvalue'] = in_array($this->data['defaultvalue'], array_keys($fieldValues)) ? $this->data['defaultvalue'] : ($this->data['type'] == 'ENUM' ? key($fieldValues) : '');
  122. } elseif (in_array($this->data['type'], ['DATE', 'TIME', 'DATETIME'])) {
  123. $this->data['length'] = '';
  124. $this->data['defaultvalue'] = "NULL";
  125. } elseif (in_array($this->data['type'], ['TEXT'])) {
  126. $this->data['length'] = "(0)";
  127. $this->data['defaultvalue'] = 'NULL';
  128. } else {
  129. $this->data['length'] = "({$this->data['length']})";
  130. }
  131. $this->data['defaultvalue'] = strtoupper($this->data['defaultvalue']) === 'NULL' ? "NULL" : "'{$this->data['defaultvalue']}'";
  132. }
  133. public function getAddSql()
  134. {
  135. $this->prebuilt();
  136. $sql = "ALTER TABLE `{$this->data['table']}` " . "ADD `{$this->data['name']}` {$this->data['type']} {$this->data['length']} " . "DEFAULT {$this->data['defaultvalue']} " . "COMMENT '{$this->data['comment']}' " . ($this->data['after'] ? "AFTER `{$this->data['after']}`" : '');
  137. return $sql;
  138. }
  139. public function getModifySql()
  140. {
  141. $this->prebuilt();
  142. $sql = "ALTER TABLE `{$this->data['table']}` " . ($this->data['oldname'] ? 'CHANGE' : 'MODIFY') . " COLUMN " . ($this->data['oldname'] ? "`{$this->data['oldname']}`" : '') . " `{$this->data['name']}` {$this->data['type']} {$this->data['length']} " . "DEFAULT {$this->data['defaultvalue']} " . "COMMENT '{$this->data['comment']}' " . ($this->data['after'] ? "AFTER `{$this->data['after']}`" : '');
  143. return $sql;
  144. }
  145. public function getDropSql()
  146. {
  147. $sql = "ALTER TABLE `{$this->data['table']}` " . "DROP `{$this->data['name']}`";
  148. return $sql;
  149. }
  150. }