Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add infrastructure for chosing index methods initial support for postgresql #469

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions adminer/drivers/elastic.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ function indexes($table, $connection2 = null) {
);
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function fields($table) {
global $connection;

Expand Down
18 changes: 18 additions & 0 deletions adminer/drivers/mongo.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function fields($table) {
return fields_from_edit();
}
Expand Down Expand Up @@ -445,6 +454,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function fields($table) {
global $driver;
$fields = fields_from_edit();
Expand Down
9 changes: 9 additions & 0 deletions adminer/drivers/mssql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function view($name) {
global $connection;
return array("select" => preg_replace('~^(?:[^[]|\[[^]]*])*\s+AS\s+~isU', '', $connection->result("SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = SCHEMA_NAME() AND TABLE_NAME = " . q($name))));
Expand Down
9 changes: 9 additions & 0 deletions adminer/drivers/mysql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

/** Get foreign keys in table
* @param string
* @return array array($name => array("db" => , "ns" => , "table" => , "source" => array(), "target" => array(), "on_delete" => , "on_update" => ))
Expand Down
9 changes: 9 additions & 0 deletions adminer/drivers/oracle.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function view($name) {
$view = views_table("view_name, text");
$rows = get_rows('SELECT text "select" FROM ' . $view . ' WHERE view_name = ' . q($name));
Expand Down
36 changes: 34 additions & 2 deletions adminer/drivers/pgsql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,11 @@ function indexes($table, $connection2 = null) {
$return = array();
$table_oid = $connection2->result("SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema()) AND relname = " . q($table));
$columns = get_key_vals("SELECT attnum, attname FROM pg_attribute WHERE attrelid = $table_oid AND attnum > 0", $connection2);
foreach (get_rows("SELECT relname, indisunique::int, indisprimary::int, indkey, indoption, (indpred IS NOT NULL)::int as indispartial FROM pg_index i, pg_class ci WHERE i.indrelid = $table_oid AND ci.oid = i.indexrelid", $connection2) as $row) {
foreach (get_rows("SELECT relname, indisunique::int, indisprimary::int, indkey, indoption, (indpred IS NOT NULL)::int as indispartial, am.amname as method FROM pg_index i, pg_class ci, pg_am am WHERE i.indrelid = $table_oid AND am.oid = ci.relam AND ci.oid = i.indexrelid ORDER BY indisprimary DESC", $connection2) as $row) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small piggyback, primary index should be the first in table

$relname = $row["relname"];
$return[$relname]["type"] = ($row["indispartial"] ? "INDEX" : ($row["indisprimary"] ? "PRIMARY" : ($row["indisunique"] ? "UNIQUE" : "INDEX")));
$return[$relname]["columns"] = array();
$return[$relname]["method"] = $row["method"];
foreach (explode(" ", $row["indkey"]) as $indkey) {
$return[$relname]["columns"][] = $columns[$indkey];
}
Expand All @@ -409,6 +410,32 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
static $methods = [];
if (!$methods) {

// default guess for old PG instances
$methods = array(
"btree",
"gin",
"gist"
);
if (min_version(9.6)) {
// better aproach is to take that from DB
$methods = [];
foreach (get_rows("SELECT amname FROM pg_am WHERE amtype = 'i'") as $row) {
$methods[] = $row['amname'];
}
}
}
return $methods;
}

function foreign_keys($table) {
global $on_actions;
$return = array();
Expand Down Expand Up @@ -562,7 +589,12 @@ function alter_indexes($table, $alter) {
} elseif ($val[2] == "DROP") {
$drop[] = idf_escape($val[1]);
} else {
$queries[] = "CREATE INDEX " . idf_escape($val[1] != "" ? $val[1] : uniqid($table . "_")) . " ON " . table($table) . " (" . implode(", ", $val[2]) . ")";
$usingSqlPart = "";
if ($val[3])
{
$usingSqlPart = "USING ".$val[3];
}
$queries[] = "CREATE INDEX " . idf_escape($val[1] != "" ? $val[1] : uniqid($table . "_")) . " ON " . table($table) . " $usingSqlPart (" . implode(", ", $val[2]) . ")";
}
}
if ($create) {
Expand Down
9 changes: 9 additions & 0 deletions adminer/drivers/sqlite.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function foreign_keys($table) {
$return = array();
foreach (get_rows("PRAGMA foreign_key_list(" . table($table) . ")") as $row) {
Expand Down
2 changes: 1 addition & 1 deletion adminer/include/adminer.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function tableIndexesPrint($indexes) {
. ($index["descs"][$key] ? " DESC" : "")
;
}
echo "<tr title='" . h($name) . "'><th>$index[type]<td>" . implode(", ", $print) . "\n";
echo "<tr title='" . h($name) . "'><th>$index[type]".(index_methods() ? " (".$index['method'].")" : "")."<td>".implode(", ", $print) . "\n";
}
echo "</table>\n";
}
Expand Down
12 changes: 10 additions & 2 deletions adminer/indexes.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
$columns = array();
$lengths = array();
$descs = array();
$indexMethod = array_key_exists("method", $index) && in_array($index["method"], index_methods()) ? $index["method"] : "";
$set = array();
ksort($index["columns"]);
foreach ($index["columns"] as $key => $column) {
Expand All @@ -48,13 +49,14 @@
&& array_values($existing["columns"]) === $columns
&& (!$existing["lengths"] || array_values($existing["lengths"]) === $lengths)
&& array_values($existing["descs"]) === $descs
&& (array_key_exists("method", $existing) && $existing["method"] === $indexMethod)
) {
// skip existing index
unset($indexes[$name]);
continue;
}
}
$alter[] = array($index["type"], $name, $set);
$alter[] = array($index["type"], $name, $set, $indexMethod);
}
}
}
Expand Down Expand Up @@ -98,6 +100,10 @@
<table cellspacing="0" class="nowrap">
<thead><tr>
<th id="label-type"><?php echo lang('Index Type'); ?>
<?php
if (index_methods())
echo "<th id=\"label-method\">".lang('Index method');
?>
<th><input type="submit" class="wayoff"><?php echo lang('Column (length)'); ?>
<th id="label-name"><?php echo lang('Name'); ?>
<th><noscript><?php echo "<input type='image' class='icon' name='add[0]' src='../adminer/static/plus.gif' alt='+' title='" . lang('Add next') . "'>"; ?></noscript>
Expand All @@ -115,7 +121,9 @@
foreach ($row["indexes"] as $index) {
if (!$_POST["drop_col"] || $j != key($_POST["drop_col"])) {
echo "<tr><td>" . html_select("indexes[$j][type]", array(-1 => "") + $index_types, $index["type"], ($j == count($row["indexes"]) ? "indexesAddRow.call(this);" : 1), "label-type");

if (index_methods()){
echo "<td>".html_select("indexes[$j][method]", array_merge([""], index_methods()), $index['method'], $onchange = true, "label-method");
}
echo "<td>";
ksort($index["columns"]);
$i = 1;
Expand Down
9 changes: 9 additions & 0 deletions plugins/drivers/clickhouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ function indexes($table, $connection2 = null) {
return array();
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function foreign_keys($table) {
return array();
}
Expand Down
9 changes: 9 additions & 0 deletions plugins/drivers/firebird.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ function indexes($table, $connection2 = null) {
return $return;
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function foreign_keys($table) {
return array();
}
Expand Down
9 changes: 9 additions & 0 deletions plugins/drivers/simpledb.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ function indexes($table, $connection2 = null) {
);
}

/**
* return list of supported index methods first one is default
* @return string[]
*/
function index_methods()
{
return array();
}

function fields($table) {
return fields_from_edit();
}
Expand Down