public function add_index($table_name, $column_name, $options = array())
{
if (empty($table_name)) {
throw new Ruckusing_Exception("Missing table name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
}
if (empty($column_name)) {
throw new Ruckusing_Exception("Missing column name parameter", Ruckusing_Exception::INVALID_ARGUMENT);
}
//unique index?
if (is_array($options) && array_key_exists('unique', $options) && $options['unique'] === true) {
$unique = true;
} else {
$unique = false;
}
//did the user specify an index name?
if (is_array($options) && array_key_exists('name', $options)) {
$index_name = $options['name'];
} else {
$index_name = Ruckusing_Util_Naming::index_name($table_name, $column_name);
}
if (strlen($index_name) > PG_MAX_IDENTIFIER_LENGTH) {
$msg = "The auto-generated index name is too long for Postgres (max is 64 chars). ";
$msg .= "Considering using 'name' option parameter to specify a custom name for this index.";
$msg .= " Note: you will also need to specify";
$msg .= " this custom name in a drop_index() - if you have one.";
throw new Ruckusing_Exception($msg, Ruckusing_Exception::INVALID_INDEX_NAME);
}
if (!is_array($column_name)) {
$column_names = array($column_name);
} else {
$column_names = $column_name;
}
$cols = array();
foreach ($column_names as $name) {
$cols[] = $this->quote_column_name($name);
}
$sql = sprintf("CREATE %sINDEX %s ON %s(%s)", $unique ? "UNIQUE " : "", $this->quote_column_name($index_name), $this->quote_column_name($table_name), join(", ", $cols));
return $this->execute_ddl($sql);
}