From 033a7570133d4183c2e162859e9b85fe7df3e40b Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 16 Aug 2010 16:31:18 -0700 Subject: More schema work in progress... removing duped code from schema child classes, rebuilding things a bit more (incomplete; non-working state) --- lib/schema.php | 64 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'lib/schema.php') diff --git a/lib/schema.php b/lib/schema.php index 9ebef8aa2..d20bced4a 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -224,7 +224,7 @@ class Schema } if (empty($name)) { - $name = "$table_".implode("_", $columnNames)."_idx"; + $name = "{$table}_".implode("_", $columnNames)."_idx"; } $res = $this->conn->query("ALTER TABLE $table ". @@ -422,7 +422,7 @@ class Schema * @return array strings for name values */ - private function _names($cds) + protected function _names($cds) { $names = array(); @@ -443,7 +443,7 @@ class Schema * @return ColumnDef matching item or null if no match. */ - private function _byName($cds, $name) + protected function _byName($cds, $name) { foreach ($cds as $cd) { if ($cd->name == $name) { @@ -466,31 +466,53 @@ class Schema * @return string correct SQL for that column */ - private function _columnSql($cd) + function columnSql(array $cd) { - $sql = "{$cd->name} "; - - if (!empty($cd->size)) { - $sql .= "{$cd->type}({$cd->size}) "; - } else { - $sql .= "{$cd->type} "; + $line = array(); + $line[] = $this->typeAndSize(); + + if (isset($cd['default'])) { + $line[] = 'default'; + $line[] = $this->quoted($cd['default']); + } else if (!empty($cd['not null'])) { + // Can't have both not null AND default! + $line[] = 'not null'; } - if (!empty($cd->default)) { - $sql .= "default {$cd->default} "; - } else { - $sql .= ($cd->nullable) ? "null " : "not null "; - } + return implode(' ', $line); + } - if (!empty($cd->auto_increment)) { - $sql .= " auto_increment "; - } + /** + * + * @param string $column canonical type name in defs + * @return string native DB type name + */ + function mapType($column) + { + return $column; + } - if (!empty($cd->extra)) { - $sql .= "{$cd->extra} "; + function typeAndSize($column) + { + $type = $this->mapType($column); + $lengths = array(); + + if ($column['type'] == 'numeric') { + if (isset($column['precision'])) { + $lengths[] = $column['precision']; + if (isset($column['scale'])) { + $lengths[] = $column['scale']; + } + } + } else if (isset($column['length'])) { + $lengths[] = $column['length']; } - return $sql; + if ($lengths) { + return $type . '(' . implode(',', $lengths) . ')'; + } else { + return $type; + } } /** -- cgit v1.2.3-54-g00ecf