dbDelta() – Modifies the database based on specified SQL statements.

You appear to be a bot. Output may be restricted

Description

Modifies the database based on specified SQL statements.

Useful for creating new tables and updating existing tables to a new structure.

Usage

$array = dbDelta( $queries, $execute );

Parameters

$queries
( string[]|string ) optional – Optional. The query to run. Can be multiple queries in an array, or a string of queries separated by semicolons. Default empty string.
$execute
( bool ) optional default: 1 – Optional. Whether or not to execute the query right away. Default true.

Returns

array Strings containing the results of the various update queries.

Source

File name: wordpress/wp-admin/includes/upgrade.php
Lines:

1 to 100 of 366
function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  global $wpdb;

  if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) {
    $queries = wp_get_db_schema( $queries );
  }

  // Separate individual queries into an array.
  if ( ! is_array( $queries ) ) {
    $queries = explode( ';', $queries );
    $queries = array_filter( $queries );
  }

  
/**
 * Filters the dbDelta SQL queries.
 *
 * @since 3.3.0
 *
 * @param string[] $queries An array of dbDelta SQL queries.
 */
  $queries = apply_filters( 'dbdelta_queries', $queries );

  $cqueries   = array(); // Creation queries.
  $iqueries   = array(); // Insertion queries.
  $for_update = array();

  // Create a tablename index for an array ($cqueries) of queries.
  foreach ( $queries as $qry ) {
    if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
      $cqueries[ trim( $matches[1], '`' ) ] = $qry;
      $for_update[ $matches[1] ]            = 'Created table ' . $matches[1];
    } elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
      array_unshift( $cqueries, $qry );
    } elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
      $iqueries[] = $qry;
    } elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
      $iqueries[] = $qry;
    } else {
      // Unrecognized query type.
    }
  }

  
/**
 * Filters the dbDelta SQL queries for creating tables and/or databases.
 *
 * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
 *
 * @since 3.3.0
 *
 * @param string[] $cqueries An array of dbDelta create SQL queries.
 */
  $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );

  
/**
 * Filters the dbDelta SQL queries for inserting or updating.
 *
 * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
 *
 * @since 3.3.0
 *
 * @param string[] $iqueries An array of dbDelta insert or update SQL queries.
 */
  $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries );

  $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
  $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' );

  $global_tables = $wpdb->tables( 'global' );
  foreach ( $cqueries as $table => $qry ) {
    // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
    if ( in_array( $table, $global_tables, true ) && ! wp_should_upgrade_global_tables() ) {
      unset( $cqueries[ $table ], $for_update[ $table ] );
      continue;
    }

    // Fetch the table column structure from the database.
    $suppress    = $wpdb->suppress_errors();
    $tablefields = $wpdb->get_results( "DESCRIBE {$table};" );
    $wpdb->suppress_errors( $suppress );

    if ( ! $tablefields ) {
      continue;
    }

    // Clear the field and index arrays.
    $cfields                  = array();
    $indices                  = array();
    $indices_without_subparts = array();

    // Get all of the field names in the query from between the parentheses.
    preg_match( '|\((.*)\)|ms', $qry, $match2 );
    $qryline = trim( $match2[1] );

    // Separate field lines into an array.
    $flds = explode( "\n", $qryline );

    // For every field line specified in the query.
    foreach ( $flds as $fld ) {
      $fld = trim( $fld, " \t\n\r\0\x0B," ); // Default trim characters, plus ','.

 

 View on GitHub View on Trac