]> git.joonet.de Git - adminer.git/commitdiff
SQLite: Support UPDATE OF triggers
authorJakub Vrana <jakub@vrana.cz>
Sat, 15 Mar 2014 17:58:24 +0000 (10:58 -0700)
committerJakub Vrana <jakub@vrana.cz>
Sat, 15 Mar 2014 18:02:14 +0000 (11:02 -0700)
adminer/drivers/mssql.inc.php
adminer/drivers/mysql.inc.php
adminer/drivers/pgsql.inc.php
adminer/drivers/sqlite.inc.php
adminer/include/editing.inc.php
adminer/static/editing.js
adminer/static/functions.js
adminer/table.inc.php
adminer/trigger.inc.php
changes.txt

index ab19737c7a44b493a3ca43006773fa6e86cb4182..c0b6268e4528cd4656e618c0115d2561a03856dc 100644 (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"),
                );
        }
index 1357fd0d49590671808a49329a7ef3ba21f84eeb..7068ac0db3d2a4d920463c38e8d962a1f7c0070e 100644 (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"),
                );
        }
index 637d0ad96df56991c88cfa96f50369d3e1594163..abd0c578052ba239e79a902b2db3967d218cf51b 100644 (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"),
                );
        }
index 6f63d4ccf21cee2f69da9fe0341fa31b84b41d03..428d3e5f2057fa70b003f2f9a3542010fc1a31dc 100644 (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"),
                );
        }
index 4da94da27ec0c3abe6abfb6e6eabc12d6810cbcb..6f16373fb00b20a8c2a7dd221f0a5b558e332a66 100644 (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)
index ac5a08b21c7abc37a998112d308d532fda39a214..556286228eeb9c6f87726b5477d148634ec47c51 100644 (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
index 90a9b49314ae9d9341698be36814927fa64f0625..c18fe9bc4c2194bd7e5ca1e75c5a54f67198ba27 100644 (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
index aa12583cef61b74d16fd2ecb0ec8374a901ef577..c3694a3e43316ce24264e14401f21fbc506bcd73 100644 (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";
        }
index 4b33b9aa50cc791ac36f0a03917d3d4925b32747..4270770e3b755676b6a4288c5768883de27cee83 100644 (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'); ?>">
index 9f898cc55d93183e7811bcabddb93b006656e7a8..39943fb2b8f5875dd94e0d13057212692ab0fe0a 100644 (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