/** * Returns metadata for all foreign keys in a table. * @param string * @return array */ public function getForeignKeys($table) { if (!$this->driver instanceof DibiSqlite3Driver) { // throw new NotSupportedException; // @see http://www.sqlite.org/foreignkeys.html } $this->driver->query("PRAGMA foreign_key_list([{$table}])"); $res = array(); while ($row = $this->driver->fetch(TRUE)) { $res[$row['id']]['name'] = $row['id']; // foreign key name $res[$row['id']]['local'][$row['seq']] = $row['from']; // local columns $res[$row['id']]['table'] = $row['table']; // referenced table $res[$row['id']]['foreign'][$row['seq']] = $row['to']; // referenced columns $res[$row['id']]['onDelete'] = $row['on_delete']; $res[$row['id']]['onUpdate'] = $row['on_update']; if ($res[$row['id']]['foreign'][0] == NULL) { $res[$row['id']]['foreign'] = NULL; } } $this->driver->free(); return array_values($res); }
/** * Returns metadata for all indexes in a table. * @param string * @return array */ public function getIndexes($table) { /*$table = $this->escape($table, dibi::TEXT); $this->query(" SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = $table AND TABLE_SCHEMA = DATABASE() AND REFERENCED_COLUMN_NAME IS NULL ");*/ $this->driver->query("SHOW INDEX FROM `{$table}`"); $res = array(); while ($row = $this->driver->fetch(TRUE)) { $res[$row['Key_name']]['name'] = $row['Key_name']; $res[$row['Key_name']]['unique'] = !$row['Non_unique']; $res[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY'; $res[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name']; } $this->driver->free(); return array_values($res); }