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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
50 static $_single = null;
51 protected $conn = null;
53 protected function __construct()
55 // XXX: there should be an easier way to do this.
57 $this->conn = $user->getDatabaseConnection();
64 if (empty(self::$_single)) {
65 self::$_single = new Schema();
67 return self::$_single;
70 public function getTableDef($name)
72 $res =& $this->conn->query('DESCRIBE ' . $name);
74 if (PEAR::isError($res)) {
75 throw new Exception($res->getMessage());
81 $td->columns = array();
85 while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
87 $cd = new ColumnDef();
89 $cd->name = $row['Field'];
91 $packed = $row['Type'];
93 if (preg_match('/^(\w+)\((\d+)\)$/', $packed, $match)) {
94 $cd->type = $match[1];
95 $cd->size = $match[2];
100 $cd->nullable = ($row['Null'] == 'YES') ? true : false;
101 $cd->key = $row['Key'];
102 $cd->default = $row['Default'];
103 $cd->extra = $row['Extra'];
105 $td->columns[] = $cd;
111 public function getColumnDef($table, $column)
113 $td = $this->getTableDef($table);
115 foreach ($td->columns as $cd) {
116 if ($cd->name == $column) {
124 public function getIndexDef($table, $index)
129 public function createTable($name, $columns, $indices=null)
135 $sql = "CREATE TABLE $name (\n";
137 for ($i = 0; $i < count($columns); $i++) {
145 $sql .= $this->_columnSql($cd);
149 $uniques[] = $cd->name;
152 $primary[] = $cd->name;
155 $indices[] = $cd->name;
160 if (count($primary) > 0) { // it really should be...
161 $sql .= ",\nconstraint primary key (" . implode(',', $primary) . ")";
164 foreach ($uniques as $u) {
165 $sql .= ",\nunique index {$name}_{$u}_idx ($u)";
168 foreach ($indices as $i) {
169 $sql .= ",\nindex {$name}_{$i}_idx ($i)";
176 $res =& $this->conn->query($sql);
178 if (PEAR::isError($res)) {
179 throw new Exception($res->getMessage());
185 public function dropTable($name)
187 $res =& $this->conn->query("DROP TABLE $name");
189 if (PEAR::isError($res)) {
190 throw new Exception($res->getMessage());
196 public function createIndex($name, $table, $columns)
200 public function dropIndex($name, $table)
204 public function addColumn($table, $columndef)
208 public function modifyColumn($table, $column, $columndef)
212 public function dropColumn($table, $column)
216 public function ensureTable($name, $columns, $indices)
218 $def = $this->tableDef($name);
220 return $this->createTable($name, $columns, $indices);
224 function _columnSql($cd)
226 $sql = "{$cd->name} ";
228 if (!empty($cd->size)) {
229 $sql .= "{$cd->type}({$cd->size}) ";
231 $sql .= "{$cd->type} ";
234 if (!empty($cd->default)) {
235 $sql .= "default {$cd->default} ";
237 $sql .= ($cd->nullable) ? "null " : "not null ";
260 function __construct($name, $type, $size=null, $nullable=null,
261 $key=null, $default=null, $extra=null) {
265 $this->nullable = $nullable;
267 $this->default = $default;
268 $this->extra = $extra;