AppUtility.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace migration;
  3. use Yii;
  4. class AppUtility
  5. {
  6. public $string = '';
  7. private $Tab = "\t";
  8. private $Nw = "\n";
  9. private $array = [];
  10. private $dbType = '';
  11. private $schema;
  12. function __construct($array, $databaseType="mysql")
  13. {
  14. $this->array = self::objectToArray($array);
  15. switch (strtolower($databaseType)) {
  16. case "mssql":
  17. self::runMsSql();
  18. break;
  19. case "mysql":
  20. self::runMySql();
  21. break;
  22. case "sqlite":
  23. self::runSqlite();
  24. break;
  25. case "pgsql":
  26. self::runPgSql();
  27. break;
  28. }
  29. }
  30. private function objectToArray($array_in)
  31. {
  32. $array = array();
  33. if (is_object($array_in)) {
  34. return self::objectToArray(get_object_vars($array_in));
  35. } else {
  36. if (is_array($array_in)) {
  37. foreach ($array_in as $key => $value) {
  38. if (is_object($value)) {
  39. $array[$key] = self::objectToArray($value);
  40. } elseif (is_array($value)) {
  41. $array[$key] = self::objectToArray($value);
  42. } else {
  43. $array[$key] = $value;
  44. }
  45. }
  46. }
  47. }
  48. return $array;
  49. }
  50. private function runMsSql()
  51. {
  52. if (isset($this->array['dbType']))
  53. $this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
  54. if (isset($this->array['autoIncrement']))
  55. $this->string .= ($this->array['autoIncrement']) ? ' IDENTITY' : '';
  56. if (isset($this->array['allowNull']))
  57. $this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
  58. if (isset($this->array['defaultValue']))
  59. $this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
  60. }
  61. private function runMySql()
  62. {
  63. if (isset($this->array['dbType'])) {
  64. if (strpos($this->array['dbType'], 'enum') !== FALSE) {
  65. $this->array['dbType'] = str_replace('enum', 'ENUM', $this->array['dbType']);
  66. $this->array['dbType'] = str_replace("'", "\\'", $this->array['dbType']);
  67. $this->string .= $this->Tab . "'{$this->array['name']}' => '" . $this->array['dbType'] . "";
  68. } elseif (strpos($this->array['dbType'], 'set') !== FALSE) {
  69. $this->array['dbType'] = str_replace('set', 'SET', $this->array['dbType']);
  70. $this->array['dbType'] = str_replace("'", "\\'", $this->array['dbType']);
  71. $this->string .= $this->Tab . "'{$this->array['name']}' => '" . $this->array['dbType'] . "";
  72. } else {
  73. $this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
  74. }
  75. }
  76. if (isset($this->array['allowNull']))
  77. $this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
  78. if (isset($this->array['autoIncrement']))
  79. $this->string .= ($this->array['autoIncrement']) ? ' AUTO_INCREMENT' : '';
  80. if (isset($this->array['defaultValue']))
  81. if (!is_array($this->array['defaultValue'])) {
  82. //0 is int
  83. if(is_int($this->array['defaultValue'])||!empty($this->array['defaultValue']))
  84. {
  85. $this->string .= " DEFAULT \'{$this->array['defaultValue']}\'";
  86. }
  87. } else {
  88. $this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT " . $this->array['defaultValue']['expression'] . " ";
  89. }
  90. if (isset($this->array['comment']))
  91. $this->string .= (empty($this->array['comment'])) ? '' : " COMMENT \'{$this->array['comment']}\'";
  92. }
  93. private function runSqlite()
  94. {
  95. if (isset($this->array['dbType']))
  96. $this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
  97. if (isset($this->array['allowNull']))
  98. $this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
  99. if (isset($this->array['autoIncrement']))
  100. $this->string .= ($this->array['autoIncrement']) ? ' AUTOINCREMENT' : '';
  101. if (isset($this->array['defaultValue']))
  102. $this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
  103. }
  104. private function runPgSql()
  105. {
  106. if (isset($this->array['dbType']))
  107. $this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
  108. if (isset($this->array['autoIncrement']))
  109. $this->string .= ($this->array['autoIncrement']) ? ' SERIAL' : '';
  110. if (isset($this->array['allowNull']))
  111. $this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
  112. if (isset($this->array['defaultValue']))
  113. $this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
  114. }
  115. }