3 * Plugins may load this file to gain access to special helper functions for
4 * plugin installation. This file is not included by WordPress and it is
5 * recommended, to prevent fatal errors, that this file is included using
8 * These functions are not optimized for speed, but they should only be used
9 * once in a while, so speed shouldn't be a concern. If it is and you are
10 * needing to use these functions a lot, you might experience time outs. If you
11 * do, then it is advised to just write the SQL code yourself.
13 * You can turn debugging on, by setting $debug to 1 after you include this
17 * check_column('wp_links', 'link_description', 'mediumtext');
18 * if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
22 * $tablename = $wpdb->links;
24 * if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) {
25 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
26 * $q = $wpdb->query($ddl);
29 * if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
30 * $res .= $tablename . ' - ok <br />';
32 * $res .= 'There was a problem with ' . $tablename . '<br />';
41 /** Load WordPress Bootstrap */
42 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
45 * Turn debugging on or off.
46 * @global bool|int $debug
53 if ( ! function_exists('maybe_create_table') ) :
55 * Create database table, if it doesn't already exist.
62 * @param string $table_name Database table name.
63 * @param string $create_ddl Create database table SQL.
64 * @return bool False on error, true if already exists or success.
66 function maybe_create_table($table_name, $create_ddl) {
68 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
69 if ($table == $table_name) {
73 //didn't find it try to create it.
74 $wpdb->query($create_ddl);
75 // we cannot directly tell that whether this succeeded!
76 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
77 if ($table == $table_name) {
85 if ( ! function_exists('maybe_add_column') ) :
87 * Add column to database table, if column doesn't already exist in table.
95 * @param string $table_name Database table name
96 * @param string $column_name Table column name
97 * @param string $create_ddl SQL to add column to table.
98 * @return bool False on failure. True, if already exists or was successful.
100 function maybe_add_column($table_name, $column_name, $create_ddl) {
101 global $wpdb, $debug;
102 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
103 if ($debug) echo("checking $column == $column_name<br />");
105 if ($column == $column_name) {
109 //didn't find it try to create it.
110 $wpdb->query($create_ddl);
111 // we cannot directly tell that whether this succeeded!
112 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
113 if ($column == $column_name) {
122 * Drop column from database table, if it exists.
129 * @param string $table_name Table name
130 * @param string $column_name Column name
131 * @param string $drop_ddl SQL statement to drop column.
132 * @return bool False on failure, true on success or doesn't exist.
134 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
136 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
137 if ($column == $column_name) {
138 //found it try to drop it.
139 $wpdb->query($drop_ddl);
140 // we cannot directly tell that whether this succeeded!
141 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
142 if ($column == $column_name) {
148 // else didn't find it
153 * Check column matches criteria.
155 * Uses the SQL DESC for retrieving the table info for the column. It will help
156 * understand the parameters, if you do more research on what column information
157 * is returned by the SQL statement. Pass in null to skip checking that
160 * Column names returned from DESC table are case sensitive and are listed:
172 * @param string $table_name Table name
173 * @param string $col_name Column name
174 * @param string $col_type Column type
175 * @param bool $is_null Optional. Check is null.
176 * @param mixed $key Optional. Key info.
177 * @param mixed $default Optional. Default value.
178 * @param mixed $extra Optional. Extra value.
179 * @return bool True, if matches. False, if not matching.
181 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
182 global $wpdb, $debug;
184 $results = $wpdb->get_results("DESC $table_name");
186 foreach ($results as $row ) {
187 if ($debug > 1) print_r($row);
189 if ($row->Field == $col_name) {
190 // got our column, check the params
191 if ($debug) echo ("checking $row->Type against $col_type\n");
192 if (($col_type != null) && ($row->Type != $col_type)) {
195 if (($is_null != null) && ($row->Null != $is_null)) {
198 if (($key != null) && ($row->Key != $key)) {
201 if (($default != null) && ($row->Default != $default)) {
204 if (($extra != null) && ($row->Extra != $extra)) {
208 if ($debug) echo ("diffs = $diffs returning false\n");
212 } // end if found our column