bp_bb_dbDelta

Parse and execute queries for updating a set of database tables.

Description

(array) bp_bb_dbDelta( (array) $queries, (constant) $execute = true ); 

Copied from wp-admin/includes/upgrade.php, this will take care of creating the bbPress stand-alone tables without loading a conflicting WP Admin.

Returns (array)

See {@link dbDelta()}.

Parameters (2)

0. $queries (array)
The queries.
1. $execute — Optional. (constant) => true
The execute.

Usage

  1. if ( !function_exists( 'bp_bb_dbDelta' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bp-forums-bbpress-sa.php'; 
  3.  
  4. // The queries. 
  5. $queries = array(); 
  6.  
  7. // The execute. 
  8. $execute = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_bb_dbDelta($queries, $execute); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bp-forums-bbpress-sa.php  
  1. function bp_bb_dbDelta($queries, $execute = true) { 
  2. global $wpdb; 
  3.  
  4. // Separate individual queries into an array. 
  5. if ( !is_array($queries) ) { 
  6. $queries = explode( ';', $queries ); 
  7. if ('' == $queries[count($queries) - 1]) array_pop($queries); 
  8.  
  9. $cqueries = array(); // Creation Queries. 
  10. $iqueries = array(); // Insertion Queries. 
  11. $for_update = array(); 
  12.  
  13. // Create a tablename index for an array ($cqueries) of queries. 
  14. foreach($queries as $qry) { 
  15. if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { 
  16. $cqueries[trim( strtolower($matches[1]), '`' )] = $qry; 
  17. $for_update[$matches[1]] = 'Created table '.$matches[1]; 
  18. } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { 
  19. array_unshift($cqueries, $qry); 
  20. } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { 
  21. $iqueries[] = $qry; 
  22. } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { 
  23. $iqueries[] = $qry; 
  24. } else { 
  25. // Unrecognized query type. 
  26.  
  27. // Check to see which tables and fields exist. 
  28. if ($tables = $wpdb->get_col('SHOW TABLES;')) { 
  29. // For every table in the database. 
  30. foreach ($tables as $table) { 
  31. // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined. 
  32. if ( in_array($table, $wpdb->tables('global')) && ( !is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) ) 
  33. continue; 
  34.  
  35. // If a table query exists for the database table... 
  36. if ( array_key_exists(strtolower($table), $cqueries) ) { 
  37. // Clear the field and index arrays. 
  38. $cfields = $indices = array(); 
  39. // Get all of the field names in the query from between the parents. 
  40. preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); 
  41. $qryline = trim($match2[1]); 
  42.  
  43. // Separate field lines into an array. 
  44. $flds = explode("\n", $qryline); 
  45.  
  46. //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; 
  47.  
  48. // For every field line specified in the query. 
  49. foreach ($flds as $fld) { 
  50. // Extract the field name. 
  51. preg_match("|^([^ ]*)|", trim($fld), $fvals); 
  52. $fieldname = trim( $fvals[1], '`' ); 
  53.  
  54. // Verify the found field name. 
  55. $validfield = true; 
  56. switch (strtolower($fieldname)) { 
  57. case '': 
  58. case 'primary': 
  59. case 'index': 
  60. case 'fulltext': 
  61. case 'unique': 
  62. case 'key': 
  63. $validfield = false; 
  64. $indices[] = trim(trim($fld), ", \n"); 
  65. break; 
  66. $fld = trim($fld); 
  67.  
  68. // If it's a valid field, add it to the field array. 
  69. if ($validfield) { 
  70. $cfields[strtolower($fieldname)] = trim($fld, ", \n"); 
  71.  
  72. // Fetch the table column structure from the database. 
  73. $tablefields = $wpdb->get_results("DESCRIBE {$table};"); 
  74.  
  75. // For every field in the table. 
  76. foreach ($tablefields as $tablefield) { 
  77. // If the table field exists in the field array... 
  78. if (array_key_exists(strtolower($tablefield->Field), $cfields)) { 
  79. // Get the field type from the query. 
  80. preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); 
  81. $fieldtype = $matches[1]; 
  82.  
  83. // Is actual field type different from the field type in query? 
  84. if ($tablefield->Type != $fieldtype) { 
  85. // Add a query to change the column type. 
  86. $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; 
  87. $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; 
  88.  
  89. // Get the default value from the array. 
  90. //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; 
  91. if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { 
  92. $default_value = $matches[1]; 
  93. if ($tablefield->Default != $default_value) { 
  94. // Add a query to change the column's default value. 
  95. $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; 
  96. $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; 
  97.  
  98. // Remove the field from the array (so it's not added). 
  99. unset($cfields[strtolower($tablefield->Field)]); 
  100. } else { 
  101. // This field exists in the table, but not in the creation queries? 
  102.  
  103. // For every remaining field specified for the table. 
  104. foreach ($cfields as $fieldname => $fielddef) { 
  105. // Push a query line into $cqueries that adds the field to that table. 
  106. $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; 
  107. $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; 
  108.  
  109. // Index stuff goes here 
  110. // Fetch the table index structure from the database. 
  111. $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); 
  112.  
  113. if ($tableindices) { 
  114. // Clear the index array. 
  115. unset($index_ary); 
  116.  
  117. // For every index in the table. 
  118. foreach ($tableindices as $tableindex) { 
  119. // Add the index to the index data array. 
  120. $keyname = $tableindex->Key_name; 
  121. $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); 
  122. $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; 
  123.  
  124. // For each actual index in the index array. 
  125. foreach ($index_ary as $index_name => $index_data) { 
  126. // Build a create string to compare to the query. 
  127. $index_string = ''; 
  128. if ($index_name == 'PRIMARY') { 
  129. $index_string .= 'PRIMARY '; 
  130. } else if($index_data['unique']) { 
  131. $index_string .= 'UNIQUE '; 
  132. $index_string .= 'KEY '; 
  133. if ($index_name != 'PRIMARY') { 
  134. $index_string .= $index_name; 
  135. $index_columns = ''; 
  136. // For each column in the index. 
  137. foreach ($index_data['columns'] as $column_data) { 
  138. if ($index_columns != '') $index_columns .= ', '; 
  139. // Add the field to the column list string. 
  140. $index_columns .= $column_data['fieldname']; 
  141. if ($column_data['subpart'] != '') { 
  142. $index_columns .= '('.$column_data['subpart'].')'; 
  143. // Add the column list to the index create string. 
  144. $index_string .= ' ('.$index_columns.')'; 
  145. if (!(($aindex = array_search($index_string, $indices)) === false)) { 
  146. unset($indices[$aindex]); 
  147. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; 
  148. //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n"; 
  149.  
  150. // For every remaining index specified for the table. 
  151. foreach ( (array) $indices as $index ) { 
  152. // Push a query line into $cqueries that adds the index to that table. 
  153. $cqueries[] = "ALTER TABLE {$table} ADD $index"; 
  154. $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; 
  155.  
  156. // Remove the original table creation query from processing. 
  157. unset($cqueries[strtolower($table)]); 
  158. unset($for_update[strtolower($table)]); 
  159. } else { 
  160. // This table exists in the database, but not in the creation queries? 
  161.  
  162. $allqueries = array_merge($cqueries, $iqueries); 
  163. if ($execute) { 
  164. foreach ($allqueries as $query) { 
  165. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n"; 
  166. $wpdb->query($query); 
  167.  
  168. return $for_update;