| @@ 40-70 (lines=31) @@ | ||
| 37 | /** Load WordPress Bootstrap */ |
|
| 38 | require_once(dirname(dirname(__FILE__)).'/wp-load.php'); |
|
| 39 | ||
| 40 | if ( ! function_exists('maybe_create_table') ) : |
|
| 41 | /** |
|
| 42 | * Create database table, if it doesn't already exist. |
|
| 43 | * |
|
| 44 | * @since 1.0.0 |
|
| 45 | * |
|
| 46 | * @global wpdb $wpdb WordPress database abstraction object. |
|
| 47 | * |
|
| 48 | * @param string $table_name Database table name. |
|
| 49 | * @param string $create_ddl Create database table SQL. |
|
| 50 | * @return bool False on error, true if already exists or success. |
|
| 51 | */ |
|
| 52 | function maybe_create_table($table_name, $create_ddl) { |
|
| 53 | global $wpdb; |
|
| 54 | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|
| 55 | if ($table == $table_name) { |
|
| 56 | return true; |
|
| 57 | } |
|
| 58 | } |
|
| 59 | // Didn't find it, so try to create it. |
|
| 60 | $wpdb->query($create_ddl); |
|
| 61 | ||
| 62 | // We cannot directly tell that whether this succeeded! |
|
| 63 | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
|
| 64 | if ($table == $table_name) { |
|
| 65 | return true; |
|
| 66 | } |
|
| 67 | } |
|
| 68 | return false; |
|
| 69 | } |
|
| 70 | endif; |
|
| 71 | ||
| 72 | if ( ! function_exists('maybe_add_column') ) : |
|
| 73 | /** |
|
| @@ 72-105 (lines=34) @@ | ||
| 69 | } |
|
| 70 | endif; |
|
| 71 | ||
| 72 | if ( ! function_exists('maybe_add_column') ) : |
|
| 73 | /** |
|
| 74 | * Add column to database table, if column doesn't already exist in table. |
|
| 75 | * |
|
| 76 | * @since 1.0.0 |
|
| 77 | * |
|
| 78 | * @global wpdb $wpdb WordPress database abstraction object. |
|
| 79 | * |
|
| 80 | * @param string $table_name Database table name |
|
| 81 | * @param string $column_name Table column name |
|
| 82 | * @param string $create_ddl SQL to add column to table. |
|
| 83 | * @return bool False on failure. True, if already exists or was successful. |
|
| 84 | */ |
|
| 85 | function maybe_add_column($table_name, $column_name, $create_ddl) { |
|
| 86 | global $wpdb; |
|
| 87 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
|
| 88 | ||
| 89 | if ($column == $column_name) { |
|
| 90 | return true; |
|
| 91 | } |
|
| 92 | } |
|
| 93 | ||
| 94 | // Didn't find it, so try to create it. |
|
| 95 | $wpdb->query($create_ddl); |
|
| 96 | ||
| 97 | // We cannot directly tell that whether this succeeded! |
|
| 98 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
|
| 99 | if ($column == $column_name) { |
|
| 100 | return true; |
|
| 101 | } |
|
| 102 | } |
|
| 103 | return false; |
|
| 104 | } |
|
| 105 | endif; |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Drop column from database table, if it exists. |
|