From 4078c7a5a589dd42a26b7fc948fa30298ed690a6 Mon Sep 17 00:00:00 2001 From: Earl Miles <merlin@logrus.com> Date: Tue, 3 Feb 2009 04:09:06 +0000 Subject: [PATCH] #367962: Remove incorrect "default" values from schema that prevents tables from being created. --- ctools.module | 96 +++++++++++++++++++++++++++++++++++++ delegator/delegator.install | 4 -- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/ctools.module b/ctools.module index fe62bfab..580c422b 100644 --- a/ctools.module +++ b/ctools.module @@ -159,3 +159,99 @@ function ctools_cron() { } } +/** + * Initialize an object from schema. + * + * The schema for the table will be queried and flags on the fields will + * be used to initialize the data. + * + * - $schema['class'] will be used to determine the class of the new object. + * stdClass will be used if this is not set. + * - $schema['fields'][$field]['serialize'] will be queried to see if the + * data is serialized. + * - $schema['fields'][$field']'object default'] will be queried to see if + * the object gets a default that the database does not. + * + * @param $table + * The name of the table which must be available via drupal_get_schema(). + */ +function ctools_object_init($table) { + $schema = drupal_get_schema($table); + + if (!$schema) { + return; + } + + $class = isset($schema['class']) && class_exists($schema['class']) ? $schema['class'] : 'stdClass'; + $object = new $class; + + + // Go through our schema and build correlations. + foreach ($schema['fields'] as $field => $info) { + if ($info['type'] == 'serial') { + $object->$field = NULL; + } + if (!isset($object->$field)) { + if (!empty($info['serialize']) && isset($info['serialized default'])) { + $object->$field = unserialize($info['serialized default']); + } + else if (isset($info['default'])) { + $object->$field = $info['default']; + } + // Some database types do not support defaults but we want to + // provide one in code. This one will only be used on the in code + // object, never in the database. + else if (isset($info['object default'])) { + $object->$field = $info['object default']; + } + else { + $object->$field = ''; + } + } + } + + return $object; +} + +/** + * Load an object with data, presumably retrieved via db_fetch_object. + * + * @param &$object + * The object to load data onto. This will be modified in place. + * @param $table + * The name of the table, which will be loaded from schema. + * @param $data + * The data to load via db_fetch_object(). + */ +function ctools_object_load(&$object, $table, $data) { + $schema = drupal_get_schema($table); + + if (!$schema) { + return; + } + + // Go through our schema and build correlations. + foreach ($schema['fields'] as $field => $info) { + if (isset($data->$field) { + $object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field); + } + } +} + +/** + * Create an object from data loaded from the database. + * + * @param $table + * The name of the table, which will be loaded from schema. + * @param $data + * The data to load via db_fetch_object(). + * + * @return + * The initialized object. + */ +function ctools_object_create($table, $data) { + $object = ctools_object_init($table); + ctools_object_load($object, $table, $data); + + return $object; +} \ No newline at end of file diff --git a/delegator/delegator.install b/delegator/delegator.install index c6e1889c..06e922a3 100644 --- a/delegator/delegator.install +++ b/delegator/delegator.install @@ -60,7 +60,6 @@ function delegator_schema_1() { 'size' => 'big', 'description' => t('Serialized configuration of the handler, if needed.'), 'not null' => TRUE, - 'default' => '', 'serialize' => TRUE, ), ), @@ -118,7 +117,6 @@ function delegator_schema_1() { 'size' => 'big', 'description' => t('Access configuration for this path.'), 'not null' => TRUE, - 'default' => '', 'serialize' => TRUE, ), 'multiple' => array( @@ -132,7 +130,6 @@ function delegator_schema_1() { 'size' => 'big', 'description' => t('Serialized configuration of Drupal menu visibility settings for this item.'), 'not null' => TRUE, - 'default' => '', 'serialize' => TRUE, ), 'arguments' => array( @@ -140,7 +137,6 @@ function delegator_schema_1() { 'size' => 'big', 'description' => t('Configuration of arguments for this menu item.'), 'not null' => TRUE, - 'default' => '', 'serialize' => TRUE, ), ), -- GitLab