SQLite: Support UPDATE OF triggers

This commit is contained in:
Jakub Vrana 2014-03-15 10:58:24 -07:00
parent fc668ea326
commit 46bb56cab9
10 changed files with 40 additions and 13 deletions

View file

@ -567,6 +567,7 @@ WHERE sys1.xtype = 'TR' AND sys2.name = " . q($table)
function trigger_options() {
return array(
"Timing" => array("AFTER", "INSTEAD OF"),
"Event" => array("INSERT", "UPDATE", "DELETE"),
"Type" => array("AS"),
);
}

View file

@ -755,7 +755,7 @@ if (!defined("DRIVER")) {
/** Get information about trigger
* @param string trigger name
* @return array array("Trigger" => , "Timing" => , "Event" => , "Type" => , "Statement" => )
* @return array array("Trigger" => , "Timing" => , "Event" => , "Of" => , "Type" => , "Statement" => )
*/
function trigger($name) {
if ($name == "") {
@ -778,12 +778,12 @@ if (!defined("DRIVER")) {
}
/** Get trigger options
* @return array ("Timing" => array(), "Type" => array())
* @return array ("Timing" => array(), "Event" => array(), "Type" => array())
*/
function trigger_options() {
return array(
"Timing" => array("BEFORE", "AFTER"),
// Event is always INSERT, UPDATE, DELETE
"Event" => array("INSERT", "UPDATE", "DELETE"),
"Type" => array("FOR EACH ROW"),
);
}

View file

@ -507,6 +507,7 @@ ORDER BY conkey, conname") as $row) {
function trigger_options() {
return array(
"Timing" => array("BEFORE", "AFTER"),
"Event" => array("INSERT", "UPDATE", "DELETE"),
"Type" => array("FOR EACH ROW", "FOR EACH STATEMENT"),
);
}

View file

@ -634,18 +634,21 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
if ($name == "") {
return array("Statement" => "BEGIN\n\t;\nEND");
}
$idf = '(?:[^`"\\s]+|`[^`]*`|"[^"]*")+';
$trigger_options = trigger_options();
preg_match(
'~^CREATE\\s+TRIGGER\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*(BEFORE|AFTER|INSTEAD\\s+OF)\\s+([a-z]+)\\s+ON\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*(?:FOR\\s*EACH\\s*ROW\\s)?(.*)~is',
"~^CREATE\\s+TRIGGER\\s*$idf\\s*(" . implode("|", $trigger_options["Timing"]) . ")\\s+([a-z]+)(?:\\s+OF\\s+($idf))?\\s+ON\\s*$idf\\s*(?:FOR\\s+EACH\\s+ROW\\s)?(.*)~is",
$connection->result("SELECT sql FROM sqlite_master WHERE type = 'trigger' AND name = " . q($name)),
$match
);
return array("Timing" => strtoupper($match[1]), "Event" => strtoupper($match[2]), "Trigger" => $name, "Statement" => $match[3]);
return array("Timing" => strtoupper($match[1]), "Event" => strtoupper($match[2]), "Of" => $match[3], "Trigger" => $name, "Statement" => $match[4]);
}
function triggers($table) {
$return = array();
$trigger_options = trigger_options();
foreach (get_rows("SELECT * FROM sqlite_master WHERE type = 'trigger' AND tbl_name = " . q($table)) as $row) {
preg_match('~^CREATE\\s+TRIGGER\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*([a-z]+)\\s*([a-z]+)~i', $row["sql"], $match);
preg_match('~^CREATE\\s+TRIGGER\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*(' . implode("|", $trigger_options["Timing"]) . ')\\s*(.*)\\s+ON\\b~iU', $row["sql"], $match);
$return[$row["name"]] = array($match[1], $match[2]);
}
return $return;
@ -654,6 +657,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
function trigger_options() {
return array(
"Timing" => array("BEFORE", "AFTER", "INSTEAD OF"),
"Event" => array("INSERT", "UPDATE", "UPDATE OF", "DELETE"),
"Type" => array("FOR EACH ROW"),
);
}

View file

@ -394,7 +394,7 @@ function drop_create($drop, $create, $drop_created, $test, $drop_test, $location
*/
function create_trigger($on, $row) {
global $jush;
$timing_event = " $row[Timing] $row[Event]";
$timing_event = " $row[Timing] $row[Event]" . ($row["Event"] == "UPDATE OF" ? " " . idf_escape($row["Of"]) : "");
return "CREATE TRIGGER "
. idf_escape($row["Trigger"])
. ($jush == "mssql" ? $on . $timing_event : $timing_event . $on)

View file

@ -488,6 +488,21 @@ function indexesAddColumn(field, prefix) {
/** Handle changing trigger time or event
* @param RegExp
* @param string
* @param HTMLFormElement
*/
function triggerChange(tableRe, table, form) {
var formEvent = selectValue(form['Event']);
if (tableRe.test(form['Trigger'].value)) {
form['Trigger'].value = table + '_' + (selectValue(form['Timing']).charAt(0) + formEvent.charAt(0)).toLowerCase();
}
alterClass(form['Of'], 'hidden', formEvent != 'UPDATE OF');
}
var that, x, y; // em and tablePos defined in schema.inc.php
/** Get mouse position
@ -565,6 +580,8 @@ function schemaMouseup(ev, db) {
}
}
var helpOpen, helpIgnore; // when mouse outs <option> then it mouse overs border of <select> - ignore it
/** Display help

View file

@ -5,7 +5,9 @@
* @param [bool]
*/
function alterClass(el, className, enable) {
el.className = el.className.replace(RegExp('(^|\\s)' + className + '(\\s|$)'), '$2') + (enable ? ' ' + className : '');
if (el) {
el.className = el.className.replace(RegExp('(^|\\s)' + className + '(\\s|$)'), '$2') + (enable ? ' ' + className : '');
}
}
/** Toggle visibility

View file

@ -79,7 +79,7 @@ if (support(is_view($table_status) ? "view_trigger" : "trigger")) {
if ($triggers) {
echo "<table cellspacing='0'>\n";
foreach ($triggers as $key => $val) {
echo "<tr valign='top'><td>$val[0]<td>$val[1]<th>" . h($key) . "<td><a href='" . h(ME . 'trigger=' . urlencode($TABLE) . '&name=' . urlencode($key)) . "'>" . lang('Alter') . "</a>\n";
echo "<tr valign='top'><td>" . h($val[0]) . "<td>" . h($val[1]) . "<th>" . h($key) . "<td><a href='" . h(ME . 'trigger=' . urlencode($TABLE) . '&name=' . urlencode($key)) . "'>" . lang('Alter') . "</a>\n";
}
echo "</table>\n";
}

View file

@ -2,11 +2,10 @@
$TABLE = $_GET["trigger"];
$name = $_GET["name"];
$trigger_options = trigger_options();
$trigger_event = array("INSERT", "UPDATE", "DELETE");
$row = (array) trigger($name) + array("Trigger" => $TABLE . "_bi");
if ($_POST) {
if (!$error && in_array($_POST["Timing"], $trigger_options["Timing"]) && in_array($_POST["Event"], $trigger_event) && in_array($_POST["Type"], $trigger_options["Type"])) {
if (!$error && in_array($_POST["Timing"], $trigger_options["Timing"]) && in_array($_POST["Event"], $trigger_options["Event"]) && in_array($_POST["Type"], $trigger_options["Type"])) {
// don't use drop_create() because there may not be more triggers for the same action
$on = " ON " . table($TABLE);
$drop = "DROP TRIGGER " . idf_escape($name) . ($jush == "pgsql" ? $on : "");
@ -35,11 +34,13 @@ page_header(($name != "" ? lang('Alter trigger') . ": " . h($name) : lang('Creat
<form action="" method="post" id="form">
<table cellspacing="0">
<tr><th><?php echo lang('Time'); ?><td><?php echo html_select("Timing", $trigger_options["Timing"], $row["Timing"], "if (/^" . preg_quote($TABLE, "/") . "_[ba][iud]$/.test(this.form['Trigger'].value)) this.form['Trigger'].value = '" . js_escape($TABLE) . "_' + selectValue(this).charAt(0).toLowerCase() + selectValue(this.form['Event']).charAt(0).toLowerCase();"); ?>
<tr><th><?php echo lang('Event'); ?><td><?php echo html_select("Event", $trigger_event, $row["Event"], "this.form['Timing'].onchange();"); ?>
<tr><th><?php echo lang('Time'); ?><td><?php echo html_select("Timing", $trigger_options["Timing"], $row["Timing"], "triggerChange(/^" . preg_quote($TABLE, "/") . "_[ba][iud]$/, '" . js_escape($TABLE) . "', this.form);"); ?>
<tr><th><?php echo lang('Event'); ?><td><?php echo html_select("Event", $trigger_options["Event"], $row["Event"], "this.form['Timing'].onchange();"); ?>
<?php echo (in_array("UPDATE OF", $trigger_options["Event"]) ? " <input name='Of' value='" . h($row["Of"]) . "' class='hidden'>": ""); ?>
<tr><th><?php echo lang('Type'); ?><td><?php echo html_select("Type", $trigger_options["Type"], $row["Type"]); ?>
</table>
<p><?php echo lang('Name'); ?>: <input name="Trigger" value="<?php echo h($row["Trigger"]); ?>" maxlength="64" autocapitalize="off">
<script type="text/javascript">document.getElementById('form')['Timing'].onchange();</script>
<p><?php textarea("Statement", $row["Statement"]); ?>
<p>
<input type="submit" value="<?php echo lang('Save'); ?>">

View file

@ -7,6 +7,7 @@ Print elapsed time in HTML instead of SQL command comment
Improve gzip export ratio (bug #387)
MySQL: Fix editing rows by binary values, bug since Adminer 3.7.1
MySQL: Respect daylight saving time in dump, bug since Adminer 3.6.4
SQLite: Support UPDATE OF triggers
SQLite: Display auto-created unique indexes, bug since Adminer 3.5.0
Editor: Fix login() method, bug since Adminer 4.0.0