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 * |
12 * |
13 * <code> |
13 * check_column( 'wp_links', 'link_description', 'mediumtext' ); |
14 * check_column('wp_links', 'link_description', 'mediumtext'); |
14 * if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) { |
15 * if (check_column($wpdb->comments, 'comment_author', 'tinytext')) |
15 * echo "ok\n"; |
16 * echo "ok\n"; |
16 * } |
17 * |
17 * |
18 * $error_count = 0; |
18 * $error_count = 0; |
19 * $tablename = $wpdb->links; |
19 * $tablename = $wpdb->links; |
20 * // check the column |
20 * // Check the column. |
21 * if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) { |
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 '' "; |
22 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' "; |
23 * $q = $wpdb->query($ddl); |
23 * $q = $wpdb->query( $ddl ); |
24 * } |
24 * } |
25 * |
25 * |
26 * if (check_column($wpdb->links, 'link_description', 'varchar(255)')) { |
26 * if ( check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) { |
27 * $res .= $tablename . ' - ok <br />'; |
27 * $res .= $tablename . ' - ok <br />'; |
28 * } else { |
28 * } else { |
29 * $res .= 'There was a problem with ' . $tablename . '<br />'; |
29 * $res .= 'There was a problem with ' . $tablename . '<br />'; |
30 * ++$error_count; |
30 * ++$error_count; |
31 * } |
31 * } |
32 * </code> |
|
33 * |
32 * |
34 * @package WordPress |
33 * @package WordPress |
35 * @subpackage Plugin |
34 * @subpackage Plugin |
36 */ |
35 */ |
37 |
36 |
41 if ( ! function_exists('maybe_create_table') ) : |
40 if ( ! function_exists('maybe_create_table') ) : |
42 /** |
41 /** |
43 * Create database table, if it doesn't already exist. |
42 * Create database table, if it doesn't already exist. |
44 * |
43 * |
45 * @since 1.0.0 |
44 * @since 1.0.0 |
46 * @package WordPress |
45 * |
47 * @subpackage Plugin |
46 * @global wpdb $wpdb WordPress database abstraction object. |
48 * @uses $wpdb |
|
49 * |
47 * |
50 * @param string $table_name Database table name. |
48 * @param string $table_name Database table name. |
51 * @param string $create_ddl Create database table SQL. |
49 * @param string $create_ddl Create database table SQL. |
52 * @return bool False on error, true if already exists or success. |
50 * @return bool False on error, true if already exists or success. |
53 */ |
51 */ |
56 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
54 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
57 if ($table == $table_name) { |
55 if ($table == $table_name) { |
58 return true; |
56 return true; |
59 } |
57 } |
60 } |
58 } |
61 //didn't find it try to create it. |
59 // Didn't find it, so try to create it. |
62 $wpdb->query($create_ddl); |
60 $wpdb->query($create_ddl); |
63 // we cannot directly tell that whether this succeeded! |
61 |
|
62 // We cannot directly tell that whether this succeeded! |
64 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
63 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
65 if ($table == $table_name) { |
64 if ($table == $table_name) { |
66 return true; |
65 return true; |
67 } |
66 } |
68 } |
67 } |
73 if ( ! function_exists('maybe_add_column') ) : |
72 if ( ! function_exists('maybe_add_column') ) : |
74 /** |
73 /** |
75 * Add column to database table, if column doesn't already exist in table. |
74 * Add column to database table, if column doesn't already exist in table. |
76 * |
75 * |
77 * @since 1.0.0 |
76 * @since 1.0.0 |
78 * @package WordPress |
77 * |
79 * @subpackage Plugin |
78 * @global wpdb $wpdb WordPress database abstraction object. |
80 * @uses $wpdb |
|
81 * |
79 * |
82 * @param string $table_name Database table name |
80 * @param string $table_name Database table name |
83 * @param string $column_name Table column name |
81 * @param string $column_name Table column name |
84 * @param string $create_ddl SQL to add column to table. |
82 * @param string $create_ddl SQL to add column to table. |
85 * @return bool False on failure. True, if already exists or was successful. |
83 * @return bool False on failure. True, if already exists or was successful. |
90 |
88 |
91 if ($column == $column_name) { |
89 if ($column == $column_name) { |
92 return true; |
90 return true; |
93 } |
91 } |
94 } |
92 } |
95 //didn't find it try to create it. |
93 |
|
94 // Didn't find it, so try to create it. |
96 $wpdb->query($create_ddl); |
95 $wpdb->query($create_ddl); |
97 // we cannot directly tell that whether this succeeded! |
96 |
|
97 // We cannot directly tell that whether this succeeded! |
98 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
98 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
99 if ($column == $column_name) { |
99 if ($column == $column_name) { |
100 return true; |
100 return true; |
101 } |
101 } |
102 } |
102 } |
106 |
106 |
107 /** |
107 /** |
108 * Drop column from database table, if it exists. |
108 * Drop column from database table, if it exists. |
109 * |
109 * |
110 * @since 1.0.0 |
110 * @since 1.0.0 |
111 * @package WordPress |
111 * |
112 * @subpackage Plugin |
112 * @global wpdb $wpdb WordPress database abstraction object. |
113 * @uses $wpdb |
|
114 * |
113 * |
115 * @param string $table_name Table name |
114 * @param string $table_name Table name |
116 * @param string $column_name Column name |
115 * @param string $column_name Column name |
117 * @param string $drop_ddl SQL statement to drop column. |
116 * @param string $drop_ddl SQL statement to drop column. |
118 * @return bool False on failure, true on success or doesn't exist. |
117 * @return bool False on failure, true on success or doesn't exist. |
119 */ |
118 */ |
120 function maybe_drop_column($table_name, $column_name, $drop_ddl) { |
119 function maybe_drop_column($table_name, $column_name, $drop_ddl) { |
121 global $wpdb; |
120 global $wpdb; |
122 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
121 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
123 if ($column == $column_name) { |
122 if ($column == $column_name) { |
124 //found it try to drop it. |
123 |
|
124 // Found it, so try to drop it. |
125 $wpdb->query($drop_ddl); |
125 $wpdb->query($drop_ddl); |
126 // we cannot directly tell that whether this succeeded! |
126 |
|
127 // We cannot directly tell that whether this succeeded! |
127 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
128 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
128 if ($column == $column_name) { |
129 if ($column == $column_name) { |
129 return false; |
130 return false; |
130 } |
131 } |
131 } |
132 } |
132 } |
133 } |
133 } |
134 } |
134 // else didn't find it |
135 // Else didn't find it. |
135 return true; |
136 return true; |
136 } |
137 } |
137 |
138 |
138 /** |
139 /** |
139 * Check column matches criteria. |
140 * Check column matches criteria. |
170 $results = $wpdb->get_results("DESC $table_name"); |
169 $results = $wpdb->get_results("DESC $table_name"); |
171 |
170 |
172 foreach ($results as $row ) { |
171 foreach ($results as $row ) { |
173 |
172 |
174 if ($row->Field == $col_name) { |
173 if ($row->Field == $col_name) { |
175 // got our column, check the params |
174 |
|
175 // Got our column, check the params. |
176 if (($col_type != null) && ($row->Type != $col_type)) { |
176 if (($col_type != null) && ($row->Type != $col_type)) { |
177 ++$diffs; |
177 ++$diffs; |
178 } |
178 } |
179 if (($is_null != null) && ($row->Null != $is_null)) { |
179 if (($is_null != null) && ($row->Null != $is_null)) { |
180 ++$diffs; |
180 ++$diffs; |