You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

139 lines
3.0 KiB

  1. <?php
  2. namespace Kuxin\Db;
  3. use Closure;
  4. use Kuxin\Config;
  5. use Kuxin\DI;
  6. class Migrate
  7. {
  8. /** @var Mysql $db */
  9. protected $db;
  10. public function __construct($db = 'common')
  11. {
  12. $this->setDb($db);
  13. }
  14. /**
  15. * @param null $db
  16. * @throws \Exception
  17. */
  18. public function setDb($db): void
  19. {
  20. if (is_string($db)) {
  21. $this->db = DI::DB($db);
  22. } else if ($db instanceof Mysql) {
  23. $this->db = $db;
  24. } else {
  25. throw new \Exception('param $db error');
  26. }
  27. }
  28. /**
  29. * @param string $table
  30. * @param \Closure $func
  31. * @param string $engine
  32. * @return bool
  33. * @throws \Exception
  34. */
  35. public function create(string $table, Closure $func, $engine = 'innodb')
  36. {
  37. if (!$table) {
  38. return false;
  39. }
  40. //执行回调函数
  41. $func();
  42. $field = $this->combineCommand();
  43. $engine = $engine ?: Config::get('database.engine', 'innodb');
  44. if ($field) {
  45. $sql = "CREATE TABLE IF NOT EXISTS `{$table}` ({$field}) ENGINE={$engine} DEFAULT CHARSET=utf8;";
  46. return $this->executeSql($sql);
  47. }
  48. }
  49. /**
  50. * @param string $table
  51. * @param \Closure $func
  52. * @return bool
  53. * @throws \Exception
  54. */
  55. public function alter(string $table, Closure $func)
  56. {
  57. if (!$table) {
  58. return false;
  59. }
  60. //执行回调函数
  61. $func();
  62. $this->comands = array_map(function ($k) use ($table) {
  63. return 'alter table ' . $table . ' ' . trim($k, ';') . ';';
  64. }, $this->comands);
  65. $sql = $this->combineCommand();
  66. return $this->executeSql($sql);
  67. }
  68. /**
  69. * @param string $table
  70. * @return bool
  71. * @throws \Exception
  72. */
  73. public function drop(string $table)
  74. {
  75. if (!$table) {
  76. return false;
  77. }
  78. $sql = "DROP TABLE IF EXISTS {$table}";
  79. return $this->executeSql($sql);
  80. }
  81. public function addComand(string $string): void
  82. {
  83. $this->comands[] = trim($string, ',');
  84. }
  85. /**
  86. * @return string
  87. */
  88. protected function combineCommand(): string
  89. {
  90. $command = implode(',', $this->comands);
  91. $this->comands = [];
  92. return $command;
  93. }
  94. /**
  95. * @param string $sql
  96. * @return bool
  97. * @throws \Exception
  98. */
  99. protected function executeSql(string $sql)
  100. {
  101. if ($this->db->execute($sql)) {
  102. return true;
  103. } else {
  104. throw new \Exception($this->db->errorInfo() . PHP_EOL . $this->db->getRealSql($sql));
  105. }
  106. }
  107. /**
  108. * @param string $sql
  109. * @return array
  110. * @throws \Exception
  111. */
  112. protected function fetchAll(string $sql)
  113. {
  114. if (($records = $this->db->fetchAll($sql)) !== false){
  115. return $records;
  116. }else{
  117. throw new \Exception($this->db->errorInfo() . PHP_EOL . $this->db->getRealSql($sql));
  118. }
  119. }
  120. }