Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. <?
  2. include "common.inc.php";
  3. include "header.inc.php";
  4. ?>
  5. <h2>MySQL Strict Mode Column Compatiblility</h2>
  6. <?
  7. $r_resultTables = dbQuery("SHOW TABLES");
  8.  
  9. echo '<textarea style="width: 95%; height: 80vh;">';
  10. while ($a_rowTable = dbFetchRow($r_resultTables)) {
  11. $table = dbEscape($a_rowTable[0]);
  12.  
  13. $tableNameAdded = false;
  14.  
  15. $r_result = dbQuery("SHOW FULL FIELDS FROM `$table`");
  16. while ($a_row = dbFetchAssoc($r_result)) {
  17.  
  18. $comment = null;
  19. $default = (($a_row['Default'] != NULL) ? "DEFAULT '" . $a_row['Default'] . "'" : '');
  20. $extra = null;
  21.  
  22. $type = strtok($a_row['Type'],'(');
  23.  
  24. $field = dbEscape($a_row['Field']);
  25.  
  26. // Is the field allowed to be null?
  27. if ($a_row['Null'] == 'YES') {
  28. $nullable = 'NULL';
  29. } else {
  30. $nullable = 'NOT NULL';
  31. }
  32.  
  33. // skip nullable fields with no set default (they will default to NULL)
  34. if (($a_row['Default'] === NULL) AND ($a_row['Null'] == 'YES')) {
  35. continue;
  36. }
  37.  
  38. // skip PK fields
  39. if ($a_row['Key'] == 'PRI') { continue; }
  40.  
  41. // fields that need a set default if they don't have one
  42. if ($a_row['Default'] === NULL) {
  43. switch ($type) {
  44. case 'integer':
  45. case 'int':
  46. case 'smallint':
  47. case 'tinyint':
  48. case 'mediumint':
  49. case 'bigint':
  50. case 'decimal':
  51. case 'numeric':
  52. case 'float':
  53. case 'double':
  54. $comment = "Numeric field needs '0' as default";
  55. $default = "DEFAULT '0'";
  56. break;
  57. case 'char':
  58. case 'varchar':
  59. $comment = "(VAR)CHAR fields need empty string as default";
  60. $default = "DEFAULT ''";
  61. break;
  62. }
  63. }
  64.  
  65. // fields that need changing anyway if they're not already nullable, or can be skipped
  66. switch ($type) {
  67. case 'datetime':
  68. case 'date':
  69. case 'timestamp':
  70. case 'year':
  71. case 'time':
  72. if (substr($a_row['Default'],0,2) == '00') {
  73. $comment = "Date/Time types cannot default to zeroes, must be NULLable";
  74. $extra = "UPDATE `$table` SET `$field` = NULL WHERE `$field` = '" . $a_row['Default'] . "';";
  75. $default = "DEFAULT NULL";
  76. } elseif ($a_row['Default'] == 'CURRENT_TIMESTAMP') {
  77. continue 2;
  78. } else {
  79. $comment = "Date/Time types should be NULLable";
  80. }
  81. $nullable = 'NULL';
  82. break;
  83. case 'enum':
  84. case 'set':
  85. continue 2; // ENUM and SET types don't need any change
  86. break;
  87. case 'tinytext':
  88. case 'text':
  89. case 'mediumtext':
  90. case 'longtext':
  91. continue 2; // TEXT types cannot have a default value
  92. break;
  93. default:
  94. if ($a_row['Default'] !== NULL) { continue 2; } // skip any other types that already have defaults
  95. break;
  96. }
  97.  
  98. if (!$tableNameAdded) {
  99. echo "\r\n# `$table`\r\n\r\n";
  100. $tableNameAdded = true;
  101. }
  102.  
  103. // Alter table query
  104. if ($comment) { echo '# `' . $field . '` - ' . $comment . "\r\n"; } else { echo "# UNKNOWN\r\n" . '# '; }
  105. echo "ALTER TABLE `$table` CHANGE `$field` `$field` $a_row[Type] $nullable $default;\r\n";
  106. if ($extra) { echo $extra . "\r\n"; }
  107. echo "\r\n";
  108. }
  109. }
  110. echo '</textarea>';
  111.  
  112. include "footer.inc.php";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement