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:

201 to 300 of 366
          }

          // Build the normalized index definition and add it to the list of indices.
          $indices[]                  = "{$index_type} {$index_name} (" . implode( ',', $index_columns ) . ')';
          $indices_without_subparts[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns_without_subparts ) . ')';

          // Destroy no longer needed variables.
          unset( $index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns, $index_columns_without_subparts );

          break;
      }

      // If it's a valid field, add it to the field array.
      if ( $validfield ) {
        $cfields[ $fieldname_lowercased ] = $fld;
      }
    }

    // For every field in the table.
    foreach ( $tablefields as $tablefield ) {
      $tablefield_field_lowercased = strtolower( $tablefield->Field );
      $tablefield_type_lowercased  = strtolower( $tablefield->Type );

      // If the table field exists in the field array...
      if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {

        // Get the field type from the query.
        preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
        $fieldtype            = $matches[1];
        $fieldtype_lowercased = strtolower( $fieldtype );

        // Is actual field type different from the field type in query?
        if ( $tablefield->Type != $fieldtype ) {
          $do_change = true;
          if ( in_array( $fieldtype_lowercased, $text_fields, true ) && in_array( $tablefield_type_lowercased, $text_fields, true ) ) {
            if ( array_search( $fieldtype_lowercased, $text_fields, true ) < array_search( $tablefield_type_lowercased, $text_fields, true ) ) {
              $do_change = false;
            }
          }

          if ( in_array( $fieldtype_lowercased, $blob_fields, true ) && in_array( $tablefield_type_lowercased, $blob_fields, true ) ) {
            if ( array_search( $fieldtype_lowercased, $blob_fields, true ) < array_search( $tablefield_type_lowercased, $blob_fields, true ) ) {
              $do_change = false;
            }
          }

          if ( $do_change ) {
            // Add a query to change the column type.
            $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];

            $for_update[ $table . '.' . $tablefield->Field ] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
          }
        }

        // Get the default value from the array.
        if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
          $default_value = $matches[1];
          if ( $tablefield->Default != $default_value ) {
            // Add a query to change the column's default value
            $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";

            $for_update[ $table . '.' . $tablefield->Field ] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
          }
        }

        // Remove the field from the array (so it's not added).
        unset( $cfields[ $tablefield_field_lowercased ] );
      } else {
        // This field exists in the table, but not in the creation queries?
      }
    }

    // For every remaining field specified for the table.
    foreach ( $cfields as $fieldname => $fielddef ) {
      // Push a query line into $cqueries that adds the field to that table.
      $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";

      $for_update[ $table . '.' . $fieldname ] = 'Added column ' . $table . '.' . $fieldname;
    }

    // Index stuff goes here. Fetch the table index structure from the database.
    $tableindices = $wpdb->get_results( "SHOW INDEX FROM {$table};" );

    if ( $tableindices ) {
      // Clear the index array.
      $index_ary = array();

      // For every index in the table.
      foreach ( $tableindices as $tableindex ) {
        $keyname = strtolower( $tableindex->Key_name );

        // Add the index to the index data array.
        $index_ary[ $keyname ]['columns'][]  = array(
          'fieldname' => $tableindex->Column_name,
          'subpart'   => $tableindex->Sub_part,
        );
        $index_ary[ $keyname ]['unique']     = ( 0 == $tableindex->Non_unique ) ? true : false;
        $index_ary[ $keyname ]['index_type'] = $tableindex->Index_type;
      }

 

 View on GitHub View on Trac