| 1 |
<?php |
|---|
| 2 |
require_once('../wp-config.php'); |
|---|
| 3 |
$debug = 0; |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
function maybe_create_table($table_name, $create_ddl) { |
|---|
| 12 |
global $wpdb; |
|---|
| 13 |
foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|---|
| 14 |
if ($table == $table_name) { |
|---|
| 15 |
return true; |
|---|
| 16 |
} |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
$q = $wpdb->query($create_ddl); |
|---|
| 20 |
|
|---|
| 21 |
foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|---|
| 22 |
if ($table == $table_name) { |
|---|
| 23 |
return true; |
|---|
| 24 |
} |
|---|
| 25 |
} |
|---|
| 26 |
return false; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
function maybe_add_column($table_name, $column_name, $create_ddl) { |
|---|
| 36 |
global $wpdb, $debug; |
|---|
| 37 |
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
|---|
| 38 |
if ($debug) echo("checking $column == $column_name<br />"); |
|---|
| 39 |
if ($column == $column_name) { |
|---|
| 40 |
return true; |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
$q = $wpdb->query($create_ddl); |
|---|
| 45 |
|
|---|
| 46 |
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
|---|
| 47 |
if ($column == $column_name) { |
|---|
| 48 |
return true; |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
return false; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
function maybe_drop_column($table_name, $column_name, $drop_ddl) { |
|---|
| 62 |
global $wpdb; |
|---|
| 63 |
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
|---|
| 64 |
if ($column == $column_name) { |
|---|
| 65 |
|
|---|
| 66 |
$q = $wpdb->query($drop_ddl); |
|---|
| 67 |
|
|---|
| 68 |
foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
|---|
| 69 |
if ($column == $column_name) { |
|---|
| 70 |
return false; |
|---|
| 71 |
} |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
return true; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { |
|---|
| 95 |
global $wpdb, $debug; |
|---|
| 96 |
$diffs = 0; |
|---|
| 97 |
$results = $wpdb->get_results("DESC $table_name"); |
|---|
| 98 |
|
|---|
| 99 |
foreach ($results as $row ) { |
|---|
| 100 |
if ($debug > 1) print_r($row); |
|---|
| 101 |
if ($row->Field == $col_name) { |
|---|
| 102 |
|
|---|
| 103 |
if ($debug) echo ("checking $row->Type against $col_type\n"); |
|---|
| 104 |
if (($col_type != null) && ($row->Type != $col_type)) { |
|---|
| 105 |
++$diffs; |
|---|
| 106 |
} |
|---|
| 107 |
if (($is_null != null) && ($row->Null != $is_null)) { |
|---|
| 108 |
++$diffs; |
|---|
| 109 |
} |
|---|
| 110 |
if (($key != null) && ($row->Key != $key)) { |
|---|
| 111 |
++$diffs; |
|---|
| 112 |
} |
|---|
| 113 |
if (($default != null) && ($row->Default != $default)) { |
|---|
| 114 |
++$diffs; |
|---|
| 115 |
} |
|---|
| 116 |
if (($extra != null) && ($row->Extra != $extra)) { |
|---|
| 117 |
++$diffs; |
|---|
| 118 |
} |
|---|
| 119 |
if ($diffs > 0) { |
|---|
| 120 |
if ($debug) echo ("diffs = $diffs returning false\n"); |
|---|
| 121 |
return false; |
|---|
| 122 |
} |
|---|
| 123 |
return true; |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
return false; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
?> |
|---|
| 153 |
|
|---|