From af5d519266f3e38bce3b0f7b05adb8d0dbb0f824 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 7 Mar 2025 05:55:09 +0100 Subject: [PATCH] SQLite: Support generated columns --- adminer/drivers/sqlite.inc.php | 21 +++++++++++++++++++-- changes.txt | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index 7c5f2647..8d0e10d6 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -146,6 +146,13 @@ if (isset($_GET["sqlite"])) { var $functions = array("hex", "length", "lower", "round", "unixepoch", "upper"); var $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"); + function __construct($connection) { + parent::__construct($connection); + if (min_version(3.31, 0, $connection)) { + $this->generated = array("STORED", "VIRTUAL"); + } + } + function structuredTypes() { return array_keys($this->types[0]); } @@ -255,7 +262,7 @@ if (isset($_GET["sqlite"])) { global $connection; $return = array(); $primary = ""; - foreach (get_rows("PRAGMA table_info(" . table($table) . ")") as $row) { + foreach (get_rows("PRAGMA table_" . (min_version(3.31) ? "x" : "") . "info(" . table($table) . ")") as $row) { $name = $row["name"]; $type = strtolower($row["type"]); $default = $row["dflt_value"]; @@ -278,13 +285,20 @@ if (isset($_GET["sqlite"])) { } } $sql = $connection->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table)); - preg_match_all('~(("[^"]*+")+|[a-z0-9_]+)\s+text\s+COLLATE\s+(\'[^\']+\'|\S+)~i', $sql, $matches, PREG_SET_ORDER); + $idf = '(("[^"]*+")+|[a-z0-9_]+)'; + preg_match_all('~' . $idf . '\s+text\s+COLLATE\s+(\'[^\']+\'|\S+)~i', $sql, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $name = str_replace('""', '"', preg_replace('~^"|"$~', '', $match[1])); if ($return[$name]) { $return[$name]["collation"] = trim($match[3], "'"); } } + preg_match_all('~' . $idf . '\s.*GENERATED ALWAYS AS \((.+)\) (STORED|VIRTUAL)~i', $sql, $matches, PREG_SET_ORDER); + foreach ($matches as $match) { + $name = str_replace('""', '"', preg_replace('~^"|"$~', '', $match[1])); + $return[$name]["default"] = $match[3]; + $return[$name]["generated"] = strtoupper($match[4]); + } return $return; } @@ -537,6 +551,9 @@ if (isset($_GET["sqlite"])) { queries("BEGIN"); } foreach ($fields as $key => $field) { + if (preg_match('~GENERATED~', $field[3])) { + unset($originals[array_search($field[0], $originals)]); + } $fields[$key] = " " . implode($field); } $fields = array_merge($fields, array_filter($foreign)); diff --git a/changes.txt b/changes.txt index 68971e68..e707dd7a 100644 --- a/changes.txt +++ b/changes.txt @@ -15,6 +15,7 @@ PostgreSQL: Show accessible databases to non-owners (regression from 4.9.1) PostgreSQL: Skip editing generated columns PostgreSQL, MS SQL, Oracle: Hide table actions for information_schema SQLite: Support CHECK constraint +SQLite: Support generated columns SQLite: Add command Check tables SQLite: Display all rows of variable values SQLite: Remove support for SQLite version 2