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());
261 * Ensures that a table exists with the given
262 * name and the given column definitions.
264 * If the table does not yet exist, it will
265 * create the table. If it does exist, it will
266 * alter the table to match the column definitions.
268 * @param string $tableName name of the table
269 * @param array $columns array of ColumnDef
270 * objects for the table
272 * @return boolean success flag
275 public function ensureTable($tableName, $columns)
277 // XXX: DB engine portability -> toilet
280 $td = $this->getTableDef($tableName);
282 } catch (Exception $e) {
283 if (preg_match('/no such table/', $e->getMessage())) {
284 return $this->createTable($tableName, $columns);
290 $cur = $this->_names($td->columns);
291 $new = $this->_names($columns);
293 $toadd = array_diff($new, $cur);
294 $todrop = array_diff($cur, $new);
295 $same = array_intersect($new, $cur);
297 foreach ($same as $m) {
298 $curCol = $this->_byName($td->columns, $m);
299 $newCol = $this->_byName($columns, $m);
302 if (!$newCol->equals($curCol)) {
304 // stop it detecting different types and trying to modify on every page request
305 // $tomod[] = $newCol->name;
308 if (count($toadd) + count($todrop) + count($tomod) == 0) {
313 // For efficiency, we want this all in one
314 // query, instead of using our methods.
318 foreach ($toadd as $columnName) {
319 $cd = $this->_byName($columns, $columnName);
321 $phrase[] = 'ADD COLUMN ' . $this->_columnSql($cd);
324 foreach ($todrop as $columnName) {
325 $phrase[] = 'DROP COLUMN ' . $columnName;
328 foreach ($tomod as $columnName) {
329 $cd = $this->_byName($columns, $columnName);
332 $phrase[] = 'DROP COLUMN ' . $columnName;
333 $phrase[] = 'ADD COLUMN ' . $this->_columnSql($cd);
336 $sql = 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $phrase);
337 $res = $this->conn->query($sql);
339 if (PEAR::isError($res)) {
340 throw new Exception($res->getMessage());
347 * Return the proper SQL for creating or
350 * Appropriate for use in CREATE TABLE or
351 * ALTER TABLE statements.
353 * @param string $tableName
354 * @param array $tableDef
355 * @param string $columnName
356 * @param array $cd column to create
358 * @return string correct SQL for that column
361 function columnSql($name, array $cd)
364 $line[] = parent::_columnSql($cd);
366 if ($table['foreign keys'][$name]) {
367 foreach ($table['foreign keys'][$name] as $foreignTable => $foreignColumn) {
368 $line[] = 'references';
369 $line[] = $this->quoteId($foreignTable);
370 $line[] = '(' . $this->quoteId($foreignColumn) . ')';
374 return implode(' ', $line);
377 function mapType($column)
379 $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.
380 'numeric' => 'decimal',
381 'datetime' => 'timestamp',
384 $type = $column['type'];
385 if (isset($map[$type])) {
389 if (!empty($column['size'])) {
390 $size = $column['size'];
391 if ($type == 'integer' &&
392 in_array($size, array('small', 'big'))) {
393 $type = $size . 'int';
400 // @fixme need name... :P
401 function typeAndSize($column)
403 if ($column['type'] == 'enum') {
404 $vals = array_map(array($this, 'quote'), $column['enum']);
405 return "text check ($name in " . implode(',', $vals) . ')';
407 return parent::typeAndSize($column);
412 * Map a native type back to an independent type + size
414 * @param string $type
415 * @return array ($type, $size) -- $size may be null
417 protected function reverseMapType($type)
419 $type = strtolower($type);
421 'int4' => array('int', null),
422 'int8' => array('int', 'big'),
423 'bytea' => array('blob', null),
425 if (isset($map[$type])) {
428 return array($type, null);