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:

301 to 366 of 366
      // For each actual index in the index array.
      foreach ( $index_ary as $index_name => $index_data ) {

        // Build a create string to compare to the query.
        $index_string = '';
        if ( 'primary' === $index_name ) {
          $index_string .= 'PRIMARY ';
        } elseif ( $index_data['unique'] ) {
          $index_string .= 'UNIQUE ';
        }
        if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
          $index_string .= 'FULLTEXT ';
        }
        if ( 'SPATIAL' === strtoupper( $index_data['index_type'] ) ) {
          $index_string .= 'SPATIAL ';
        }
        $index_string .= 'KEY ';
        if ( 'primary' !== $index_name ) {
          $index_string .= '`' . $index_name . '`';
        }
        $index_columns = '';

        // For each column in the index.
        foreach ( $index_data['columns'] as $column_data ) {
          if ( '' !== $index_columns ) {
            $index_columns .= ',';
          }

          // Add the field to the column list string.
          $index_columns .= '`' . $column_data['fieldname'] . '`';
        }

        // Add the column list to the index create string.
        $index_string .= " ($index_columns)";

        // Check if the index definition exists, ignoring subparts.
        $aindex = array_search( $index_string, $indices_without_subparts, true );
        if ( false !== $aindex ) {
          // If the index already exists (even with different subparts), we don't need to create it.
          unset( $indices_without_subparts[ $aindex ] );
          unset( $indices[ $aindex ] );
        }
      }
    }

    // For every remaining index specified for the table.
    foreach ( (array) $indices as $index ) {
      // Push a query line into $cqueries that adds the index to that table.
      $cqueries[] = "ALTER TABLE {$table} ADD $index";

      $for_update[] = 'Added index ' . $table . ' ' . $index;
    }

    // Remove the original table creation query from processing.
    unset( $cqueries[ $table ], $for_update[ $table ] );
  }

  $allqueries = array_merge( $cqueries, $iqueries );
  if ( $execute ) {
    foreach ( $allqueries as $query ) {
      $wpdb->query( $query );
    }
  }

  return $for_update;
}
 

 View on GitHub View on Trac