| 1 |
<?php namespace Datafeedr\Api\Wuwei\Database; |
| 2 |
|
| 3 |
/** |
| 4 |
* @todo Don't forget to handle Multisite DB issues - look into "Network: false" in the plugin |
| 5 |
* header or a filter that prevents the plugin from being network activated. |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Database_Table |
| 10 |
* |
| 11 |
* A set of utilities for interacting with a database table. |
| 12 |
* |
| 13 |
* Example Usages: |
| 14 |
* |
| 15 |
* Include with "use" keyword |
| 16 |
* use Datafeedr\Api\Wuwei\Database\Table; |
| 17 |
* |
| 18 |
* Instantiate Class (Do NOT include $wpdb->prefix). |
| 19 |
* $table = new Table( 'datafeedr_networks' ); |
| 20 |
* |
| 21 |
* Check if database table exists. |
| 22 |
* return ( $table->exists() ) ? true : false; |
| 23 |
* |
| 24 |
* Create or Update a database table. |
| 25 |
* $attributes = array(); |
| 26 |
* $attributes[] = 'id INT(11) NOT NULL'; |
| 27 |
* $attributes[] = 'test_id INT(11) NOT NULL COMMENT "Just a comment about this table."'; |
| 28 |
* $attributes[] = 'updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'; |
| 29 |
* $attributes[] = 'PRIMARY KEY (id)'; |
| 30 |
* $attributes[] = 'KEY test_col (test_id)'; |
| 31 |
* $table->create( $attributes ); |
| 32 |
* |
| 33 |
* Drop table |
| 34 |
* $table->drop(); |
| 35 |
* |
| 36 |
* Truncate (Empty) Table |
| 37 |
* $table->truncate(); |
| 38 |
* |
| 39 |
* Drop an index |
| 40 |
* $table->drop_index( 'test_col' ); |
| 41 |
* |
| 42 |
* Add an index |
| 43 |
* $table->add_index( 'test_col' ); |
| 44 |
* |
| 45 |
* Add a column |
| 46 |
* $table->add_column( 'deleted_at', 'TIMESTAMP NULL DEFAULT NULL AFTER updated_at' ); |
| 47 |
* |
| 48 |
* Drop a column |
| 49 |
* $table->drop_column( 'deleted_at' ); |
| 50 |
* |
| 51 |
* @since 2.0.0 |
| 52 |
*/ |
| 53 |
class Table { |
| 54 |
|
| 55 |
/** |
| 56 |
* Name of the database table WITHOUT the WordPress prefix. |
| 57 |
* |
| 58 |
* @since 2.0.0 |
| 59 |
* @access public |
| 60 |
* @var string $table_name |
| 61 |
*/ |
| 62 |
protected $table_name; |
| 63 |
|
| 64 |
/** |
| 65 |
* Table constructor. |
| 66 |
* |
| 67 |
* @since 2.0.0 |
| 68 |
* |
| 69 |
* @param string $table_name |
| 70 |
*/ |
| 71 |
public function __construct( $table_name ) { |
| 72 |
$this->table_name = $table_name; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the un-prefixed $table_name. |
| 77 |
* |
| 78 |
* @since 2.0.0 |
| 79 |
* |
| 80 |
* @return string Unprefixed table name. |
| 81 |
*/ |
| 82 |
public function table_name() { |
| 83 |
return trim( $this->table_name ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the prefixed $table_name. |
| 88 |
* |
| 89 |
* @since 2.0.0 |
| 90 |
* |
| 91 |
* @global \wpdb $wpdb |
| 92 |
* |
| 93 |
* @return string Prefixed table name. |
| 94 |
*/ |
| 95 |
public function prefixed_table_name() { |
| 96 |
global $wpdb; |
| 97 |
|
| 98 |
return $wpdb->prefix . $this->table_name(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns the Charset Collate for CREATE statements. |
| 103 |
* |
| 104 |
* @since 2.0.0 |
| 105 |
* |
| 106 |
* @global \wpdb $wpdb |
| 107 |
* |
| 108 |
* @return string Returns the charset collate value. |
| 109 |
*/ |
| 110 |
private function get_charset_collate() { |
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
return $wpdb->get_charset_collate(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Wrapper for dbDelta(). |
| 118 |
* |
| 119 |
* @since 2.0.0 |
| 120 |
* |
| 121 |
* @param string|array $queries Optional. The query to run. Can be multiple queries |
| 122 |
* in an array, or a string of queries separated by |
| 123 |
* semicolons. Default empty. |
| 124 |
* @param bool $execute Optional. Whether or not to execute the query right away. |
| 125 |
* Default true. |
| 126 |
* |
| 127 |
* @return array Strings containing the results of the various update queries. |
| 128 |
*/ |
| 129 |
public function db_delta( $queries = '', $execute = true ) { |
| 130 |
$this->include_required_files(); |
| 131 |
|
| 132 |
return dbDelta( $queries, $execute ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Check if the database table exists. |
| 137 |
* |
| 138 |
* Majority of code is from WordPress's maybe_create_table() function. |
| 139 |
* |
| 140 |
* @since 2.0.0 |
| 141 |
* |
| 142 |
* @see maybe_create_table() |
| 143 |
* |
| 144 |
* @global \wpdb $wpdb |
| 145 |
* |
| 146 |
* @return bool True if table exists, else false. |
| 147 |
*/ |
| 148 |
public function exists() { |
| 149 |
|
| 150 |
global $wpdb; |
| 151 |
|
| 152 |
$query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $this->prefixed_table_name() ) ); |
| 153 |
|
| 154 |
if ( $wpdb->get_var( $query ) == $this->prefixed_table_name() ) { |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Will create or update database table. |
| 163 |
* |
| 164 |
* @since 2.0.0 |
| 165 |
* |
| 166 |
* @param array $data |
| 167 |
* |
| 168 |
* @return array Strings containing the results of the various update queries. |
| 169 |
*/ |
| 170 |
public function create( $data = [] ) { |
| 171 |
|
| 172 |
$data = array_filter( $data ); |
| 173 |
$table = $this->prefixed_table_name(); |
| 174 |
$char_coll = $this->get_charset_collate(); |
| 175 |
|
| 176 |
$query = "CREATE TABLE " . $table . " (\n "; |
| 177 |
$query .= implode( ",\n ", $data ); |
| 178 |
$query .= "\n) $char_coll "; |
| 179 |
|
| 180 |
$result = $this->db_delta( $query ); |
| 181 |
|
| 182 |
return $result; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Drops a table if it exists. |
| 187 |
* |
| 188 |
* @since 2.0.0 |
| 189 |
* |
| 190 |
* @global \wpdb $wpdb |
| 191 |
* |
| 192 |
* @return int|false Number of rows affected/selected or false on error |
| 193 |
*/ |
| 194 |
public function drop() { |
| 195 |
global $wpdb; |
| 196 |
$sql = "DROP TABLE IF EXISTS " . $this->prefixed_table_name(); |
| 197 |
$result = $wpdb->query( $sql ); |
| 198 |
|
| 199 |
return $result; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Truncates a table if it exists. |
| 204 |
* |
| 205 |
* @since 2.0.0 |
| 206 |
* |
| 207 |
* @global \wpdb $wpdb |
| 208 |
* |
| 209 |
* @return int|false Number of rows affected/selected or false on error |
| 210 |
*/ |
| 211 |
public function truncate() { |
| 212 |
|
| 213 |
global $wpdb; |
| 214 |
|
| 215 |
if ( ! $this->exists() ) { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
$sql = 'TRUNCATE TABLE ' . $this->prefixed_table_name(); |
| 220 |
$result = $wpdb->query( $sql ); |
| 221 |
|
| 222 |
return $result; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Wrapper for add_clean_index(). |
| 227 |
* |
| 228 |
* @since 2.0.0 |
| 229 |
* |
| 230 |
* @param string $index Database table index column. |
| 231 |
* |
| 232 |
* @return true True, when done with execution. |
| 233 |
*/ |
| 234 |
public function add_index( $index ) { |
| 235 |
$result = add_clean_index( $this->prefixed_table_name(), $index ); |
| 236 |
|
| 237 |
return $result; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Wrapper for drop_index(). |
| 242 |
* |
| 243 |
* @since 2.0.0 |
| 244 |
* |
| 245 |
* @param string $index Index name to drop. |
| 246 |
* |
| 247 |
* @return true True, when finished. |
| 248 |
*/ |
| 249 |
public function drop_index( $index ) { |
| 250 |
$result = drop_index( $this->prefixed_table_name(), $index ); |
| 251 |
|
| 252 |
return $result; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Adds column to a database table if it doesn't already exist. |
| 257 |
* |
| 258 |
* @since 2.0.0 |
| 259 |
* |
| 260 |
* @param string $column_name The column name to add to the table. |
| 261 |
* Example: "deleted_at" |
| 262 |
* Example: "name" |
| 263 |
* @param string $column_query The SQL statement used to add the column. Should include the column name. |
| 264 |
* Example: "TIMESTAMP NULL DEFAULT NULL AFTER updated_at" |
| 265 |
* Example: "varchar(10) NOT NULL" |
| 266 |
* |
| 267 |
* @return bool True if already exists or on successful completion, false on error. |
| 268 |
*/ |
| 269 |
public function add_column( $column_name, $column_query ) { |
| 270 |
|
| 271 |
$this->include_required_files(); |
| 272 |
|
| 273 |
/** |
| 274 |
* Generates SQL which looks like this: |
| 275 |
* |
| 276 |
* ALTER TABLE wp_table_name ADD COLUMN deleted_at TIMESTAMP NULL DEFAULT NULL AFTER updated_at; |
| 277 |
*/ |
| 278 |
$sql = sprintf( |
| 279 |
'ALTER TABLE %1$s ADD COLUMN %2$s %3$s', |
| 280 |
$this->prefixed_table_name(), |
| 281 |
$column_name, |
| 282 |
$column_query |
| 283 |
); |
| 284 |
|
| 285 |
$result = maybe_add_column( $this->prefixed_table_name(), $column_name, $sql ); |
| 286 |
|
| 287 |
return $result; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Drops a column from a table. |
| 292 |
* |
| 293 |
* @since 2.0.0 |
| 294 |
* |
| 295 |
* @param string $column_name |
| 296 |
* |
| 297 |
* @return bool False on failure, true on success or doesn't exist. |
| 298 |
*/ |
| 299 |
public function drop_column( $column_name ) { |
| 300 |
|
| 301 |
$this->include_required_files(); |
| 302 |
|
| 303 |
/** |
| 304 |
* Generates SQL which looks like this: |
| 305 |
* |
| 306 |
* ALTER TABLE table_name DROP deleted_at; |
| 307 |
*/ |
| 308 |
$sql = sprintf( |
| 309 |
'ALTER TABLE %1$s DROP COLUMN %2$s', |
| 310 |
$this->prefixed_table_name(), |
| 311 |
$column_name |
| 312 |
); |
| 313 |
|
| 314 |
$result = maybe_drop_column( $this->prefixed_table_name(), $column_name, $sql ); |
| 315 |
|
| 316 |
return $result; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Include required files for dbDelta() style queries. |
| 321 |
* |
| 322 |
* @since 2.0.0 |
| 323 |
*/ |
| 324 |
private function include_required_files() { |
| 325 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 326 |
} |
| 327 |
} |