2 require_once(dirname(dirname(__FILE__)).'/wp-config.php');
6 ** maybe_create_table()
7 ** Create db table if it doesn't exist.
8 ** Returns: true if already exists or on successful completion
11 function maybe_create_table($table_name, $create_ddl) {
13 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
14 if ($table == $table_name) {
18 //didn't find it try to create it.
19 $q = $wpdb->query($create_ddl);
20 // we cannot directly tell that whether this succeeded!
21 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
22 if ($table == $table_name) {
31 ** Add column to db table if it doesn't exist.
32 ** Returns: true if already exists or on successful completion
35 function maybe_add_column($table_name, $column_name, $create_ddl) {
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) {
43 //didn't find it try to create it.
44 $q = $wpdb->query($create_ddl);
45 // we cannot directly tell that whether this succeeded!
46 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
47 if ($column == $column_name) {
56 ** maybe_drop_column()
57 ** Drop column from db table if it exists.
58 ** Returns: true if it doesn't already exist or on successful drop
61 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
63 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
64 if ($column == $column_name) {
65 //found it try to drop it.
66 $q = $wpdb->query($drop_ddl);
67 // we cannot directly tell that whether this succeeded!
68 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
69 if ($column == $column_name) {
75 // else didn't find it
82 ** Check column matches passed in criteria.
83 ** Pass in null to skip checking that criteria
84 ** Returns: true if it matches
86 ** (case sensitive) Column names returned from DESC table are:
94 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
97 $results = $wpdb->get_results("DESC $table_name");
99 foreach ($results as $row ) {
100 if ($debug > 1) print_r($row);
101 if ($row->Field == $col_name) {
102 // got our column, check the params
103 if ($debug) echo ("checking $row->Type against $col_type\n");
104 if (($col_type != null) && ($row->Type != $col_type)) {
107 if (($is_null != null) && ($row->Null != $is_null)) {
110 if (($key != null) && ($row->Key != $key)) {
113 if (($default != null) && ($row->Default != $default)) {
116 if (($extra != null) && ($row->Extra != $extra)) {
120 if ($debug) echo ("diffs = $diffs returning false\n");
124 } // end if found our column
130 echo "<p>testing</p>";
133 //check_column('wp_links', 'link_description', 'mediumtext');
134 //if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
137 $tablename = $wpdb->links;
139 if (!check_column($wpdb->links, 'link_description', 'varchar(255)'))
141 $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
142 $q = $wpdb->query($ddl);
144 if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
145 $res .= $tablename . ' - ok <br />';
147 $res .= 'There was a problem with ' . $tablename . '<br />';