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.
 
 
 
 
 
 

170 lines
4.9 KiB

  1. <?php
  2. /**
  3. * 根据配置自动生成SQL建表语句
  4. *
  5. * @author dogstar <chanzonghuang@gmail.com> 2015-02-04
  6. */
  7. define('CUR_PATH', dirname(__FILE__));
  8. if ($argc < 3) {
  9. echo "\n";
  10. echo colorfulString("Usage:\n", 'WARNING');
  11. echo " $argv[0] <dbs_config> <table> [engine]\n";
  12. echo "\n";
  13. echo colorfulString("Options:\n", 'WARNING');
  14. echo colorfulString(' dbs_config', 'NOTE'), " Require. Path to ./Config/dbs.php\n";
  15. echo colorfulString(' table', 'NOTE'), " Require. Table name\n";
  16. echo colorfulString(' engine', 'NOTE'), " NOT require. Database engine, default is Innodb\n";
  17. echo "\n";
  18. echo colorfulString("Demo:\n", 'WARNING');
  19. echo " php ./build_sqls.php ../Config/dbs.php User\n";
  20. echo "\n";
  21. echo colorfulString("Tips:\n", 'WARNING');
  22. echo " This will output the sql directly, enjoy yourself!\n";
  23. echo "\n";
  24. //echo "\n", implode("\n", array_keys($dbsConfig['tables'])), "\n\n";
  25. exit(1);
  26. }
  27. $dbsConfigFile = trim($argv[1]);
  28. $tableName = trim($argv[2]);
  29. $engine = isset($argv[3]) ? $argv[3] : 'InnoDB';
  30. if (!file_exists($dbsConfigFile)) {
  31. echo colorfulString("Error: file $dbsConfigFile not exists!\n\n", 'FAILURE');
  32. exit();
  33. }
  34. $dbsConfig = include($dbsConfigFile);
  35. if (empty($dbsConfig) || empty($dbsConfig['servers']) || empty($dbsConfig['tables'])
  36. || !is_array($dbsConfig['servers']) || !is_array($dbsConfig['tables'])) {
  37. echo colorfulString("Error: db config is incorrect, it should be format as:
  38. <?php
  39. return array(
  40. /**
  41. * avaiable db servers
  42. */
  43. 'servers' => array(
  44. 'db_X' => array(
  45. 'host' => 'localhost', //数据库域名
  46. 'name' => 'phalapi', //数据库名字
  47. 'user' => 'root', //数据库用户名
  48. 'password' => '', //数据库密码
  49. 'port' => '3306', //数据库端口
  50. 'charset' => 'UTF8', //数据库字符集
  51. ),
  52. ),
  53. /**
  54. * custom table map
  55. */
  56. 'tables' => array(
  57. 'demo' => array(
  58. 'prefix' => 'weili_',
  59. 'key' => 'id',
  60. 'map' => array(
  61. array('start' => 0, 'end' => 2, 'db' => 'db_X'),
  62. ),
  63. ),
  64. ),
  65. );
  66. ", 'FAILURE');
  67. exit();
  68. }
  69. $tableMap = isset($dbsConfig['tables'][$tableName]) ? $dbsConfig['tables'][$tableName] : $dbsConfig['tables']['__default__'];
  70. if (empty($tableMap)) {
  71. echo colorfulString("Error: no table map for $tableName !\n\n", 'FAILURE');
  72. exit();
  73. }
  74. $tableMap['prefix'] = isset($tableMap['prefix']) ? trim($tableMap['prefix']) : '';
  75. $tableMap['key'] = isset($tableMap['key']) ? trim($tableMap['key']) : 'id';
  76. $tableMap['map'] = isset($tableMap['map']) ? $tableMap['map'] : array();
  77. if (empty($tableMap['map'])) {
  78. echo colorfulString("Error: miss map for table $tableName !\n\n", 'FAILURE');
  79. exit();
  80. }
  81. $sqlFilePath = CUR_PATH . '/../Data/' . $tableName . '.sql';
  82. if (!file_exists($sqlFilePath)) {
  83. echo colorfulString("Error: sql file $sqlFilePath not exists!\n\n", 'FAILURE');
  84. exit();
  85. }
  86. $sqlContent = file_get_contents($sqlFilePath);
  87. $sqlContent = trim($sqlContent);
  88. $outputSql = '';
  89. foreach ($tableMap['map'] as $mapItem) {
  90. $dbName = isset($mapItem['db']) ? $mapItem['db'] : 'db';
  91. if (!isset($dbsConfig['servers'][$dbName])) {
  92. echo colorfulString("Error: no such db server as db = $dbName !\n\n", 'FAILURE');
  93. exit();
  94. }
  95. $outputSql .= "
  96. /**
  97. * DB: {$dbsConfig['servers'][$dbName]['host']} {$dbsConfig['servers'][$dbName]['name']}
  98. */
  99. ";
  100. $charset = isset($dbsConfig['servers'][$dbName]['charset'])
  101. ? $dbsConfig['servers'][$dbName]['charset'] : 'utf8';
  102. if (isset($mapItem['start']) && isset($mapItem['end'])) {
  103. for ($i = $mapItem['start']; $i <= $mapItem['end']; $i ++) {
  104. $outputSql .= genSql(
  105. $tableMap['prefix'] . $tableName . '_' . $i,
  106. $tableMap['key'],
  107. $sqlContent,
  108. $engine,
  109. $charset
  110. );
  111. }
  112. } else {
  113. $outputSql .= genSql($tableMap['prefix'] . $tableName, $tableMap['key'], $sqlContent, $engine, $charset);
  114. }
  115. }
  116. echo $outputSql;
  117. function colorfulString($text, $type = NULL) {
  118. $colors = array(
  119. 'WARNING' => '1;33',
  120. 'NOTE' => '1;36',
  121. 'SUCCESS' => '1;32',
  122. 'FAILURE' => '1;35',
  123. );
  124. if (empty($type) || !isset($colors[$type])){
  125. return $text;
  126. }
  127. return "\033[" . $colors[$type] . "m" . $text . "\033[0m";
  128. }
  129. function genSql($tableName, $tableKey, $sqlContent, $engine, $charset) {
  130. return sprintf("
  131. CREATE TABLE `%s` (
  132. `%s` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  133. %s
  134. `ext_data` text COMMENT 'json data here',
  135. PRIMARY KEY (`%s`)
  136. ) ENGINE=%s DEFAULT CHARSET=%s;
  137. ", $tableName, $tableKey, $sqlContent, $tableKey, $engine, $charset);
  138. }