From eaa4ded053ca3d4a3d46ccbb0cf585d0608dd9c0 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 16 Aug 2010 15:14:16 -0700 Subject: [PATCH] first pass at columndef->drupal-style array converter (need to handle some more things probably; untested) --- classes/Managed_DataObject.php | 4 +- lib/schema.php | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index 3bc99c2eba..31aa7c3593 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -21,9 +21,9 @@ * Wrapper for Memcached_DataObject which knows its own schema definition. * Builds its own damn settings from a schema definition. * - * @author brion + * @author Brion Vibber */ -class Managed_DataObject extends Memcached_DataObject +abstract class Managed_DataObject extends Memcached_DataObject { /** * The One True Thingy that must be defined and declared. diff --git a/lib/schema.php b/lib/schema.php index e5def514e3..9ebef8aa26 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -41,6 +41,7 @@ if (!defined('STATUSNET')) { * @category Database * @package StatusNet * @author Evan Prodromou + * @author Brion Vibber * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ @@ -491,6 +492,75 @@ class Schema return $sql; } + + /** + * Convert an old-style set of ColumnDef objects into the current + * Drupal-style schema definition array, for backwards compatibility + * with plugins written for 0.9.x. + * + * @param string $tableName + * @param array $defs + * @return array + */ + function oldToNew($tableName, $defs) + { + $table = array(); + $prefixes = array( + 'tiny', + 'small', + 'medium', + 'big', + ); + foreach ($defs as $cd) { + $cd->addToTableDef($table); + $column = array(); + $column['type'] = $cd->type; + foreach ($prefixes as $prefix) { + if (substr($cd->type, 0, strlen($prefix)) == $prefix) { + $column['type'] = substr($cd->type, strlen($prefix)); + $column['size'] = $prefix; + break; + } + } + + if ($cd->size) { + if ($cd->type == 'varchar' || $cd->type == 'char') { + $column['length'] = $cd->size; + } + } + if (!$cd->nullable) { + $column['not null'] = true; + } + if ($cd->autoincrement) { + $column['type'] = 'serial'; + } + if ($cd->default) { + $column['default'] = $cd->default; + } + $table['fields'][$cd->name] = $column; + + if ($cd->key == 'PRI') { + // If multiple columns are defined as primary key, + // we'll pile them on in sequence. + if (!isset($table['primary key'])) { + $table['primary key'] = array(); + } + $table['primary key'][] = $cd->name; + } else if ($cd->key == 'MUL') { + // Individual multiple-value indexes are only per-column + // using the old ColumnDef syntax. + $idx = "{$tableName}_{$cd->name}_idx"; + $table['indexes'][$idx] = array($cd->name); + } else if ($cd->key == 'UNI') { + // Individual unique-value indexes are only per-column + // using the old ColumnDef syntax. + $idx = "{$tableName}_{$cd->name}_idx"; + $table['unique keys'][$idx] = array($cd->name); + } + } + + return $table; + } } class SchemaTableMissingException extends Exception -- 2.39.5