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 * check_column( 'wp_links', 'link_description', 'mediumtext' );
14 * if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) {
19 * $tablename = $wpdb->links;
20 * // Check the column.
21 * if ( ! check_column($wpdb->links, 'link_description', 'varchar( 255 )' ) ) {
22 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
23 * $q = $wpdb->query( $ddl );
26 * if ( check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) {
27 * $res .= $tablename . ' - ok <br />';
29 * $res .= 'There was a problem with ' . $tablename . '<br />';
37 /** Load WordPress Bootstrap */
38 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
40 if ( ! function_exists('maybe_create_table') ) :
42 * Create database table, if it doesn't already exist.
46 * @global wpdb $wpdb WordPress database abstraction object.
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.
52 function maybe_create_table($table_name, $create_ddl) {
54 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
55 if ($table == $table_name) {
59 // Didn't find it, so try to create it.
60 $wpdb->query($create_ddl);
62 // We cannot directly tell that whether this succeeded!
63 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
64 if ($table == $table_name) {
72 if ( ! function_exists('maybe_add_column') ) :
74 * Add column to database table, if column doesn't already exist in table.
78 * @global wpdb $wpdb WordPress database abstraction object.
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.
85 function maybe_add_column($table_name, $column_name, $create_ddl) {
87 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
89 if ($column == $column_name) {
94 // Didn't find it, so try to create it.
95 $wpdb->query($create_ddl);
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) {
108 * Drop column from database table, if it exists.
112 * @global wpdb $wpdb WordPress database abstraction object.
114 * @param string $table_name Table name
115 * @param string $column_name Column name
116 * @param string $drop_ddl SQL statement to drop column.
117 * @return bool False on failure, true on success or doesn't exist.
119 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
121 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
122 if ($column == $column_name) {
124 // Found it, so try to drop it.
125 $wpdb->query($drop_ddl);
127 // We cannot directly tell that whether this succeeded!
128 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
129 if ($column == $column_name) {
135 // Else didn't find it.
140 * Check column matches criteria.
142 * Uses the SQL DESC for retrieving the table info for the column. It will help
143 * understand the parameters, if you do more research on what column information
144 * is returned by the SQL statement. Pass in null to skip checking that
147 * Column names returned from DESC table are case sensitive and are listed:
157 * @global wpdb $wpdb WordPress database abstraction object.
159 * @param string $table_name Table name
160 * @param string $col_name Column name
161 * @param string $col_type Column type
162 * @param bool $is_null Optional. Check is null.
163 * @param mixed $key Optional. Key info.
164 * @param mixed $default Optional. Default value.
165 * @param mixed $extra Optional. Extra value.
166 * @return bool True, if matches. False, if not matching.
168 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
171 $results = $wpdb->get_results("DESC $table_name");
173 foreach ($results as $row ) {
175 if ($row->Field == $col_name) {
177 // Got our column, check the params.
178 if (($col_type != null) && ($row->Type != $col_type)) {
181 if (($is_null != null) && ($row->Null != $is_null)) {
184 if (($key != null) && ($row->Key != $key)) {
187 if (($default != null) && ($row->Default != $default)) {
190 if (($extra != null) && ($row->Extra != $extra)) {
197 } // end if found our column