]> git.joonet.de Git - adminer.git/commitdiff
Respect PostgreSQL custom types
authorJakub Vrana <jakub@vrana.cz>
Fri, 21 May 2010 13:07:59 +0000 (15:07 +0200)
committerJakub Vrana <jakub@vrana.cz>
Fri, 21 May 2010 13:07:59 +0000 (15:07 +0200)
adminer/create.inc.php
adminer/drivers/pgsql.inc.php
adminer/include/editing.inc.php
adminer/lang/cs.inc.php
changes.txt
todo.txt

index 0a0569b0675a2bf9f4d6010f4d30af67b7865d09..5bc501dea5240d86dd03c41ae805e3f981c766f5 100644 (file)
@@ -25,27 +25,26 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"]
                $orig_field = reset($orig_fields);
                $after = "FIRST";
                foreach ($_POST["fields"] as $key => $field) {
-                       $type_field = (isset($types[$field["type"]]) ? $field : $referencable_primary[$foreign_keys[$field["type"]]]);
+                       $foreign_key = $foreign_keys[$field["type"]];
+                       $type_field = (isset($foreign_key) ? $referencable_primary[$foreign_key] : $field); //! can collide with user defined type
                        if ($field["field"] != "") {
-                               if ($type_field) {
-                                       if (!$field["has_default"]) {
-                                               $field["default"] = null;
-                                       }
-                                       $default = eregi_replace(" *on update CURRENT_TIMESTAMP", "", $field["default"]);
-                                       if ($default != $field["default"]) { // preg_replace $count is available since PHP 5.1.0
-                                               $field["on_update"] = "CURRENT_TIMESTAMP";
-                                               $field["default"] = $default;
-                                       }
-                                       if ($key == $_POST["auto_increment_col"]) {
-                                               $field["auto_increment"] = true;
-                                       }
-                                       $process_field = process_field($field, $type_field);
-                                       if ($process_field != process_field($orig_field, $orig_field)) {
-                                               $fields[] = array($field["orig"], $process_field, $after);
-                                       }
-                                       if (!isset($types[$field["type"]])) {
-                                               $foreign[] = ($TABLE != "" ? "ADD " : "  ") . "FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . idf_escape($foreign_keys[$field["type"]]) . " (" . idf_escape($type_field["field"]) . ")";
-                                       }
+                               if (!$field["has_default"]) {
+                                       $field["default"] = null;
+                               }
+                               $default = eregi_replace(" *on update CURRENT_TIMESTAMP", "", $field["default"]);
+                               if ($default != $field["default"]) { // preg_replace $count is available since PHP 5.1.0
+                                       $field["on_update"] = "CURRENT_TIMESTAMP";
+                                       $field["default"] = $default;
+                               }
+                               if ($key == $_POST["auto_increment_col"]) {
+                                       $field["auto_increment"] = true;
+                               }
+                               $process_field = process_field($field, $type_field);
+                               if ($process_field != process_field($orig_field, $orig_field)) {
+                                       $fields[] = array($field["orig"], $process_field, $after);
+                               }
+                               if (isset($foreign_key)) {
+                                       $foreign[] = ($TABLE != "" ? "ADD" : " ") . " FOREIGN KEY (" . idf_escape($field["field"]) . ") REFERENCES " . idf_escape($foreign_key) . " (" . idf_escape($type_field["field"]) . ")";
                                }
                                $after = "AFTER " . idf_escape($field["field"]);
                        } elseif ($field["orig"] != "") {
index baa57cf5711023e1d61f4126ee6c7bd61d124851..51cd4e0aed761d3689c174f0a1fd6cc8ddf074c2 100644 (file)
@@ -201,8 +201,8 @@ if (isset($_GET["pgsql"])) {
        function table_status($name = "") {
                global $connection;
                $return = array();
-               $result = $connection->query("SELECT relname AS \"Name\", CASE relkind WHEN 'r' THEN '' ELSE 'view' END AS \"Engine\", pg_relation_size(oid) AS \"Data_length\", pg_total_relation_size(oid) - pg_relation_size(oid) AS \"Index_length\", pg_catalog.obj_description(oid, 'pg_class') AS \"Comment\"
-FROM pg_catalog.pg_class
+               $result = $connection->query("SELECT relname AS \"Name\", CASE relkind WHEN 'r' THEN '' ELSE 'view' END AS \"Engine\", pg_relation_size(oid) AS \"Data_length\", pg_total_relation_size(oid) - pg_relation_size(oid) AS \"Index_length\", obj_description(oid, 'pg_class') AS \"Comment\"
+FROM pg_class
 WHERE relkind IN ('r','v')
 AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema())"
                        . ($name != "" ? " AND relname = " . $connection->quote($name) : "")
@@ -220,24 +220,25 @@ AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema(
        function fields($table) {
                global $connection;
                $return = array();
-               $table_oid = $connection->result("SELECT oid FROM pg_class WHERE relname = " . $connection->quote($table));
-               $result = $connection->query("SELECT *, col_description($table_oid, ordinal_position) AS comment FROM information_schema.columns WHERE table_name = " . $connection->quote($table) . " ORDER BY ordinal_position");
+               $result = $connection->query("SELECT a.attname AS field, format_type(a.atttypid, a.atttypmod) AS full_type, d.adsrc AS default, a.attnotnull, col_description(c.oid, a.attnum) AS comment
+FROM pg_class c
+JOIN pg_namespace n ON c.relnamespace = n.oid
+JOIN pg_attribute a ON c.oid = a.attrelid
+LEFT JOIN pg_attrdef d ON c.oid = d.adrelid AND a.attnum = d.adnum
+WHERE c.relname = " . $connection->quote($table) . "
+AND n.nspname = current_schema()
+AND a.attnum > 0
+ORDER BY a.attnum");
                if ($result) {
                        while ($row = $result->fetch_assoc()) {
-                               $length = $row["character_maximum_length"];
-                               $return[$row["column_name"]] = array(
-                                       "field" => $row["column_name"],
-                                       "full_type" => $row["data_type"] . ($length ? "($length)" : ""),
-                                       "type" => $row["data_type"],
-                                       "length" => $length,
-                                       "default" => $row["column_default"],
-                                       "null" => ($row["is_nullable"] == "YES"),
-                                       "auto_increment" => eregi("^nextval\\(", $row["column_default"]),
-                                       "collation" => $row["collation_name"],
-                                       "privileges" => array("insert" => 1, "select" => 1, "update" => 1), //! is_updatable
-                                       "primary" => false, //!
-                                       "comment" => $row["comment"],
-                               );
+                               //! collation, primary
+                               ereg('(.*)(\\((.*)\\))?', $row["full_type"], $match);
+                               list(, $row["type"], , $row["length"]) = $match;
+                               $row["full_type"] = $row["type"] . ($row["length"] ? "($row[length])" : "");
+                               $row["null"] = ($row["attnotnull"] == "f");
+                               $row["auto_increment"] = eregi("^nextval\\(", $row["default"]);
+                               $row["privileges"] = array("insert" => 1, "select" => 1, "update" => 1);
+                               $return[$row["field"]] = $row;
                        }
                }
                return $return;
@@ -478,7 +479,18 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = " . $connection->qu
        }
        
        function set_schema($schema) {
-               global $connection;
+               global $connection, $types, $structured_types;
+               foreach (get_vals("SELECT typname
+FROM pg_type
+WHERE typnamespace = (SELECT oid FROM pg_namespace WHERE nspname = " . $connection->quote($schema) . ")
+AND typtype IN ('b','d','e')
+AND typelem = 0"
+               ) as $type) { //! get types from current_schemas('t')
+                       if (!isset($types[$type])) {
+                               $types[$type] = 0;
+                               $structured_types[lang('User types')][] = $type;
+                       }
+               }
                return $connection->query("SET search_path TO " . idf_escape($schema));
        }
        
@@ -504,7 +516,7 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = " . $connection->qu
                lang('Binary') => array("bit" => 0, "bit varying" => 0, "bytea" => 0),
                lang('Network') => array("cidr" => 43, "inet" => 43, "macaddr" => 17, "txid_snapshot" => 0),
                lang('Geometry') => array("box" => 0, "circle" => 0, "line" => 0, "lseg" => 0, "path" => 0, "point" => 0, "polygon" => 0),
-       ) as $key => $val) {
+       ) as $key => $val) { //! can be retrieved from pg_type
                $types += $val;
                $structured_types[$key] = array_keys($val);
        }
index c22aeaab048a0c470e255370fe7310f307b40ac5..aa8ff07a6829ba32068ea8f7cb3cc20bb660cdb9 100644 (file)
@@ -131,9 +131,10 @@ function process_length($length) {
 * @return string
 */
 function process_type($field, $collate = "COLLATE") {
-       global $connection, $unsigned;
-       return " $field[type]"
-               . ($field["length"] != "" && !ereg('^date|time$', $field["type"]) ? "(" . process_length($field["length"]) . ")" : "")
+       global $types, $connection, $unsigned;
+       $type = $field["type"];
+       return " " . (isset($types[$type]) ? $type : idf_escape($type))
+               . ($field["length"] != "" ? "(" . process_length($field["length"]) . ")" : "")
                . (ereg('int|float|double|decimal', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
                . (ereg('char|text|enum|set', $field["type"]) && $field["collation"] ? " $collate " . $connection->quote($field["collation"]) : "")
        ;
index 5a534de771e4c3920ef8d1c6817ffa5f7eb6f35f..70131b78897e264e8325beca6863d6fc397d87cd 100644 (file)
@@ -250,5 +250,6 @@ $translations = array(
        'Sequence has been created.' => 'Sekvence byla vytvořena.',
        'Sequence has been altered.' => 'Sekvence byla změněna.',
        'Alter sequence' => 'Pozměnit sekvenci',
+       'User types' => 'Uživatelské typy',
        'Search data in tables' => 'Vyhledat data v tabulkách',
 );
index af3676accb9ddfa14266be4d57cf8338fb31d122..7c619dab76fb2265301346987e816f637c52e022 100644 (file)
@@ -3,10 +3,12 @@ Drivers for MS SQL, SQLite, PostgreSQL, Oracle
 Allow concurrent logins on the same server
 Allow permanent login without customization
 In-place editation in select
+Foreign key options in Table creation
 Show number of tables in server overview
 Operator LIKE %%
 Remember export parameters in cookie
 Allow semicolon as CSV separator
+Schema and sequences support (PostgreSQL)
 Autofocus username in login form
 Disable spellchecking in SQL textareas
 Display auto_increment value of inserted item
index 4143150e8e2391680aa6fa388e51af99128df926..b6de65548591d252b14e891bb6bd9dc8a20cedcc 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -32,6 +32,7 @@ Delimiter in export and SQL command
 Backward keys in Editor
 
 PostgreSQL:
+user types
 Users - SELECT * FROM pg_user
 ORDER BY COUNT(*)
 Export - http://www.postgresql.org/docs/8.4/static/functions-info.html