You are here

number.install in Content Construction Kit (CCK) 5

File

number.install
View source
<?php

/**
 * Implementation of hook_install().
 */
function number_install() {
}
function number_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'node_field_float_data', 'nid', 'integer', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      db_add_column($ret, 'node_field_int_data', 'nid', 'integer', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_field_float_data} ADD COLUMN nid int(10) NOT NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {node_field_int_data} ADD COLUMN nid int(10) NOT NULL DEFAULT 0");
      break;
  }
  return $ret;
}
function number_update_2() {

  // Multi-part update
  if (!isset($_SESSION['number_update_2'])) {
    $_SESSION['number_update_2'] = 0;
    $_SESSION['number_update_2_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_float_data}"));
  }
  $limit = 20;
  $result = db_query_range("SELECT nr.nid, nffd.vid, nffd.field_name, nffd.delta FROM {node_field_float_data} nffd LEFT JOIN {node_revisions} nr ON nr.vid = nffd.vid", $_SESSION['number_update_2'], $limit);
  if (db_num_rows($result) == 0) {
    unset($_SESSION['number_update_2']);
    unset($_SESSION['number_update_2_max']);
    return array();
  }
  while ($data = db_fetch_object($result)) {
    $_SESSION['number_update_2']++;
    db_query("UPDATE {node_field_float_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
  }
  return array(
    '#finished' => $_SESSION['number_update_2'] / $_SESSION['number_update_2_max'],
  );
}
function number_update_3() {

  // Multi-part update
  if (!isset($_SESSION['number_update_3'])) {
    $_SESSION['number_update_3'] = 0;
    $_SESSION['number_update_3_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_int_data}"));
  }
  $limit = 20;
  $result = db_query_range("SELECT nr.nid, nfid.vid, nfid.field_name, nfid.delta FROM {node_field_int_data} nfid LEFT JOIN {node_revisions} nr ON nr.vid = nfid.vid", $_SESSION['number_update_3'], $limit);
  if (db_num_rows($result) == 0) {
    unset($_SESSION['number_update_3']);
    unset($_SESSION['number_update_3_max']);
    return array();
  }
  while ($data = db_fetch_object($result)) {
    $_SESSION['number_update_3']++;
    db_query("UPDATE {node_field_int_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
  }
  return array(
    '#finished' => $_SESSION['number_update_3'] / $_SESSION['number_update_3_max'],
  );
}

/**
 * Data is now stored in per-field tables.
 */
function number_update_4() {
  $ret = array();
  include_once './' . drupal_get_path('module', 'content') . '/content.module';
  include_once './' . drupal_get_path('module', 'content') . '/content_admin.inc';
  content_clear_type_cache();
  $fields = content_fields();
  foreach ($fields as $field) {
    switch ($field['type']) {
      case 'number_integer':
        $columns = array(
          'value' => array(
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
          ),
        );

        // the following line will trigger (negligible) warnings if content_update_5 was run before
        // (column already exists)
        @content_alter_db_field(array(), array(), $field, $columns);
        $db_info = content_database_info($field);
        $table = $db_info['table'];
        if ($field['multiple']) {
          $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, delta, nid, ' . $field['field_name'] . "_value) SELECT vid, delta, nid, field_int FROM {node_field_int_data} WHERE field_name = '" . $field['field_name'] . "'");
        }
        else {
          $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, nid, ' . $field['field_name'] . "_value) SELECT vid, nid, field_int FROM {node_field_int_data} WHERE field_name = '" . $field['field_name'] . "'");
        }
        break;
      case 'number_decimal':
        $columns = array(
          'value' => array(
            'type' => 'float',
            'not null' => TRUE,
            'default' => 0,
          ),
        );

        // the following line will trigger (negligible) warnings if content_update_5 was run before
        // (column already exists)
        @content_alter_db_field(array(), array(), $field, $columns);
        $db_info = content_database_info($field);
        $table = $db_info['table'];
        if ($field['multiple']) {
          $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, delta, nid, ' . $field['field_name'] . "_value) SELECT vid, delta, nid, field_float FROM {node_field_float_data} WHERE field_name = '" . $field['field_name'] . "'");
        }
        else {
          $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, nid, ' . $field['field_name'] . "_value) SELECT vid, nid, field_float FROM {node_field_float_data} WHERE field_name = '" . $field['field_name'] . "'");
        }
        break;
    }
  }
  $ret[] = update_sql('DROP TABLE {node_field_int_data}');
  $ret[] = update_sql('DROP TABLE {node_field_float_data}');
  db_query('DELETE FROM {cache}');
  return $ret;
}

/**
 * Set the value columns to accept NULL values
 */
function number_update_5() {
  $ret = array();
  include_once './' . drupal_get_path('module', 'content') . '/content.module';
  include_once './' . drupal_get_path('module', 'content') . '/content_admin.inc';
  content_clear_type_cache();
  $fields = content_fields();
  foreach ($fields as $field) {
    switch ($field['type']) {
      case 'number_integer':
      case 'number_decimal':
        $db_info = content_database_info($field);
        $columns_old = $db_info['columns'];
        $columns = $columns_old;
        $columns['value']['not null'] = FALSE;
        $columns['value']['default'] = NULL;

        // force the old values : if the db info was rebuilt before the update is run,
        // it will already contain the new values, and nothing gets changed in content_alter_db_field
        $columns_old['value']['not null'] = TRUE;
        $columns_old['value']['default'] = 0;
        content_alter_db_field($field, $columns_old, $field, $columns);
        $ret[] = array(
          'query' => strtr('The field %field_name has been updated to accept NULL values.', array(
            '%field_name' => $field['field_name'],
          )),
          'success' => TRUE,
        );
        break;
    }
  }
  db_query('DELETE FROM {cache}');
  return $ret;
}

Functions

Namesort descending Description
number_install Implementation of hook_install().
number_update_1
number_update_2
number_update_3
number_update_4 Data is now stored in per-field tables.
number_update_5 Set the value columns to accept NULL values