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.
14 * check_column('wp_links', 'link_description', 'mediumtext');
15 * if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
19 * $tablename = $wpdb->links;
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 />';
38 /** Load WordPress Bootstrap */
39 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
41 if ( ! function_exists('maybe_create_table') ) :
43 * Create database table, if it doesn't already exist.
50 * @param string $table_name Database table name.
51 * @param string $create_ddl Create database table SQL.
52 * @return bool False on error, true if already exists or success.
54 function maybe_create_table($table_name, $create_ddl) {
56 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
57 if ($table == $table_name) {
61 //didn't find it try to create it.
62 $wpdb->query($create_ddl);
63 // we cannot directly tell that whether this succeeded!
64 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
65 if ($table == $table_name) {
73 if ( ! function_exists('maybe_add_column') ) :
75 * Add column to database table, if column doesn't already exist in table.
82 * @param string $table_name Database table name
83 * @param string $column_name Table column name
84 * @param string $create_ddl SQL to add column to table.
85 * @return bool False on failure. True, if already exists or was successful.
87 function maybe_add_column($table_name, $column_name, $create_ddl) {
89 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
91 if ($column == $column_name) {
95 //didn't find it try to create it.
96 $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.
115 * @param string $table_name Table name
116 * @param string $column_name Column name
117 * @param string $drop_ddl SQL statement to drop column.
118 * @return bool False on failure, true on success or doesn't exist.
120 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
122 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
123 if ($column == $column_name) {
124 //found it try to drop it.
125 $wpdb->query($drop_ddl);
126 // we cannot directly tell that whether this succeeded!
127 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
128 if ($column == $column_name) {
134 // else didn't find it
139 * Check column matches criteria.
141 * Uses the SQL DESC for retrieving the table info for the column. It will help
142 * understand the parameters, if you do more research on what column information
143 * is returned by the SQL statement. Pass in null to skip checking that
146 * Column names returned from DESC table are case sensitive and are listed:
158 * @param string $table_name Table name
159 * @param string $col_name Column name
160 * @param string $col_type Column type
161 * @param bool $is_null Optional. Check is null.
162 * @param mixed $key Optional. Key info.
163 * @param mixed $default Optional. Default value.
164 * @param mixed $extra Optional. Extra value.
165 * @return bool True, if matches. False, if not matching.
167 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
170 $results = $wpdb->get_results("DESC $table_name");
172 foreach ($results as $row ) {
174 if ($row->Field == $col_name) {
175 // got our column, check the params
176 if (($col_type != null) && ($row->Type != $col_type)) {
179 if (($is_null != null) && ($row->Null != $is_null)) {
182 if (($key != null) && ($row->Key != $key)) {
185 if (($default != null) && ($row->Default != $default)) {
188 if (($extra != null) && ($row->Extra != $extra)) {
195 } // end if found our column