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 />';
42 * @global bool $wp_only_load_config
43 * @name $wp_only_load_config
47 $wp_only_load_config = true;
49 /** Load WordPress Bootstrap */
50 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
53 * Turn debugging on or off.
54 * @global bool|int $debug
61 if ( ! function_exists('maybe_create_table') ) :
63 * Create database table, if it doesn't already exist.
70 * @param string $table_name Database table name.
71 * @param string $create_ddl Create database table SQL.
72 * @return bool False on error, true if already exists or success.
74 function maybe_create_table($table_name, $create_ddl) {
76 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
77 if ($table == $table_name) {
81 //didn't find it try to create it.
82 $wpdb->query($create_ddl);
83 // we cannot directly tell that whether this succeeded!
84 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
85 if ($table == $table_name) {
93 if ( ! function_exists('maybe_add_column') ) :
95 * Add column to database table, if column doesn't already exist in table.
103 * @param string $table_name Database table name
104 * @param string $column_name Table column name
105 * @param string $create_ddl SQL to add column to table.
106 * @return bool False on failure. True, if already exists or was successful.
108 function maybe_add_column($table_name, $column_name, $create_ddl) {
109 global $wpdb, $debug;
110 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
111 if ($debug) echo("checking $column == $column_name<br />");
113 if ($column == $column_name) {
117 //didn't find it try to create it.
118 $wpdb->query($create_ddl);
119 // we cannot directly tell that whether this succeeded!
120 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
121 if ($column == $column_name) {
130 * Drop column from database table, if it exists.
137 * @param string $table_name Table name
138 * @param string $column_name Column name
139 * @param string $drop_ddl SQL statement to drop column.
140 * @return bool False on failure, true on success or doesn't exist.
142 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
144 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
145 if ($column == $column_name) {
146 //found it try to drop it.
147 $wpdb->query($drop_ddl);
148 // we cannot directly tell that whether this succeeded!
149 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
150 if ($column == $column_name) {
156 // else didn't find it
161 * Check column matches criteria.
163 * Uses the SQL DESC for retrieving the table info for the column. It will help
164 * understand the parameters, if you do more research on what column information
165 * is returned by the SQL statement. Pass in null to skip checking that
168 * Column names returned from DESC table are case sensitive and are listed:
180 * @param string $table_name Table name
181 * @param string $col_name Column name
182 * @param string $col_type Column type
183 * @param bool $is_null Optional. Check is null.
184 * @param mixed $key Optional. Key info.
185 * @param mixed $default Optional. Default value.
186 * @param mixed $extra Optional. Extra value.
187 * @return bool True, if matches. False, if not matching.
189 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
190 global $wpdb, $debug;
192 $results = $wpdb->get_results("DESC $table_name");
194 foreach ($results as $row ) {
195 if ($debug > 1) print_r($row);
197 if ($row->Field == $col_name) {
198 // got our column, check the params
199 if ($debug) echo ("checking $row->Type against $col_type\n");
200 if (($col_type != null) && ($row->Type != $col_type)) {
203 if (($is_null != null) && ($row->Null != $is_null)) {
206 if (($key != null) && ($row->Key != $key)) {
209 if (($default != null) && ($row->Default != $default)) {
212 if (($extra != null) && ($row->Extra != $extra)) {
216 if ($debug) echo ("diffs = $diffs returning false\n");
220 } // end if found our column