人人商城

db.func.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * [淘淘源码铺微擎系统 System] Copyright (c) 2018 www.330code.com
  4. */
  5. defined('IN_IA') or exit('Access Denied');
  6. define('HTTP_X_FOR', (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://');
  7. $GLOBALS['_W']['config']['db']['tablepre'] = empty($GLOBALS['_W']['config']['db']['tablepre']) ? $GLOBALS['_W']['config']['db']['master']['tablepre'] : $GLOBALS['_W']['config']['db']['tablepre'];
  8. function db_table_schema($db, $tablename = '') {
  9. $result = $db->fetch("SHOW TABLE STATUS LIKE '" . trim($db->tablename($tablename), '`') . "'");
  10. if(empty($result) || empty($result['Create_time'])) {
  11. return array();
  12. }
  13. $ret['tablename'] = $result['Name'];
  14. $ret['charset'] = $result['Collation'];
  15. $ret['engine'] = $result['Engine'];
  16. $ret['increment'] = $result['Auto_increment'];
  17. $result = $db->fetchall("SHOW FULL COLUMNS FROM " . $db->tablename($tablename));
  18. foreach($result as $value) {
  19. $temp = array();
  20. $type = explode(" ", $value['Type'], 2);
  21. $temp['name'] = $value['Field'];
  22. $pieces = explode('(', $type[0], 2);
  23. $temp['type'] = $pieces[0];
  24. $temp['length'] = rtrim($pieces[1], ')');
  25. $temp['null'] = $value['Null'] != 'NO';
  26. $temp['signed'] = empty($type[1]);
  27. $temp['increment'] = $value['Extra'] == 'auto_increment';
  28. $ret['fields'][$value['Field']] = $temp;
  29. }
  30. $result = $db->fetchall("SHOW INDEX FROM " . $db->tablename($tablename));
  31. foreach($result as $value) {
  32. $ret['indexes'][$value['Key_name']]['name'] = $value['Key_name'];
  33. $ret['indexes'][$value['Key_name']]['type'] = ($value['Key_name'] == 'PRIMARY') ? 'primary' : ($value['Non_unique'] == 0 ? 'unique' : 'index');
  34. $ret['indexes'][$value['Key_name']]['fields'][] = $value['Column_name'];
  35. }
  36. return $ret;
  37. }
  38. function db_table_serialize($db, $dbname) {
  39. $tables = $db->fetchall('SHOW TABLES');
  40. if (empty($tables)) {
  41. return '';
  42. }
  43. $struct = array();
  44. foreach ($tables as $value) {
  45. $structs[] = db_table_schema($db, substr($value['Tables_in_' . $dbname], strpos($value['Tables_in_' . $dbname], '_') + 1));
  46. }
  47. return iserializer($structs);
  48. }
  49. function db_table_create_sql($schema) {
  50. $pieces = explode('_', $schema['charset']);
  51. $charset = $pieces[0];
  52. $engine = $schema['engine'];
  53. $schema['tablename'] = str_replace('ims_', $GLOBALS['_W']['config']['db']['tablepre'], $schema['tablename']);
  54. $sql = "CREATE TABLE IF NOT EXISTS `{$schema['tablename']}` (\n";
  55. foreach ($schema['fields'] as $value) {
  56. $piece = _db_build_field_sql($value);
  57. $sql .= "`{$value['name']}` {$piece},\n";
  58. }
  59. foreach ($schema['indexes'] as $value) {
  60. $fields = implode('`,`', $value['fields']);
  61. if($value['type'] == 'index') {
  62. $sql .= "KEY `{$value['name']}` (`{$fields}`),\n";
  63. }
  64. if($value['type'] == 'unique') {
  65. $sql .= "UNIQUE KEY `{$value['name']}` (`{$fields}`),\n";
  66. }
  67. if($value['type'] == 'primary') {
  68. $sql .= "PRIMARY KEY (`{$fields}`),\n";
  69. }
  70. }
  71. $sql = rtrim($sql);
  72. $sql = rtrim($sql, ',');
  73. $sql .= "\n) ENGINE=$engine DEFAULT CHARSET=$charset;\n\n";
  74. return $sql;
  75. }
  76. function db_schema_compare($table1, $table2) {
  77. $table1['charset'] == $table2['charset'] ? '' : $ret['diffs']['charset'] = true;
  78. $fields1 = array_keys($table1['fields']);
  79. $fields2 = array_keys($table2['fields']);
  80. $diffs = array_diff($fields1, $fields2);
  81. if(!empty($diffs)) {
  82. $ret['fields']['greater'] = array_values($diffs);
  83. }
  84. $diffs = array_diff($fields2, $fields1);
  85. if(!empty($diffs)) {
  86. $ret['fields']['less'] = array_values($diffs);
  87. }
  88. $diffs = array();
  89. $intersects = array_intersect($fields1, $fields2);
  90. if(!empty($intersects)) {
  91. foreach($intersects as $field) {
  92. if($table1['fields'][$field] != $table2['fields'][$field]) {
  93. $diffs[] = $field;
  94. }
  95. }
  96. }
  97. if(!empty($diffs)) {
  98. $ret['fields']['diff'] = array_values($diffs);
  99. }
  100. $indexes1 = array_keys($table1['indexes']);
  101. $indexes2 = array_keys($table2['indexes']);
  102. $diffs = array_diff($indexes1, $indexes2);
  103. if(!empty($diffs)) {
  104. $ret['indexes']['greater'] = array_values($diffs);
  105. }
  106. $diffs = array_diff($indexes2, $indexes1);
  107. if(!empty($diffs)) {
  108. $ret['indexes']['less'] = array_values($diffs);
  109. }
  110. $diffs = array();
  111. $intersects = array_intersect($indexes1, $indexes2);
  112. if(!empty($intersects)) {
  113. foreach($intersects as $index) {
  114. if($table1['indexes'][$index] != $table2['indexes'][$index]) {
  115. $diffs[] = $index;
  116. }
  117. }
  118. }
  119. if(!empty($diffs)) {
  120. $ret['indexes']['diff'] = array_values($diffs);
  121. }
  122. return $ret;
  123. }
  124. function db_table_fix_sql($schema1, $schema2, $strict = false) {
  125. if(empty($schema1)) {
  126. return array(db_table_create_sql($schema2));
  127. }
  128. $diff = $result = db_schema_compare($schema1, $schema2);
  129. if(!empty($diff['diffs']['tablename'])) {
  130. return array(db_table_create_sql($schema2));
  131. }
  132. $sqls = array();
  133. if(!empty($diff['diffs']['engine'])) {
  134. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` ENGINE = {$schema2['engine']}";
  135. }
  136. if(!empty($diff['diffs']['charset'])) {
  137. $pieces = explode('_', $schema2['charset']);
  138. $charset = $pieces[0];
  139. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` DEFAULT CHARSET = {$charset}";
  140. }
  141. if(!empty($diff['fields'])) {
  142. if(!empty($diff['fields']['less'])) {
  143. foreach($diff['fields']['less'] as $fieldname) {
  144. $field = $schema2['fields'][$fieldname];
  145. $piece = _db_build_field_sql($field);
  146. if(!empty($field['rename']) && !empty($schema1['fields'][$field['rename']])) {
  147. $sql = "ALTER TABLE `{$schema1['tablename']}` CHANGE `{$field['rename']}` `{$field['name']}` {$piece}";
  148. unset($schema1['fields'][$field['rename']]);
  149. } else {
  150. if($field['position']) {
  151. $pos = ' ' . $field['position'];
  152. }
  153. $sql = "ALTER TABLE `{$schema1['tablename']}` ADD `{$field['name']}` {$piece}{$pos}";
  154. }
  155. $primary = array();
  156. $isincrement = array();
  157. if (strexists($sql, 'AUTO_INCREMENT')) {
  158. $isincrement = $field;
  159. $sql = str_replace('AUTO_INCREMENT', '', $sql);
  160. foreach ($schema1['fields'] as $field) {
  161. if ($field['increment'] == 1) {
  162. $primary = $field;
  163. break;
  164. }
  165. }
  166. if (!empty($primary)) {
  167. $piece = _db_build_field_sql($primary);
  168. if (!empty($piece)) {
  169. $piece = str_replace('AUTO_INCREMENT', '', $piece);
  170. }
  171. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` CHANGE `{$primary['name']}` `{$primary['name']}` {$piece}";
  172. }
  173. }
  174. $sqls[] = $sql;
  175. }
  176. }
  177. if(!empty($diff['fields']['diff'])) {
  178. foreach($diff['fields']['diff'] as $fieldname) {
  179. $field = $schema2['fields'][$fieldname];
  180. $piece = _db_build_field_sql($field);
  181. if(!empty($schema1['fields'][$fieldname])) {
  182. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` CHANGE `{$field['name']}` `{$field['name']}` {$piece}";
  183. }
  184. }
  185. }
  186. if($strict && !empty($diff['fields']['greater'])) {
  187. foreach($diff['fields']['greater'] as $fieldname) {
  188. if(!empty($schema1['fields'][$fieldname])) {
  189. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` DROP `{$fieldname}`";
  190. }
  191. }
  192. }
  193. }
  194. if(!empty($diff['indexes'])) {
  195. if(!empty($diff['indexes']['less'])) {
  196. foreach($diff['indexes']['less'] as $indexname) {
  197. $index = $schema2['indexes'][$indexname];
  198. $piece = _db_build_index_sql($index);
  199. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` ADD {$piece}";
  200. }
  201. }
  202. if(!empty($diff['indexes']['diff'])) {
  203. foreach($diff['indexes']['diff'] as $indexname) {
  204. $index = $schema2['indexes'][$indexname];
  205. $piece = _db_build_index_sql($index);
  206. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` DROP ".($indexname == 'PRIMARY' ? " PRIMARY KEY " : "INDEX {$indexname}").", ADD {$piece}";
  207. }
  208. }
  209. if($strict && !empty($diff['indexes']['greater'])) {
  210. foreach($diff['indexes']['greater'] as $indexname) {
  211. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` DROP `{$indexname}`";
  212. }
  213. }
  214. }
  215. if (!empty($isincrement)) {
  216. $piece = _db_build_field_sql($isincrement);
  217. $sqls[] = "ALTER TABLE `{$schema1['tablename']}` CHANGE `{$isincrement['name']}` `{$isincrement['name']}` {$piece}";
  218. }
  219. return $sqls;
  220. }
  221. function _db_build_index_sql($index) {
  222. $piece = '';
  223. $fields = implode('`,`', $index['fields']);
  224. if($index['type'] == 'index') {
  225. $piece .= " INDEX `{$index['name']}` (`{$fields}`)";
  226. }
  227. if($index['type'] == 'unique') {
  228. $piece .= "UNIQUE `{$index['name']}` (`{$fields}`)";
  229. }
  230. if($index['type'] == 'primary') {
  231. $piece .= "PRIMARY KEY (`{$fields}`)";
  232. }
  233. return $piece;
  234. }
  235. function _db_build_field_sql($field) {
  236. if(!empty($field['length'])) {
  237. $length = "({$field['length']})";
  238. } else {
  239. $length = '';
  240. }
  241. if (strpos(strtolower($field['type']), 'int') !== false) {
  242. $signed = empty($field['signed']) ? ' unsigned' : '';
  243. } else {
  244. $signed = '';
  245. }
  246. if(empty($field['null'])) {
  247. $null = ' NOT NULL';
  248. } else {
  249. $null = '';
  250. }
  251. if(isset($field['default'])) {
  252. $default = " DEFAULT '" . $field['default'] . "'";
  253. } else {
  254. $default = '';
  255. }
  256. if($field['increment']) {
  257. $increment = ' AUTO_INCREMENT';
  258. } else {
  259. $increment = '';
  260. }
  261. return "{$field['type']}{$length}{$signed}{$null}{$default}{$increment}";
  262. }
  263. function db_table_schemas($table) {
  264. $dump = "DROP TABLE IF EXISTS {$table};\n";
  265. $sql = "SHOW CREATE TABLE {$table}";
  266. $row = pdo_fetch($sql);
  267. $dump .= $row['Create Table'];
  268. $dump .= ";\n\n";
  269. return $dump;
  270. }
  271. function db_table_insert_sql($tablename, $start, $size) {
  272. $data = '';
  273. $tmp = '';
  274. $sql = "SELECT * FROM {$tablename} LIMIT {$start}, {$size}";
  275. $result = pdo_fetchall($sql);
  276. if (!empty($result)) {
  277. foreach($result as $row) {
  278. $tmp .= '(';
  279. foreach($row as $k => $v) {
  280. $value = str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $v);
  281. $tmp .= "'" . $value . "',";
  282. }
  283. $tmp = rtrim($tmp, ',');
  284. $tmp .= "),\n";
  285. }
  286. $tmp = rtrim($tmp, ",\n");
  287. $data .= "INSERT INTO {$tablename} VALUES \n{$tmp};\n";
  288. $datas = array (
  289. 'data' => $data,
  290. 'result' => $result
  291. );
  292. return $datas;
  293. } else {
  294. return false ;
  295. }
  296. }