build_sqls.php 5.1 KB

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