1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-06 06:37:33 +02:00

MySQL: Support CHECK constraint

added list of check constraints to mysql table view

added create, alter, drop check constraints (mysql)

chore: use the correct function to sql-escape

fix: feature detection for check constraints (mysql)

fix: get check constraint info (mysql)

correct the capitalization of a section title

to be consistent with the other section titles

added a missing `</thead>`
This commit is contained in:
Joe Koop
2022-10-28 20:05:58 -05:00
committed by Jakub Vrana
parent f652ff807e
commit 550289de51
9 changed files with 89 additions and 7 deletions

35
adminer/check.inc.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
$TABLE = $_GET["check"];
$name = $_GET["name"];
$row = $_POST;
if ($row && !$error) {
$result = ($name == "" || queries("ALTER TABLE " . table($TABLE) . " DROP CHECK " . idf_escape($name)));
if (!$row["drop"] && $result) {
$result = queries("ALTER TABLE " . table($TABLE) . " ADD" . ($row["name"] != "" ? " CONSTRAINT " . idf_escape($row["name"]) . "" : "") . " CHECK ($row[clause])"); //! SQL injection
}
queries_redirect(
ME . "table=" . urlencode($TABLE),
($row["drop"] ? lang('Check has been dropped.') : ($name != "" ? lang('Check has been altered.') : lang('Check has been created.'))),
$result
);
}
page_header(($name != "" ? lang('Alter check') . ": " . h($name) : lang('Create check')), $error, array("table" => $TABLE));
if (!$row) {
$checks = check_constraints($TABLE);
$row = array("name" => $name, "clause" => $checks[$name]);
}
?>
<form action="" method="post">
<p><?php echo lang('Name'); ?>: <input name="name" value="<?php echo h($row["name"]); ?>" data-maxlength="64" autocapitalize="off"><?php echo doc_link(array(
'sql' => 'create-table-check-constraints.html',
'mariadb' => 'constraint/',
)); ?>
<p><?php textarea("clause", $row["clause"]); ?>
<p><input type="submit" value="<?php echo lang('Save'); ?>">
<?php if ($name != "") { ?><input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"><?php echo confirm(lang('Drop %s?', $name)); ?><?php } ?>
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>

View File

@@ -864,6 +864,18 @@ if (!defined("DRIVER")) {
return true; return true;
} }
/** Get defined check constraints
* @param string
* @return array array($name => $statement)
*/
function check_constraints($table) {
// MariaDB contains CHECK_CONSTRAINTS.TABLE_NAME, MySQL not
return get_key_vals("SELECT c.CONSTRAINT_NAME, c.CHECK_CLAUSE
FROM information_schema.CHECK_CONSTRAINTS c
JOIN information_schema.TABLE_CONSTRAINTS t ON c.CONSTRAINT_SCHEMA = t.CONSTRAINT_SCHEMA AND c.CONSTRAINT_NAME = t.CONSTRAINT_NAME
WHERE c.CONSTRAINT_SCHEMA = " . q(DB) . " AND t.TABLE_NAME = " . q($table));
}
/** Get information about trigger /** Get information about trigger
* @param string trigger name * @param string trigger name
* @return array array("Trigger" => , "Timing" => , "Event" => , "Of" => , "Type" => , "Statement" => ) * @return array array("Trigger" => , "Timing" => , "Event" => , "Of" => , "Type" => , "Statement" => )
@@ -1128,11 +1140,11 @@ if (!defined("DRIVER")) {
} }
/** Check whether a feature is supported /** Check whether a feature is supported
* @param string "comment", "copy", "database", "descidx", "drop_col", "dump", "event", "indexes", "kill", "materializedview", "partitioning", "privileges", "procedure", "processlist", "routine", "scheme", "sequence", "status", "table", "trigger", "type", "variables", "view", "view_trigger" * @param string "check", "comment", "copy", "database", "descidx", "drop_col", "dump", "event", "indexes", "kill", "materializedview", "partitioning", "privileges", "procedure", "processlist", "routine", "scheme", "sequence", "status", "table", "trigger", "type", "variables", "view", "view_trigger"
* @return bool * @return bool
*/ */
function support($feature) { function support($feature) {
return !preg_match("~scheme|sequence|type|view_trigger|materializedview" . (min_version(8) ? "" : "|descidx" . (min_version(5.1) ? "" : "|event|partitioning" . (min_version(5) ? "" : "|routine|trigger|view"))) . "~", $feature); return !preg_match("~scheme|sequence|type|view_trigger|materializedview" . (min_version(8) ? "" : "|descidx" . (min_version(5.1) ? "" : "|event|partitioning" . (min_version(5) ? "" : "|routine|trigger|view"))) . (min_version('8.0.16', '10.2.1') ? "" : "|check") . "~", $feature);
} }
/** Kill a process /** Kill a process

View File

@@ -1,2 +1,2 @@
<?php <?php
$VERSION = "4.16.1-dev"; $VERSION = "4.17.0-dev";

View File

@@ -59,6 +59,8 @@ if (isset($_GET["download"])) {
include "./sequence.inc.php"; include "./sequence.inc.php";
} elseif (isset($_GET["type"])) { } elseif (isset($_GET["type"])) {
include "./type.inc.php"; include "./type.inc.php";
} elseif (isset($_GET["check"])) {
include "./check.inc.php";
} elseif (isset($_GET["trigger"])) { } elseif (isset($_GET["trigger"])) {
include "./trigger.inc.php"; include "./trigger.inc.php";
} elseif (isset($_GET["user"])) { } elseif (isset($_GET["user"])) {

View File

@@ -345,4 +345,12 @@ $translations = array(
'Type has been dropped.' => 'Typ byl odstraněn.', 'Type has been dropped.' => 'Typ byl odstraněn.',
'Type has been created.' => 'Typ byl vytvořen.', 'Type has been created.' => 'Typ byl vytvořen.',
'Alter type' => 'Pozměnit typ', 'Alter type' => 'Pozměnit typ',
// MySQL check constraints
'Checks' => 'Kontroly',
'Create check' => 'Vytvořit kontrolu',
'Alter check' => 'Změnit kontrolu',
'Check has been created.' => 'Kontrola byla vytvořena.',
'Check has been altered.' => 'Kontrola byla změněna.',
'Check has been dropped.' => 'Kontrola byla odstraněna.',
); );

View File

@@ -345,4 +345,12 @@ $translations = array(
'Type has been dropped.' => 'Xx.', 'Type has been dropped.' => 'Xx.',
'Type has been created.' => 'Xx.', 'Type has been created.' => 'Xx.',
'Alter type' => 'Xx', 'Alter type' => 'Xx',
// MySQL check constraints
'Checks' => 'Xx',
'Create check' => 'Xx',
'Alter check' => 'Xx',
'Check has been created.' => 'Xx.',
'Check has been altered.' => 'Xx.',
'Check has been dropped.' => 'Xx.',
); );

View File

@@ -48,14 +48,31 @@ if (!is_view($table_status)) {
. "</a>" . "</a>"
; ;
echo "(<i>" . implode("</i>, <i>", array_map('h', $foreign_key["target"])) . "</i>)"; echo "(<i>" . implode("</i>, <i>", array_map('h', $foreign_key["target"])) . "</i>)";
echo "<td>" . h($foreign_key["on_delete"]) . "\n"; echo "<td>" . h($foreign_key["on_delete"]);
echo "<td>" . h($foreign_key["on_update"]) . "\n"; echo "<td>" . h($foreign_key["on_update"]);
echo '<td><a href="' . h(ME . 'foreign=' . urlencode($TABLE) . '&name=' . urlencode($name)) . '">' . lang('Alter') . '</a>'; echo '<td><a href="' . h(ME . 'foreign=' . urlencode($TABLE) . '&name=' . urlencode($name)) . '">' . lang('Alter') . '</a>';
echo "\n";
} }
echo "</table>\n"; echo "</table>\n";
} }
echo '<p class="links"><a href="' . h(ME) . 'foreign=' . urlencode($TABLE) . '">' . lang('Add foreign key') . "</a>\n"; echo '<p class="links"><a href="' . h(ME) . 'foreign=' . urlencode($TABLE) . '">' . lang('Add foreign key') . "</a>\n";
} }
if (support("check")) {
echo "<h3 id='checks'>" . lang('Checks') . "</h3>\n";
$check_constraints = check_constraints($TABLE);
if ($check_constraints) {
echo "<table cellspacing='0'>\n";
foreach ($check_constraints as $key => $val) {
echo "<tr title='" . h($key) . "'>";
echo "<td><code class='jush-$jush'>" . h($val);
echo "<td><a href='" . h(ME . 'check=' . urlencode($TABLE) . '&name=' . urlencode($key)) . "'>" . lang('Alter') . "</a>";
echo "\n";
}
echo "</table>\n";
}
echo '<p class="links"><a href="' . h(ME) . 'check=' . urlencode($TABLE) . '">' . lang('Create check') . "</a>\n";
}
} }
if (support(is_view($table_status) ? "view_trigger" : "trigger")) { if (support(is_view($table_status) ? "view_trigger" : "trigger")) {

View File

@@ -1,5 +1,6 @@
Adminer 4.16.1-dev: Adminer 4.17.0-dev:
Hide index column options by default Hide index column options by default
MySQL: Support CHECK constraint
PostgreSQL: Link user defined types PostgreSQL: Link user defined types
PostgreSQL: Constraint enum values in editing (bug #270) PostgreSQL: Constraint enum values in editing (bug #270)
SQLite: Show all supported pragmas in Variables SQLite: Show all supported pragmas in Variables

View File

@@ -30,7 +30,6 @@ Rank, Tree structure
MySQL: MySQL:
Data longer than max_allowed_packet can be sent by mysqli_stmt_send_long_data() Data longer than max_allowed_packet can be sent by mysqli_stmt_send_long_data()
Check constraints: https://github.com/vrana/adminer/pull/458
MariaDB: MariaDB:
Documentation links Documentation links