diff options
author | Brion Vibber <brion@pobox.com> | 2010-08-16 15:14:16 -0700 |
---|---|---|
committer | Brion Vibber <brion@pobox.com> | 2010-08-16 15:14:16 -0700 |
commit | eaa4ded053ca3d4a3d46ccbb0cf585d0608dd9c0 (patch) | |
tree | 42d0494c4484f7ab146442b1e9865f689f719e2f /lib/schema.php | |
parent | aff54d8efb9c984fb5c10b8dda0fab88727d97e5 (diff) |
first pass at columndef->drupal-style array converter (need to handle some more things probably; untested)
Diffstat (limited to 'lib/schema.php')
-rw-r--r-- | lib/schema.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/schema.php b/lib/schema.php index e5def514e..9ebef8aa2 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -41,6 +41,7 @@ if (!defined('STATUSNET')) { * @category Database * @package StatusNet * @author Evan Prodromou <evan@status.net> + * @author Brion Vibber <brion@status.net> * @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 |