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:

101 to 200 of 366
      // Extract the field name.
      preg_match( '|^([^ ]*)|', $fld, $fvals );
      $fieldname            = trim( $fvals[1], '`' );
      $fieldname_lowercased = strtolower( $fieldname );

      // Verify the found field name.
      $validfield = true;
      switch ( $fieldname_lowercased ) {
        case '':
        case 'primary':
        case 'index':
        case 'fulltext':
        case 'unique':
        case 'key':
        case 'spatial':
          $validfield = false;

          /*
					 * Normalize the index definition.
					 *
					 * This is done so the definition can be compared against the result of a
					 * `SHOW INDEX FROM $table_name` query which returns the current table
					 * index information.
					 */

          // Extract type, name and columns from the definition.
          // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
          preg_match(
            '/^'
            .   '(?P<index_type>'             // 1) Type of the index.
            .       'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX'
            .   ')'
            .   '\s+'                         // Followed by at least one white space character.
            .   '(?:'                         // Name of the index. Optional if type is PRIMARY KEY.
            .       '`?'                      // Name can be escaped with a backtick.
            .           '(?P<index_name>'     // 2) Name of the index.
            .               '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
            .           ')'
            .       '`?'                      // Name can be escaped with a backtick.
            .       '\s+'                     // Followed by at least one white space character.
            .   ')*'
            .   '\('                          // Opening bracket for the columns.
            .       '(?P<index_columns>'
            .           '.+?'                 // 3) Column names, index prefixes, and orders.
            .       ')'
            .   '\)'                          // Closing bracket for the columns.
            . '$/im',
            $fld,
            $index_matches
          );
          // phpcs:enable

          // Uppercase the index type and normalize space characters.
          $index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) );

          // 'INDEX' is a synonym for 'KEY', standardize on 'KEY'.
          $index_type = str_replace( 'INDEX', 'KEY', $index_type );

          // Escape the index name with backticks. An index for a primary key has no name.
          $index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . strtolower( $index_matches['index_name'] ) . '`';

          // Parse the columns. Multiple columns are separated by a comma.
          $index_columns                  = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
          $index_columns_without_subparts = $index_columns;

          // Normalize columns.
          foreach ( $index_columns as $id => &$index_column ) {
            // Extract column name and number of indexed characters (sub_part).
            preg_match(
              '/'
              . '`?'                      // Name can be escaped with a backtick.
              . '(?P<column_name>'    // 1) Name of the column.
              . '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
              . ')'
              . '`?'                      // Name can be escaped with a backtick.
              . '(?:'                     // Optional sub part.
              . '\s*'                 // Optional white space character between name and opening bracket.
              . '\('                  // Opening bracket for the sub part.
              . '\s*'             // Optional white space character after opening bracket.
              . '(?P<sub_part>'
              . '\d+'         // 2) Number of indexed characters.
              . ')'
              . '\s*'             // Optional white space character before closing bracket.
              . '\)'                 // Closing bracket for the sub part.
              . ')?'
              . '/',
              $index_column,
              $index_column_matches
            );

            // Escape the column name with backticks.
            $index_column = '`' . $index_column_matches['column_name'] . '`';

            // We don't need to add the subpart to $index_columns_without_subparts
            $index_columns_without_subparts[ $id ] = $index_column;

            // Append the optional sup part with the number of indexed characters.
            if ( isset( $index_column_matches['sub_part'] ) ) {
              $index_column .= '(' . $index_column_matches['sub_part'] . ')';
            }
 

 View on GitHub View on Trac