3 * StatusNet, the distributed open-source microblogging tool
5 * Database schema utilities
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * Class representing the database schema
37 * A class representing the database schema. Can be used to
38 * manipulate the schema -- especially for plugins and upgrade
43 * @author Evan Prodromou <evan@status.net>
44 * @author Brenda Wallace <shiny@cpan.org>
45 * @author Brion Vibber <brion@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
50 class PgsqlSchema extends Schema
54 * Returns a table definition array for the table
55 * in the schema with the given name.
57 * Throws an exception if the table is not found.
59 * @param string $table Name of the table to get
61 * @return array tabledef for that table.
64 public function getTableDef($table)
69 // Pull column data from INFORMATION_SCHEMA
70 $columns = $this->fetchMetaInfo($table, 'columns', 'ordinal_position');
71 if (count($columns) == 0) {
72 throw new SchemaTableMissingException("No such table: $table");
75 foreach ($columns as $row) {
77 $name = $row['column_name'];
81 list($type, $size) = $this->reverseMapType($row['udt_name']);
82 $field['type'] = $type;
84 $field['size'] = $size;
87 if ($type == 'char' || $type == 'varchar') {
88 if ($row['character_maximum_length'] !== null) {
89 $field['length'] = intval($row['character_maximum_length']);
92 if ($type == 'numeric') {
93 // Other int types may report these values, but they're irrelevant.
95 if ($row['numeric_precision'] !== null) {
96 $field['precision'] = intval($row['numeric_precision']);
98 if ($row['numeric_scale'] !== null) {
99 $field['scale'] = intval($row['numeric_scale']);
102 if ($row['is_nullable'] == 'NO') {
103 $field['not null'] = true;
105 if ($row['column_default'] !== null) {
106 $field['default'] = $row['column_default'];
107 if ($this->isNumericType($type)) {
108 $field['default'] = intval($field['default']);
112 $def['fields'][$name] = $field;
115 // Pull constraint data from INFORMATION_SCHEMA
116 // @fixme also find multi-val indexes
117 // @fixme distinguish the primary key
118 // @fixme pull foreign key references
119 $keyColumns = $this->fetchMetaInfo($table, 'key_column_usage', 'constraint_name,ordinal_position');
122 foreach ($keyColumns as $row) {
123 $keyName = $row['constraint_name'];
124 $keyCol = $row['column_name'];
125 if (!isset($keys[$keyName])) {
126 $keys[$keyName] = array();
128 $keys[$keyName][] = $keyCol;
131 foreach ($keys as $keyName => $cols) {
132 $def['unique indexes'][$keyName] = $cols;
138 * Pull some INFORMATION.SCHEMA data for the given table.
140 * @param string $table
141 * @return array of arrays
143 function fetchMetaInfo($table, $infoTable, $orderBy=null)
145 $query = "SELECT * FROM information_schema.%s " .
146 "WHERE table_name='%s'";
147 $sql = sprintf($query, $infoTable, $table);
149 $sql .= ' ORDER BY ' . $orderBy;
151 return $this->fetchQueryData($sql);
155 * Creates a table with the given names and columns.
157 * @param string $name Name of the table
158 * @param array $columns Array of ColumnDef objects
161 * @return boolean success flag
164 public function createTable($name, $columns)
171 $sql = "CREATE TABLE $name (\n";
173 for ($i = 0; $i < count($columns); $i++) {
181 $sql .= $this->_columnSql($cd);
184 $uniques[] = $cd->name;
187 $primary[] = $cd->name;
190 $indices[] = $cd->name;
195 if (count($primary) > 0) { // it really should be...
196 $sql .= ",\n PRIMARY KEY (" . implode(',', $primary) . ")";
202 foreach ($uniques as $u) {
203 $sql .= "\n CREATE index {$name}_{$u}_idx ON {$name} ($u); ";
206 foreach ($indices as $i) {
207 $sql .= "CREATE index {$name}_{$i}_idx ON {$name} ($i)";
209 $res = $this->conn->query($sql);
211 if (PEAR::isError($res)) {
212 throw new Exception($res->getMessage(). ' SQL was '. $sql);
219 * Translate the (mostly) mysql-ish column types into somethings more standard
220 * @param string column type
222 * @return string postgres happy column type
224 private function _columnTypeTranslation($type) {
226 'datetime' => 'timestamp',
228 if(!empty($map[$type])) {
235 * Modifies a column in the schema.
237 * The name must match an existing column and table.
239 * @param string $table name of the table
240 * @param ColumnDef $columndef new definition of the column.
242 * @return boolean success flag
245 public function modifyColumn($table, $columndef)
247 $sql = "ALTER TABLE $table ALTER COLUMN TYPE " .
248 $this->_columnSql($columndef);
250 $res = $this->conn->query($sql);
252 if (PEAR::isError($res)) {
253 throw new Exception($res->getMessage());
260 * Return the proper SQL for creating or
263 * Appropriate for use in CREATE TABLE or
264 * ALTER TABLE statements.
266 * @param string $tableName
267 * @param array $tableDef
268 * @param string $columnName
269 * @param array $cd column to create
271 * @return string correct SQL for that column
274 function columnSql($name, array $cd)
277 $line[] = parent::_columnSql($cd);
279 if ($table['foreign keys'][$name]) {
280 foreach ($table['foreign keys'][$name] as $foreignTable => $foreignColumn) {
281 $line[] = 'references';
282 $line[] = $this->quoteId($foreignTable);
283 $line[] = '(' . $this->quoteId($foreignColumn) . ')';
287 return implode(' ', $line);
291 * Append phrase(s) to an array of partial ALTER TABLE chunks in order
292 * to alter the given column from its old state to a new one.
294 * @param array $phrase
295 * @param string $columnName
296 * @param array $old previous column definition as found in DB
297 * @param array $cd current column definition
299 function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
301 $prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' ';
303 $oldType = $this->mapType($old);
304 $newType = $this->mapType($cd);
305 if ($oldType != $newType) {
306 $phrase[] = $prefix . 'TYPE ' . $newType;
309 if (!empty($old['not null']) && empty($cd['not null'])) {
310 $phrase[] = $prefix . 'DROP NOT NULL';
311 } else if (empty($old['not null']) && !empty($cd['not null'])) {
312 $phrase[] = $prefix . 'SET NOT NULL';
315 if (isset($old['default']) && !isset($cd['default'])) {
316 $phrase[] = $prefix . 'DROP DEFAULT';
317 } else if (!isset($old['default']) && isset($cd['default'])) {
318 $phrase[] = $prefix . 'SET DEFAULT ' . $this->quoteDefaultValue($cd);
323 * Quote a db/table/column identifier if necessary.
325 * @param string $name
328 function quoteIdentifier($name)
330 return '"' . $name . '"';
333 function mapType($column)
335 $map = array('serial' => 'bigserial', // FIXME: creates the wrong name for the sequence for some internal sequence-lookup function, so better fix this to do the real 'create sequence' dance.
336 'numeric' => 'decimal',
337 'datetime' => 'timestamp',
340 $type = $column['type'];
341 if (isset($map[$type])) {
345 if (!empty($column['size'])) {
346 $size = $column['size'];
347 if ($type == 'integer' &&
348 in_array($size, array('small', 'big'))) {
349 $type = $size . 'int';
356 // @fixme need name... :P
357 function typeAndSize($column)
359 if ($column['type'] == 'enum') {
360 $vals = array_map(array($this, 'quote'), $column['enum']);
361 return "text check ($name in " . implode(',', $vals) . ')';
363 return parent::typeAndSize($column);
368 * Map a native type back to an independent type + size
370 * @param string $type
371 * @return array ($type, $size) -- $size may be null
373 protected function reverseMapType($type)
375 $type = strtolower($type);
377 'int4' => array('int', null),
378 'int8' => array('int', 'big'),
379 'bytea' => array('blob', null),
381 if (isset($map[$type])) {
384 return array($type, null);