<?php
/**
 * @file
 * Install, update, and uninstall functions for the field_property_list module.
 */

function field_property_list_field_schema($field) {
  $columns = array(
    'key' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE),
    'value' => array('type' => 'varchar', 'length' => 256, 'not null' => FALSE),
  );
  $indexes = array(
    'key' => array('key'),
  );
  return array(
    'columns' => $columns,
    'indexes' => $indexes,
  );
}

/**
  * Returns all fields created on the system of the type defined in field_property_list.
  */
function field_property_list_get_field_property_list_fields() {
  $types = array_keys(field_property_list_field_info()); // field types defined in mymodule
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  return $fields;
}

/**
 * Change field schema's value.length to 256.
 */
function field_property_list_update_7000() {
  $fields = field_property_list_get_field_property_list_fields();

  foreach ($fields as $field) {
    $table_prefixes = array(
      _field_sql_storage_tablename($field),
      _field_sql_storage_revision_tablename($field)
    );
    foreach ($table_prefixes as $table_prefix) {
      $field_name = $field['field_name'];
      $table = $table_prefix;
      $column = $field_name . '_value';
      $spec = array('type' => 'varchar', 'length' => 256, 'not null' => FALSE);
      db_change_field($table, $column, $column, $spec);
    }
  }
  return array();
}
