7 * |
7 * |
8 * These functions are not optimized for speed, but they should only be used |
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 |
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 |
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. |
11 * do, then it is advised to just write the SQL code yourself. |
12 * |
|
13 * You can turn debugging on, by setting $debug to 1 after you include this |
|
14 * file. |
|
15 * |
12 * |
16 * <code> |
13 * <code> |
17 * check_column('wp_links', 'link_description', 'mediumtext'); |
14 * check_column('wp_links', 'link_description', 'mediumtext'); |
18 * if (check_column($wpdb->comments, 'comment_author', 'tinytext')) |
15 * if (check_column($wpdb->comments, 'comment_author', 'tinytext')) |
19 * echo "ok\n"; |
16 * echo "ok\n"; |
36 * |
33 * |
37 * @package WordPress |
34 * @package WordPress |
38 * @subpackage Plugin |
35 * @subpackage Plugin |
39 */ |
36 */ |
40 |
37 |
41 /** |
|
42 * @global bool $wp_only_load_config |
|
43 * @name $wp_only_load_config |
|
44 * @var bool |
|
45 * @since unknown |
|
46 */ |
|
47 $wp_only_load_config = true; |
|
48 |
|
49 /** Load WordPress Bootstrap */ |
38 /** Load WordPress Bootstrap */ |
50 require_once(dirname(dirname(__FILE__)).'/wp-load.php'); |
39 require_once(dirname(dirname(__FILE__)).'/wp-load.php'); |
51 |
|
52 /** |
|
53 * Turn debugging on or off. |
|
54 * @global bool|int $debug |
|
55 * @name $debug |
|
56 * @var bool|int |
|
57 * @since unknown |
|
58 */ |
|
59 $debug = 0; |
|
60 |
40 |
61 if ( ! function_exists('maybe_create_table') ) : |
41 if ( ! function_exists('maybe_create_table') ) : |
62 /** |
42 /** |
63 * Create database table, if it doesn't already exist. |
43 * Create database table, if it doesn't already exist. |
64 * |
44 * |
65 * @since unknown |
45 * @since 1.0.0 |
66 * @package WordPress |
46 * @package WordPress |
67 * @subpackage Plugin |
47 * @subpackage Plugin |
68 * @uses $wpdb |
48 * @uses $wpdb |
69 * |
49 * |
70 * @param string $table_name Database table name. |
50 * @param string $table_name Database table name. |
92 |
72 |
93 if ( ! function_exists('maybe_add_column') ) : |
73 if ( ! function_exists('maybe_add_column') ) : |
94 /** |
74 /** |
95 * Add column to database table, if column doesn't already exist in table. |
75 * Add column to database table, if column doesn't already exist in table. |
96 * |
76 * |
97 * @since unknown |
77 * @since 1.0.0 |
98 * @package WordPress |
78 * @package WordPress |
99 * @subpackage Plugin |
79 * @subpackage Plugin |
100 * @uses $wpdb |
80 * @uses $wpdb |
101 * @uses $debug |
|
102 * |
81 * |
103 * @param string $table_name Database table name |
82 * @param string $table_name Database table name |
104 * @param string $column_name Table column name |
83 * @param string $column_name Table column name |
105 * @param string $create_ddl SQL to add column to table. |
84 * @param string $create_ddl SQL to add column to table. |
106 * @return bool False on failure. True, if already exists or was successful. |
85 * @return bool False on failure. True, if already exists or was successful. |
107 */ |
86 */ |
108 function maybe_add_column($table_name, $column_name, $create_ddl) { |
87 function maybe_add_column($table_name, $column_name, $create_ddl) { |
109 global $wpdb, $debug; |
88 global $wpdb; |
110 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
89 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
111 if ($debug) echo("checking $column == $column_name<br />"); |
|
112 |
90 |
113 if ($column == $column_name) { |
91 if ($column == $column_name) { |
114 return true; |
92 return true; |
115 } |
93 } |
116 } |
94 } |
185 * @param mixed $default Optional. Default value. |
163 * @param mixed $default Optional. Default value. |
186 * @param mixed $extra Optional. Extra value. |
164 * @param mixed $extra Optional. Extra value. |
187 * @return bool True, if matches. False, if not matching. |
165 * @return bool True, if matches. False, if not matching. |
188 */ |
166 */ |
189 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { |
167 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { |
190 global $wpdb, $debug; |
168 global $wpdb; |
191 $diffs = 0; |
169 $diffs = 0; |
192 $results = $wpdb->get_results("DESC $table_name"); |
170 $results = $wpdb->get_results("DESC $table_name"); |
193 |
171 |
194 foreach ($results as $row ) { |
172 foreach ($results as $row ) { |
195 if ($debug > 1) print_r($row); |
|
196 |
173 |
197 if ($row->Field == $col_name) { |
174 if ($row->Field == $col_name) { |
198 // got our column, check the params |
175 // 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)) { |
176 if (($col_type != null) && ($row->Type != $col_type)) { |
201 ++$diffs; |
177 ++$diffs; |
202 } |
178 } |
203 if (($is_null != null) && ($row->Null != $is_null)) { |
179 if (($is_null != null) && ($row->Null != $is_null)) { |
204 ++$diffs; |
180 ++$diffs; |